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 no_attributes = {} 43 44 class Namespace: 45 46 "A mix-in providing basic namespace functionality." 47 48 def all_attributes(self): 49 50 """ 51 Return all attributes accessible through a namespace, whether provided 52 directly by the namespace or by other mechanisms. 53 """ 54 55 return no_attributes 56 57 class Constant: 58 59 "A superclass for all constant or context-free structures." 60 61 pass 62 63 # Instances are special in that they need to be wrapped together with context in 64 # a running program, but they are not generally constant. 65 66 class Instance(Namespace): 67 68 "A placeholder indicating the involvement of an instance." 69 70 def __init__(self): 71 self.parent = None 72 73 # Image generation details. 74 75 self.location = None 76 77 def __repr__(self): 78 return "<instance>" 79 80 def __eq__(self, other): 81 return other.__class__ is Instance 82 83 def __ne__(self, other): 84 return not self.__eq__(other) 85 86 def __hash__(self): 87 return 0 88 89 __shortrepr__ = __repr__ 90 91 # Common instance construction. 92 93 common_instance = Instance() 94 95 def make_instance(): 96 return common_instance 97 98 # Data objects appearing in programs before run-time. 99 100 class Const(Constant, Instance): 101 102 "A constant object with no context." 103 104 def __init__(self, value): 105 Instance.__init__(self) 106 self.value = value 107 108 def get_value(self): 109 return self.value 110 111 def __repr__(self): 112 if self.location is not None: 113 return "Const(%r, location=%r)" % (self.value, self.location) 114 else: 115 return "Const(%r)" % self.value 116 117 __shortrepr__ = __repr__ 118 119 # Support constants as dictionary keys in order to build constant tables. 120 121 def __eq__(self, other): 122 return other is not None and isinstance(other, Const) and \ 123 self.value == other.value and self.value.__class__ is other.value.__class__ 124 125 def __ne__(self, other): 126 return not self.__eq__(other) 127 128 def __hash__(self): 129 return hash(self.value) 130 131 # Constants are instances of various built-in types. 132 133 def value_type_name(self): 134 return ".".join(self.value_type_name_parts()) 135 136 def value_type_name_parts(self): 137 return "__builtins__", self.get_class_name() 138 139 def get_class_name(self): 140 return self.value.__class__.__name__ 141 142 # vim: tabstop=4 expandtab shiftwidth=4