-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (43 loc) · 1.68 KB
/
main.py
File metadata and controls
56 lines (43 loc) · 1.68 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
from sys import argv as sys_argv
from ns_engine import __version__ as ns_version, wrapper as ns_wrapper
from ns_engine.utils.misc import get_filedata, look_like_path
def argument(shortflag: str, flag: str) -> bool:
return (shortflag in sys_argv and shortflag != None) or (flag in sys_argv and flag != None)
def shell():
print(f"Welcome to NakaScript v{ns_version} Shell")
while True:
try:
command_text = input(">>> ")
if not command_text.strip(): continue
result, error, _ = ns_wrapper.interpret("<shell>", command_text)
if error: print(error.as_string())
elif result:
to_print = repr(result if len(result.value) > 1 else result.value[0])
print(to_print)
except KeyboardInterrupt:
break
def run(filename: str):
try:
source_code = get_filedata(filename)
except FileNotFoundError as e:
print(f"Failed to load script \"{filename}\": {e}")
source_code = None
if source_code is None or not source_code.strip(): return
try:
_, error, _ = ns_wrapper.interpret(filename, source_code)
if error: print(error.as_string())
except KeyboardInterrupt:
return
if __name__ == "__main__":
args_len = len(sys_argv) - 1
is_first_arg_scriptfile = False
if args_len >= 1:
first_arg = sys_argv[1]
is_first_arg_scriptfile = look_like_path(first_arg)
if is_first_arg_scriptfile:
run(first_arg)
else:
if not args_len:
shell()
elif argument("-v", "--version"):
print(f"v{ns_version}")