1 #!/usr/bin/env python 2 3 """ 4 Common classes. 5 6 Copyright (C) 2007, 2008 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 # Program data representations. 88 89 class DataObject: 90 91 "A representation of a raw program data object." 92 93 def __init__(self, classcode, attrcode, codeaddr, codedetails, instance, name): 94 self.classcode = classcode 95 self.attrcode = attrcode 96 self.codeaddr = codeaddr 97 self.codedetails = codedetails 98 self.instance = instance 99 self.name = name 100 101 def __repr__(self): 102 return "%r # %s" % ((self.classcode, self.attrcode, self.codeaddr, self.codedetails, self.instance), self.name) 103 104 # Inspection representations. 105 106 class AtLeast: 107 108 "A special representation for numbers of a given value or greater." 109 110 def __init__(self, count): 111 self.count = count 112 113 def __eq__(self, other): 114 return 0 115 116 __lt__ = __le__ = __eq__ 117 118 def __ne__(self, other): 119 return 1 120 121 def __gt__(self, other): 122 if isinstance(other, AtLeast): 123 return 0 124 else: 125 return self.count > other 126 127 def __ge__(self, other): 128 if isinstance(other, AtLeast): 129 return 0 130 else: 131 return self.count >= other 132 133 def __iadd__(self, other): 134 if isinstance(other, AtLeast): 135 self.count += other.count 136 else: 137 self.count += other 138 return self 139 140 def __radd__(self, other): 141 if isinstance(other, AtLeast): 142 return AtLeast(self.count + other.count) 143 else: 144 return AtLeast(self.count + other) 145 146 def __repr__(self): 147 return "AtLeast(%r)" % self.count 148 149 class Naming: 150 151 "A mix-in providing naming conveniences." 152 153 def full_name(self): 154 if self.name is not None: 155 return self.parent.full_name() + "." + self.name 156 else: 157 return self.parent.full_name() 158 159 class Undefined: 160 161 "A special class of undefined values." 162 163 pass 164 165 # Useful data. 166 167 comparison_methods = { 168 "==" : ("__eq__", "__ne__"), 169 "!=" : ("__ne__", "__eq__"), 170 "<" : ("__lt__", "__gt__"), 171 "<=" : ("__le__", "__ge__"), 172 ">=" : ("__ge__", "__le__"), 173 ">" : ("__gt__", "__lt__"), 174 "is" : None, 175 "is not" : None, 176 "in" : None, 177 "not in" : None 178 } 179 180 augassign_methods = { 181 "+=" : ("__iadd__", ("__add__", "__radd__")), 182 "-=" : ("__isub__", ("__sub__", "__rsub__")), 183 "*=" : ("__imul__", ("__mul__", "__rmul__")), 184 "/=" : ("__idiv__", ("__div__", "__rdiv__")), 185 "%=" : ("__imod__", ("__mod__", "__rmod__")), 186 "**=" : ("__ipow__", ("__pow__", "__rpow__")), 187 "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), 188 ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), 189 "&=" : ("__iand__", ("__and__", "__rand__")), 190 "^=" : ("__ixor__", ("__xor__", "__rxor__")), 191 "|=" : ("__ior__", ("__or__", "__ror__")) 192 } 193 194 binary_methods = { 195 "Add" : ("__add__", "__radd__"), 196 "Bitand" : ("__and__", "__rand__"), 197 "Bitor" : ("__or__", "__ror__"), 198 "Bitxor" : ("__xor__", "__rxor__"), 199 "Div" : ("__div__", "__rdiv__"), 200 "FloorDiv" : ("__floordiv__", "__rfloordiv__"), 201 "LeftShift" : ("__lshift__", "__rlshift__"), 202 "Mod" : ("__mod__", "__rmod__"), 203 "Mul" : ("__mul__", "__rmul__"), 204 "Power" : ("__pow__", "__rpow__"), 205 "RightShift" : ("__rshift__", "__rrshift__"), 206 "Sub" : ("__sub__", "__rsub__") 207 } 208 209 unary_methods = { 210 "Invert" : "__invert__", 211 "UnaryAdd" : "__pos__", 212 "UnarySub" : "__neg__" 213 } 214 215 # vim: tabstop=4 expandtab shiftwidth=4