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
+19 -2
View File
@@ -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)
+9 -7
View File
@@ -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):