1 #!/usr/bin/env python 2 3 """ 4 Program code and data representations. 5 """ 6 7 class Block: 8 9 "A code block." 10 11 def __init__(self): 12 self.code = [] 13 self.location = None 14 15 def __repr__(self): 16 return "Block(%r, location=%r)" % (id(self), self.location) 17 18 def as_raw(self, objtable, paramtable): 19 for i, item in enumerate(self.code): 20 if hasattr(item, "location"): 21 item.location = location + i 22 return self.code 23 24 class DataObject: 25 26 "A representation of a raw program data object." 27 28 def __init__(self, classcode, attrcode, codeaddr, name, size, funccode=None): 29 self.classcode = classcode 30 self.attrcode = attrcode 31 self.codeaddr = codeaddr 32 self.name = name 33 self.size = size 34 self.funccode = funccode 35 36 def with_size(self, size): 37 return DataObject(self.classcode, self.attrcode, self.codeaddr, self.name, size, self.funccode) 38 39 def with_callable(self, codeaddr): 40 return DataObject(self.classcode, self.attrcode, codeaddr, self.name, self.size, self.funccode) 41 42 def __repr__(self): 43 return "%r # %s" % ( 44 (self.classcode, self.attrcode, self.codeaddr, self.funccode, self.size), self.name 45 ) 46 47 class FragmentObject: 48 49 "A representation of a list fragment, used by list instances." 50 51 def __init__(self, occupied_size, allocated_size): 52 self.occupied_size = occupied_size 53 self.allocated_size = allocated_size 54 55 def __repr__(self): 56 return "%r" % ((self.occupied_size, self.allocated_size),) 57 58 class Context: 59 60 """ 61 A representation of a context used in a program, providing the special 62 context value types. 63 """ 64 65 def __init__(self, truth_value, repr): 66 self.truth_value = truth_value 67 self.repr = repr 68 69 def __repr__(self): 70 return self.repr 71 72 def __nonzero__(self): 73 return self.truth_value 74 75 __shortrepr__ = __repr__ 76 77 # A representation of a context that is replaced upon certain assignment 78 # operations. 79 80 ReplaceableContext = Context(0, "Replace") 81 82 # A representation of a context that is employed by classes so that 83 # instantiators have enough slots in the invocation frame, yet does not cause 84 # testing of the context or adjustment of the frame. 85 86 PlaceholderContext = Context(0, "Placeholder") 87 88 # vim: tabstop=4 expandtab shiftwidth=4