forked from bartlomiejlagosz/M-Lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
40 lines (30 loc) · 996 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
33
34
35
36
37
38
39
40
import sys, os
from interpreter import Interpreter
sys.path.append(os.path.abspath('/'))
sys.path.append(os.path.abspath(''))
import Mparser
import scanner
from TypeChecker import TypeChecker
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 = Mparser.parser
text = file.read()
ast = parser.parse(text, lexer=scanner.lexer)
# res = ast.printTree()
# print(res)
# Below code shows how to use visitor
typeChecker = TypeChecker()
typeChecker.visit(ast) # or alternatively ast.accept(typeChecker)
if typeChecker.should_stop_processing:
sys.exit(1)
ast.accept(Interpreter())
# in future
# ast.accept(OptimizationPass1())
# ast.accept(OptimizationPass2())
# ast.accept(CodeGenerator())
# or alternatively ast.accept(typeChecker)