/
transpiler.py
199 lines (161 loc) · 5.63 KB
/
transpiler.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import sys
from functools import partial
from lark import *
from lark.tree import *
class TreeToASM(Transformer):
def __init__(self):
self.add = partial(self.alu_op, 'ADD')
self.sub = partial(self.alu_op, 'SUB')
self.mul = partial(self.alu_op, 'MUL')
self.xor = partial(self.alu_op, 'XOR')
self.or_ = partial(self.alu_op, 'OR')
self.cmp_gt = partial(self.cmp_op, 'JLE')
self.cmp_lt = partial(self.cmp_op, 'JLE', swap=True)
self.cmp_ge = partial(self.cmp_op, 'JLE', swap=True)
self.cmp_le = partial(self.cmp_op, 'JLE')
self.cmp_eq = partial(self.cmp_op, 'JEQ')
self.compare = self.nop
self.paren_compare = self.nop
self.refs = self.nop
self.infix_math = self.nop
self.frame = []
self.label_count = 0
self.stackmap = set()
def program(self, data):
return 'STACKMAP ' + ','.join(self.stackmap) + '\n' + '\n'.join(sum(data, []))
def cmp_op(self, name, (left, right), swap=False):
return (left +
right +
['POP r0',
'POP r1',
name + ' ' + ('r0 r1' if not swap else 'r1 r0') + ' {label}'])
def nop(self, (val, )):
return val
# ref : "[" expr "]" "=" expr
def ref(self, (dst, src)):
return (src +
dst +
['POP r0',
'POP r1',
'LD (r0), r1'])
# ref2 : "[" expr "]" "=" "[" expr "]"
def ref2(self, (dst, src)):
return (src +
dst +
['POP r0',
'POP r1',
'LD r2, (r1)',
'LD (r0), r2'])
# ref3 : id "=" "[" expr "]"
def ref3(self, (dst, src)):
return (src +
['POP r0',
'LD r1, (r0)',
'PUSH r1',
'POP ' + dst[0]])
def for_loop(self, (assign, compare, loop, body)):
start = 'for_start_label_' + str(self.label_count)
base = 'for_base_label_' + str(self.label_count)
end = 'for_end_label_' + str(self.label_count)
self.label_count += 1
return (assign +
[start + ':'] +
[op.format(label=base) for op in compare] +
['JMP ' + end] +
[base + ':'] +
loop +
[op.format(loop_end_label=end) for op in body] +
['JMP ' + start,
end + ':'])
def while_loop(self, (comparison, body)):
start = 'while_start_label_' + str(self.label_count)
end = 'while_end_label_' + str(self.label_count)
self.label_count += 1
return ([start + ':'] +
[op.format(label=end) for op in comparison] +
[op.format(loop_end_label=end) for op in body] +
['JMP ' + start, end + ':'])
def break_(self, _):
return ['JMP {loop_end_label}']
def if_statement(self, (comparison, body)):
start = 'if_label_' + str(self.label_count)
end = 'if_end_label_' + str(self.label_count)
self.label_count += 1
return ([op.format(label=start) for op in comparison] +
['JMP ' + end, start + ':'] +
body +
[end + ':'])
def if_else_statement(self, (comparison, if_body, else_body)):
start = 'if_label_' + str(self.label_count)
end = 'if_end_label_' + str(self.label_count)
self.label_count += 1
return ([op.format(label=start) for op in comparison] +
else_body +
['JMP ' + end + ':'] +
[start + ':'] +
if_body +
[end + ':'])
def inc(self, (val, )):
return ['PUSH ' + val[0],
'POP r0',
'INC r0',
'PUSH r0',
'POP ' + val[0]]
def dec(self, (val, )):
return ['PUSH ' + val[0],
'POP r0',
'DEC r0',
'PUSH r0',
'POP ' + val[0]]
def infix_add(self, (lhs, rhs)):
return (['PUSH ' + lhs[0]] +
rhs +
['POP r0',
'POP r1',
'ADD r0, r1',
'POP ' + lhs[0]])
def infix_sub(self, (lhs, rhs)):
return (['PUSH ' + lhs[0]] +
rhs +
['POP r0',
'POP r1',
'SUB r0, r1',
'POP ' + lhs[0]])
def infix_mul(self, (lhs, rhs)):
return (['PUSH ' + lhs[0]] +
rhs +
['POP r0',
'POP r1',
'MUL r0, r1',
'POP ' + lhs[0]])
def statement(self, sub):
return sum(sub, [])
def expr(self, data):
return sum(data, [])
def comment(self, data):
return []
def term(self, (val, )):
return val
def factor(self, (val, )):
if isinstance(val, list):
val = val[0]
return ['PUSH {val}'.format(val=val)]
def id(self, (val, )):
self.stackmap.add(val)
return ['[{val}]'.format(val=val)]
def assign(self, (left, right)):
return (right +
['POP ' + left[0]])
def alu_op(self, name, (left, right)):
return (left +
right +
['POP r0',
'POP r1',
'{name} r0, r1'.format(name=name),
'PUSH r0'])
if __name__ == '__main__':
with open('tudorc.g') as f:
grammar = Lark(f.read(), start='program', parser='lalr')
parsed = grammar.parse(open(sys.argv[1]).read())
# print parsed.pretty()
print TreeToASM().transform(parsed)