-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
45 lines (34 loc) · 976 Bytes
/
gen.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
import sys
import lex
import parse
import ir3
def main():
if len(sys.argv) != 2:
print("Usage: python3 gen.py <filename>")
exit(1)
filename = sys.argv[1]
# open file, lex input text, print out all tokens
with open(filename) as f:
run(f.read(), filename)
def run(text: str, filename: str):
# generate tokens
lexer = lex.Lexer(text, filename)
tokens, err = lexer.lex()
if err: return print(err)
# generate AST
parser = parse.Parser(tokens)
cst, err, astt, _ = parser.parse()
if err: return print(err)
# if parse succeeds, proceed to static checking
try:
astt.static_check()
except Exception as err:
print("Error during static checking!")
return print(err)
#print(astt)
# if typecheck succeeds, proceed to intermediate code generation
ir: ir3.Program3 = ir3.run(astt)
print(ir)
return cst, err, astt, _
if __name__ == "__main__":
main()