diff --git a/implementations/Python/joy/library.py b/implementations/Python/joy/library.py index de7aa3e..e660389 100644 --- a/implementations/Python/joy/library.py +++ b/implementations/Python/joy/library.py @@ -137,74 +137,6 @@ def app3(S, expression, dictionary): return stack, expression, dictionary -@inscribe -@FunctionWrapper -def step(S, expression, dictionary): - ''' - Run a quoted program on each item in a sequence. - :: - - ... [] [Q] . step - ----------------------- - ... . - - - ... [a] [Q] . step - ------------------------ - ... a . Q - - - ... [a b c] [Q] . step - ---------------------------------------- - ... a . Q [b c] [Q] step - - The step combinator executes the quotation on each member of the list - on top of the stack. - ''' - (quote, (aggregate, stack)) = S - if not aggregate: - return stack, expression, dictionary - head, tail = aggregate - stack = quote, (head, stack) - if tail: - expression = tail, (quote, (S_step, expression)) - expression = S_i, expression - return stack, expression, dictionary - - -@inscribe -@FunctionWrapper -def times(stack, expression, dictionary): - ''' - times == [-- dip] cons [swap] infra [0 >] swap while pop - :: - - ... n [Q] . times - --------------------- w/ n <= 0 - ... . - - - ... 1 [Q] . times - ----------------------- - ... . Q - - - ... n [Q] . times - ------------------------------------- w/ n > 1 - ... . Q (n - 1) [Q] times - - ''' - # times == [-- dip] cons [swap] infra [0 >] swap while pop - (quote, (n, stack)) = stack - if n <= 0: - return stack, expression, dictionary - n -= 1 - if n: - expression = n, (quote, (S_times, expression)) - expression = concat(quote, expression) - return stack, expression, dictionary - - # The current definition above works like this: # [P] [Q] while diff --git a/implementations/Python/simplejoy.py b/implementations/Python/simplejoy.py index d796a66..160be0e 100755 --- a/implementations/Python/simplejoy.py +++ b/implementations/Python/simplejoy.py @@ -2145,20 +2145,97 @@ def cmp_(stack, expr, dictionary): return stack, expr, dictionary +S_step = Symbol('step') +S_times = Symbol('times') + + +@inscribe +def step(stack, expr, dictionary): + ''' + Run a quoted program on each item in a sequence. + :: + + ... [] [Q] step + --------------------- + ... + + + ... [a] [Q] step + ---------------------- + ... a Q + + + ... [a b c] [Q] step + ---------------------------- + ... a Q [b c] [Q] step + + The step combinator executes the quotation on each member of the list + on top of the stack. + ''' + quote, aggregate, stack = get_n_items(2, stack) + isnt_stack(quote) + isnt_stack(aggregate) + if not aggregate: + return stack, expr, dictionary + + head, tail = aggregate + stack = head, stack + if tail: + expr = push_quote((tail, (quote, (S_step, ()))), expr) + expr = push_quote(quote, expr) + return stack, expr, dictionary + + +@inscribe +def times(stack, expr, dictionary): + ''' + times == [-- dip] cons [swap] infra [0 >] swap while pop + :: + + ... n [Q] times + --------------------- w/ n <= 0 + ... + + + ... 1 [Q] times + --------------------- + ... Q + + + ... n [Q] times + --------------------------- w/ n > 1 + ... Q (n-1) [Q] times + + ''' + # times == [-- dip] cons [swap] infra [0 >] swap while pop + quote, n, stack = get_n_items(2, stack) + isnt_stack(quote) + isnt_int(n) + if n <= 0: + return stack, expr, dictionary + n -= 1 + if n: + expr = push_quote((n, (quote, (S_times, ()))), expr) + expr = push_quote(quote, expr) + return stack, expr, dictionary + + if __name__ == '__main__': import sys J = interp if '-q' in sys.argv else repl dictionary = initialize() Def.load_definitions(__doc__.splitlines(), dictionary) -## try: -## stack = J(dictionary=dictionary) -## except SystemExit: -## pass - jcode = "5 10 [>][++][*]ifte" - jcode = '1 2 [[+]] cond' - jcode = '1 2 [[[>] -] [[<] +] [*]] cond' - jcode = '2 1 [[[>] -] [[<] +] [*]] cond' - jcode = '3 3 [[[>] -] [[<] +] [*]] cond' - stack, _ = run(jcode, (), dictionary) + try: + stack = J(dictionary=dictionary) + except SystemExit: + pass +## jcode = "5 10 [>][++][*]ifte" +## jcode = '1 2 [[+]] cond' +## jcode = '1 2 [[[>] -] [[<] +] [*]] cond' +## jcode = '2 1 [[[>] -] [[<] +] [*]] cond' +## jcode = '3 3 [[[>] -] [[<] +] [*]] cond' +## jcode = '3 dup [dup mul] times' +## jcode = '0 [1 2 3] [add] step' +## stack, _ = run(jcode, (), dictionary) print(stack_to_string(stack), '•')