-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
47 lines (36 loc) · 980 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
41
42
43
44
45
46
47
from interpreter import run
from context import Context
import argparse
parser = argparse.ArgumentParser(description="CLI for Excuting Noxan")
parser.add_argument("--code", "-c", type=str, help="optional argument to pass code")
parser.add_argument(
"--file", "-f", type=str, help="optional argument to pass file name"
)
def main(code):
"""Main function, which returns output of code"""
ctx = Context()
output = run(code, ctx=ctx)
if ctx.printed:
return ctx.print
else:
return output[-1]
def cli():
"""Function for CLI support"""
args = parser.parse_args()
if args.code:
print(main(args.code))
elif args.file:
print(main(open(args.file).read()))
else:
shell()
def shell():
"""
Interactive shell
"""
while True:
code = input(">>> ")
if code == ":quit:" or code == ":exit:":
break
print(main(code))
if __name__ == "__main__":
cli()