paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | """ |
paul@39 | 4 | Common classes. |
paul@39 | 5 | |
paul@296 | 6 | Copyright (C) 2007, 2008, 2009, 2010 Paul Boddie <paul@boddie.org.uk> |
paul@39 | 7 | |
paul@39 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@39 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@39 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@39 | 11 | version. |
paul@39 | 12 | |
paul@39 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@39 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@39 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@39 | 16 | details. |
paul@39 | 17 | |
paul@39 | 18 | You should have received a copy of the GNU General Public License along with |
paul@39 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@39 | 20 | """ |
paul@39 | 21 | |
paul@339 | 22 | import compiler.visitor |
paul@339 | 23 | |
paul@83 | 24 | try: |
paul@83 | 25 | set |
paul@83 | 26 | except NameError: |
paul@83 | 27 | from sets import Set as set |
paul@83 | 28 | |
paul@339 | 29 | # Visitors. |
paul@339 | 30 | |
paul@339 | 31 | class ASTVisitor(compiler.visitor.ASTVisitor): |
paul@339 | 32 | |
paul@339 | 33 | "A base class for visitors." |
paul@339 | 34 | |
paul@339 | 35 | def dispatch(self, node, *args): |
paul@339 | 36 | try: |
paul@339 | 37 | return compiler.visitor.ASTVisitor.dispatch(self, node, *args) |
paul@339 | 38 | except NodeProcessingError, exc: |
paul@340 | 39 | if exc.astnode is None: |
paul@340 | 40 | exc.astnode = node |
paul@342 | 41 | |
paul@342 | 42 | # NOTE: Should perhaps specialise the subclasses appropriately. |
paul@342 | 43 | |
paul@342 | 44 | if hasattr(self, "unit"): |
paul@342 | 45 | exc.unit_name = self.unit.full_name() |
paul@342 | 46 | else: |
paul@342 | 47 | exc.unit_name = self.full_name() |
paul@339 | 48 | raise |
paul@339 | 49 | |
paul@353 | 50 | def used_by_unit(node): |
paul@353 | 51 | |
paul@353 | 52 | """ |
paul@353 | 53 | Return whether the definition made by a 'node' is actually employed by the |
paul@353 | 54 | program unit within which it is found. |
paul@353 | 55 | """ |
paul@353 | 56 | |
paul@353 | 57 | return node.unit.parent.has_key(node.unit.name) |
paul@353 | 58 | |
paul@83 | 59 | # Errors. |
paul@83 | 60 | |
paul@39 | 61 | class ProcessingError(Exception): |
paul@39 | 62 | |
paul@39 | 63 | "A processing error." |
paul@39 | 64 | |
paul@63 | 65 | pass |
paul@63 | 66 | |
paul@84 | 67 | class TableError(ProcessingError): |
paul@84 | 68 | |
paul@84 | 69 | "An error occurring during access to a lookup table." |
paul@84 | 70 | |
paul@84 | 71 | pass |
paul@84 | 72 | |
paul@243 | 73 | class TableGenerationError(ProcessingError): |
paul@243 | 74 | |
paul@243 | 75 | "An error occurring when generating a lookup table." |
paul@243 | 76 | |
paul@243 | 77 | pass |
paul@243 | 78 | |
paul@63 | 79 | class NodeProcessingError(ProcessingError): |
paul@63 | 80 | |
paul@63 | 81 | "A processing error associated with a particular program node." |
paul@63 | 82 | |
paul@339 | 83 | def __init__(self, message): |
paul@39 | 84 | self.message = message |
paul@339 | 85 | self.unit_name = None |
paul@339 | 86 | self.astnode = None |
paul@339 | 87 | |
paul@339 | 88 | def get_lineno(self, node): |
paul@339 | 89 | lineno = node.lineno |
paul@339 | 90 | if lineno is not None: |
paul@339 | 91 | return lineno |
paul@339 | 92 | else: |
paul@339 | 93 | for child in node.getChildNodes(): |
paul@339 | 94 | lineno = self.get_lineno(child) |
paul@339 | 95 | if lineno is not None: |
paul@339 | 96 | return lineno |
paul@339 | 97 | return None |
paul@39 | 98 | |
paul@39 | 99 | def __repr__(self): |
paul@339 | 100 | return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) |
paul@39 | 101 | |
paul@39 | 102 | def __str__(self): |
paul@39 | 103 | return repr(self) |
paul@39 | 104 | |
paul@296 | 105 | class InspectError(NodeProcessingError): |
paul@296 | 106 | |
paul@296 | 107 | "An error during the module inspection process." |
paul@296 | 108 | |
paul@296 | 109 | pass |
paul@296 | 110 | |
paul@83 | 111 | class TranslateError(NodeProcessingError): |
paul@83 | 112 | |
paul@83 | 113 | "An error during the module translation process." |
paul@83 | 114 | |
paul@83 | 115 | pass |
paul@83 | 116 | |
paul@100 | 117 | class TranslationNotImplementedError(TranslateError): |
paul@100 | 118 | |
paul@100 | 119 | "An error caused by a node not being supported in translation." |
paul@100 | 120 | |
paul@100 | 121 | pass |
paul@100 | 122 | |
paul@343 | 123 | # Special representations. |
paul@343 | 124 | |
paul@343 | 125 | class AtLeast: |
paul@343 | 126 | |
paul@343 | 127 | "A special representation for numbers of a given value or greater." |
paul@343 | 128 | |
paul@343 | 129 | def __init__(self, count): |
paul@343 | 130 | self.count = count |
paul@343 | 131 | |
paul@343 | 132 | def __eq__(self, other): |
paul@343 | 133 | return 0 |
paul@343 | 134 | |
paul@343 | 135 | __lt__ = __le__ = __eq__ |
paul@343 | 136 | |
paul@343 | 137 | def __ne__(self, other): |
paul@343 | 138 | return 1 |
paul@343 | 139 | |
paul@343 | 140 | def __gt__(self, other): |
paul@343 | 141 | if isinstance(other, AtLeast): |
paul@343 | 142 | return 0 |
paul@343 | 143 | else: |
paul@343 | 144 | return self.count > other |
paul@343 | 145 | |
paul@343 | 146 | def __ge__(self, other): |
paul@343 | 147 | if isinstance(other, AtLeast): |
paul@343 | 148 | return 0 |
paul@343 | 149 | else: |
paul@343 | 150 | return self.count >= other |
paul@343 | 151 | |
paul@343 | 152 | def __iadd__(self, other): |
paul@343 | 153 | if isinstance(other, AtLeast): |
paul@343 | 154 | self.count += other.count |
paul@343 | 155 | else: |
paul@343 | 156 | self.count += other |
paul@343 | 157 | return self |
paul@343 | 158 | |
paul@343 | 159 | def __radd__(self, other): |
paul@343 | 160 | if isinstance(other, AtLeast): |
paul@343 | 161 | return AtLeast(self.count + other.count) |
paul@343 | 162 | else: |
paul@343 | 163 | return AtLeast(self.count + other) |
paul@343 | 164 | |
paul@343 | 165 | def __repr__(self): |
paul@343 | 166 | return "AtLeast(%r)" % self.count |
paul@343 | 167 | |
paul@172 | 168 | # Useful data. |
paul@172 | 169 | |
paul@172 | 170 | comparison_methods = { |
paul@232 | 171 | "==" : ("__eq__", "__eq__"), |
paul@232 | 172 | "!=" : ("__ne__", "__ne__"), |
paul@172 | 173 | "<" : ("__lt__", "__gt__"), |
paul@172 | 174 | "<=" : ("__le__", "__ge__"), |
paul@172 | 175 | ">=" : ("__ge__", "__le__"), |
paul@172 | 176 | ">" : ("__gt__", "__lt__"), |
paul@172 | 177 | "is" : None, |
paul@172 | 178 | "is not" : None, |
paul@172 | 179 | "in" : None, |
paul@172 | 180 | "not in" : None |
paul@172 | 181 | } |
paul@172 | 182 | |
paul@172 | 183 | augassign_methods = { |
paul@172 | 184 | "+=" : ("__iadd__", ("__add__", "__radd__")), |
paul@172 | 185 | "-=" : ("__isub__", ("__sub__", "__rsub__")), |
paul@172 | 186 | "*=" : ("__imul__", ("__mul__", "__rmul__")), |
paul@172 | 187 | "/=" : ("__idiv__", ("__div__", "__rdiv__")), |
paul@324 | 188 | "//=" : ("__ifloordiv__", ("__floordiv__", "__rfloordiv__")), |
paul@172 | 189 | "%=" : ("__imod__", ("__mod__", "__rmod__")), |
paul@172 | 190 | "**=" : ("__ipow__", ("__pow__", "__rpow__")), |
paul@172 | 191 | "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), |
paul@172 | 192 | ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), |
paul@172 | 193 | "&=" : ("__iand__", ("__and__", "__rand__")), |
paul@172 | 194 | "^=" : ("__ixor__", ("__xor__", "__rxor__")), |
paul@172 | 195 | "|=" : ("__ior__", ("__or__", "__ror__")) |
paul@172 | 196 | } |
paul@172 | 197 | |
paul@172 | 198 | binary_methods = { |
paul@172 | 199 | "Add" : ("__add__", "__radd__"), |
paul@172 | 200 | "Bitand" : ("__and__", "__rand__"), |
paul@172 | 201 | "Bitor" : ("__or__", "__ror__"), |
paul@172 | 202 | "Bitxor" : ("__xor__", "__rxor__"), |
paul@172 | 203 | "Div" : ("__div__", "__rdiv__"), |
paul@172 | 204 | "FloorDiv" : ("__floordiv__", "__rfloordiv__"), |
paul@172 | 205 | "LeftShift" : ("__lshift__", "__rlshift__"), |
paul@172 | 206 | "Mod" : ("__mod__", "__rmod__"), |
paul@172 | 207 | "Mul" : ("__mul__", "__rmul__"), |
paul@172 | 208 | "Power" : ("__pow__", "__rpow__"), |
paul@172 | 209 | "RightShift" : ("__rshift__", "__rrshift__"), |
paul@172 | 210 | "Sub" : ("__sub__", "__rsub__") |
paul@172 | 211 | } |
paul@172 | 212 | |
paul@172 | 213 | unary_methods = { |
paul@172 | 214 | "Invert" : "__invert__", |
paul@172 | 215 | "UnaryAdd" : "__pos__", |
paul@172 | 216 | "UnarySub" : "__neg__" |
paul@172 | 217 | } |
paul@172 | 218 | |
paul@39 | 219 | # vim: tabstop=4 expandtab shiftwidth=4 |