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@90 | 76 | class Label: |
paul@90 | 77 | |
paul@90 | 78 | "A reference to a location." |
paul@90 | 79 | |
paul@90 | 80 | def __init__(self, number, location=None): |
paul@90 | 81 | self.number = number |
paul@90 | 82 | self.location = location |
paul@90 | 83 | |
paul@90 | 84 | def __repr__(self): |
paul@90 | 85 | return "Label(%r, location=%r)" % (self.number, self.location) |
paul@90 | 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@39 | 148 | # vim: tabstop=4 expandtab shiftwidth=4 |