forked from gennad/Design-Patterns-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisitor.py
More file actions
34 lines (29 loc) · 710 Bytes
/
visitor.py
File metadata and controls
34 lines (29 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class CodeGeneratorVisitor(object):
@dispatch.on('node')
def visit(self, node):
"""This is the generic method"""
@visit.when(ASTNode)
def visit(self, node):
map(self.visit, node.children)
@visit.when(EchoStatement)
def visit(self, node):
self.visit(node.children)
print "print"
@visit.when(BinaryExpression)
def visit(self, node):
map(self.visit, node.children)
print node.props['operator']
@visit.when(Constant)
def visit(self, node):
print "push %d" % node.props['value']
sometree = None
CodeGeneratorVisitor().visit(sometree)
# Output:
# push 1
# print
# push 2
# push 4
# push 3
# multiply
# plus
# print