-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (52 loc) · 1.95 KB
/
main.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
from src.lexer import *
from src.parser import *
from src.codegen import ASMGenerator
import sys
import argparse
def process(file_path, args):
try:
with open(file_path, "r") as f:
text = f.read()
if args.all:
args.lex = args.parse = args.codegen = True
tokens = tokenize(text)
if args.lex:
print()
print("---------- TOKENS ----------")
for token in tokens:
print(token)
parser = Parser(tokens)
ast = parser.parse()
printer = ASTPrinter()
if args.parse:
print()
print("---------- Abstract Syntax Tree ----------")
print(printer.print(ast))
print()
generator = ASMGenerator()
asm_code = generator.generate(ast)
if args.codegen:
print()
print("---------- Assembly Generated ----------")
print(asm_code)
print()
if not (args.lex or args.parse or args.codegen):
generator.emit()
except FileNotFoundError:
print(f"[ERROR]: File `{file_path}` not found")
sys.exit(1);
except Exception as e:
print(f"Error during compilation: {str(e)}")
sys.exit(1)
if __name__=="__main__":
parser = argparse.ArgumentParser(
description="Tiny C Compiler written in Python",
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("input_file", help="Input source file to compile")
parser.add_argument("-l", "--lex", action="store_true", help="Print tokens from lexical analysis")
parser.add_argument("-p", "--parse", action="store_true", help="Show parsing completion status")
parser.add_argument("-cg", "--codegen", action="store_true", help="Print generated assembly code")
parser.add_argument("--all", action="store_true", help="Enable all output phases")
args = parser.parse_args()
process(args.input_file, args)