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@264 | 39 | class DataValue: |
paul@264 | 40 | |
paul@264 | 41 | "A representation of a raw program value." |
paul@264 | 42 | |
paul@264 | 43 | def __init__(self, context, ref): |
paul@264 | 44 | self.context = context |
paul@264 | 45 | self.ref = ref |
paul@264 | 46 | |
paul@264 | 47 | def __repr__(self): |
paul@264 | 48 | return "%r, %r" % ( |
paul@264 | 49 | self.context, self.ref |
paul@264 | 50 | ) |
paul@264 | 51 | |
paul@201 | 52 | class DataObject: |
paul@201 | 53 | |
paul@201 | 54 | "A representation of a raw program data object." |
paul@201 | 55 | |
paul@219 | 56 | def __init__(self, classcode, attrcode, codeaddr, name, size, funccode=None): |
paul@201 | 57 | self.classcode = classcode |
paul@201 | 58 | self.attrcode = attrcode |
paul@201 | 59 | self.codeaddr = codeaddr |
paul@201 | 60 | self.name = name |
paul@203 | 61 | self.size = size |
paul@201 | 62 | self.funccode = funccode |
paul@201 | 63 | |
paul@203 | 64 | def with_size(self, size): |
paul@219 | 65 | return DataObject(self.classcode, self.attrcode, self.codeaddr, self.name, size, self.funccode) |
paul@203 | 66 | |
paul@233 | 67 | def with_callable(self, codeaddr): |
paul@233 | 68 | return DataObject(self.classcode, self.attrcode, codeaddr, self.name, self.size, self.funccode) |
paul@233 | 69 | |
paul@201 | 70 | def __repr__(self): |
paul@203 | 71 | return "%r # %s" % ( |
paul@219 | 72 | (self.classcode, self.attrcode, self.codeaddr, self.funccode, self.size), self.name |
paul@203 | 73 | ) |
paul@201 | 74 | |
paul@246 | 75 | class FragmentObject: |
paul@246 | 76 | |
paul@246 | 77 | "A representation of a list fragment, used by list instances." |
paul@246 | 78 | |
paul@246 | 79 | def __init__(self, occupied_size, allocated_size): |
paul@246 | 80 | self.occupied_size = occupied_size |
paul@246 | 81 | self.allocated_size = allocated_size |
paul@246 | 82 | |
paul@246 | 83 | def __repr__(self): |
paul@246 | 84 | return "%r" % ((self.occupied_size, self.allocated_size),) |
paul@246 | 85 | |
paul@237 | 86 | class Context: |
paul@237 | 87 | |
paul@237 | 88 | """ |
paul@237 | 89 | A representation of a context used in a program, providing the special |
paul@237 | 90 | context value types. |
paul@237 | 91 | """ |
paul@237 | 92 | |
paul@237 | 93 | def __init__(self, truth_value, repr): |
paul@237 | 94 | self.truth_value = truth_value |
paul@237 | 95 | self.repr = repr |
paul@237 | 96 | |
paul@237 | 97 | def __repr__(self): |
paul@237 | 98 | return self.repr |
paul@237 | 99 | |
paul@237 | 100 | def __nonzero__(self): |
paul@237 | 101 | return self.truth_value |
paul@237 | 102 | |
paul@243 | 103 | __shortrepr__ = __repr__ |
paul@243 | 104 | |
paul@237 | 105 | # A representation of a context that is replaced upon certain assignment |
paul@237 | 106 | # operations. |
paul@237 | 107 | |
paul@237 | 108 | ReplaceableContext = Context(0, "Replace") |
paul@237 | 109 | |
paul@237 | 110 | # A representation of a context that is employed by classes so that |
paul@237 | 111 | # instantiators have enough slots in the invocation frame, yet does not cause |
paul@237 | 112 | # testing of the context or adjustment of the frame. |
paul@237 | 113 | |
paul@237 | 114 | PlaceholderContext = Context(0, "Placeholder") |
paul@237 | 115 | |
paul@201 | 116 | # vim: tabstop=4 expandtab shiftwidth=4 |