Bringing over some of the "upgrades". 2
This commit is contained in:
parent
4bd32f2c0b
commit
a2cabe2189
|
|
@ -61,50 +61,6 @@ S_times = Symbol('times')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@inscribe
|
|
||||||
@FunctionWrapper
|
|
||||||
def primrec(stack, expression, dictionary):
|
|
||||||
'''
|
|
||||||
From the "Overview of the language JOY":
|
|
||||||
|
|
||||||
> The primrec combinator expects two quoted programs in addition to a
|
|
||||||
data parameter. For an integer data parameter it works like this: If
|
|
||||||
the data parameter is zero, then the first quotation has to produce
|
|
||||||
the value to be returned. If the data parameter is positive then the
|
|
||||||
second has to combine the data parameter with the result of applying
|
|
||||||
the function to its predecessor.::
|
|
||||||
|
|
||||||
5 [1] [*] primrec
|
|
||||||
|
|
||||||
> Then primrec tests whether the top element on the stack (initially
|
|
||||||
the 5) is equal to zero. If it is, it pops it off and executes one of
|
|
||||||
the quotations, the [1] which leaves 1 on the stack as the result.
|
|
||||||
Otherwise it pushes a decremented copy of the top element and
|
|
||||||
recurses. On the way back from the recursion it uses the other
|
|
||||||
quotation, [*], to multiply what is now a factorial on top of the
|
|
||||||
stack by the second element on the stack.::
|
|
||||||
|
|
||||||
n [Base] [Recur] primrec
|
|
||||||
|
|
||||||
0 [Base] [Recur] primrec
|
|
||||||
------------------------------
|
|
||||||
Base
|
|
||||||
|
|
||||||
n [Base] [Recur] primrec
|
|
||||||
------------------------------------------ n > 0
|
|
||||||
n (n-1) [Base] [Recur] primrec Recur
|
|
||||||
|
|
||||||
'''
|
|
||||||
recur, (base, (n, stack)) = stack
|
|
||||||
if n <= 0:
|
|
||||||
expression = concat(base, expression)
|
|
||||||
else:
|
|
||||||
expression = S_primrec, concat(recur, expression)
|
|
||||||
stack = recur, (base, (n - 1, (n, stack)))
|
|
||||||
return stack, expression, dictionary
|
|
||||||
|
|
||||||
|
|
||||||
#def cleave(S, expression, dictionary):
|
#def cleave(S, expression, dictionary):
|
||||||
# '''
|
# '''
|
||||||
# The cleave combinator expects two quotations, and below that an item X.
|
# The cleave combinator expects two quotations, and below that an item X.
|
||||||
|
|
|
||||||
|
|
@ -1999,6 +1999,38 @@ def primrec(stack, expr, dictionary):
|
||||||
return stack, expr, dictionary
|
return stack, expr, dictionary
|
||||||
|
|
||||||
|
|
||||||
|
S_choice = Symbol('choice')
|
||||||
|
S_i = Symbol('i')
|
||||||
|
|
||||||
|
|
||||||
|
@inscribe
|
||||||
|
def ifte(stack, expr, dictionary):
|
||||||
|
'''
|
||||||
|
If-Then-Else Combinator
|
||||||
|
::
|
||||||
|
|
||||||
|
... [if] [then] [else] ifte
|
||||||
|
---------------------------------------------------
|
||||||
|
... [[else] [then]] [...] [if] infra select i
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
... [if] [then] [else] ifte
|
||||||
|
-------------------------------------------------------
|
||||||
|
... [else] [then] [...] [if] infra first choice i
|
||||||
|
|
||||||
|
|
||||||
|
Has the effect of grabbing a copy of the stack on which to run the
|
||||||
|
if-part using infra.
|
||||||
|
'''
|
||||||
|
else_, then, if_, stack = get_n_items(3, stack)
|
||||||
|
e = (S_infra, (S_first, (S_choice, (S_i, ()))))
|
||||||
|
expr = push_quote(e, expr)
|
||||||
|
stack = (if_, (stack, (then, (else_, stack))))
|
||||||
|
return stack, expr, dictionary
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
@ -2009,5 +2041,5 @@ if __name__ == '__main__':
|
||||||
## stack = J(dictionary=dictionary)
|
## stack = J(dictionary=dictionary)
|
||||||
## except SystemExit:
|
## except SystemExit:
|
||||||
## pass
|
## pass
|
||||||
stack, _ = run("5 [1] [*] primrec", (), dictionary)
|
stack, _ = run("5 10 [>][++][*]ifte", (), dictionary)
|
||||||
print(stack_to_string(stack), '•')
|
print(stack_to_string(stack), '•')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue