1 class Expr: 2 3 "An expression." 4 5 def __init__(self, ops): 6 self.ops = ops 7 8 def children(self): 9 return self.ops 10 11 class Binary: 12 13 "A binary operator." 14 15 def __init__(self, left, op, right): 16 self.left = left 17 self.op = op 18 self.right = right 19 20 def children(self): 21 return self.left, self.right 22 23 class Unary: 24 25 "A unary operator." 26 27 def __init__(self, op, operand): 28 self.op = op 29 self.operand = operand 30 31 def children(self): 32 return self.operand, 33 34 class Value: 35 36 "A general value." 37 38 def __init__(self, value): 39 self.value = value 40 41 def children(self): 42 return () 43 44 class Visitor: 45 46 "Visit nodes in an expression tree." 47 48 def __init__(self): 49 self.indent = 0 50 51 def visit(self, node): 52 53 # Obtain the method for the node name. 54 55 fn = getattr(self, node.__name__) 56 57 # Call the method. 58 59 fn(node) 60 61 # Visit the node's children. 62 63 self.visitChildren(node) 64 65 def visitChildren(self, node): 66 self.indent += 1 67 for n in node.children(): 68 self.visit(n) 69 self.indent -= 1 70 71 def writeIndent(self): 72 i = 0 73 while i < self.indent: 74 print "", 75 i += 1 76 77 def Expr(self, node): 78 self.writeIndent() 79 print "Expression..." 80 81 def Binary(self, node): 82 self.writeIndent() 83 print "Binary operation", node.op 84 85 def Unary(self, node): 86 self.writeIndent() 87 print "Unary operation", node.op 88 89 def Value(self, node): 90 self.writeIndent() 91 print "Value", node.value 92 93 # Test the visitor with an example expression. 94 95 expr = Expr([Binary(Value(1), "+", Binary(Unary("-", Value(2)), "*", Value(3)))]) 96 Visitor().visit(expr)