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