1.1 --- a/simplified.py Sat Oct 07 00:21:19 2006 +0200
1.2 +++ b/simplified.py Sat Oct 07 00:22:58 2006 +0200
1.3 @@ -22,6 +22,7 @@
1.4 """
1.5
1.6 from compiler.visitor import ASTVisitor
1.7 +import sys
1.8
1.9 # Elementary visitor support.
1.10
1.11 @@ -104,73 +105,75 @@
1.12 else:
1.13 return "%s" % (self.__class__.__name__,)
1.14
1.15 - def _pprint(self, indent, continuation, s):
1.16 + def _pprint(self, indent, continuation, s, stream=None):
1.17 + stream = stream or sys.stdout
1.18 if continuation:
1.19 - print (" " * max(0, indent - len(continuation))) + continuation + s
1.20 + print >>stream, (" " * max(0, indent - len(continuation))) + continuation + s
1.21 else:
1.22 - print (" " * indent) + s
1.23 + print >>stream, (" " * indent) + s
1.24
1.25 - def pprint(self, indent=0, continuation=None):
1.26 - self._pprint(indent, continuation, repr(self))
1.27 + def pprint(self, indent=0, continuation=None, stream=None):
1.28 + stream = stream or sys.stdout
1.29 + self._pprint(indent, continuation, repr(self), stream)
1.30
1.31 # Subprogram-related details.
1.32
1.33 if hasattr(self, "params"):
1.34 for name, default in self.params:
1.35 - self._pprint(indent + 2, "( ", "%s -> %s" % (name, default))
1.36 + self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream)
1.37 if hasattr(self, "star") and self.star:
1.38 name, default = self.star
1.39 - self._pprint(indent + 2, "( ", "%s -> %s" % (name, default))
1.40 + self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream)
1.41 if hasattr(self, "dstar") and self.dstar:
1.42 name, default = self.dstar
1.43 - self._pprint(indent + 2, "( ", "%s -> %s" % (name, default))
1.44 + self._pprint(indent + 2, "( ", "%s -> %s" % (name, default), stream=stream)
1.45 if getattr(self, "acquire_locals", 0):
1.46 - self._pprint(indent + 2, "( ", "acquiring locals")
1.47 + self._pprint(indent + 2, "( ", "acquiring locals", stream=stream)
1.48 if getattr(self, "structure", 0):
1.49 - self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name)
1.50 + self._pprint(indent + 2, "( ", "structure '%s'" % self.structure.name, stream=stream)
1.51
1.52 # Statement-related details.
1.53
1.54 if hasattr(self, "test"):
1.55 - self.test.pprint(indent + 2, "? ")
1.56 + self.test.pprint(indent + 2, "? ", stream=stream)
1.57 for attr in "code", "body", "else_", "handler", "finally_", "choices":
1.58 if hasattr(self, attr) and getattr(self, attr):
1.59 - self._pprint(indent, "", "%s {" % attr)
1.60 + self._pprint(indent, "", "%s {" % attr, stream=stream)
1.61 for node in getattr(self, attr):
1.62 - node.pprint(indent + 2)
1.63 - self._pprint(indent, "", "}")
1.64 + node.pprint(indent + 2, stream=stream)
1.65 + self._pprint(indent, "", "}", stream=stream)
1.66
1.67 # Expression-related details.
1.68
1.69 if hasattr(self, "expr"):
1.70 - self.expr.pprint(indent + 2, "- ")
1.71 + self.expr.pprint(indent + 2, "- ", stream=stream)
1.72 if hasattr(self, "nodes"):
1.73 for node in self.nodes:
1.74 - node.pprint(indent + 2, "- ")
1.75 + node.pprint(indent + 2, "- ", stream=stream)
1.76 if hasattr(self, "lvalue"):
1.77 - self.lvalue.pprint(indent + 2, "->")
1.78 + self.lvalue.pprint(indent + 2, "->", stream=stream)
1.79 if hasattr(self, "nstype"):
1.80 - self._pprint(indent + 2, "", self.nstype)
1.81 + self._pprint(indent + 2, "", self.nstype, stream=stream)
1.82 if hasattr(self, "args"):
1.83 for arg in self.args:
1.84 - arg.pprint(indent + 2, "( ")
1.85 + arg.pprint(indent + 2, "( ", stream=stream)
1.86 if hasattr(self, "star") and self.star:
1.87 - self.star.pprint(indent + 2, "( ")
1.88 + self.star.pprint(indent + 2, "( ", stream=stream)
1.89 if hasattr(self, "dstar") and self.dstar:
1.90 - self.dstar.pprint(indent + 2, "( ")
1.91 + self.dstar.pprint(indent + 2, "( ", stream=stream)
1.92
1.93 # Annotations.
1.94
1.95 if hasattr(self, "accesses"):
1.96 - self._pprint(indent, "", "--------")
1.97 + self._pprint(indent, "", "--------", stream=stream)
1.98 for ref, attributes in self.accesses.items():
1.99 - self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes])))
1.100 - self._pprint(indent, "", "--------")
1.101 + self._pprint(indent + 2, "| ", "when %s: %s" % (ref, ", ".join([("%s via %s" % attr_acc) for attr_acc in attributes])), stream=stream)
1.102 + self._pprint(indent, "", "--------", stream=stream)
1.103 if hasattr(self, "writes"):
1.104 - self._pprint(indent, "", "--------")
1.105 + self._pprint(indent, "", "--------", stream=stream)
1.106 for ref, attribute in self.writes.items():
1.107 - self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute))
1.108 - self._pprint(indent, "", "--------")
1.109 + self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream)
1.110 + self._pprint(indent, "", "--------", stream=stream)
1.111
1.112 class Module(Node): "A Python module."
1.113 class Subprogram(Node): "A subprogram: functions, methods and loops."
1.114 @@ -206,7 +209,8 @@
1.115
1.116 "A non-program node containing some kind of namespace."
1.117
1.118 - def __init__(self, **kw):
1.119 + def __init__(self, original=None, **kw):
1.120 + self.original = original
1.121 for name, value in kw.items():
1.122 setattr(self, name, value)
1.123
1.124 @@ -226,15 +230,15 @@
1.125
1.126 "An instance."
1.127
1.128 - def __init__(self, **kw):
1.129 - Structure.__init__(self, **kw)
1.130 + def __init__(self, *args, **kw):
1.131 + Structure.__init__(self, *args, **kw)
1.132
1.133 class Constant(Instance):
1.134
1.135 "A constant initialised with a type name for future processing."
1.136
1.137 - def __init__(self, **kw):
1.138 - Instance.__init__(self, **kw)
1.139 + def __init__(self, *args, **kw):
1.140 + Instance.__init__(self, *args, **kw)
1.141 self.typename = self.value.__class__.__name__
1.142
1.143 # vim: tabstop=4 expandtab shiftwidth=4