forked from kcecireyes/JIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjit_ast_visitor.py
More file actions
executable file
·138 lines (112 loc) · 5.09 KB
/
jit_ast_visitor.py
File metadata and controls
executable file
·138 lines (112 loc) · 5.09 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from jit_code_generator import *
from collections import defaultdict
class AstVisitor:
def __init__(self):
#self.output = open(filename, 'w')
self.code_generator = CodeGenerator()
#list of dicts
self.env = [defaultdict(lambda:None)]
def enter_scope(self, env):
self.env.append(env)
def exit_scope(self):
self.env.pop()
def visit_fun(self, fun_node):
if fun_node.subtype == "say":
sentences = [param.accept(self) for param in fun_node.params]
if len(sentences) == 1:
return "print %s\n" % sentences[0]
lst = self.code_generator.generate_list(sentences)
prtstr = "'\\n'.join(%s)" % lst
code = self.code_generator.generate_print(prtstr)
#self.output.write(code + '\n')
elif fun_node.subtype == "createNode":
#self.output.write("Node()")
code = "Node()"
elif fun_node.subtype == "listen":
code = "raw_input()"
elif fun_node.subtype == "push":
visited_params = map(lambda node : node.accept(self), fun_node.params)
params_str = ', '.join(visited_params)
code = "%s.push()\n" % params_str
elif fun_node.subtype == "pull":
visited_params = map(lambda node : node.accept(self), fun_node.params)
params_str = ', '.join(visited_params)
code = "pull(%s)\n" % params_str.replace('\n', '').replace('start', 'node')
elif fun_node.subtype == "save":
visited_params = map(lambda node : node.accept(self), fun_node.params)
params_str = ', '.join(visited_params)
code = "%s.save()\n" % params_str
elif fun_node.subtype == "get":
visited_params = map(lambda node : node.accept(self), fun_node.params)
params_str = ', '.join(visited_params)
code = "node_get(%s)\n" % params_str
elif fun_node.subtype == "import":
visited_params = map(lambda node : node.accept(self), fun_node.params)
params_str = ', '.join(visited_params)
code = "import(%s)\n" % params_str
elif fun_node.subtype == "search":
visited_params = map(lambda node : node.accept(self), fun_node.params)
params_str = ', '.join(visited_params)
code = "search(%s)\n" % params_str
else:
visited_params = map(lambda node : node.accept(self), fun_node.params)
params_str = ', '.join(visited_params)
code = "%s(%s)\n" % (fun_node.subtype, ', '.join(visited_params))
return code
def visit_binop(self, binop_node):
op = binop_node.op
if (binop_node.right.type == "articleop"):
lhs = op = ""
else:
lhs = binop_node.left.accept(self).strip()
rhs = binop_node.right.accept(self).strip()
ls = lhs.split('.')
if ls and ls[-1] == "body" and rhs.startswith("import("):
return "%s.add_body(%s)\n" % (ls[0], rhs[7:-1])
if binop_node.op == "=":
self.env[-1][lhs] = rhs
code = self.code_generator.generate_binaryOp(lhs, op, rhs)
return code
def visit_articleop(self, articleop_node):
if not articleop_node: return
lhs = articleop_node.left.accept(self).strip()
rhs = articleop_node.right.accept(self).strip()
#if articleop_node.op == "=":
# self.env[-1][lhs] = rhs
code = self.code_generator.generate_articleOp(lhs, articleop_node.op, rhs, articleop_node.assign_to, articleop_node.list_of_things)
return code
def visit_str(self, str_node):
return str_node.value
def visit_list(self, list_node):
return list_node.value
def visit_num(self, str_node):
return str(str_node)
def visit_id(self, id_node):
name = id_node.name
current_env = self.env[-1]
val = current_env[name]
return name
def visit_vardecl(self, decl_node):
init_vals = defaultdict(lambda:None)
init_vals['string'] = ''
init_vals['int'] = 0
self.env[-1][decl_node.name] = None
code = "%s = %s\n" % (decl_node.name, str(init_vals[decl_node.type]))
return code
def visit_forloop(self, for_node):
itr = for_node.itr.accept(self)
span = for_node.span.accept(self)
body = map(lambda n : n.accept(self), for_node.body)
code = self.code_generator.generate_forloop(itr, span, body)
return code
def visit_ifblock(self, if_node):
ifc = if_node.if_clause.accept(self).strip()
thc = map(lambda n : n.accept(self), if_node.then_clause)
elc = map(lambda n : n.accept(self), if_node.else_clause)
code = self.code_generator.generate_ifblock(ifc, thc, elc)
return code
def visit_astfundecl(self, fundecl_node):
varlist = map(lambda n : n.accept(self).strip(), fundecl_node.varlist)
fun_body = map(lambda n : n.accept(self), fundecl_node.stmtlist)
code = self.code_generator.generate_fundecl(fundecl_node.name, varlist, fun_body)
return code