forked from pgrabiec/Interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
32 lines (28 loc) · 915 Bytes
/
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
import sys
import ply.yacc as yacc
from Cparser import Cparser
# import TreePrinter # -- Parser --
from interpreter.Interpreter import Interpreter
from semantics.TypeChecker import TypeChecker
# from interpreter.Interpreter import Interpreter
if __name__ == '__main__':
try:
filename = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
file = open(filename, "r")
except IOError:
print("Cannot open {0} file".format(filename))
sys.exit(0)
# -- Parser --
Cparser = Cparser()
parser = yacc.yacc(module=Cparser)
text = file.read()
ast = parser.parse(text, lexer=Cparser.scanner)
# print(ast)
# -- Type Checker --
typeChecker = TypeChecker()
if ast is not None:
typeChecker.visit(ast)
for err in typeChecker.errors:
print(err)
if len(typeChecker.errors) == 0:
ast.accept(Interpreter())