-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.py
397 lines (315 loc) · 12.4 KB
/
compiler.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
from typing import Dict, List, Tuple, Type, Union
from lark import Lark, Tree, Token
from grammar_static import GRAMMAR
parser = Lark(
GRAMMAR,
start="program",
parser="lalr",
keep_all_tokens=True,
propagate_positions=True,
)
class I32:
def __str__(self):
return "i32"
class F64:
def __str__(self):
return "f64"
Params = List[Tuple[str, Union[Type[I32], Type[F64]]]]
def format_params(params: Params):
return "(" + (", ".join(map(lambda p: f"{p[1]} {p[0]}", params))) + ")"
class Func:
def __init__(
self,
params: Params,
ntype: Union[Type[I32], Type[F64]],
):
self.params = params
self.ntype = ntype
def __str__(self):
return f"func ({format_params(self.params)}) -> {self.ntype}"
def ntype_to_class(ntype: str):
if ntype == "i32":
return I32()
elif ntype == "f64":
return F64()
raise Exception(f"unknown ntype {ntype}")
Ntype = Union[Type[I32], Type[F64], Type[Func]]
class Context:
scope: Dict[str, Ntype] = {}
func_return_ntype: None | Ntype = None
wat = "(module\n"
def write(self, code: str):
self.wat += f"{code}"
def finish(self):
self.wat += ")\n"
return self.wat
def visit_functions(node: Tree, context):
for child in node.children:
if isinstance(child, Token):
continue
if child.data == "fun_stmt":
visit_fun_stmt_for_types(child, context)
else:
visit_functions(child, context)
def visit_declaration(node: Tree, context: Context):
for child in node.children:
if child.data == "expression_stmt":
visit_expression_stmt(child, context)
elif child.data == "fun_stmt":
visit_fun_stmt(child, context)
elif child.data == "return_stmt":
visit_return_stmt(child, context)
elif child.data == "if_stmt":
visit_if_stmt(child, context)
def visit_expression_stmt(node: Tree, context: Context):
for child in node.children:
if child == ";":
continue
if child.data == "expression":
visit_expression(child, context)
def visit_expression(node: Tree, context: Context) -> Ntype:
line, col = node.meta.line, node.meta.column
if node.children[0].data == "type":
identifier = node.children[1].children[0].value
if identifier in context.scope:
raise Exception(f"can't redeclare identifier: {identifier} ({line}:{col})")
context.write(f"(local ${identifier} {node.children[0].children[0]})\n")
expr_ntype = visit_expression(node.children[3], context)
context.write(f"(local.set ${identifier})\n")
context.scope[identifier] = expr_ntype
return expr_ntype
elif node.children[0].data == "identifier":
identifier = node.children[0].children[0].value
if identifier not in context.scope:
raise Exception(f"unknown identifier: {identifier} ({line}:{col})")
ntype = context.scope[identifier]
expr_ntype = visit_expression(node.children[2], context)
context.write(f"(local.set ${identifier})\n")
if type(ntype) != type(expr_ntype):
raise Exception(
f"type error {identifier}: expected {ntype} got {expr_ntype} ({line}:{col})"
)
return expr_ntype
return visit_equality(node.children[0], context)
def visit_equality(node: Tree, context: Context) -> Ntype:
if len(node.children) == 1:
return visit_comparison(node.children[0], context)
line, col = node.meta.line, node.meta.column
op = "eq" if node.children[1] == "==" else "ne"
left_nytpe = visit_comparison(node.children[0], context)
right_nytpe = visit_comparison(node.children[2], context)
if type(left_nytpe) != type(right_nytpe):
raise Exception(
f"type error {node.children[1]}: mismatched types got {left_nytpe} and {right_nytpe} ({line}:{col})"
)
context.write(f"({left_nytpe}.{op})\n")
return left_nytpe
def visit_comparison(node: Tree, context: Context) -> Ntype:
if len(node.children) == 1:
return visit_term(node.children[0], context)
line, col = node.meta.line, node.meta.column
if node.children[1] == "<":
op = "lt"
elif node.children[1] == "<=":
op = "le"
elif node.children[1] == ">":
op = "gt"
elif node.children[1] == ">=":
op = "ge"
left_nytpe = visit_term(node.children[0], context)
right_nytpe = visit_term(node.children[2], context)
if type(left_nytpe) != type(right_nytpe):
raise Exception(
f"type error {node.children[1]}: mismatched types got {left_nytpe} and {right_nytpe} ({line}:{col})"
)
context.write(f"({left_nytpe}.{op})\n")
return left_nytpe
def visit_term(node: Tree, context: Context) -> Ntype:
if len(node.children) == 1:
return visit_factor(node.children[0], context)
line, col = node.meta.line, node.meta.column
op = "add" if node.children[1] == "+" else "sub"
left_nytpe = visit_factor(node.children[0], context)
right_nytpe = visit_factor(node.children[2], context)
if type(left_nytpe) != type(right_nytpe):
raise Exception(
f"type error {node.children[1]}: mismatched types got {left_nytpe} and {right_nytpe} ({line}:{col})"
)
context.write(f"({left_nytpe}.{op})\n")
return left_nytpe
def visit_factor(node: Tree, context: Context) -> Ntype:
if len(node.children) == 1:
return visit_call(node.children[0], context)
line, col = node.meta.line, node.meta.column
op = "mul" if node.children[1] == "*" else "div"
left_nytpe = visit_call(node.children[0], context)
right_nytpe = visit_call(node.children[2], context)
if type(left_nytpe) != type(right_nytpe):
raise Exception(
f"type error {node.children[1]}: mismatched types got {left_nytpe} and {right_nytpe} ({line}:{col})"
)
context.write(f"({left_nytpe}.{op})\n")
return left_nytpe
def visit_call(node: Tree, context: Context) -> Ntype:
if len(node.children) == 1:
return visit_primary(node.children[0], context)
line, col = node.meta.line, node.meta.column
identifier = node.children[0].children[0].children[0]
if identifier not in context.scope:
raise Exception(f"unknown function: {identifier} ({line}:{col})")
func = context.scope[identifier]
if type(func) != Func:
raise Exception(f"can only call functions: {identifier} ({line}:{col})")
args = []
if not isinstance(node.children[2], Token):
args = list(
filter(lambda arg: not isinstance(arg, Token), node.children[2].children)
)
if len(func.params) != len(args):
raise Exception(
f"type error {identifier}: expected {len(func.params)} args got {len(args)} ({line}:{col})"
)
context.write(f"(call ${identifier} ")
for i, arg in enumerate(args):
if isinstance(arg, Token):
continue
arg_ntype = visit_expression(arg, context)
if type(arg_ntype) != type(func.params[i][1]):
raise Exception(
f"type error {identifier}: expected {format_params(func.params)} got {arg_ntype} at pos {i} ({line}:{col})"
)
context.write(")\n")
return func.ntype
def visit_primary(node: Tree, context: Context) -> Ntype:
line, col = node.meta.line, node.meta.column
inner = node.children[0]
if isinstance(inner, Token):
if "." in inner:
context.write(f"(f64.const {inner})\n")
return F64()
else:
context.write(f"(i32.const {inner})\n")
return I32()
if inner.data == "identifier":
identifier = inner.children[0]
if identifier in context.scope:
context.write(f"(local.get ${identifier})\n")
return context.scope[identifier]
raise Exception(f"unknown identifier: {identifier} ({line}:{col})")
raise Exception("unreachable")
def visit_fun_stmt_for_types(node: Tree, context: Context):
line, col = node.meta.line, node.meta.column
func_parts = node.children[1].children
identifier = func_parts[0].children[0].value
if identifier in context.scope:
raise Exception(f"can't redeclare identifier: {identifier} ({line}:{col})")
ntype: None | Tree = None
args: None | Tree = None
i = 0
while i < len(func_parts):
if func_parts[i] == "(" and func_parts[i + 1] != ")":
args = func_parts[i + 1].children
if func_parts[i] == "->":
ntype = ntype_to_class(func_parts[i + 1].children[0].value)
i += 1
if not ntype:
raise Exception(f"missing ntype for function {identifier} ({line}:{col})")
params: Params = []
if args:
param_parts = list(filter(lambda x: x != ",", func_parts[2].children))
for i in range(0, len(param_parts), 2):
param_ntype = ntype_to_class(param_parts[i].children[0])
param_id = param_parts[i + 1].children[0]
params.append((param_id, param_ntype))
context.scope[identifier] = Func(params, ntype)
def visit_fun_stmt(node: Tree, context: Context):
line, col = node.meta.line, node.meta.column
func_parts = node.children[1].children
identifier = func_parts[0].children[0].value
if identifier not in context.scope:
raise Exception(
f"could't find function (this really shouldn't happen): {identifier} ({line}:{col})"
)
func: Func = context.scope[identifier]
if type(func) != Func:
raise Exception(
f"expected func to be of type Func (this really shouldn't happen): {identifier} ({line}:{col})"
)
bodies_idx = 0
while bodies_idx < len(func_parts):
if func_parts[bodies_idx] == "->":
bodies_idx += 2
break
bodies_idx += 1
func_bodies = func_parts[bodies_idx:]
if context.func_return_ntype is not None:
raise Exception(f"nesting functions isn't allowed: {identifier} ({line}:{col})")
context.func_return_ntype = func.ntype
func_scope = {}
for param in func.params:
func_scope[param[0]] = param[1]
for id, maybe_func in context.scope.items():
if type(maybe_func) == Func:
func_scope[id] = maybe_func
prev_scope = context.scope
context.scope = func_scope
wat_params = " ".join(map(lambda p: f"(param ${p[0]} {p[1]})", func.params))
context.write(
f'\n(func ${identifier} (export "{identifier}") {wat_params} (result {func.ntype})\n'
)
for func_body in func_bodies:
visit_declaration(func_body, context)
# write a default value to avoid type errors
# functions without a return statement return the default value (e.g. `0`)
context.write(f"({func.ntype}.const 0)")
context.write(")\n\n")
context.scope = prev_scope
context.func_return_ntype = None
def visit_return_stmt(node: Tree, context: Context):
line, col = node.meta.line, node.meta.column
for child in node.children[1].children:
if child == ";":
continue
if child.data == "expression":
ntype = visit_expression(child, context)
if context.func_return_ntype is None:
raise Exception(f"can't return outside of functions ({line}:{col})")
if type(ntype) != type(context.func_return_ntype):
raise Exception(
f"type error return: expected {context.func_return_ntype} got {ntype} ({line}:{col})"
)
context.write("(return)\n")
def visit_if_stmt(node: Tree, context: Context):
line, col = node.meta.line, node.meta.column
ntype = visit_expression(node.children[2], context)
if type(ntype) != I32:
raise Exception(f"type error if: expected {I32()} got {ntype} ({line}:{col})")
context.write(
"""(if
(then\n"""
)
for i in range(3, len(node.children)):
if isinstance(node.children[i], Token):
continue
visit_declaration(node.children[i], context)
context.write(")\n)\n")
def compile(source: str, context: Context):
root = parser.parse(source)
visit_functions(root, context)
visit_declaration(root, context)
if __name__ == "__main__":
source = """
fn fib(i32 n) -> i32
if (n == 0)
return 0;
fi
if (n == 1)
return 1;
fi
return fib(n - 1) + fib(n - 2);
nf
"""
context = Context()
compile(source, context)
context.finish()
print(context.wat)