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