paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | """ |
paul@39 | 4 | Common classes. |
paul@39 | 5 | |
paul@190 | 6 | Copyright (C) 2007, 2008, 2009 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@83 | 22 | try: |
paul@83 | 23 | set |
paul@83 | 24 | except NameError: |
paul@83 | 25 | from sets import Set as set |
paul@83 | 26 | |
paul@83 | 27 | # Errors. |
paul@83 | 28 | |
paul@39 | 29 | class ProcessingError(Exception): |
paul@39 | 30 | |
paul@39 | 31 | "A processing error." |
paul@39 | 32 | |
paul@63 | 33 | pass |
paul@63 | 34 | |
paul@83 | 35 | class InspectError(ProcessingError): |
paul@83 | 36 | |
paul@83 | 37 | "An error during the module inspection process." |
paul@83 | 38 | |
paul@83 | 39 | pass |
paul@83 | 40 | |
paul@84 | 41 | class TableError(ProcessingError): |
paul@84 | 42 | |
paul@84 | 43 | "An error occurring during access to a lookup table." |
paul@84 | 44 | |
paul@84 | 45 | pass |
paul@84 | 46 | |
paul@243 | 47 | class TableGenerationError(ProcessingError): |
paul@243 | 48 | |
paul@243 | 49 | "An error occurring when generating a lookup table." |
paul@243 | 50 | |
paul@243 | 51 | pass |
paul@243 | 52 | |
paul@63 | 53 | class NodeProcessingError(ProcessingError): |
paul@63 | 54 | |
paul@63 | 55 | "A processing error associated with a particular program node." |
paul@63 | 56 | |
paul@39 | 57 | def __init__(self, unit_name, node, message): |
paul@39 | 58 | self.unit_name = unit_name |
paul@91 | 59 | self.astnode = node |
paul@39 | 60 | self.message = message |
paul@39 | 61 | |
paul@39 | 62 | def __repr__(self): |
paul@91 | 63 | return "Error in %r at line %d: %s" % (self.unit_name, self.astnode.lineno, self.message) |
paul@39 | 64 | |
paul@39 | 65 | def __str__(self): |
paul@39 | 66 | return repr(self) |
paul@39 | 67 | |
paul@83 | 68 | class TranslateError(NodeProcessingError): |
paul@83 | 69 | |
paul@83 | 70 | "An error during the module translation process." |
paul@83 | 71 | |
paul@83 | 72 | pass |
paul@83 | 73 | |
paul@100 | 74 | class TranslationNotImplementedError(TranslateError): |
paul@100 | 75 | |
paul@100 | 76 | "An error caused by a node not being supported in translation." |
paul@100 | 77 | |
paul@100 | 78 | pass |
paul@100 | 79 | |
paul@172 | 80 | # Useful data. |
paul@172 | 81 | |
paul@172 | 82 | comparison_methods = { |
paul@232 | 83 | "==" : ("__eq__", "__eq__"), |
paul@232 | 84 | "!=" : ("__ne__", "__ne__"), |
paul@172 | 85 | "<" : ("__lt__", "__gt__"), |
paul@172 | 86 | "<=" : ("__le__", "__ge__"), |
paul@172 | 87 | ">=" : ("__ge__", "__le__"), |
paul@172 | 88 | ">" : ("__gt__", "__lt__"), |
paul@172 | 89 | "is" : None, |
paul@172 | 90 | "is not" : None, |
paul@172 | 91 | "in" : None, |
paul@172 | 92 | "not in" : None |
paul@172 | 93 | } |
paul@172 | 94 | |
paul@172 | 95 | augassign_methods = { |
paul@172 | 96 | "+=" : ("__iadd__", ("__add__", "__radd__")), |
paul@172 | 97 | "-=" : ("__isub__", ("__sub__", "__rsub__")), |
paul@172 | 98 | "*=" : ("__imul__", ("__mul__", "__rmul__")), |
paul@172 | 99 | "/=" : ("__idiv__", ("__div__", "__rdiv__")), |
paul@172 | 100 | "%=" : ("__imod__", ("__mod__", "__rmod__")), |
paul@172 | 101 | "**=" : ("__ipow__", ("__pow__", "__rpow__")), |
paul@172 | 102 | "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), |
paul@172 | 103 | ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), |
paul@172 | 104 | "&=" : ("__iand__", ("__and__", "__rand__")), |
paul@172 | 105 | "^=" : ("__ixor__", ("__xor__", "__rxor__")), |
paul@172 | 106 | "|=" : ("__ior__", ("__or__", "__ror__")) |
paul@172 | 107 | } |
paul@172 | 108 | |
paul@172 | 109 | binary_methods = { |
paul@172 | 110 | "Add" : ("__add__", "__radd__"), |
paul@172 | 111 | "Bitand" : ("__and__", "__rand__"), |
paul@172 | 112 | "Bitor" : ("__or__", "__ror__"), |
paul@172 | 113 | "Bitxor" : ("__xor__", "__rxor__"), |
paul@172 | 114 | "Div" : ("__div__", "__rdiv__"), |
paul@172 | 115 | "FloorDiv" : ("__floordiv__", "__rfloordiv__"), |
paul@172 | 116 | "LeftShift" : ("__lshift__", "__rlshift__"), |
paul@172 | 117 | "Mod" : ("__mod__", "__rmod__"), |
paul@172 | 118 | "Mul" : ("__mul__", "__rmul__"), |
paul@172 | 119 | "Power" : ("__pow__", "__rpow__"), |
paul@172 | 120 | "RightShift" : ("__rshift__", "__rrshift__"), |
paul@172 | 121 | "Sub" : ("__sub__", "__rsub__") |
paul@172 | 122 | } |
paul@172 | 123 | |
paul@172 | 124 | unary_methods = { |
paul@172 | 125 | "Invert" : "__invert__", |
paul@172 | 126 | "UnaryAdd" : "__pos__", |
paul@172 | 127 | "UnarySub" : "__neg__" |
paul@172 | 128 | } |
paul@172 | 129 | |
paul@39 | 130 | # vim: tabstop=4 expandtab shiftwidth=4 |