1 #!/usr/bin/env python 2 3 """ 4 Program code and data representations. 5 6 Copyright (C) 2009, 2011 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 Block: 23 24 "A code block." 25 26 def __init__(self, unit): 27 self.unit = unit 28 self.code = [] 29 self.location = None 30 self.active_values = set() 31 32 def __repr__(self): 33 return "Block(%r, id=%r, location=%r)" % (self.unit, id(self), self.location) 34 35 def set_active_values(self, values): 36 self.active_values = values 37 38 def get_active_values(self): 39 return self.active_values 40 41 def insert(self, pos, op): 42 self.code.insert(pos, op) 43 44 def append(self, op): 45 self.code.append(op) 46 47 def __len__(self): 48 return len(self.code) 49 50 class DataValue: 51 52 "A representation of a raw program value." 53 54 def __init__(self, context, ref): 55 self.context = context 56 self.ref = ref 57 58 def __repr__(self): 59 return "value: (%r, %r)" % ( 60 self.context, self.ref 61 ) 62 63 class DataObject: 64 65 "A representation of a raw program data object." 66 67 def __init__(self, classcode, attrcode, codeaddr, name, size, funccode=None): 68 self.classcode = classcode 69 self.attrcode = attrcode 70 self.codeaddr = codeaddr 71 self.name = name 72 self.size = size 73 self.funccode = funccode 74 75 def with_size(self, size): 76 return DataObject(self.classcode, self.attrcode, self.codeaddr, self.name, size, self.funccode) 77 78 def with_callable(self, codeaddr): 79 return DataObject(self.classcode, self.attrcode, codeaddr, self.name, self.size, self.funccode) 80 81 def __repr__(self): 82 return "object: %r # %s" % ( 83 (self.classcode, self.attrcode, self.codeaddr, self.funccode, self.size), self.name 84 ) 85 86 class FragmentObject: 87 88 "A representation of a list fragment, used by list instances." 89 90 def __init__(self, occupied_size, allocated_size): 91 self.occupied_size = occupied_size 92 self.allocated_size = allocated_size 93 94 def __repr__(self): 95 return "%r" % ((self.occupied_size, self.allocated_size),) 96 97 class Context: 98 99 """ 100 A representation of a context used in a program, providing the special 101 context value types. 102 """ 103 104 def __init__(self, truth_value, repr): 105 self.truth_value = truth_value 106 self.repr = repr 107 108 def __repr__(self): 109 return self.repr 110 111 def __nonzero__(self): 112 return self.truth_value 113 114 __shortrepr__ = __repr__ 115 116 # A representation of a context that is replaced upon certain assignment 117 # operations. 118 119 ReplaceableContext = Context(0, "Replace") 120 121 # A representation of a context that is employed by classes so that 122 # instantiators have enough slots in the invocation frame, yet does not cause 123 # testing of the context or adjustment of the frame. 124 125 PlaceholderContext = Context(0, "Placeholder") 126 127 # vim: tabstop=4 expandtab shiftwidth=4