dipd, dipdd, cmp
This commit is contained in:
@@ -75,62 +75,6 @@ S_times = Symbol('times')
|
||||
# return (q, (p, stack)), expression, dictionary
|
||||
|
||||
|
||||
@inscribe
|
||||
@FunctionWrapper
|
||||
def dip(stack, expression, dictionary):
|
||||
'''
|
||||
The dip combinator expects a quoted program on the stack and below it
|
||||
some item, it hoists the item into the expression and runs the program
|
||||
on the rest of the stack.
|
||||
::
|
||||
|
||||
... x [Q] dip
|
||||
-------------------
|
||||
... Q x
|
||||
|
||||
'''
|
||||
try:
|
||||
(quote, (x, stack)) = stack
|
||||
except ValueError:
|
||||
raise StackUnderflowError('Not enough values on stack.')
|
||||
expression = (x, expression)
|
||||
return stack, concat(quote, expression), dictionary
|
||||
|
||||
|
||||
@inscribe
|
||||
@FunctionWrapper
|
||||
def dipd(S, expression, dictionary):
|
||||
'''
|
||||
Like dip but expects two items.
|
||||
::
|
||||
|
||||
... y x [Q] dip
|
||||
---------------------
|
||||
... Q y x
|
||||
|
||||
'''
|
||||
(quote, (x, (y, stack))) = S
|
||||
expression = (y, (x, expression))
|
||||
return stack, concat(quote, expression), dictionary
|
||||
|
||||
|
||||
@inscribe
|
||||
@FunctionWrapper
|
||||
def dipdd(S, expression, dictionary):
|
||||
'''
|
||||
Like dip but expects three items.
|
||||
::
|
||||
|
||||
... z y x [Q] dip
|
||||
-----------------------
|
||||
... Q z y x
|
||||
|
||||
'''
|
||||
(quote, (x, (y, (z, stack)))) = S
|
||||
expression = (z, (y, (x, expression)))
|
||||
return stack, concat(quote, expression), dictionary
|
||||
|
||||
|
||||
@inscribe
|
||||
@FunctionWrapper
|
||||
def app1(S, expression, dictionary):
|
||||
@@ -277,68 +221,3 @@ def times(stack, expression, dictionary):
|
||||
# return stack, expression, dictionary
|
||||
|
||||
|
||||
@inscribe
|
||||
@FunctionWrapper
|
||||
def loop(stack, expression, dictionary):
|
||||
'''
|
||||
Basic loop combinator.
|
||||
::
|
||||
|
||||
... True [Q] loop
|
||||
-----------------------
|
||||
... Q [Q] loop
|
||||
|
||||
... False [Q] loop
|
||||
------------------------
|
||||
...
|
||||
|
||||
'''
|
||||
try:
|
||||
quote, stack = stack
|
||||
except ValueError:
|
||||
raise StackUnderflowError('Not enough values on stack.')
|
||||
if not isinstance(quote, tuple):
|
||||
raise NotAListError('Loop body not a list.')
|
||||
try:
|
||||
(flag, stack) = stack
|
||||
except ValueError:
|
||||
raise StackUnderflowError('Not enough values on stack.')
|
||||
if flag:
|
||||
expression = concat(quote, (quote, (S_loop, expression)))
|
||||
return stack, expression, dictionary
|
||||
|
||||
|
||||
@inscribe
|
||||
@FunctionWrapper
|
||||
def cmp_(stack, expression, dictionary):
|
||||
'''
|
||||
cmp takes two values and three quoted programs on the stack and runs
|
||||
one of the three depending on the results of comparing the two values:
|
||||
::
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a > b
|
||||
G
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a = b
|
||||
E
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a < b
|
||||
L
|
||||
'''
|
||||
L, (E, (G, (b, (a, stack)))) = stack
|
||||
expression = concat(G if a > b else L if a < b else E, expression)
|
||||
return stack, expression, dictionary
|
||||
|
||||
|
||||
# FunctionWrapper(cleave),
|
||||
# FunctionWrapper(while_),
|
||||
|
||||
|
||||
|
||||
for name, primitive in getmembers(genlib, isfunction):
|
||||
inscribe(SimpleFunctionWrapper(primitive))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user