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