-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
executable file
·42 lines (30 loc) · 1.03 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
import argparse
from src.lexer import CompilerLexer
from src.parser import CompilerParser
from src.variables import VariableManager
from src.program import Program
from src.static_analysis import StaticAnalyser
def main(in_file: str, out_file: str):
with open(in_file, "r") as f:
data = f.read()
# Defining lexer and parser
lexer = CompilerLexer()
parser = CompilerParser()
# Running lexer and parser
tokens = lexer.tokenize(data)
program = parser.parse(tokens)
# Allocating variables
VariableManager.allocate()
# Performing static analysis
program = StaticAnalyser().run(program)
if program is not None:
code = program.generate_code()
with open(out_file, "w") as f:
code.append("HALT")
f.write("\n".join(code))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("in_file")
parser.add_argument("out_file")
args = parser.parse_args()
main(**vars(args))