-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssforth.py
64 lines (48 loc) · 1.48 KB
/
ssforth.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
import core as c
class Forth:
def __init__(self):
self.stack = []
self.cStack = []
self.input = []
def getToken(self, prompt="... "):
while not self.input:
try:
lin = input(prompt)+"\n"
except:
return None
self.input.extend(lin.lower().split())
token = self.input[0]
del self.input[0]
return token
def run(self):
while True:
code = self.compile()
e = c.ExecutionBlock(self.stack, code)
e.execute()
def compile(self):
prompt = "SuperSeriousForth > "
code = []
while True:
token = self.getToken(prompt)
cFunction = c.compileFn.get(token)
rFunction = c.runtimeFn.get(token)
if cFunction:
cFunction(self, self.cStack, code)
elif rFunction:
if type(rFunction) == type([]):
code.append(c.systemFn.get("run"))
code.append(token)
else:
code.append(rFunction)
else:
code.append(c.systemFn.get("push"))
try:
code.append(int(token))
except:
code[-1] = c.systemFn.get("run")
if not self.cStack:
return code
prompt = "... "
if __name__ == "__main__":
main = Forth()
main.run()