-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·63 lines (53 loc) · 1.49 KB
/
main.py
File metadata and controls
executable file
·63 lines (53 loc) · 1.49 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/home/loya/py-language/venv/bin/python
from src.exceptions import *
from src.parser_ import Parser
from src.interpreter import *
from src.environment import Environment
import readline
import sys
import os
def repl():
if not os.path.exists(".input_history"):
with open(".input_history", "w", encoding="utf-8") as f:
...
readline.read_history_file(".input_history")
parser = Parser()
print("\nRepl v0.1")
while True:
try:
code = input(">>>")
except (EOFError, KeyboardInterrupt):
print("\n\nexiting......\n")
break
code = code.strip()
if not code:
continue
if code == "exit":
print("\nexiting......\n")
break
try:
program = parser.produce_ast(code)
# program.print()
rst = evaluate(program, env)
if not isinstance(rst, NullVal):
print(rst)
except PyException as e:
e.print()
readline.append_history_file(999, ".input_history")
if __name__ == "__main__":
env = Environment()
d = Environment(env)
if len(sys.argv) > 1:
f = open(sys.argv[1])
codes = f.read()
f.close()
try:
parser = Parser()
program = parser.produce_ast(codes)
# program.print()
rst = evaluate(program, env)
# print(rst)
except PyException as e:
e.print()
else:
repl()