1 #!/usr/bin/env python 2 3 """ 4 Fundamental program data structure abstractions. 5 6 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 # Short representation display support. 23 24 def shortrepr(obj): 25 if obj is None: 26 return repr(None) 27 else: 28 return obj.__shortrepr__() 29 30 # Mix-ins and abstract classes. 31 32 class Naming: 33 34 "A mix-in providing naming conveniences." 35 36 def full_name(self): 37 if self.name is not None: 38 return self.parent.full_name() + "." + self.name 39 else: 40 return self.parent.full_name() 41 42 # Instances are special in that they need to be wrapped together with context in 43 # a running program, but they are not generally constant. 44 45 class Instance: 46 47 "A placeholder indicating the involvement of an instance." 48 49 def __init__(self): 50 self.parent = None 51 52 # Image generation details. 53 54 self.location = None 55 56 def __repr__(self): 57 return "<instance>" 58 59 def __eq__(self, other): 60 return other.__class__ is Instance 61 62 def __ne__(self, other): 63 return not self.__eq__(other) 64 65 def __hash__(self): 66 return 0 67 68 __shortrepr__ = __repr__ 69 70 # Common instance construction. 71 72 common_instance = Instance() 73 74 def make_instance(): 75 return common_instance 76 77 class Constant: 78 79 "A superclass for all constant or context-free structures." 80 81 pass 82 83 # Data objects appearing in programs before run-time. 84 85 class Const(Constant, Instance): 86 87 "A constant object with no context." 88 89 def __init__(self, value): 90 Instance.__init__(self) 91 self.value = value 92 93 def get_value(self): 94 return self.value 95 96 def __repr__(self): 97 if self.location is not None: 98 return "Const(%r, location=%r)" % (self.value, self.location) 99 else: 100 return "Const(%r)" % self.value 101 102 __shortrepr__ = __repr__ 103 104 # Support constants as dictionary keys in order to build constant tables. 105 106 def __eq__(self, other): 107 return other is not None and isinstance(other, Const) and \ 108 self.value == other.value and self.value.__class__ is other.value.__class__ 109 110 def __ne__(self, other): 111 return not self.__eq__(other) 112 113 def __hash__(self): 114 return hash(self.value) 115 116 # Constants are instances of various built-in types. 117 118 def value_type_name(self): 119 return ".".join(self.value_type_name_parts()) 120 121 def value_type_name_parts(self): 122 return "__builtins__", self.value.__class__.__name__ 123 124 # vim: tabstop=4 expandtab shiftwidth=4