Loop and while combinators.

This commit is contained in:
Simon Forman 2018-07-20 12:33:06 -07:00
parent b89754a30b
commit ca06c626e1
2 changed files with 28 additions and 9 deletions

View File

@ -703,6 +703,7 @@ def zip_(S):
@inscribe @inscribe
@sec_unary_math
@SimpleFunctionWrapper @SimpleFunctionWrapper
def succ(S): def succ(S):
'''Increment TOS.''' '''Increment TOS.'''
@ -711,6 +712,7 @@ def succ(S):
@inscribe @inscribe
@sec_unary_math
@SimpleFunctionWrapper @SimpleFunctionWrapper
def pred(S): def pred(S):
'''Decrement TOS.''' '''Decrement TOS.'''
@ -885,6 +887,7 @@ S_loop = Symbol('loop')
S_i = Symbol('i') S_i = Symbol('i')
S_ifte = Symbol('ifte') S_ifte = Symbol('ifte')
S_infra = Symbol('infra') S_infra = Symbol('infra')
S_pop = Symbol('pop')
S_step = Symbol('step') S_step = Symbol('step')
S_times = Symbol('times') S_times = Symbol('times')
S_swaack = Symbol('swaack') S_swaack = Symbol('swaack')
@ -1398,8 +1401,21 @@ def times(stack, expression, dictionary):
# return 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 @inscribe
#@combinator_effect(_COMB_NUMS(), b1, s6) @poly_combinator_effect(_COMB_NUMS(), [loop_two_true, loop_true, loop_false], b1, s6)
@FunctionWrapper @FunctionWrapper
def loop(stack, expression, dictionary): def loop(stack, expression, dictionary):
''' '''
@ -1523,8 +1539,8 @@ EXPECTATIONS = dict(
ifte=(s7, (s6, (s5, s4))), ifte=(s7, (s6, (s5, s4))),
nullary=(s7, s6), nullary=(s7, s6),
run=(s7, s6), run=(s7, s6),
) )
EXPECTATIONS['while'] = (s7, (s6, s5))
for name in ''' for name in '''
@ -1533,6 +1549,7 @@ for name in '''
ifte ifte
run run
dupdipd codireco dupdipd codireco
while
'''.split(): '''.split():
C = _dictionary[name] C = _dictionary[name]
expect = EXPECTATIONS.get(name) expect = EXPECTATIONS.get(name)

View File

@ -1,7 +1,8 @@
# -*- coding: utf_8 # -*- coding: utf_8
from logging import getLogger from logging import getLogger, addLevelName
_log = getLogger(__name__) _log = getLogger(__name__)
addLevelName(15, 'hmm')
from collections import Counter from collections import Counter
from itertools import imap, chain, product from itertools import imap, chain, product
@ -585,12 +586,13 @@ def _interpret(f, fi, fo, e):
def _log_it(e, F): def _log_it(e, F):
_log.debug( _log.log(
u'%3i %s%s', 15,
len(inspect_stack()), u'%3i %s%s',
doc_from_stack_effect(*F), len(inspect_stack()),
expression_to_string(e), doc_from_stack_effect(*F),
) expression_to_string(e),
)
def infer(*expression): def infer(*expression):