-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.py
362 lines (327 loc) · 9.91 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
import pprint
import re
import sys
def die(s):
print(s)
sys.exit(1)
class Token:
def __init__(self, cat, lexeme=None):
self.cat = cat
self.lexeme = lexeme
def __repr__(self):
return "<%s: %s>" % (self.cat, self.lexeme)
def scan(chars):
i = 0
while i < len(chars):
if chars[i].isspace():
i += 1
elif chars[i] in ':;+-*/=()':
yield Token(chars[i])
i += 1
elif chars[i].isalpha() or chars[i] == '_':
r = re.compile(r'([_a-zA-Z][_a-zA-Z0-9]*)')
m = re.match(r, chars[i:])
ident = m.group(1)
if ident in ["var", "int", "float", "while", "do", "done", "print", "read" ]:
yield Token(ident)
else:
yield Token("id", ident)
i += len(ident)
elif chars[i].isdigit():
r = re.compile(r'(\d+(.\d+)?)')
m = re.match(r, chars[i:])
digits = m.group(1)
if '.' in digits:
yield Token("float_lit", float(digits))
else:
yield Token("int_lit", int(digits))
i += len(digits)
else:
die("unknown char: %c" % chars[i])
def parse(tokens):
i = 0
def peek():
return tokens[i].cat
def eat(cat):
nonlocal i
if tokens[i].cat == cat:
i += 1
return tokens[i-1]
else:
die("expected %s, found %s" % (cat, tokens[i].cat))
def program():
return (decls(), list(stmts()))
def decls():
symtab = {}
while i < len(tokens) and peek() == "var":
symtab.update(decl())
return symtab
def decl():
eat("var")
ident = eat("id")
eat(":")
if peek() == "int":
ty = eat("int")
elif peek() == "float":
ty = eat("float")
else:
die("expected type, found %r" % tokens[i])
eat(";")
return { ident.lexeme: ty.cat }
def stmts():
stmts = []
while i < len(tokens) and (peek() in ["while", "id", "print", "read"]):
stmts.append(stmt())
return stmts
def stmt():
if peek() == "read":
eat("read")
ident = eat("id")
eat(";")
return { "node": "read", "id": ident.lexeme }
elif peek() == "print":
eat("print")
e = expr()
eat(";")
return { "node": "print", "expr": e }
elif peek() == "id":
ident = eat("id")
eat("=")
e = expr()
eat(";")
return { "node": "assign", "id": ident.lexeme, "expr": e }
elif peek() == "while":
eat("while")
e = expr()
eat("do")
body = stmts()
eat("done")
return { "node": "while", "expr": e, "body": body }
else:
die("unexpected token %s" % peek())
def expr():
e1 = expr1()
while peek() in "+-":
op = eat(peek())
e2 = expr1()
e1 = { "node": op.cat, "e1": e1, "e2": e2 }
return e1
def expr1():
e1 = expr2()
while peek() in "*/":
op = eat(peek())
e2 = expr2()
e1 = { "node": op.cat, "e1": e1, "e2": e2 }
return e1
def expr2():
if peek() == "id":
ident = eat("id")
return { "node": "id", "id": ident.lexeme }
elif peek() == "int_lit":
x = eat("int_lit")
return { "node": "int_lit", "value": x.lexeme }
elif peek() == "float_lit":
x = eat("float_lit")
return { "node": "float_lit", "value": x.lexeme }
elif peek() == "(":
eat("(")
e = expr()
eat(")")
return e
else:
die("unexpected token: %s" % peek())
return program()
def typecheck(symtable, ast):
if ast["node"] == "int_lit":
ast["type"] = "int"
elif ast["node"] == "float_lit":
ast["type"] = "float"
elif ast["node"] == "id":
ast["type"] = symtable[ast["id"]]
elif ast["node"] in "+-*/":
typecheck(symtable, ast["e1"])
typecheck(symtable, ast["e2"])
if ast["e1"]["type"] == ast["e2"]["type"]:
ast["type"] = ast["e1"]["type"]
else:
die("type error in %s" % ast["node"])
elif ast["node"] == "read":
if ast["id"] not in symtable:
die("undeclared variable %s" % ast["id"])
elif ast["node"] == "print":
typecheck(symtable, ast["expr"])
elif ast["node"] == "assign":
typecheck(symtable, ast["expr"])
if ast["id"] not in symtable:
die("undeclared variable %s" % ast["id"])
if ast["expr"]["type"] != symtable[ast["id"]]:
die("type error in assign")
elif ast["node"] == "while":
typecheck(symtable, ast["expr"])
if ast["expr"]["type"] != "int":
die("type error in while")
for stmt in ast["body"]:
typecheck(symtable, stmt)
else:
die("Unknown type of node: %s" % ast["node"])
def create_offsets(symtable):
i = 0
offsets = {}
for var in symtable:
offsets[var] = i
i -= 4
return offsets
def codegen(offsets, symtable, ast):
label = 0
newline = [
'la $a0, newline',
'li $v0, 4',
'syscall'
]
sp_off = -4*len(offsets)
program =[
'.data',
'newline: .asciiz "\\n"',
'.text',
'main:',
'addi $fp, $sp, 0',
'addi $sp, $sp, %d' % sp_off
]
def codegen_expr(expr):
nonlocal program
if expr["node"] == "int_lit":
program += [
'# int literal',
'li $s0, %d' % expr["value"],
]
elif expr["node"] == "float_lit":
program += [
'# float literal',
'li.s $f0, %f' % expr["value"],
'mfc1 $s0, $f0',
]
elif expr["node"] == "id":
program += [
'# identifier',
'lw $s0, %d($fp)' % offsets[expr["id"]],
]
elif expr["node"] in "+-*/" and expr["type"] == "int":
op = {
"+": "add", "-": "sub",
"*": "mulo", "/": "div"
}[expr["node"]]
codegen_expr(expr["e1"])
codegen_expr(expr["e2"])
program += [
'# int arithmetic',
'lw $s2, 4($sp)',
'lw $s1, 8($sp)',
'%s $s0, $s1, $s2' % op,
'addi $sp, $sp, 8'
]
elif expr["node"] in "+-*/" and expr["type"] == "float":
op = {
"+": "add.s", "-": "sub.s",
"*": "mul.s", "/": "div.s"
}[expr["node"]]
codegen_expr(expr["e1"])
codegen_expr(expr["e2"])
program += [
'# float arithmetic',
'lw $s2, 4($sp)',
'lw $s1, 8($sp)',
'mtc1 $s1, $f1',
'mtc1 $s2, $f2',
'%s $f0, $f1, $f2' % op,
'mfc1 $s0, $f0',
'addi $sp, $sp, 8'
]
program += [
'sw $s0, 0($sp)',
'addi $sp, $sp, -4'
]
def codegen_stmt(stmt):
nonlocal program, label
if stmt["node"] == "read" and symtable[stmt["id"]] == "int":
program += [
'# read int',
'li $v0, 5',
'syscall',
'sw $v0, %d($fp)' % offsets[stmt["id"]]
]
elif stmt["node"] == "print" and stmt["expr"]["type"] == "int":
codegen_expr(stmt["expr"])
program += [
'# print int',
'addi $sp, $sp, 4',
'lw $a0, 0($sp)',
'li $v0, 1',
'syscall'
]
program += newline
elif stmt["node"] == "read" and symtable[stmt["id"]] == "float":
program += [
'# read float',
'li $v0, 6',
'syscall',
'mfc1 $s0, $f0',
'sw $s0, %d($fp)' % offsets[stmt["id"]]
]
elif stmt["node"] == "print" and stmt["expr"]["type"] == "float":
codegen_expr(stmt["expr"])
program += [
'# print float',
'addi $sp, $sp, 4',
'lw $a0, 0($sp)',
'mtc1 $a0, $f12',
'li $v0, 2',
'syscall'
]
program += newline
elif stmt["node"] == "assign":
codegen_expr(stmt["expr"])
program += [
'# assign',
'addi $sp, $sp, 4',
'lw $s0, 0($sp)',
'sw $s0, %d($fp)' % offsets[stmt["id"]]
]
elif stmt["node"] == "while":
start_label = "L_" + str(label)
label += 1
end_label = "L_" + str(label)
label += 1
program += [
'\n# while',
start_label + ":"
]
codegen_expr(stmt["expr"])
program += [
'addi $sp, $sp, 4',
'lw $s0, 0($sp)',
'beqz $s0, %s' % end_label
]
for substmt in stmt["body"]:
codegen_stmt(substmt)
program += [
'j %s' % start_label,
end_label + ":"
]
for stmt in ast:
codegen_stmt(stmt)
program += [
'# exit',
'li $v0, 10',
'syscall'
]
print('\n'.join(program))
def main():
src = sys.stdin.read()
tokens = list(scan(src))
(symtable, ast) = parse(tokens)
for stmt in ast:
typecheck(symtable, stmt)
offsets = create_offsets(symtable)
codegen(offsets, symtable, ast)
if __name__ == "__main__":
main()