-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (74 loc) · 3.17 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
import sys
from semantic.visitors.SymbolTableCreatorVisitor import *
from semantic.visitors.TypeCheckingVisitor import *
from codeGen.visitors.MemAllocationVisitor import *
from codeGen.visitors.CodeGenVisitor import *
from codeGen.Moon import Moon
from lexical.LexicalAnalyser import *
from syntactic.SyntacticalAnalyser import parse
LEXICAL_OUTPUT_FILE = "./logs/lexical/output.log"
SYNTACTICAL_OUTPUT_FILE = "./logs/syntactical/output.log"
SYNTACTICAL_ERROR_FILE = "./logs/syntactical/error.log"
def showUsage(msg):
print("ERROR: "+msg+":\nUsage: py Main.py -f <file path> -o\n-f <file path>: source code to analyse\n-o: generate output on log [optional]")
#get argument count
arg_count = len(sys.argv)
# if num of arguments is correct
if(arg_count > 1):
flag = sys.argv[1]
# check if -f flag is present
if flag == '-f':
# get file path
filepath =sys.argv[2]
lexOut = None
synOut = None
synErrOut = None
# open file
f = open(filepath, 'r')
src = f.read()
f.close()
# check if -o flag was present, if so create logs folder and create output file
if arg_count == 4:
if not os.path.exists("./logs/"):
os.makedirs("./logs/")
if not os.path.exists("./logs/syntactical/"):
os.makedirs("./logs/syntactical/")
if not os.path.exists("./logs/lexical/"):
os.makedirs("./logs/lexical/")
lexOut = open(LEXICAL_OUTPUT_FILE, 'w')
synOut = open(SYNTACTICAL_OUTPUT_FILE, 'w')
synErrOut = open(SYNTACTICAL_ERROR_FILE, 'w')
#initialize Lexical Analyser
l = LexicalAnalyser(src, filepath, lexOut)
ast = parse(l, synOut, synErrOut)
if ast:
f = open('./logs/syntactical/parseTree.txt', 'w')
f.write(str(ast))
f.close()
# create symbol table
tableCreationVisitor = SymbolTableCreatorVisitor()
typeCheckingVisitor = TypeCheckingVisitor()
memAllocationVisitor = MemAllocationVisitor()
codeGenVisitor = CodeGenVisitor('./bin/'+filepath.split('/')[-1].split('.')[0]+".m")
print('=====================TABLE CREATION================')
ast.accept(tableCreationVisitor)
print('=====================TYPE CHECKING================')
ast.accept(typeCheckingVisitor)
try:
print('=====================MEMORY================')
ast.accept(memAllocationVisitor)
print('=====================CODE GEN================')
ast.accept(codeGenVisitor, True)
codeGenVisitor.output()
moon = Moon(codeGenVisitor.outputLocation)
except Exception:
print("Compilation Errors. Check logs and fix them to run the program.")
ErrorLogger.dump("error.log", True)
ast.symTable.outputTable()
SymTableLogger.dump('./logs/semantic/'+filepath.split('/')[-1].split('.')[0]+".table", False)
if lexOut:
lexOut.close()
if synOut:
synOut.close()
if synErrOut:
synErrOut.close()