paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Simplified AST nodes for easier type propagation and analysis. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This software is free software; you can redistribute it and/or |
paul@0 | 9 | modify it under the terms of the GNU General Public License as |
paul@0 | 10 | published by the Free Software Foundation; either version 2 of |
paul@0 | 11 | the License, or (at your option) any later version. |
paul@0 | 12 | |
paul@0 | 13 | This software is distributed in the hope that it will be useful, |
paul@0 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 16 | GNU General Public License for more details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public |
paul@0 | 19 | License along with this library; see the file LICENCE.txt |
paul@0 | 20 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paul@0 | 22 | """ |
paul@0 | 23 | |
paulb@19 | 24 | from compiler.visitor import ASTVisitor |
paulb@19 | 25 | |
paulb@24 | 26 | # Elementary visitor support. |
paulb@24 | 27 | |
paulb@24 | 28 | class Visitor(ASTVisitor): |
paulb@24 | 29 | |
paulb@24 | 30 | "A visitor base class." |
paulb@24 | 31 | |
paulb@24 | 32 | def __init__(self): |
paulb@24 | 33 | ASTVisitor.__init__(self) |
paulb@24 | 34 | |
paulb@24 | 35 | def default(self, node, *args): |
paulb@24 | 36 | raise ValueError, node.__class__ |
paulb@24 | 37 | |
paulb@24 | 38 | def dispatch(self, node, *args): |
paulb@24 | 39 | return ASTVisitor.dispatch(self, node, *args) |
paulb@24 | 40 | |
paulb@24 | 41 | def dispatches(self, nodes, *args): |
paulb@24 | 42 | results = [] |
paulb@24 | 43 | for node in nodes: |
paulb@24 | 44 | results.append(self.dispatch(node, *args)) |
paulb@24 | 45 | return results |
paulb@24 | 46 | |
paulb@24 | 47 | # Simplified program nodes. |
paulb@24 | 48 | |
paul@0 | 49 | class Node: |
paul@0 | 50 | |
paul@0 | 51 | """ |
paul@0 | 52 | A result node with common attributes: |
paul@0 | 53 | |
paul@0 | 54 | original The original node from which this node was created. |
paulb@16 | 55 | name Any name involved (variable or attribute). |
paulb@17 | 56 | index Any index involved (temporary variable name). |
paulb@17 | 57 | value Any constant value. |
paulb@17 | 58 | ref Any reference to (for example) subprograms. |
paulb@17 | 59 | |
paulb@17 | 60 | Expression-related attributes: |
paulb@17 | 61 | |
paul@0 | 62 | expr Any contributing expression. |
paulb@16 | 63 | lvalue Any target expression. |
paulb@16 | 64 | test Any test expression in a conditional instruction. |
paulb@16 | 65 | |
paulb@16 | 66 | Invocation and subprogram attributes: |
paulb@16 | 67 | |
paulb@16 | 68 | args Any collection of argument nodes. |
paulb@16 | 69 | params Any collection of parameter nodes and defaults. |
paulb@16 | 70 | |
paulb@16 | 71 | Statement-grouping attributes: |
paulb@16 | 72 | |
paulb@16 | 73 | body Any conditional code depending on the success of a test. |
paulb@16 | 74 | else_ Any conditional code depending on the failure of a test. |
paulb@35 | 75 | handler Any exception handler code. |
paulb@16 | 76 | finally_ Any code which will be executed regardless. |
paulb@16 | 77 | code Any unconditional code. |
paulb@32 | 78 | choices Any choices which may be included in the final program. |
paul@0 | 79 | """ |
paul@0 | 80 | |
paul@0 | 81 | def __init__(self, original=None, **kw): |
paul@0 | 82 | self.original = original |
paulb@8 | 83 | if self.original is not None: |
paulb@8 | 84 | self.original._node = self |
paul@0 | 85 | for name, value in kw.items(): |
paul@0 | 86 | setattr(self, name, value) |
paul@0 | 87 | |
paul@0 | 88 | def __repr__(self): |
paul@0 | 89 | if hasattr(self, "name"): |
paul@0 | 90 | return "%s '%s' (at %x)" % (self.__class__, self.name, id(self)) |
paul@2 | 91 | if hasattr(self, "index"): |
paulb@6 | 92 | return "%s (%s) (at %x)" % (self.__class__, self.index, id(self)) |
paul@0 | 93 | elif hasattr(self, "value"): |
paul@0 | 94 | return "%s %s (at %x)" % (self.__class__, repr(self.value), id(self)) |
paul@0 | 95 | elif hasattr(self, "ref"): |
paulb@28 | 96 | return "%s '%x' (at %x)" % (self.__class__, id(self.ref), id(self)) |
paul@0 | 97 | else: |
paul@0 | 98 | return "%s (at %x)" % (self.__class__, id(self)) |
paul@0 | 99 | |
paul@0 | 100 | def _pprint(self, indent, continuation, s): |
paul@0 | 101 | if continuation: |
paul@0 | 102 | print (" " * max(0, indent - len(continuation))) + continuation + s |
paul@0 | 103 | else: |
paul@0 | 104 | print (" " * indent) + s |
paul@0 | 105 | |
paul@0 | 106 | def pprint(self, indent=0, continuation=None): |
paul@0 | 107 | self._pprint(indent, continuation, repr(self)) |
paul@0 | 108 | |
paul@0 | 109 | # Show other details. |
paul@0 | 110 | |
paul@0 | 111 | if hasattr(self, "params"): |
paul@0 | 112 | for name, default in self.params: |
paul@0 | 113 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) |
paulb@24 | 114 | if hasattr(self, "star") and self.star: |
paulb@24 | 115 | name, default = self.star |
paulb@24 | 116 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) |
paulb@24 | 117 | if hasattr(self, "dstar") and self.dstar: |
paulb@24 | 118 | name, default = self.dstar |
paulb@24 | 119 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) |
paul@0 | 120 | if getattr(self, "acquire_locals", 0): |
paul@0 | 121 | self._pprint(indent + 2, "( ", "acquiring locals") |
paulb@18 | 122 | if getattr(self, "structure", 0): |
paulb@18 | 123 | self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name) |
paul@0 | 124 | if hasattr(self, "test"): |
paul@0 | 125 | self.test.pprint(indent + 2, "? ") |
paulb@35 | 126 | for attr in "code", "body", "else_", "handler", "finally_", "choices": |
paul@0 | 127 | if hasattr(self, attr) and getattr(self, attr): |
paul@0 | 128 | self._pprint(indent, "", "{ (%s)" % attr) |
paul@0 | 129 | for node in getattr(self, attr): |
paul@0 | 130 | node.pprint(indent + 2) |
paul@0 | 131 | self._pprint(indent, "", "}") |
paul@0 | 132 | if hasattr(self, "expr"): |
paul@0 | 133 | self.expr.pprint(indent + 2, "- ") |
paul@1 | 134 | if hasattr(self, "nodes"): |
paul@1 | 135 | for node in self.nodes: |
paul@1 | 136 | node.pprint(indent + 2, "- ") |
paul@0 | 137 | if hasattr(self, "lvalue"): |
paul@0 | 138 | self.lvalue.pprint(indent + 2, "= ") |
paul@0 | 139 | if hasattr(self, "args"): |
paul@0 | 140 | for arg in self.args: |
paul@0 | 141 | arg.pprint(indent + 2, "( ") |
paulb@24 | 142 | if hasattr(self, "star") and self.star: |
paulb@24 | 143 | self.star.pprint(indent + 2, "( ") |
paulb@24 | 144 | if hasattr(self, "dstar") and self.dstar: |
paulb@24 | 145 | self.dstar.pprint(indent + 2, "( ") |
paul@0 | 146 | |
paul@0 | 147 | class Module(Node): "A Python module." |
paul@0 | 148 | class Subprogram(Node): "A subprogram: functions, methods and loops." |
paul@0 | 149 | class Pass(Node): "A placeholder node corresponding to pass." |
paul@0 | 150 | class Invoke(Node): "A function, method or loop invocation." |
paul@0 | 151 | class Return(Node): "Return an evaluated expression." |
paul@0 | 152 | class Assign(Node): "A grouping node for assignment-related operations." |
paul@0 | 153 | class Keyword(Node): "A grouping node for keyword arguments." |
paul@1 | 154 | class Global(Node): "A global name designator." |
paul@3 | 155 | class Import(Node): "A module import operation." |
paul@0 | 156 | class LoadTemp(Node): "Load a previously-stored temporary value." |
paul@0 | 157 | class LoadName(Node): "Load a named object." |
paulb@21 | 158 | class LoadGlobal(Node): "Load a named global object." |
paul@0 | 159 | class LoadAttr(Node): "Load an object attribute." |
paulb@34 | 160 | class LoadRef(Node): "Load a reference, typically a subprogram or a constant." |
paul@0 | 161 | class LoadExc(Node): "Load a handled exception." |
paul@0 | 162 | class StoreTemp(Node): "Store a temporary value." |
paul@0 | 163 | class StoreName(Node): "Associate a name with an object." |
paulb@21 | 164 | class StoreGlobal(Node): "Associate a name with an object in the global namespace." |
paul@0 | 165 | class StoreAttr(Node): "Associate an object's attribute with a value." |
paul@0 | 166 | class ReleaseTemp(Node): "Release a temporary value." |
paul@0 | 167 | class Conditional(Node): "A conditional node consisting of a test and outcomes." |
paul@0 | 168 | class Try(Node): "A try...except...else...finally grouping node." |
paul@5 | 169 | class Raise(Node): "An exception raising node." |
paul@1 | 170 | class Not(Node): "A negation of an expression." |
paulb@32 | 171 | class Choice(Node): "A special node which indicates a choice of expressions." |
paulb@28 | 172 | |
paulb@28 | 173 | # Special non-program nodes. |
paulb@28 | 174 | |
paulb@28 | 175 | class Structure: |
paulb@28 | 176 | |
paulb@28 | 177 | "A non-program node containing some kind of namespace." |
paulb@28 | 178 | |
paulb@28 | 179 | def __init__(self, **kw): |
paulb@28 | 180 | for name, value in kw.items(): |
paulb@28 | 181 | setattr(self, name, value) |
paulb@28 | 182 | |
paulb@28 | 183 | def __repr__(self): |
paulb@28 | 184 | if hasattr(self, "name"): |
paulb@28 | 185 | return "%s '%s' (at %x)" % (self.__class__, self.name, id(self)) |
paulb@28 | 186 | else: |
paulb@28 | 187 | return "%s (at %x)" % (self.__class__, id(self)) |
paulb@28 | 188 | |
paulb@34 | 189 | class Class(Structure): |
paulb@34 | 190 | |
paulb@34 | 191 | "A Python class." |
paulb@34 | 192 | |
paulb@34 | 193 | class Instance(Structure): |
paulb@34 | 194 | |
paulb@34 | 195 | "An instance." |
paulb@34 | 196 | |
paulb@34 | 197 | def __init__(self, **kw): |
paulb@34 | 198 | Structure.__init__(self, **kw) |
paulb@34 | 199 | self.types = [self] |
paulb@34 | 200 | |
paulb@34 | 201 | class Constant(Instance): |
paulb@34 | 202 | |
paulb@34 | 203 | "A constant initialised with a type name for future processing." |
paulb@34 | 204 | |
paulb@34 | 205 | def __init__(self, **kw): |
paulb@34 | 206 | Instance.__init__(self, **kw) |
paulb@34 | 207 | self.typename = self.value.__class__.__name__ |
paulb@19 | 208 | |
paul@0 | 209 | # vim: tabstop=4 expandtab shiftwidth=4 |