1 #!/usr/bin/env python 2 3 """ 4 Program code and data representations. 5 6 Copyright (C) 2009 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 30 def __repr__(self): 31 return "Block(%r, location=%r)" % (id(self), self.location) 32 33 def as_raw(self, objtable, paramtable): 34 for i, item in enumerate(self.code): 35 if hasattr(item, "location"): 36 item.location = location + i 37 return self.code 38 39 class DataObject: 40 41 "A representation of a raw program data object." 42 43 def __init__(self, classcode, attrcode, codeaddr, name, size, funccode=None): 44 self.classcode = classcode 45 self.attrcode = attrcode 46 self.codeaddr = codeaddr 47 self.name = name 48 self.size = size 49 self.funccode = funccode 50 51 def with_size(self, size): 52 return DataObject(self.classcode, self.attrcode, self.codeaddr, self.name, size, self.funccode) 53 54 def with_callable(self, codeaddr): 55 return DataObject(self.classcode, self.attrcode, codeaddr, self.name, self.size, self.funccode) 56 57 def __repr__(self): 58 return "%r # %s" % ( 59 (self.classcode, self.attrcode, self.codeaddr, self.funccode, self.size), self.name 60 ) 61 62 class FragmentObject: 63 64 "A representation of a list fragment, used by list instances." 65 66 def __init__(self, occupied_size, allocated_size): 67 self.occupied_size = occupied_size 68 self.allocated_size = allocated_size 69 70 def __repr__(self): 71 return "%r" % ((self.occupied_size, self.allocated_size),) 72 73 class Context: 74 75 """ 76 A representation of a context used in a program, providing the special 77 context value types. 78 """ 79 80 def __init__(self, truth_value, repr): 81 self.truth_value = truth_value 82 self.repr = repr 83 84 def __repr__(self): 85 return self.repr 86 87 def __nonzero__(self): 88 return self.truth_value 89 90 __shortrepr__ = __repr__ 91 92 # A representation of a context that is replaced upon certain assignment 93 # operations. 94 95 ReplaceableContext = Context(0, "Replace") 96 97 # A representation of a context that is employed by classes so that 98 # instantiators have enough slots in the invocation frame, yet does not cause 99 # testing of the context or adjustment of the frame. 100 101 PlaceholderContext = Context(0, "Placeholder") 102 103 # vim: tabstop=4 expandtab shiftwidth=4