-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
46 lines (40 loc) · 1.44 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
from scanner import Scanner
from parser import Parser
from symboltable import SymbolTable
from semanticanalyzer import SemanticAnalyzer
from unvectorizer import Unvectorizer
from codegen import CodeGenerator
import sys
output_file = "ir.ll"
def print_out(path,data):
with open(path,'w') as f:
f.write(data)
file_name = sys.argv[1]
with open(file_name,'r') as f:
code = f.read()
errors = False
scanner = Scanner()
parser = Parser()
symbol_table = SymbolTable()
semantic_analyzer = SemanticAnalyzer()
unvectorizer = Unvectorizer()
code_generator = CodeGenerator()
tokens = scanner.scan(code)
#print_out(file_name+'.scan.temp','\n'.join([t.as_string() for t in tokens]))
parse_tree = parser.parse(tokens)
errors = errors or parser.errors()
#print_out(file_name+'.parse.temp',parse_tree.printable_string())
if not errors:
symbol_table.populate(parse_tree)
errors = errors or symbol_table.errors()
#print_out(file_name+'.symbols.temp',symbol_table.printable_string())
if not errors:
semantic_analyzer.analyze(parse_tree,symbol_table)
errors = errors or semantic_analyzer.errors()
#print_out(file_name+'.operations.temp',semantic_analyzer.printable_string())
if not errors:
unvectorizer.unvectorize(parse_tree, symbol_table)
#print_out(file_name+'.parse.temp',parse_tree.printable_string())
output_code = code_generator.generate(parse_tree, symbol_table)
with open(output_file,'w') as f:
f.write(output_code)