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@17 | 65 | handler Any exception handler selector expression. |
paulb@16 | 66 | |
paulb@16 | 67 | Invocation and subprogram attributes: |
paulb@16 | 68 | |
paulb@16 | 69 | args Any collection of argument nodes. |
paulb@16 | 70 | params Any collection of parameter nodes and defaults. |
paulb@16 | 71 | |
paulb@16 | 72 | Statement-grouping attributes: |
paulb@16 | 73 | |
paulb@16 | 74 | body Any conditional code depending on the success of a test. |
paulb@16 | 75 | else_ Any conditional code depending on the failure of a test. |
paulb@16 | 76 | finally_ Any code which will be executed regardless. |
paulb@16 | 77 | code Any unconditional code. |
paul@0 | 78 | """ |
paul@0 | 79 | |
paul@0 | 80 | def __init__(self, original=None, **kw): |
paul@0 | 81 | self.original = original |
paulb@8 | 82 | if self.original is not None: |
paulb@8 | 83 | self.original._node = self |
paul@0 | 84 | for name, value in kw.items(): |
paul@0 | 85 | setattr(self, name, value) |
paul@0 | 86 | |
paul@0 | 87 | def __repr__(self): |
paul@0 | 88 | if hasattr(self, "name"): |
paul@0 | 89 | return "%s '%s' (at %x)" % (self.__class__, self.name, id(self)) |
paul@2 | 90 | if hasattr(self, "index"): |
paulb@6 | 91 | return "%s (%s) (at %x)" % (self.__class__, self.index, id(self)) |
paul@0 | 92 | elif hasattr(self, "value"): |
paul@0 | 93 | return "%s %s (at %x)" % (self.__class__, repr(self.value), id(self)) |
paul@0 | 94 | elif hasattr(self, "ref"): |
paulb@28 | 95 | return "%s '%x' (at %x)" % (self.__class__, id(self.ref), id(self)) |
paul@0 | 96 | else: |
paul@0 | 97 | return "%s (at %x)" % (self.__class__, id(self)) |
paul@0 | 98 | |
paul@0 | 99 | def _pprint(self, indent, continuation, s): |
paul@0 | 100 | if continuation: |
paul@0 | 101 | print (" " * max(0, indent - len(continuation))) + continuation + s |
paul@0 | 102 | else: |
paul@0 | 103 | print (" " * indent) + s |
paul@0 | 104 | |
paul@0 | 105 | def pprint(self, indent=0, continuation=None): |
paul@0 | 106 | self._pprint(indent, continuation, repr(self)) |
paul@0 | 107 | |
paul@0 | 108 | # Show other details. |
paul@0 | 109 | |
paul@0 | 110 | if hasattr(self, "params"): |
paul@0 | 111 | for name, default in self.params: |
paul@0 | 112 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) |
paulb@24 | 113 | if hasattr(self, "star") and self.star: |
paulb@24 | 114 | name, default = self.star |
paulb@24 | 115 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) |
paulb@24 | 116 | if hasattr(self, "dstar") and self.dstar: |
paulb@24 | 117 | name, default = self.dstar |
paulb@24 | 118 | self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) |
paul@0 | 119 | if getattr(self, "acquire_locals", 0): |
paul@0 | 120 | self._pprint(indent + 2, "( ", "acquiring locals") |
paulb@18 | 121 | if getattr(self, "structure", 0): |
paulb@18 | 122 | self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name) |
paul@0 | 123 | if hasattr(self, "test"): |
paul@0 | 124 | self.test.pprint(indent + 2, "? ") |
paulb@17 | 125 | for attr in "code", "body", "else_", "finally_": |
paul@0 | 126 | if hasattr(self, attr) and getattr(self, attr): |
paul@0 | 127 | self._pprint(indent, "", "{ (%s)" % attr) |
paul@0 | 128 | for node in getattr(self, attr): |
paul@0 | 129 | node.pprint(indent + 2) |
paul@0 | 130 | self._pprint(indent, "", "}") |
paulb@17 | 131 | if hasattr(self, "handler"): |
paulb@17 | 132 | self.handler.pprint(indent + 2, "! ") |
paul@0 | 133 | if hasattr(self, "expr"): |
paul@0 | 134 | self.expr.pprint(indent + 2, "- ") |
paul@1 | 135 | if hasattr(self, "nodes"): |
paul@1 | 136 | for node in self.nodes: |
paul@1 | 137 | node.pprint(indent + 2, "- ") |
paul@0 | 138 | if hasattr(self, "lvalue"): |
paul@0 | 139 | self.lvalue.pprint(indent + 2, "= ") |
paul@0 | 140 | if hasattr(self, "args"): |
paul@0 | 141 | for arg in self.args: |
paul@0 | 142 | arg.pprint(indent + 2, "( ") |
paulb@24 | 143 | if hasattr(self, "star") and self.star: |
paulb@24 | 144 | self.star.pprint(indent + 2, "( ") |
paulb@24 | 145 | if hasattr(self, "dstar") and self.dstar: |
paulb@24 | 146 | self.dstar.pprint(indent + 2, "( ") |
paul@0 | 147 | |
paul@0 | 148 | class Module(Node): "A Python module." |
paul@0 | 149 | class Subprogram(Node): "A subprogram: functions, methods and loops." |
paul@0 | 150 | class Pass(Node): "A placeholder node corresponding to pass." |
paul@0 | 151 | class Invoke(Node): "A function, method or loop invocation." |
paul@0 | 152 | class Return(Node): "Return an evaluated expression." |
paul@0 | 153 | class Assign(Node): "A grouping node for assignment-related operations." |
paul@0 | 154 | class Keyword(Node): "A grouping node for keyword arguments." |
paul@1 | 155 | class Global(Node): "A global name designator." |
paul@3 | 156 | class Import(Node): "A module import operation." |
paul@0 | 157 | class LoadTemp(Node): "Load a previously-stored temporary value." |
paul@0 | 158 | class LoadName(Node): "Load a named object." |
paulb@21 | 159 | class LoadGlobal(Node): "Load a named global object." |
paul@0 | 160 | class LoadAttr(Node): "Load an object attribute." |
paul@0 | 161 | class LoadRef(Node): "Load a reference, typically a subprogram." |
paul@0 | 162 | class LoadExc(Node): "Load a handled exception." |
paul@0 | 163 | class StoreTemp(Node): "Store a temporary value." |
paul@0 | 164 | class StoreName(Node): "Associate a name with an object." |
paulb@21 | 165 | class StoreGlobal(Node): "Associate a name with an object in the global namespace." |
paul@0 | 166 | class StoreAttr(Node): "Associate an object's attribute with a value." |
paul@0 | 167 | class ReleaseTemp(Node): "Release a temporary value." |
paul@0 | 168 | class Conditional(Node): "A conditional node consisting of a test and outcomes." |
paul@0 | 169 | class Try(Node): "A try...except...else...finally grouping node." |
paul@5 | 170 | class Raise(Node): "An exception raising node." |
paul@1 | 171 | class Not(Node): "A negation of an expression." |
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@28 | 189 | class Class(Structure): "A Python class." |
paulb@28 | 190 | class Instance(Structure): "An instance." |
paulb@28 | 191 | class Constant(Instance): "A constant." |
paulb@19 | 192 | |
paulb@31 | 193 | class Namespace: |
paulb@31 | 194 | |
paulb@31 | 195 | """ |
paulb@31 | 196 | A local namespace which may either relate to a genuine set of function |
paulb@31 | 197 | locals or the initialisation of a structure. |
paulb@31 | 198 | """ |
paulb@31 | 199 | |
paulb@31 | 200 | def __init__(self, structure=None): |
paulb@31 | 201 | self.structure = structure |
paulb@31 | 202 | if structure is not None: |
paulb@31 | 203 | self.local = "structure" |
paulb@31 | 204 | else: |
paulb@31 | 205 | self.local = "local" |
paulb@31 | 206 | self.names = {} |
paulb@31 | 207 | self.not_local = [] |
paulb@31 | 208 | |
paulb@31 | 209 | def make_global(self, name): |
paulb@31 | 210 | if name not in self.not_local: |
paulb@31 | 211 | self.not_local.append(name) |
paulb@31 | 212 | |
paulb@31 | 213 | def find_for_store(self, name): |
paulb@31 | 214 | if name not in self.not_local: |
paulb@31 | 215 | return self.local |
paulb@31 | 216 | else: |
paulb@31 | 217 | return "global" |
paulb@31 | 218 | |
paulb@31 | 219 | def find_for_load(self, name): |
paulb@31 | 220 | if name not in self.not_local and self.names.has_key(name): |
paulb@31 | 221 | return self.local |
paulb@31 | 222 | else: |
paulb@31 | 223 | return "global" |
paulb@31 | 224 | |
paulb@31 | 225 | def store(self, name, types=None): |
paulb@31 | 226 | if name not in self.not_local: |
paulb@31 | 227 | self.names[name] = types |
paulb@31 | 228 | else: |
paulb@31 | 229 | raise KeyError, name |
paulb@31 | 230 | |
paulb@31 | 231 | def load(self, name): |
paulb@31 | 232 | if name in self.not_local or not self.names.has_key(name): |
paulb@31 | 233 | raise KeyError, name |
paulb@31 | 234 | else: |
paulb@31 | 235 | return self.names[name] |
paulb@31 | 236 | |
paulb@31 | 237 | def merge(self, namespace): |
paulb@31 | 238 | self.merge_items(namespace.names.items()) |
paulb@31 | 239 | |
paulb@31 | 240 | def merge_items(self, items): |
paulb@31 | 241 | for name, types in items: |
paulb@31 | 242 | if not self.names.has_key(name): |
paulb@31 | 243 | self.names[name] = types |
paulb@31 | 244 | else: |
paulb@31 | 245 | existing = self.names[name] |
paulb@31 | 246 | for type in types: |
paulb@31 | 247 | if type not in existing: |
paulb@31 | 248 | existing.append(type) |
paulb@31 | 249 | |
paul@0 | 250 | # vim: tabstop=4 expandtab shiftwidth=4 |