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