-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpn_translator.py
49 lines (38 loc) · 1.11 KB
/
rpn_translator.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
import lexer
import parser
class NodeVisitor(object):
def visit(self, node):
method_name = 'visit_' + type(node).__name__
visitor = getattr(self, method_name, self.generic_visit)
return visitor(node)
def generic_visit(self, node):
raise Exception('No visit_{} method'.format(type(node).__name__))
class RpnTranslator(NodeVisitor):
def __init__(self, parser):
self.parser = parser
def visit_BinOp(self, node):
return '{} {} {}'.format(
self.visit(node.left),
self.visit(node.right),
node.op.value)
def visit_Num(self, node):
return str(node.value)
def translate(self):
tree = self.parser.parse()
return self.visit(tree)
def main():
while True:
try:
text = raw_input('rpnt> ')
except EOFError:
break
if not text:
continue
l = lexer.Lexer(text)
p = parser.Parser(l)
t = RpnTranslator(p)
result = t.translate()
print(result)
if __name__ == '__main__':
main()