-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.py
More file actions
54 lines (41 loc) · 1.12 KB
/
compile.py
File metadata and controls
54 lines (41 loc) · 1.12 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
import sys
import lex
import parse
import ir3
import backend
def main():
# verify user input
if len(sys.argv) != 2:
print("Usage: python3 compile.py <filename>")
exit(1)
# execute main logic
filename = sys.argv[1]
with open(filename) as f:
content = f.read()
run(content, filename)
def run(text: str, filename: str):
# lexing - extract tokens
tokens, err = lex.Lexer(text, filename).lex()
if err: return print(err)
# parsing - generate AST
cst, err, astt, _ = parse.Parser(tokens).parse()
if err: return print(err)
# static checking
try:
astt.static_check()
except Exception as err:
print("Error during static checking!")
return print(err)
#print(astt)
# intermediate code generation
ir: ir3.Program3 = ir3.run(astt)
#print(ir)
# backend - generate assembly code
asm = backend.Arm(ir).run()
print("\n".join(asm))
# write assembly code to disk
fname = filename.split(".")[0]
with open(f"{fname}.s", "w") as f:
f.write("\n".join(asm))
if __name__ == "__main__":
main()