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