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