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