1 #!/usr/bin/env python 2 3 """ 4 Program code and data representations. 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 try: 23 set 24 except NameError: 25 from sets import Set as set 26 27 class Location: 28 29 """ 30 A special representation for locations which are to be compared to program 31 objects. 32 """ 33 34 def __init__(self, location): 35 self.location = location 36 37 def _op(self, other, op): 38 if hasattr(other, "location"): 39 return op(self.location, other.location) 40 else: 41 raise NotImplemented 42 43 def __eq__(self, other): 44 return self._op(other, operator.eq) 45 46 def __ne__(self, other): 47 return self._op(other, operator.ne) 48 49 def __lt__(self, other): 50 return self._op(other, operator.lt) 51 52 def __le__(self, other): 53 return self._op(other, operator.le) 54 55 def __gt__(self, other): 56 return self._op(other, operator.gt) 57 58 def __ge__(self, other): 59 return self._op(other, operator.ge) 60 61 def __repr__(self): 62 return "Location(%r)" % self.location 63 64 class Block: 65 66 "A code block." 67 68 def __init__(self, unit): 69 self.unit = unit 70 self.code = [] 71 self.location = None 72 self.active_values = set() 73 74 def __repr__(self): 75 return "Block(%r, id=%r, location=%r)" % (self.unit, id(self), self.location) 76 77 def set_active_values(self, values): 78 self.active_values = values 79 80 def get_active_values(self): 81 return self.active_values 82 83 def insert(self, pos, op): 84 self.code.insert(pos, op) 85 86 def append(self, op): 87 self.code.append(op) 88 89 def __len__(self): 90 return len(self.code) 91 92 class DataValue: 93 94 "A representation of a raw program value." 95 96 def __init__(self, context, ref): 97 self.context = context 98 self.ref = ref 99 100 def __repr__(self): 101 return "value: (%r, %r)" % ( 102 self.context, self.ref 103 ) 104 105 class DataObject: 106 107 "A representation of a raw program data object." 108 109 def __init__(self, classcode, attrcode, codeaddr, name, size, funccode=None): 110 self.classcode = classcode 111 self.attrcode = attrcode 112 self.codeaddr = codeaddr 113 self.name = name 114 self.size = size 115 self.funccode = funccode 116 117 def with_size(self, size): 118 return DataObject(self.classcode, self.attrcode, self.codeaddr, self.name, size, self.funccode) 119 120 def with_callable(self, codeaddr): 121 return DataObject(self.classcode, self.attrcode, codeaddr, self.name, self.size, self.funccode) 122 123 def __repr__(self): 124 return "object: %r # %s" % ( 125 (self.classcode, self.attrcode, self.codeaddr, self.funccode, self.size), self.name 126 ) 127 128 class FragmentObject: 129 130 "A representation of a list fragment, used by list instances." 131 132 def __init__(self, occupied_size, allocated_size): 133 self.occupied_size = occupied_size 134 self.allocated_size = allocated_size 135 136 def __repr__(self): 137 return "%r" % ((self.occupied_size, self.allocated_size),) 138 139 class Context: 140 141 """ 142 A representation of a context used in a program, providing the special 143 context value types. 144 """ 145 146 def __init__(self, truth_value, repr): 147 self.truth_value = truth_value 148 self.repr = repr 149 150 def __repr__(self): 151 return self.repr 152 153 def __nonzero__(self): 154 return self.truth_value 155 156 __shortrepr__ = __repr__ 157 158 # A representation of a context that is replaced upon certain assignment 159 # operations. 160 161 ReplaceableContext = Context(0, "Replace") 162 163 # A representation of a context that is employed by classes so that 164 # instantiators have enough slots in the invocation frame, yet does not cause 165 # testing of the context or adjustment of the frame. 166 167 PlaceholderContext = Context(0, "Placeholder") 168 169 # vim: tabstop=4 expandtab shiftwidth=4