-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_sblib_builtins.py
107 lines (90 loc) · 3.17 KB
/
_sblib_builtins.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import logging
class Function:
def __init__(self):
self.commands = []
def runAll(self, runtime):
logging.debug(f"Call func: {self}")
for command in self.commands:
runtime.execute(command)
return runtime
def __repr__(self) -> str:
return f"[SBLangV3 Function Object](cmdlen={len(self.commands)})"
def out(args, runtime):
string = runtime.vars[args[0]]
print(string, end="", flush=True)
return 0
def string(args, runtime):
varname = args[0]
logging.debug(f"Assign string: {args}")
vardata = " ".join(args[1:])
runtime.vars[varname] = vardata
return 0
def import_(args, runtime):
# import the lib
logging.debug(f"Import lib: {args[0]}")
runtime = __import__(f"_sblib_{args[0]}").init(runtime)
return 0 # 0:status code
def define(args, runtime):
logging.debug(f"Define new function: {args}")
if runtime.data.get("DEFINEING", False):
logging.critical("'define' called twice")
if runtime.exitorerr:
exit(1)
return 1
name = "".join(args)
runtime.data["DEFINEING"] = True
runtime.data["DEFINE_NAME"] = name
runtime.data["DEFINE_OBJECT"] = Function()
logging.debug(
f"Adding DEFINEING, DEFINE_NAME, DEFINE_OBJECT into runtime data")
return 0
def d_process(args, runtime):
command = " ".join(args)
if not runtime.data.get("DEFINEING", False):
logging.critical("d called outside define")
if runtime.exitorerr:
exit(1)
return 1
if command == "end":
logging.debug("Processing d end command")
runtime.data["DEFINEING"] = False
def_name = runtime.data["DEFINE_NAME"]
def_obj = runtime.data["DEFINE_OBJECT"]
runtime.vars[def_name] = def_obj
runtime.data["DEFINE_NAME"] = None
runtime.data["DEFINE_OBJECT"] = None
logging.debug(
"Cleaned up and put the function object into runtime var list")
return 0
runtime.data["DEFINE_OBJECT"].commands.append(command)
return 0
def checkD(runtime,command):
logging.debug("Checking define")
if runtime.data.get("DEFINEING", False) and not command.startswith("d "):
logging.critical("'d end' missing")
if runtime.exitonerr:
exit(1)
return 1
return 0
def callfun(args, runtime):
logging.debug(f"Call function: {args}")
name = args[0]
if type(runtime.vars[name]) != Function:
logging.critical(f"Not a function.")
if runtime.exitorerr:
exit(1)
return 1
runtime = runtime.vars[name].runAll(runtime)
return 0
def comment(args,runtime):
return 0
def init(runtime):
runtime.namespace.addCommand("out", out)
runtime.namespace.addCommand("string", string)
runtime.namespace.addCommand("import", import_)
runtime.namespace.addCommand("define", define)
runtime.namespace.addCommand("d", d_process)
runtime.namespace.addCommand("call", callfun)
runtime.namespace.addCommand("//",comment)
runtime.globalChecker.append(checkD)
return runtime