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@60 | 25 | import sys |
paulb@19 | 26 | |
paulb@24 | 27 | # Elementary visitor support. |
paulb@24 | 28 | |
paulb@24 | 29 | class Visitor(ASTVisitor): |
paulb@24 | 30 | |
paulb@24 | 31 | "A visitor base class." |
paulb@24 | 32 | |
paulb@24 | 33 | def __init__(self): |
paulb@24 | 34 | ASTVisitor.__init__(self) |
paulb@24 | 35 | |
paulb@24 | 36 | def default(self, node, *args): |
paulb@24 | 37 | raise ValueError, node.__class__ |
paulb@24 | 38 | |
paulb@24 | 39 | def dispatch(self, node, *args): |
paulb@24 | 40 | return ASTVisitor.dispatch(self, node, *args) |
paulb@24 | 41 | |
paulb@24 | 42 | def dispatches(self, nodes, *args): |
paulb@24 | 43 | results = [] |
paulb@24 | 44 | for node in nodes: |
paulb@24 | 45 | results.append(self.dispatch(node, *args)) |
paulb@24 | 46 | return results |
paulb@24 | 47 | |
paulb@24 | 48 | # Simplified program nodes. |
paulb@24 | 49 | |
paul@0 | 50 | class Node: |
paul@0 | 51 | |
paul@0 | 52 | """ |
paul@0 | 53 | A result node with common attributes: |
paul@0 | 54 | |
paul@0 | 55 | original The original node from which this node was created. |
paulb@16 | 56 | name Any name involved (variable or attribute). |
paulb@17 | 57 | index Any index involved (temporary variable name). |
paulb@17 | 58 | value Any constant value. |
paulb@17 | 59 | ref Any reference to (for example) subprograms. |
paulb@47 | 60 | nstype Any indication of the namespace type involved in a name access. |
paulb@17 | 61 | |
paulb@17 | 62 | Expression-related attributes: |
paulb@17 | 63 | |
paul@0 | 64 | expr Any contributing expression. |
paulb@16 | 65 | lvalue Any target expression. |
paulb@16 | 66 | test Any test expression in a conditional instruction. |
paulb@16 | 67 | |
paulb@16 | 68 | Invocation and subprogram attributes: |
paulb@16 | 69 | |
paulb@16 | 70 | args Any collection of argument nodes. |
paulb@16 | 71 | params Any collection of parameter nodes and defaults. |
paulb@16 | 72 | |
paulb@16 | 73 | Statement-grouping attributes: |
paulb@16 | 74 | |
paulb@16 | 75 | body Any conditional code depending on the success of a test. |
paulb@16 | 76 | else_ Any conditional code depending on the failure of a test. |
paulb@35 | 77 | handler Any exception handler code. |
paulb@16 | 78 | finally_ Any code which will be executed regardless. |
paulb@16 | 79 | code Any unconditional code. |
paulb@32 | 80 | choices Any choices which may be included in the final program. |
paul@0 | 81 | """ |
paul@0 | 82 | |
paul@0 | 83 | def __init__(self, original=None, **kw): |
paulb@47 | 84 | |
paulb@47 | 85 | """ |
paulb@47 | 86 | Initialise a program node with an optional link to an 'original' AST |
paulb@47 | 87 | node. |
paulb@47 | 88 | """ |
paulb@47 | 89 | |
paul@0 | 90 | self.original = original |
paulb@8 | 91 | if self.original is not None: |
paulb@8 | 92 | self.original._node = self |
paul@0 | 93 | for name, value in kw.items(): |
paul@0 | 94 | setattr(self, name, value) |
paul@0 | 95 | |
paul@0 | 96 | def __repr__(self): |
paul@0 | 97 | if hasattr(self, "name"): |
paulb@57 | 98 | return "%s '%s' (at %x)" % (self.__class__.__name__, self.name, id(self)) |
paul@2 | 99 | if hasattr(self, "index"): |
paulb@57 | 100 | return "%s (%s)" % (self.__class__.__name__, self.index) |
paul@0 | 101 | elif hasattr(self, "value"): |
paulb@57 | 102 | return "%s %s" % (self.__class__.__name__, repr(self.value)) |
paul@0 | 103 | elif hasattr(self, "ref"): |
paulb@57 | 104 | return "%s '%x' (at %x)" % (self.__class__.__name__, id(self.ref), id(self)) |
paul@0 | 105 | else: |
paulb@57 | 106 | return "%s" % (self.__class__.__name__,) |
paul@0 | 107 | |
paulb@60 | 108 | def _pprint(self, indent, continuation, s, stream=None): |
paulb@60 | 109 | stream = stream or sys.stdout |
paul@0 | 110 | if continuation: |
paulb@60 | 111 | print >>stream, (" " * max(0, indent - len(continuation))) + continuation + s |
paul@0 | 112 | else: |
paulb@60 | 113 | print >>stream, (" " * indent) + s |
paul@0 | 114 | |
paulb@60 | 115 | def pprint(self, indent=0, continuation=None, stream=None): |
paulb@60 | 116 | stream = stream or sys.stdout |
paulb@60 | 117 | self._pprint(indent, continuation, repr(self), stream) |
paul@0 | 118 | |
paulb@47 | 119 | # Subprogram-related details. |
paul@0 | 120 | |
paul@0 | 121 | if hasattr(self, "params"): |
paul@0 | 122 | for name, default in self.params: |
paulb@60 | 123 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream) |
paulb@24 | 124 | if hasattr(self, "star") and self.star: |
paulb@24 | 125 | name, default = self.star |
paulb@60 | 126 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream) |
paulb@24 | 127 | if hasattr(self, "dstar") and self.dstar: |
paulb@24 | 128 | name, default = self.dstar |
paulb@60 | 129 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream) |
paul@0 | 130 | if getattr(self, "acquire_locals", 0): |
paulb@60 | 131 | self._pprint(indent + 2, "( ", "acquiring locals", stream=stream) |
paulb@18 | 132 | if getattr(self, "structure", 0): |
paulb@60 | 133 | self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name, stream=stream) |
paulb@47 | 134 | |
paulb@47 | 135 | # Statement-related details. |
paulb@47 | 136 | |
paul@0 | 137 | if hasattr(self, "test"): |
paulb@60 | 138 | self.test.pprint(indent + 2, "? ", stream=stream) |
paulb@35 | 139 | for attr in "code", "body", "else_", "handler", "finally_", "choices": |
paul@0 | 140 | if hasattr(self, attr) and getattr(self, attr): |
paulb@60 | 141 | self._pprint(indent, "", "%s {" % attr, stream=stream) |
paul@0 | 142 | for node in getattr(self, attr): |
paulb@60 | 143 | node.pprint(indent + 2, stream=stream) |
paulb@60 | 144 | self._pprint(indent, "", "}", stream=stream) |
paulb@47 | 145 | |
paulb@47 | 146 | # Expression-related details. |
paulb@47 | 147 | |
paul@0 | 148 | if hasattr(self, "expr"): |
paulb@60 | 149 | self.expr.pprint(indent + 2, "- ", stream=stream) |
paul@1 | 150 | if hasattr(self, "nodes"): |
paul@1 | 151 | for node in self.nodes: |
paulb@60 | 152 | node.pprint(indent + 2, "- ", stream=stream) |
paul@0 | 153 | if hasattr(self, "lvalue"): |
paulb@60 | 154 | self.lvalue.pprint(indent + 2, "->", stream=stream) |
paulb@47 | 155 | if hasattr(self, "nstype"): |
paulb@60 | 156 | self._pprint(indent + 2, "", self.nstype, stream=stream) |
paul@0 | 157 | if hasattr(self, "args"): |
paul@0 | 158 | for arg in self.args: |
paulb@60 | 159 | arg.pprint(indent + 2, "( ", stream=stream) |
paulb@24 | 160 | if hasattr(self, "star") and self.star: |
paulb@60 | 161 | self.star.pprint(indent + 2, "( ", stream=stream) |
paulb@24 | 162 | if hasattr(self, "dstar") and self.dstar: |
paulb@60 | 163 | self.dstar.pprint(indent + 2, "( ", stream=stream) |
paul@0 | 164 | |
paulb@39 | 165 | # Annotations. |
paulb@39 | 166 | |
paulb@39 | 167 | if hasattr(self, "accesses"): |
paulb@60 | 168 | self._pprint(indent, "", "--------", stream=stream) |
paulb@39 | 169 | for ref, attributes in self.accesses.items(): |
paulb@60 | 170 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes])), stream=stream) |
paulb@60 | 171 | self._pprint(indent, "", "--------", stream=stream) |
paulb@57 | 172 | if hasattr(self, "writes"): |
paulb@60 | 173 | self._pprint(indent, "", "--------", stream=stream) |
paulb@57 | 174 | for ref, attribute in self.writes.items(): |
paulb@60 | 175 | self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream) |
paulb@60 | 176 | self._pprint(indent, "", "--------", stream=stream) |
paulb@39 | 177 | |
paul@0 | 178 | class Module(Node): "A Python module." |
paul@0 | 179 | class Subprogram(Node): "A subprogram: functions, methods and loops." |
paul@0 | 180 | class Pass(Node): "A placeholder node corresponding to pass." |
paulb@57 | 181 | |
paulb@57 | 182 | class Invoke(Node): "An invocation." |
paulb@57 | 183 | class InvokeFunction(Invoke): "A function or method invocation." |
paulb@57 | 184 | class InvokeBlock(Invoke): "A block or loop invocation." |
paulb@57 | 185 | |
paul@0 | 186 | class Return(Node): "Return an evaluated expression." |
paul@0 | 187 | class Assign(Node): "A grouping node for assignment-related operations." |
paul@0 | 188 | class Keyword(Node): "A grouping node for keyword arguments." |
paul@1 | 189 | class Global(Node): "A global name designator." |
paul@3 | 190 | class Import(Node): "A module import operation." |
paul@0 | 191 | class LoadTemp(Node): "Load a previously-stored temporary value." |
paul@0 | 192 | class LoadName(Node): "Load a named object." |
paul@0 | 193 | class LoadAttr(Node): "Load an object attribute." |
paulb@34 | 194 | class LoadRef(Node): "Load a reference, typically a subprogram or a constant." |
paul@0 | 195 | class LoadExc(Node): "Load a handled exception." |
paul@0 | 196 | class StoreTemp(Node): "Store a temporary value." |
paul@0 | 197 | class StoreName(Node): "Associate a name with an object." |
paul@0 | 198 | class StoreAttr(Node): "Associate an object's attribute with a value." |
paul@0 | 199 | class ReleaseTemp(Node): "Release a temporary value." |
paul@0 | 200 | class Conditional(Node): "A conditional node consisting of a test and outcomes." |
paul@0 | 201 | class Try(Node): "A try...except...else...finally grouping node." |
paul@5 | 202 | class Raise(Node): "An exception raising node." |
paul@1 | 203 | class Not(Node): "A negation of an expression." |
paulb@32 | 204 | class Choice(Node): "A special node which indicates a choice of expressions." |
paulb@28 | 205 | |
paulb@28 | 206 | # Special non-program nodes. |
paulb@28 | 207 | |
paulb@28 | 208 | class Structure: |
paulb@28 | 209 | |
paulb@28 | 210 | "A non-program node containing some kind of namespace." |
paulb@28 | 211 | |
paulb@60 | 212 | def __init__(self, original=None, **kw): |
paulb@60 | 213 | self.original = original |
paulb@28 | 214 | for name, value in kw.items(): |
paulb@28 | 215 | setattr(self, name, value) |
paulb@28 | 216 | |
paulb@28 | 217 | def __repr__(self): |
paulb@28 | 218 | if hasattr(self, "name"): |
paulb@57 | 219 | return "%s '%s' (at %x)" % (self.__class__.__name__, self.name, id(self)) |
paulb@28 | 220 | else: |
paulb@57 | 221 | return "%s (at %x)" % (self.__class__.__name__, id(self)) |
paulb@28 | 222 | |
paulb@34 | 223 | class Class(Structure): |
paulb@34 | 224 | |
paulb@34 | 225 | "A Python class." |
paulb@34 | 226 | |
paulb@53 | 227 | pass |
paulb@53 | 228 | |
paulb@34 | 229 | class Instance(Structure): |
paulb@34 | 230 | |
paulb@34 | 231 | "An instance." |
paulb@34 | 232 | |
paulb@60 | 233 | def __init__(self, *args, **kw): |
paulb@60 | 234 | Structure.__init__(self, *args, **kw) |
paulb@34 | 235 | |
paulb@34 | 236 | class Constant(Instance): |
paulb@34 | 237 | |
paulb@34 | 238 | "A constant initialised with a type name for future processing." |
paulb@34 | 239 | |
paulb@60 | 240 | def __init__(self, *args, **kw): |
paulb@60 | 241 | Instance.__init__(self, *args, **kw) |
paulb@34 | 242 | self.typename = self.value.__class__.__name__ |
paulb@19 | 243 | |
paul@0 | 244 | # vim: tabstop=4 expandtab shiftwidth=4 |