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@83 | 50 | # Errors. |
paul@83 | 51 | |
paul@39 | 52 | class ProcessingError(Exception): |
paul@39 | 53 | |
paul@39 | 54 | "A processing error." |
paul@39 | 55 | |
paul@63 | 56 | pass |
paul@63 | 57 | |
paul@84 | 58 | class TableError(ProcessingError): |
paul@84 | 59 | |
paul@84 | 60 | "An error occurring during access to a lookup table." |
paul@84 | 61 | |
paul@84 | 62 | pass |
paul@84 | 63 | |
paul@243 | 64 | class TableGenerationError(ProcessingError): |
paul@243 | 65 | |
paul@243 | 66 | "An error occurring when generating a lookup table." |
paul@243 | 67 | |
paul@243 | 68 | pass |
paul@243 | 69 | |
paul@63 | 70 | class NodeProcessingError(ProcessingError): |
paul@63 | 71 | |
paul@63 | 72 | "A processing error associated with a particular program node." |
paul@63 | 73 | |
paul@339 | 74 | def __init__(self, message): |
paul@39 | 75 | self.message = message |
paul@339 | 76 | self.unit_name = None |
paul@339 | 77 | self.astnode = None |
paul@339 | 78 | |
paul@339 | 79 | def get_lineno(self, node): |
paul@339 | 80 | lineno = node.lineno |
paul@339 | 81 | if lineno is not None: |
paul@339 | 82 | return lineno |
paul@339 | 83 | else: |
paul@339 | 84 | for child in node.getChildNodes(): |
paul@339 | 85 | lineno = self.get_lineno(child) |
paul@339 | 86 | if lineno is not None: |
paul@339 | 87 | return lineno |
paul@339 | 88 | return None |
paul@39 | 89 | |
paul@39 | 90 | def __repr__(self): |
paul@339 | 91 | return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) |
paul@39 | 92 | |
paul@39 | 93 | def __str__(self): |
paul@39 | 94 | return repr(self) |
paul@39 | 95 | |
paul@296 | 96 | class InspectError(NodeProcessingError): |
paul@296 | 97 | |
paul@296 | 98 | "An error during the module inspection process." |
paul@296 | 99 | |
paul@296 | 100 | pass |
paul@296 | 101 | |
paul@83 | 102 | class TranslateError(NodeProcessingError): |
paul@83 | 103 | |
paul@83 | 104 | "An error during the module translation process." |
paul@83 | 105 | |
paul@83 | 106 | pass |
paul@83 | 107 | |
paul@100 | 108 | class TranslationNotImplementedError(TranslateError): |
paul@100 | 109 | |
paul@100 | 110 | "An error caused by a node not being supported in translation." |
paul@100 | 111 | |
paul@100 | 112 | pass |
paul@100 | 113 | |
paul@343 | 114 | # Special representations. |
paul@343 | 115 | |
paul@343 | 116 | class AtLeast: |
paul@343 | 117 | |
paul@343 | 118 | "A special representation for numbers of a given value or greater." |
paul@343 | 119 | |
paul@343 | 120 | def __init__(self, count): |
paul@343 | 121 | self.count = count |
paul@343 | 122 | |
paul@343 | 123 | def __eq__(self, other): |
paul@343 | 124 | return 0 |
paul@343 | 125 | |
paul@343 | 126 | __lt__ = __le__ = __eq__ |
paul@343 | 127 | |
paul@343 | 128 | def __ne__(self, other): |
paul@343 | 129 | return 1 |
paul@343 | 130 | |
paul@343 | 131 | def __gt__(self, other): |
paul@343 | 132 | if isinstance(other, AtLeast): |
paul@343 | 133 | return 0 |
paul@343 | 134 | else: |
paul@343 | 135 | return self.count > other |
paul@343 | 136 | |
paul@343 | 137 | def __ge__(self, other): |
paul@343 | 138 | if isinstance(other, AtLeast): |
paul@343 | 139 | return 0 |
paul@343 | 140 | else: |
paul@343 | 141 | return self.count >= other |
paul@343 | 142 | |
paul@343 | 143 | def __iadd__(self, other): |
paul@343 | 144 | if isinstance(other, AtLeast): |
paul@343 | 145 | self.count += other.count |
paul@343 | 146 | else: |
paul@343 | 147 | self.count += other |
paul@343 | 148 | return self |
paul@343 | 149 | |
paul@343 | 150 | def __radd__(self, other): |
paul@343 | 151 | if isinstance(other, AtLeast): |
paul@343 | 152 | return AtLeast(self.count + other.count) |
paul@343 | 153 | else: |
paul@343 | 154 | return AtLeast(self.count + other) |
paul@343 | 155 | |
paul@343 | 156 | def __repr__(self): |
paul@343 | 157 | return "AtLeast(%r)" % self.count |
paul@343 | 158 | |
paul@172 | 159 | # Useful data. |
paul@172 | 160 | |
paul@172 | 161 | comparison_methods = { |
paul@232 | 162 | "==" : ("__eq__", "__eq__"), |
paul@232 | 163 | "!=" : ("__ne__", "__ne__"), |
paul@172 | 164 | "<" : ("__lt__", "__gt__"), |
paul@172 | 165 | "<=" : ("__le__", "__ge__"), |
paul@172 | 166 | ">=" : ("__ge__", "__le__"), |
paul@172 | 167 | ">" : ("__gt__", "__lt__"), |
paul@172 | 168 | "is" : None, |
paul@172 | 169 | "is not" : None, |
paul@172 | 170 | "in" : None, |
paul@172 | 171 | "not in" : None |
paul@172 | 172 | } |
paul@172 | 173 | |
paul@172 | 174 | augassign_methods = { |
paul@172 | 175 | "+=" : ("__iadd__", ("__add__", "__radd__")), |
paul@172 | 176 | "-=" : ("__isub__", ("__sub__", "__rsub__")), |
paul@172 | 177 | "*=" : ("__imul__", ("__mul__", "__rmul__")), |
paul@172 | 178 | "/=" : ("__idiv__", ("__div__", "__rdiv__")), |
paul@324 | 179 | "//=" : ("__ifloordiv__", ("__floordiv__", "__rfloordiv__")), |
paul@172 | 180 | "%=" : ("__imod__", ("__mod__", "__rmod__")), |
paul@172 | 181 | "**=" : ("__ipow__", ("__pow__", "__rpow__")), |
paul@172 | 182 | "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), |
paul@172 | 183 | ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), |
paul@172 | 184 | "&=" : ("__iand__", ("__and__", "__rand__")), |
paul@172 | 185 | "^=" : ("__ixor__", ("__xor__", "__rxor__")), |
paul@172 | 186 | "|=" : ("__ior__", ("__or__", "__ror__")) |
paul@172 | 187 | } |
paul@172 | 188 | |
paul@172 | 189 | binary_methods = { |
paul@172 | 190 | "Add" : ("__add__", "__radd__"), |
paul@172 | 191 | "Bitand" : ("__and__", "__rand__"), |
paul@172 | 192 | "Bitor" : ("__or__", "__ror__"), |
paul@172 | 193 | "Bitxor" : ("__xor__", "__rxor__"), |
paul@172 | 194 | "Div" : ("__div__", "__rdiv__"), |
paul@172 | 195 | "FloorDiv" : ("__floordiv__", "__rfloordiv__"), |
paul@172 | 196 | "LeftShift" : ("__lshift__", "__rlshift__"), |
paul@172 | 197 | "Mod" : ("__mod__", "__rmod__"), |
paul@172 | 198 | "Mul" : ("__mul__", "__rmul__"), |
paul@172 | 199 | "Power" : ("__pow__", "__rpow__"), |
paul@172 | 200 | "RightShift" : ("__rshift__", "__rrshift__"), |
paul@172 | 201 | "Sub" : ("__sub__", "__rsub__") |
paul@172 | 202 | } |
paul@172 | 203 | |
paul@172 | 204 | unary_methods = { |
paul@172 | 205 | "Invert" : "__invert__", |
paul@172 | 206 | "UnaryAdd" : "__pos__", |
paul@172 | 207 | "UnarySub" : "__neg__" |
paul@172 | 208 | } |
paul@172 | 209 | |
paul@39 | 210 | # vim: tabstop=4 expandtab shiftwidth=4 |