1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/simplified.py Sat Jul 08 21:47:12 2006 +0200
1.3 @@ -0,0 +1,109 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Simplified AST nodes for easier type propagation and analysis.
1.8 +
1.9 +Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This software is free software; you can redistribute it and/or
1.12 +modify it under the terms of the GNU General Public License as
1.13 +published by the Free Software Foundation; either version 2 of
1.14 +the License, or (at your option) any later version.
1.15 +
1.16 +This software is distributed in the hope that it will be useful,
1.17 +but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 +GNU General Public License for more details.
1.20 +
1.21 +You should have received a copy of the GNU General Public
1.22 +License along with this library; see the file LICENCE.txt
1.23 +If not, write to the Free Software Foundation, Inc.,
1.24 +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
1.25 +"""
1.26 +
1.27 +class Node:
1.28 +
1.29 + """
1.30 + A result node with common attributes:
1.31 +
1.32 + original The original node from which this node was created.
1.33 + statements Any underlying statements.
1.34 + expr Any contributing expression.
1.35 + name Any name involved (variable or attribute).
1.36 + code Any code grouped by this node.
1.37 + ref Any reference to (for example) subprograms.
1.38 + """
1.39 +
1.40 + def __init__(self, original=None, **kw):
1.41 + self.original = original
1.42 + for name, value in kw.items():
1.43 + setattr(self, name, value)
1.44 +
1.45 + def __repr__(self):
1.46 + if hasattr(self, "name"):
1.47 + return "%s '%s' (at %x)" % (self.__class__, self.name, id(self))
1.48 + elif hasattr(self, "value"):
1.49 + return "%s %s (at %x)" % (self.__class__, repr(self.value), id(self))
1.50 + elif hasattr(self, "ref"):
1.51 + return "%s '%s' (at %x)" % (self.__class__, self.ref.name, id(self))
1.52 + else:
1.53 + return "%s (at %x)" % (self.__class__, id(self))
1.54 +
1.55 + def _pprint(self, indent, continuation, s):
1.56 + if continuation:
1.57 + print (" " * max(0, indent - len(continuation))) + continuation + s
1.58 + else:
1.59 + print (" " * indent) + s
1.60 +
1.61 + def pprint(self, indent=0, continuation=None):
1.62 + self._pprint(indent, continuation, repr(self))
1.63 +
1.64 + # Show other details.
1.65 +
1.66 + if hasattr(self, "params"):
1.67 + for name, default in self.params:
1.68 + self._pprint(indent + 2, "( ", "%s -> %s" % (name, default))
1.69 + if getattr(self, "acquire_locals", 0):
1.70 + self._pprint(indent + 2, "( ", "acquiring locals")
1.71 + if hasattr(self, "spec"):
1.72 + self.spec.pprint(indent + 2, "E ")
1.73 + if hasattr(self, "test"):
1.74 + self.test.pprint(indent + 2, "? ")
1.75 + for attr in "code", "tests", "body", "handlers", "else_", "finally_":
1.76 + if hasattr(self, attr) and getattr(self, attr):
1.77 + self._pprint(indent, "", "{ (%s)" % attr)
1.78 + for node in getattr(self, attr):
1.79 + node.pprint(indent + 2)
1.80 + self._pprint(indent, "", "}")
1.81 + if hasattr(self, "expr"):
1.82 + self.expr.pprint(indent + 2, "- ")
1.83 + if hasattr(self, "lvalue"):
1.84 + self.lvalue.pprint(indent + 2, "= ")
1.85 + if hasattr(self, "args"):
1.86 + for arg in self.args:
1.87 + arg.pprint(indent + 2, "( ")
1.88 +
1.89 +class Module(Node): "A Python module."
1.90 +class Subprogram(Node): "A subprogram: functions, methods and loops."
1.91 +class Class(Node): "A Python class."
1.92 +class Pass(Node): "A placeholder node corresponding to pass."
1.93 +class Invoke(Node): "A function, method or loop invocation."
1.94 +class Return(Node): "Return an evaluated expression."
1.95 +class Assign(Node): "A grouping node for assignment-related operations."
1.96 +class Keyword(Node): "A grouping node for keyword arguments."
1.97 +class LoadTemp(Node): "Load a previously-stored temporary value."
1.98 +class LoadName(Node): "Load a named object."
1.99 +class LoadAttr(Node): "Load an object attribute."
1.100 +class LoadConst(Node): "Load a constant."
1.101 +class LoadRef(Node): "Load a reference, typically a subprogram."
1.102 +class LoadExc(Node): "Load a handled exception."
1.103 +class StoreTemp(Node): "Store a temporary value."
1.104 +class StoreName(Node): "Associate a name with an object."
1.105 +class StoreAttr(Node): "Associate an object's attribute with a value."
1.106 +class ReleaseTemp(Node): "Release a temporary value."
1.107 +class If(Node): "A multitest conditional node."
1.108 +class Conditional(Node): "A conditional node consisting of a test and outcomes."
1.109 +class Try(Node): "A try...except...else...finally grouping node."
1.110 +class Except(Node): "An exception handler node."
1.111 +
1.112 +# vim: tabstop=4 expandtab shiftwidth=4