-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.py
48 lines (30 loc) · 1.17 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
from lark import Lark
from compiler.codegen import CodeGenerator
from compiler.assembler import Assemble
## PARSER ##
with open('./compiler/grammar.lark') as file:
grammar = file.read()
parser = Lark(grammar)
scmCodeFile = input("SCMCODE File Name [in /code/SCMCODE, no extension]: ")
print("\n")
with open(f"code/SCMCODE/{str(scmCodeFile)}.scmcode", "r") as file:
scmCode = file.read()
parseTree = parser.parse(scmCode)
print(parseTree.pretty())
print("--------------------------------------------------------")
## CODE GENERATOR ##
transformer = CodeGenerator()
transformer.transform(parseTree)
generated_code = transformer.generate_code()
print(f"Generated Code [code/SCMA/{str(scmCodeFile)}.scma]:")
print(generated_code)
print("\n--------------------------------------------------------")
## ASSEMBLER ##
assembled_code = Assemble(generated_code)
print(f"Assembled Code [code/ASM/{str(scmCodeFile)}.assembly]:\n")
print(assembled_code)
with open(f"code/ASM/{str(scmCodeFile)}.assembly", "w") as file:
file.write(assembled_code)
with open(f"code/SCMA/{str(scmCodeFile)}.scma", "w") as file:
file.write(generated_code)
print("\n--- Compiled Succesfully ---\n")