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