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