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