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