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