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 Context: 48 49 """ 50 A representation of a context used in a program, providing the special 51 context value types. 52 """ 53 54 def __init__(self, truth_value, repr): 55 self.truth_value = truth_value 56 self.repr = repr 57 58 def __repr__(self): 59 return self.repr 60 61 def __nonzero__(self): 62 return self.truth_value 63 64 __shortrepr__ = __repr__ 65 66 # A representation of a context that is replaced upon certain assignment 67 # operations. 68 69 ReplaceableContext = Context(0, "Replace") 70 71 # A representation of a context that is employed by classes so that 72 # instantiators have enough slots in the invocation frame, yet does not cause 73 # testing of the context or adjustment of the frame. 74 75 PlaceholderContext = Context(0, "Placeholder") 76 77 # vim: tabstop=4 expandtab shiftwidth=4