-
Notifications
You must be signed in to change notification settings - Fork 0
/
sblangv2.py
73 lines (61 loc) · 2.3 KB
/
sblangv2.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
import logging,traceback
class Namespace:
def __init__(self):
logging.debug(f"New Namespace Object: {self}")
self.commands = {}
def addCommand(self, name, function):
self.commands[name] = function
def getCommand(self, name):
return self.commands.get(name)
class Command:
def __init__(self, line, runtime):
logging.debug(f"New Command Object: {self}")
self.line = line
self.runtime = runtime
self._resolve() # Resolve the command
def _resolve(self):
command = self.line.split(" ")
self.command = command[0]
self.args = []
if len(command) > 1:
self.args = command[1:]
logging.debug(f"Resolve command: {command}")
def execute(self, namespace):
try:
command = namespace.getCommand(self.command)
if command:
logging.debug(f"Run command: {command}")
return command(self.args, self.runtime)
logging.debug(
f"Command not found, return current runtime with status 1")
return self.runtime, 1
except:
logging.critical(f"Error when executeing {self.command} with args {self.args}. Traceback:\n{traceback.format_exc()}")
if self.runtime.exitonerr:
exit(1)
return self.runtime, 1
class Runtime:
def __init__(self, exitOnError=True):
logging.debug(f"New runtime Object at: {self}")
self.errored = False
self.vars = {}
self.data = {}
self.pointerdata = []
self.namespace = Namespace()
self.exitonerr = exitOnError
self.globalChecker = []
for _ in range(256):
self.pointerdata.append(0)
self.pointer = 0
def _get_pointer_data(self):
return self.pointerdata[self.pointer]
def execute(self, __command):
# execute GlobalBinder first
for function in self.globalChecker:
status= function(self,__command)
cmd = Command(__command, self)
result = cmd.execute(self.namespace)
if result != 0:
if self.exitonerr:
logging.fatal(f"Function {__command} execute failed. Exiting")
exit(1)