-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.py
78 lines (60 loc) · 1.65 KB
/
lua.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
"""Usage:
lua.py
lua.py [-t]
lua.py [-dt] [<script>]
Arguments:
<script> Lua script to be run
Options:
-h --help show this help message
-d --debug print debug info
-t --test run in testing mode
"""
from interpreter import Interpreter
import interpreter
import parser
from docopt import docopt
def main():
# while True:
# s = input()
s = "12 / 6 - 2 * 4"
ast = parser.parse(s, debug=False)
print("AST:", ast)
interpreter = Interpreter(ast)
result = interpreter.run()
print(result)
if __name__ == "__main__":
args = docopt(__doc__)
# if a script is specified, we try to run it
if args["<script>"]:
with open(args["<script>"]) as f:
script = f.read()
ast = parser.parse(script, args["--debug"])
if not ast:
raise SystemExit
lua_interpreter = interpreter.Interpreter(ast)
try:
result = lua_interpreter.run()
print(result)
raise SystemExit
except RuntimeError:
pass
# otherwise enter interactive mode
else:
while True:
try:
if args["--test"]:
line = input()
else:
line = input("> ")
except EOFError:
raise SystemExit
ast = parser.parse(line, args["--debug"])
if not ast:
raise SystemExit
lua_interpreter = interpreter.Interpreter(ast)
try:
result = lua_interpreter.run()
print(result)
except RuntimeError:
pass
# main()