From ca06c626e1a53b58f0301e7d00685ea60df46aee Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Fri, 20 Jul 2018 12:33:06 -0700 Subject: [PATCH] Loop and while combinators. --- joy/library.py | 21 +++++++++++++++++++-- joy/utils/types.py | 16 +++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/joy/library.py b/joy/library.py index 81c8e97..e760ddb 100644 --- a/joy/library.py +++ b/joy/library.py @@ -703,6 +703,7 @@ def zip_(S): @inscribe +@sec_unary_math @SimpleFunctionWrapper def succ(S): '''Increment TOS.''' @@ -711,6 +712,7 @@ def succ(S): @inscribe +@sec_unary_math @SimpleFunctionWrapper def pred(S): '''Decrement TOS.''' @@ -885,6 +887,7 @@ S_loop = Symbol('loop') S_i = Symbol('i') S_ifte = Symbol('ifte') S_infra = Symbol('infra') +S_pop = Symbol('pop') S_step = Symbol('step') S_times = Symbol('times') S_swaack = Symbol('swaack') @@ -1398,8 +1401,21 @@ def times(stack, expression, dictionary): # return stack, expression, dictionary +def loop_true(stack, expression, dictionary): + quote, (flag, stack) = stack # pylint: disable=unused-variable + return stack, concat(quote, (S_pop, expression)), dictionary + +def loop_two_true(stack, expression, dictionary): + quote, (flag, stack) = stack # pylint: disable=unused-variable + return stack, concat(quote, (S_pop, concat(quote, (S_pop, expression)))), dictionary + +def loop_false(stack, expression, dictionary): + quote, (flag, stack) = stack # pylint: disable=unused-variable + return stack, expression, dictionary + + @inscribe -#@combinator_effect(_COMB_NUMS(), b1, s6) +@poly_combinator_effect(_COMB_NUMS(), [loop_two_true, loop_true, loop_false], b1, s6) @FunctionWrapper def loop(stack, expression, dictionary): ''' @@ -1523,8 +1539,8 @@ EXPECTATIONS = dict( ifte=(s7, (s6, (s5, s4))), nullary=(s7, s6), run=(s7, s6), - ) +EXPECTATIONS['while'] = (s7, (s6, s5)) for name in ''' @@ -1533,6 +1549,7 @@ for name in ''' ifte run dupdipd codireco + while '''.split(): C = _dictionary[name] expect = EXPECTATIONS.get(name) diff --git a/joy/utils/types.py b/joy/utils/types.py index 28735dd..9cfa573 100644 --- a/joy/utils/types.py +++ b/joy/utils/types.py @@ -1,7 +1,8 @@ # -*- coding: utf_8 -from logging import getLogger +from logging import getLogger, addLevelName _log = getLogger(__name__) +addLevelName(15, 'hmm') from collections import Counter from itertools import imap, chain, product @@ -585,12 +586,13 @@ def _interpret(f, fi, fo, e): def _log_it(e, F): - _log.debug( - u'%3i %s ∘ %s', - len(inspect_stack()), - doc_from_stack_effect(*F), - expression_to_string(e), - ) + _log.log( + 15, + u'%3i %s ∘ %s', + len(inspect_stack()), + doc_from_stack_effect(*F), + expression_to_string(e), + ) def infer(*expression):