-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.py
More file actions
46 lines (41 loc) · 1.61 KB
/
compiler.py
File metadata and controls
46 lines (41 loc) · 1.61 KB
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 code_generator import CodeGenerator
from definitions.compiler_exception import CompilerException
from lexer import CompilerLexer
from parser import CompilerParser
from pre_parser import CompilerPreParser
from variable_prepare import VariablePrepare
class Compiler:
def __init__(self, file_in, file_out):
self.file_in = file_in
self.file_out = file_out
self.code = None
self.commands = []
self.variables = {}
self.error = False
def read_from_file(self):
with open(self.file_in, 'r') as input_file:
self.code = input_file.read()
def compile(self):
lexer = CompilerLexer()
pre_parser = CompilerPreParser()
try:
pre_parse_ready = lexer.tokenize(self.code)
parse_ready = lexer.tokenize(self.code)
self.variables = pre_parser.parse(pre_parse_ready)
variable_prepare = VariablePrepare(self.variables)
optimized_variables = variable_prepare.get_optimized_variables()
#variable_prepare.print_variables()
generator = CodeGenerator(optimized_variables)
parser = CompilerParser(generator)
parser.parse(parse_ready)
self.commands = generator.commands
except CompilerException as e:
self.error = True
print("Error: {} on line {}".format(e.error, e.line))
def write_to_file(self):
if not self.error:
with open(self.file_out, 'w') as output_file:
output_file.write("\n".join(self.commands))
return True
else:
return False