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