-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
94 lines (69 loc) · 2.59 KB
/
interpreter.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from lark.visitors import Interpreter
import transformer
from symbol_table import st
# Transfomer visits a sub-tree bottom-up and run appropriate methods
trans = transformer.TreeTransformer()
# Interpreter is used to visit the root tree top-down
class GoInterpreter(Interpreter):
def program(self, tree):
return self.visit_children(tree)
def if_stmt(self, tree):
trans_cond = trans.transform(tree.children[0])
if trans_cond:
return self.visit_children(tree.children[1])[0]
elif len(tree.children) > 2:
if tree.children[2].data == "block_statement":
return self.visit_children(tree.children[2])[0]
elif tree.children[2].data == "if_stmt":
return self.visit(tree.children[2])
def while_loop(self, tree):
rel = tree.children[0]
evs = ""
while trans.transform(rel):
#trans_tree = trans.transform(rel)
blocks = tree.find_data('block_statement')
for block in blocks:
evl = self.visit_children(block)
for ev in evl:
if ev is not None:
evs = evs + "\n" + str(ev)
return evs
def for_loop(self, tree):
assign = tree.children[0]
trans.transform(assign)
rel = tree.children[1]
ident = tree.children[2]
evs = ""
while trans.transform(rel):
#trans_tree = trans.transform(rel)
#print("DEBUG: Transformed relation")
#print(trans_tree)
blocks = tree.find_data('block_statement')
for block in blocks:
evl = self.visit_children(block)
for ev in evl:
if ev is not None:
evs = evs + "\n" + str(ev)
cur_value = st.get(ident)
st.set(ident, cur_value+1)
return evs
def bool_logic(self, tree):
return trans.transform(tree)
def bool_and(self, tree):
return trans.transform(tree)
def bool_or(self, tree):
return trans.transform(tree)
def bool_not(self, tree):
return trans.transform(tree)
def block_statement(self, tree):
self.visit_children(tree)
def assignment(self, tree):
return trans.transform(tree)
def short_assignment(self, tree):
return trans.transform(tree)
def array_assignment(self, tree):
return trans.transform(tree)
def expression(self, tree):
return trans.transform(tree)
def printf(self, tree):
return trans.transform(tree.children[0])