-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
55 lines (44 loc) · 1.19 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
from o_lexer import OLexer
from o_parser import OParser
from o_interpreter import Process
import sys
def repl():
lexer = OLexer()
parser = OParser()
env = {}
program = Process((), env=env)
while True:
try:
text = input('>> ')
except KeyboardInterrupt:
break
except EOFError:
break
if text:
tokens = lexer.tokenize(text)
try:
tree = parser.parse(tokens)
program.tree = tree
program.run()
except TypeError as e:
if str(e).startswith("'NoneType' object is not iterable"):
print("Syntax Error")
else:
print(e)
def exec_file():
lexer = OLexer()
parser = OParser()
with open(sys.argv[1]) as opened_file:
tokens = lexer.tokenize(opened_file.read())
# for token in tokens:
# print(token)
tree = parser.parse(tokens)
# print(tree)
program = Process(tree)
program.run()
# print(program.env)
if __name__ == "__main__":
if len(sys.argv) == 1:
repl()
else:
exec_file()