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