paul@201 | 1 | #!/usr/bin/env python |
paul@201 | 2 | |
paul@201 | 3 | """ |
paul@201 | 4 | Program code and data representations. |
paul@253 | 5 | |
paul@253 | 6 | Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> |
paul@253 | 7 | |
paul@253 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@253 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@253 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@253 | 11 | version. |
paul@253 | 12 | |
paul@253 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@253 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@253 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@253 | 16 | details. |
paul@253 | 17 | |
paul@253 | 18 | You should have received a copy of the GNU General Public License along with |
paul@253 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@201 | 20 | """ |
paul@201 | 21 | |
paul@201 | 22 | class Block: |
paul@201 | 23 | |
paul@201 | 24 | "A code block." |
paul@201 | 25 | |
paul@201 | 26 | def __init__(self): |
paul@201 | 27 | self.code = [] |
paul@201 | 28 | self.location = None |
paul@201 | 29 | |
paul@201 | 30 | def __repr__(self): |
paul@201 | 31 | return "Block(%r, location=%r)" % (id(self), self.location) |
paul@201 | 32 | |
paul@201 | 33 | def as_raw(self, objtable, paramtable): |
paul@201 | 34 | for i, item in enumerate(self.code): |
paul@201 | 35 | if hasattr(item, "location"): |
paul@201 | 36 | item.location = location + i |
paul@201 | 37 | return self.code |
paul@201 | 38 | |
paul@201 | 39 | class DataObject: |
paul@201 | 40 | |
paul@201 | 41 | "A representation of a raw program data object." |
paul@201 | 42 | |
paul@219 | 43 | def __init__(self, classcode, attrcode, codeaddr, name, size, funccode=None): |
paul@201 | 44 | self.classcode = classcode |
paul@201 | 45 | self.attrcode = attrcode |
paul@201 | 46 | self.codeaddr = codeaddr |
paul@201 | 47 | self.name = name |
paul@203 | 48 | self.size = size |
paul@201 | 49 | self.funccode = funccode |
paul@201 | 50 | |
paul@203 | 51 | def with_size(self, size): |
paul@219 | 52 | return DataObject(self.classcode, self.attrcode, self.codeaddr, self.name, size, self.funccode) |
paul@203 | 53 | |
paul@233 | 54 | def with_callable(self, codeaddr): |
paul@233 | 55 | return DataObject(self.classcode, self.attrcode, codeaddr, self.name, self.size, self.funccode) |
paul@233 | 56 | |
paul@201 | 57 | def __repr__(self): |
paul@203 | 58 | return "%r # %s" % ( |
paul@219 | 59 | (self.classcode, self.attrcode, self.codeaddr, self.funccode, self.size), self.name |
paul@203 | 60 | ) |
paul@201 | 61 | |
paul@246 | 62 | class FragmentObject: |
paul@246 | 63 | |
paul@246 | 64 | "A representation of a list fragment, used by list instances." |
paul@246 | 65 | |
paul@246 | 66 | def __init__(self, occupied_size, allocated_size): |
paul@246 | 67 | self.occupied_size = occupied_size |
paul@246 | 68 | self.allocated_size = allocated_size |
paul@246 | 69 | |
paul@246 | 70 | def __repr__(self): |
paul@246 | 71 | return "%r" % ((self.occupied_size, self.allocated_size),) |
paul@246 | 72 | |
paul@237 | 73 | class Context: |
paul@237 | 74 | |
paul@237 | 75 | """ |
paul@237 | 76 | A representation of a context used in a program, providing the special |
paul@237 | 77 | context value types. |
paul@237 | 78 | """ |
paul@237 | 79 | |
paul@237 | 80 | def __init__(self, truth_value, repr): |
paul@237 | 81 | self.truth_value = truth_value |
paul@237 | 82 | self.repr = repr |
paul@237 | 83 | |
paul@237 | 84 | def __repr__(self): |
paul@237 | 85 | return self.repr |
paul@237 | 86 | |
paul@237 | 87 | def __nonzero__(self): |
paul@237 | 88 | return self.truth_value |
paul@237 | 89 | |
paul@243 | 90 | __shortrepr__ = __repr__ |
paul@243 | 91 | |
paul@237 | 92 | # A representation of a context that is replaced upon certain assignment |
paul@237 | 93 | # operations. |
paul@237 | 94 | |
paul@237 | 95 | ReplaceableContext = Context(0, "Replace") |
paul@237 | 96 | |
paul@237 | 97 | # A representation of a context that is employed by classes so that |
paul@237 | 98 | # instantiators have enough slots in the invocation frame, yet does not cause |
paul@237 | 99 | # testing of the context or adjustment of the frame. |
paul@237 | 100 | |
paul@237 | 101 | PlaceholderContext = Context(0, "Placeholder") |
paul@237 | 102 | |
paul@201 | 103 | # vim: tabstop=4 expandtab shiftwidth=4 |