Move 2dup to an rforth implementation. Implement greater or equal and less or equal in rforth
This commit is contained in:
parent
620bcfc951
commit
c648adedd5
40
rforth.py
40
rforth.py
|
@ -1,3 +1,4 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
import argparse
|
import argparse
|
||||||
stack = []
|
stack = []
|
||||||
compiling = False
|
compiling = False
|
||||||
|
@ -47,12 +48,14 @@ def parse_input(input_string, say_ok=True):
|
||||||
input_string = _if(input_string)
|
input_string = _if(input_string)
|
||||||
elif string == "dup":
|
elif string == "dup":
|
||||||
dup()
|
dup()
|
||||||
elif string == "2dup":
|
|
||||||
twodup()
|
|
||||||
elif string == "swap":
|
elif string == "swap":
|
||||||
swap()
|
swap()
|
||||||
elif string == "rot":
|
elif string == "rot":
|
||||||
rot()
|
rot()
|
||||||
|
elif string == "and":
|
||||||
|
_and()
|
||||||
|
elif string == "or":
|
||||||
|
_or()
|
||||||
else:
|
else:
|
||||||
if string == ";":
|
if string == ";":
|
||||||
stop_compiling()
|
stop_compiling()
|
||||||
|
@ -66,8 +69,8 @@ def rot():
|
||||||
a = stack.pop(0)
|
a = stack.pop(0)
|
||||||
b = stack.pop(0)
|
b = stack.pop(0)
|
||||||
c = stack.pop(0)
|
c = stack.pop(0)
|
||||||
stack.insert(0,a)
|
|
||||||
stack.insert(0,b)
|
stack.insert(0,b)
|
||||||
|
stack.insert(0,a)
|
||||||
stack.insert(0,c)
|
stack.insert(0,c)
|
||||||
|
|
||||||
def swap():
|
def swap():
|
||||||
|
@ -95,14 +98,6 @@ def _if(input_string):
|
||||||
return input_string[else_index : len(input_string)]
|
return input_string[else_index : len(input_string)]
|
||||||
return input_string
|
return input_string
|
||||||
|
|
||||||
def scan_for_else_or_then(input_string):
|
|
||||||
for index in reversed(range(0, len(input_string))):
|
|
||||||
if input_string[index] == "then":
|
|
||||||
return input_string[index : len(input_string) - 1]
|
|
||||||
elif input_string[index] == "else":
|
|
||||||
return input_string[index : len(input_string) - 1]
|
|
||||||
return index
|
|
||||||
|
|
||||||
def scan_for_else(input_string, then_index):
|
def scan_for_else(input_string, then_index):
|
||||||
for index in reversed(range(0, then_index)):
|
for index in reversed(range(0, then_index)):
|
||||||
if input_string[index] == "else":
|
if input_string[index] == "else":
|
||||||
|
@ -113,15 +108,8 @@ def scan_for_then(input_string):
|
||||||
for index in reversed(range(0, len(input_string))):
|
for index in reversed(range(0, len(input_string))):
|
||||||
if input_string[index] == "then":
|
if input_string[index] == "then":
|
||||||
return index
|
return index
|
||||||
# return input_string[index : len(input_string) - 1]
|
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
def twodup():
|
|
||||||
a = stack.pop(0)
|
|
||||||
stack.insert(0,a)
|
|
||||||
stack.insert(0,a)
|
|
||||||
stack.insert(0,a)
|
|
||||||
|
|
||||||
def dup():
|
def dup():
|
||||||
a = stack.pop(0)
|
a = stack.pop(0)
|
||||||
stack.insert(0,a)
|
stack.insert(0,a)
|
||||||
|
@ -195,6 +183,22 @@ def add():
|
||||||
b = stack.pop(0)
|
b = stack.pop(0)
|
||||||
stack.insert(0, a+b)
|
stack.insert(0, a+b)
|
||||||
|
|
||||||
|
def _and():
|
||||||
|
a = stack.pop(0)
|
||||||
|
b = stack.pop(0)
|
||||||
|
if a == -1 and b == -1:
|
||||||
|
stack.insert(0, -1)
|
||||||
|
else:
|
||||||
|
stack.insert(0,0)
|
||||||
|
|
||||||
|
def _or():
|
||||||
|
a = stack.pop(0)
|
||||||
|
b = stack.pop(0)
|
||||||
|
if a == -1 or b == -1:
|
||||||
|
stack.insert(0, -1)
|
||||||
|
else:
|
||||||
|
stack.insert(0,0)
|
||||||
|
|
||||||
def show_stack():
|
def show_stack():
|
||||||
print(stack)
|
print(stack)
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,6 @@
|
||||||
: loop 1 swap - 2dup . 0 = if else loop then ;
|
: loop 1 swap - 2dup . 0 = if else loop then ;
|
||||||
: loop2 2 swap - 2dup . 0 = if else loop2 then ;
|
: loop2 2 swap - 2dup . 0 = if else loop2 then ;
|
||||||
|
: >= 2dup > rot rot = or ;
|
||||||
|
: 2dup swap dup rot dup rot rot ;
|
||||||
|
: <= 2dup < rot rot = or ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue