diff python/c3/visitor.py @ 288:a747a45dcd78

Various styling work
author Windel Bouwman
date Thu, 21 Nov 2013 14:26:13 +0100
parents e64bae57cda8
children
line wrap: on
line diff
--- a/python/c3/visitor.py	Thu Nov 21 11:57:27 2013 +0100
+++ b/python/c3/visitor.py	Thu Nov 21 14:26:13 2013 +0100
@@ -1,5 +1,6 @@
 from .astnodes import *
 
+
 class Visitor:
     """
         Visitor that can visit all nodes in the AST
@@ -54,7 +55,8 @@
             self.do(node.ptr)
         elif type(node) is Constant:
             self.do(node.value)
-        elif type(node) in [VariableUse, Variable, Literal, FunctionType, DefinedType, FormalParameter, LocalVariable]:
+        elif type(node) in [VariableUse, Variable, Literal, FunctionType,
+                            DefinedType, FormalParameter, LocalVariable]:
             # Those nodes do not have child nodes.
             pass
         elif type(node) is WhileStatement:
@@ -67,3 +69,17 @@
         if self.f_post:
             self.f_post(node)
 
+
+class AstPrinter:
+    """ Prints an AST as text """
+    def printAst(self, pkg):
+        self.indent = 0
+        visitor = Visitor()
+        visitor.visit(pkg, self.print1, self.print2)
+
+    def print1(self, node):
+        print(' ' * self.indent + str(node))
+        self.indent += 2
+
+    def print2(self, node):
+        self.indent -= 2