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