# HG changeset patch # User paulb@jeremy # Date 1160173378 -7200 # Node ID 48b747a69cc6e46e54a800525ab92c51ecad863c # Parent 91656d3aa52bb58d6150d7b6d55decd73ea1ee5e Added original node attribute initialisation to structure-related classes. Introduced stream parameterisation to the pprint methods of the Node class. diff -r 91656d3aa52b -r 48b747a69cc6 simplified.py --- a/simplified.py Sat Oct 07 00:21:19 2006 +0200 +++ b/simplified.py Sat Oct 07 00:22:58 2006 +0200 @@ -22,6 +22,7 @@ """ from compiler.visitor import ASTVisitor +import sys # Elementary visitor support. @@ -104,73 +105,75 @@ else: return "%s" % (self.__class__.__name__,) - def _pprint(self, indent, continuation, s): + def _pprint(self, indent, continuation, s, stream=None): + stream = stream or sys.stdout if continuation: - print (" " * max(0, indent - len(continuation))) + continuation + s + print >>stream, (" " * max(0, indent - len(continuation))) + continuation + s else: - print (" " * indent) + s + print >>stream, (" " * indent) + s - def pprint(self, indent=0, continuation=None): - self._pprint(indent, continuation, repr(self)) + def pprint(self, indent=0, continuation=None, stream=None): + stream = stream or sys.stdout + self._pprint(indent, continuation, repr(self), stream) # Subprogram-related details. if hasattr(self, "params"): for name, default in self.params: - self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) + self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream) if hasattr(self, "star") and self.star: name, default = self.star - self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) + self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream) if hasattr(self, "dstar") and self.dstar: name, default = self.dstar - self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) + self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream) if getattr(self, "acquire_locals", 0): - self._pprint(indent + 2, "( ", "acquiring locals") + self._pprint(indent + 2, "( ", "acquiring locals", stream=stream) if getattr(self, "structure", 0): - self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name) + self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name, stream=stream) # Statement-related details. if hasattr(self, "test"): - self.test.pprint(indent + 2, "? ") + self.test.pprint(indent + 2, "? ", stream=stream) for attr in "code", "body", "else_", "handler", "finally_", "choices": if hasattr(self, attr) and getattr(self, attr): - self._pprint(indent, "", "%s {" % attr) + self._pprint(indent, "", "%s {" % attr, stream=stream) for node in getattr(self, attr): - node.pprint(indent + 2) - self._pprint(indent, "", "}") + node.pprint(indent + 2, stream=stream) + self._pprint(indent, "", "}", stream=stream) # Expression-related details. if hasattr(self, "expr"): - self.expr.pprint(indent + 2, "- ") + self.expr.pprint(indent + 2, "- ", stream=stream) if hasattr(self, "nodes"): for node in self.nodes: - node.pprint(indent + 2, "- ") + node.pprint(indent + 2, "- ", stream=stream) if hasattr(self, "lvalue"): - self.lvalue.pprint(indent + 2, "->") + self.lvalue.pprint(indent + 2, "->", stream=stream) if hasattr(self, "nstype"): - self._pprint(indent + 2, "", self.nstype) + self._pprint(indent + 2, "", self.nstype, stream=stream) if hasattr(self, "args"): for arg in self.args: - arg.pprint(indent + 2, "( ") + arg.pprint(indent + 2, "( ", stream=stream) if hasattr(self, "star") and self.star: - self.star.pprint(indent + 2, "( ") + self.star.pprint(indent + 2, "( ", stream=stream) if hasattr(self, "dstar") and self.dstar: - self.dstar.pprint(indent + 2, "( ") + self.dstar.pprint(indent + 2, "( ", stream=stream) # Annotations. if hasattr(self, "accesses"): - self._pprint(indent, "", "--------") + self._pprint(indent, "", "--------", stream=stream) for ref, attributes in self.accesses.items(): - self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes]))) - self._pprint(indent, "", "--------") + self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes])), stream=stream) + self._pprint(indent, "", "--------", stream=stream) if hasattr(self, "writes"): - self._pprint(indent, "", "--------") + self._pprint(indent, "", "--------", stream=stream) for ref, attribute in self.writes.items(): - self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute)) - self._pprint(indent, "", "--------") + self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream) + self._pprint(indent, "", "--------", stream=stream) class Module(Node): "A Python module." class Subprogram(Node): "A subprogram: functions, methods and loops." @@ -206,7 +209,8 @@ "A non-program node containing some kind of namespace." - def __init__(self, **kw): + def __init__(self, original=None, **kw): + self.original = original for name, value in kw.items(): setattr(self, name, value) @@ -226,15 +230,15 @@ "An instance." - def __init__(self, **kw): - Structure.__init__(self, **kw) + def __init__(self, *args, **kw): + Structure.__init__(self, *args, **kw) class Constant(Instance): "A constant initialised with a type name for future processing." - def __init__(self, **kw): - Instance.__init__(self, **kw) + def __init__(self, *args, **kw): + Instance.__init__(self, *args, **kw) self.typename = self.value.__class__.__name__ # vim: tabstop=4 expandtab shiftwidth=4