-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
80 lines (60 loc) · 1.91 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import preprocessor
import tokenizer
import parser
import generation
import assembly
import optimize
import utils
import settings
import usage
import sys
options = {}
current = ""
for val in sys.argv[1:]:
if val.startswith("-"):
current = val[1:]
options[current] = []
else:
if current in options:
options[current].append(val)
else:
options[current] = [val]
if not "" in options:
print("No Input File Given")
usage.usage()
sys.exit()
input_file_name = options[""][0]
output_file_name = "result.asm"
input_file_data = open(input_file_name, "r").read()
if "o" in options:
output_file_name = options["o"][0]
settings.DISPLAY_ASM = "A" in options.keys()
settings.DISPLAY_OPTIMIZATION = "O" in options.keys()
settings.DISPLAY_TREE = "t" in options.keys()
settings.DISPLAY_INTERMEDIATE = "I" in options.keys()
settings.USE_COLORS = "C" not in options.keys()
settings.SHOW_INCREMENTAL_CHANGES = "c" in options.keys()
settings.SHOW_FINE_CHANGES = "cf" in options.keys()
preprocessed, line_map, preprocessor_context = preprocessor.preprocess(input_file_data, input_file_name)
tokens = tokenizer.tokenize(preprocessed, line_map, input_file_name)
tokens = tokenizer.macros(tokens, preprocessor_context)
tree, context = parser.parse(tokens)
if settings.DISPLAY_TREE:
tree.display()
prog = generation.generate_program(tree, context)
funcs = [str(p) for p in prog.functions]
optimzied = optimize.optimize(prog)
if settings.DISPLAY_OPTIMIZATION:
for p_func, o_func in zip(funcs, optimzied.functions):
print("\n")
utils.compare(p_func, str(o_func))
if settings.DISPLAY_INTERMEDIATE and not settings.DISPLAY_OPTIMIZATION:
for func in optimzied.functions:
print("")
print(str(func))
result = assembly.assemble(optimzied)
if settings.DISPLAY_ASM:
print(result)
f = open(output_file_name, 'w')
f.write(result)
f.close()