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