1 #!/usr/bin/env python 2 3 """ 4 Common classes. 5 6 Copyright (C) 2007, 2008, 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 try: 23 set 24 except NameError: 25 from sets import Set as set 26 27 # Errors. 28 29 class ProcessingError(Exception): 30 31 "A processing error." 32 33 pass 34 35 class InspectError(ProcessingError): 36 37 "An error during the module inspection process." 38 39 pass 40 41 class TableError(ProcessingError): 42 43 "An error occurring during access to a lookup table." 44 45 pass 46 47 class NodeProcessingError(ProcessingError): 48 49 "A processing error associated with a particular program node." 50 51 def __init__(self, unit_name, node, message): 52 self.unit_name = unit_name 53 self.astnode = node 54 self.message = message 55 56 def __repr__(self): 57 return "Error in %r at line %d: %s" % (self.unit_name, self.astnode.lineno, self.message) 58 59 def __str__(self): 60 return repr(self) 61 62 class TranslateError(NodeProcessingError): 63 64 "An error during the module translation process." 65 66 pass 67 68 class TranslationNotImplementedError(TranslateError): 69 70 "An error caused by a node not being supported in translation." 71 72 pass 73 74 # Program code representations. 75 76 class Block: 77 78 "A code block." 79 80 def __init__(self): 81 self.code = [] 82 self.location = None 83 84 def __repr__(self): 85 return "Block(%r, location=%r)" % (id(self), self.location) 86 87 def as_raw(self, objtable, paramtable): 88 for i, item in enumerate(self.code): 89 if hasattr(item, "location"): 90 item.location = location + i 91 return self.code 92 93 # Program data representations. 94 95 class DataObject: 96 97 "A representation of a raw program data object." 98 99 def __init__(self, classcode, attrcode, codeaddr, codedetails, instance, name, funccode=None): 100 self.classcode = classcode 101 self.attrcode = attrcode 102 self.codeaddr = codeaddr 103 self.codedetails = codedetails 104 self.instance = instance 105 self.name = name 106 self.funccode = funccode 107 108 def __repr__(self): 109 return "%r # %s" % ((self.classcode, self.attrcode, self.codeaddr, self.codedetails, self.instance, self.funccode), self.name) 110 111 # Inspection representations. 112 113 class AtLeast: 114 115 "A special representation for numbers of a given value or greater." 116 117 def __init__(self, count): 118 self.count = count 119 120 def __eq__(self, other): 121 return 0 122 123 __lt__ = __le__ = __eq__ 124 125 def __ne__(self, other): 126 return 1 127 128 def __gt__(self, other): 129 if isinstance(other, AtLeast): 130 return 0 131 else: 132 return self.count > other 133 134 def __ge__(self, other): 135 if isinstance(other, AtLeast): 136 return 0 137 else: 138 return self.count >= other 139 140 def __iadd__(self, other): 141 if isinstance(other, AtLeast): 142 self.count += other.count 143 else: 144 self.count += other 145 return self 146 147 def __radd__(self, other): 148 if isinstance(other, AtLeast): 149 return AtLeast(self.count + other.count) 150 else: 151 return AtLeast(self.count + other) 152 153 def __repr__(self): 154 return "AtLeast(%r)" % self.count 155 156 class Naming: 157 158 "A mix-in providing naming conveniences." 159 160 def full_name(self): 161 if self.name is not None: 162 return self.parent.full_name() + "." + self.name 163 else: 164 return self.parent.full_name() 165 166 class Undefined: 167 168 "A special class of undefined values." 169 170 pass 171 172 # Useful data. 173 174 comparison_methods = { 175 "==" : ("__eq__", "__ne__"), 176 "!=" : ("__ne__", "__eq__"), 177 "<" : ("__lt__", "__gt__"), 178 "<=" : ("__le__", "__ge__"), 179 ">=" : ("__ge__", "__le__"), 180 ">" : ("__gt__", "__lt__"), 181 "is" : None, 182 "is not" : None, 183 "in" : None, 184 "not in" : None 185 } 186 187 augassign_methods = { 188 "+=" : ("__iadd__", ("__add__", "__radd__")), 189 "-=" : ("__isub__", ("__sub__", "__rsub__")), 190 "*=" : ("__imul__", ("__mul__", "__rmul__")), 191 "/=" : ("__idiv__", ("__div__", "__rdiv__")), 192 "%=" : ("__imod__", ("__mod__", "__rmod__")), 193 "**=" : ("__ipow__", ("__pow__", "__rpow__")), 194 "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), 195 ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), 196 "&=" : ("__iand__", ("__and__", "__rand__")), 197 "^=" : ("__ixor__", ("__xor__", "__rxor__")), 198 "|=" : ("__ior__", ("__or__", "__ror__")) 199 } 200 201 binary_methods = { 202 "Add" : ("__add__", "__radd__"), 203 "Bitand" : ("__and__", "__rand__"), 204 "Bitor" : ("__or__", "__ror__"), 205 "Bitxor" : ("__xor__", "__rxor__"), 206 "Div" : ("__div__", "__rdiv__"), 207 "FloorDiv" : ("__floordiv__", "__rfloordiv__"), 208 "LeftShift" : ("__lshift__", "__rlshift__"), 209 "Mod" : ("__mod__", "__rmod__"), 210 "Mul" : ("__mul__", "__rmul__"), 211 "Power" : ("__pow__", "__rpow__"), 212 "RightShift" : ("__rshift__", "__rrshift__"), 213 "Sub" : ("__sub__", "__rsub__") 214 } 215 216 unary_methods = { 217 "Invert" : "__invert__", 218 "UnaryAdd" : "__pos__", 219 "UnarySub" : "__neg__" 220 } 221 222 # vim: tabstop=4 expandtab shiftwidth=4