Implement heap
This commit is contained in:
parent
c648adedd5
commit
d806b00b3f
|
@ -0,0 +1,4 @@
|
||||||
|
Implement control stack
|
||||||
|
On if add if to control stack, every subsequent word gets added to a string until a then or an end. On then or end, execute teh string
|
||||||
|
|
||||||
|
Implement BEGIN..UNTIL loops
|
21
rforth.py
21
rforth.py
|
@ -1,6 +1,8 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import argparse
|
import argparse
|
||||||
stack = []
|
stack = []
|
||||||
|
heap = [0] * 10
|
||||||
|
next_heap = 0
|
||||||
compiling = False
|
compiling = False
|
||||||
words = {}
|
words = {}
|
||||||
word_name = ""
|
word_name = ""
|
||||||
|
@ -56,6 +58,12 @@ def parse_input(input_string, say_ok=True):
|
||||||
_and()
|
_and()
|
||||||
elif string == "or":
|
elif string == "or":
|
||||||
_or()
|
_or()
|
||||||
|
elif string == "!":
|
||||||
|
store()
|
||||||
|
elif string == "@":
|
||||||
|
fetch()
|
||||||
|
elif string == "heap":
|
||||||
|
dump_heap()
|
||||||
else:
|
else:
|
||||||
if string == ";":
|
if string == ";":
|
||||||
stop_compiling()
|
stop_compiling()
|
||||||
|
@ -65,6 +73,19 @@ def parse_input(input_string, say_ok=True):
|
||||||
if say_ok:
|
if say_ok:
|
||||||
print("ok.")
|
print("ok.")
|
||||||
|
|
||||||
|
def dump_heap():
|
||||||
|
print(heap)
|
||||||
|
|
||||||
|
def store():
|
||||||
|
k = stack.pop(0)
|
||||||
|
v = stack.pop(0)
|
||||||
|
heap[k] = v
|
||||||
|
|
||||||
|
def fetch():
|
||||||
|
k = stack.pop(0)
|
||||||
|
v = heap[k]
|
||||||
|
stack.insert(0, v)
|
||||||
|
|
||||||
def rot():
|
def rot():
|
||||||
a = stack.pop(0)
|
a = stack.pop(0)
|
||||||
b = stack.pop(0)
|
b = stack.pop(0)
|
||||||
|
|
Loading…
Reference in New Issue