From 49941f9a33f4f6f41a7c19e0fa23de1e034dea33 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Wed, 18 Jul 2018 17:06:51 -0700 Subject: [PATCH] Log types at startup. --- joy/gui/main.py | 4 ++++ joy/library.py | 29 ++++++++++++++++++----------- joy/utils/types.py | 17 ++++++++++------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/joy/gui/main.py b/joy/gui/main.py index 0a2bf53..e6dde6c 100755 --- a/joy/gui/main.py +++ b/joy/gui/main.py @@ -8,6 +8,9 @@ Joypy - Copyright © 2018 Simon Forman ' right-click "sharing" for details.' ' Right-click on these commands to see docs on UI commands: key_bindings mouse_bindings') import logging, os, pickle, sys + +_log = logging.getLogger(__name__) + from textwrap import dedent from joy.gui.utils import init_home, FileFaker @@ -111,6 +114,7 @@ def grand_reset(s, e, d): return stack, e, d +_log.info('Starting.') STACK_FN = os.path.join(JOY_HOME, 'stack.pickle') REL_STACK_FN = repo_relative_path(STACK_FN) JOY_FN = os.path.join(JOY_HOME, 'scratch.txt') diff --git a/joy/library.py b/joy/library.py index 1694d2b..288229d 100644 --- a/joy/library.py +++ b/joy/library.py @@ -23,6 +23,11 @@ functions. Its main export is a Python function initialize() that returns a dictionary of Joy functions suitable for use with the joy() function. ''' +from logging import getLogger + +_log = getLogger(__name__) +_log.info('Loading library.') + from inspect import getdoc from functools import wraps from itertools import count @@ -55,6 +60,7 @@ from .utils.types import ( JoyTypeError, combinator_effect, poly_combinator_effect, + doc_from_stack_effect, ) @@ -194,10 +200,7 @@ def yin_functions(): _Tree_delete_clear_stuff = compose(rollup, popop, rest) _Tree_delete_R0 = compose(over, first, swap, dup) - return { - name.rstrip('_'): stack_effect - for name, stack_effect in locals().iteritems() - } + return locals() definitions = ('''\ @@ -376,13 +379,16 @@ class DefinitionWrapper(object): # print F.name, F._body secs = infer(*F._body) except JoyTypeError: - pass - print F.name, '==', expression_to_string(F.body), ' --failed to infer stack effect.' + _log.error( + 'Failed to infer stack effect of %s == %s', + F.name, + expression_to_string(F.body), + ) if fail_fails: - print 'Function not inscribed.' return else: FUNCTIONS[F.name] = SymbolJoyType(F.name, secs, _SYM_NUMS()) + _log.info('Setting stack effect for definition %s := %s', F.name, secs) dictionary[F.name] = F @@ -1506,11 +1512,12 @@ _functions.update(YIN_STACK_EFFECTS) # of = compose(swap, at) # ''' in dict(compose=compose), _functions +for name in sorted(_functions): + sec = _functions[name] + F = FUNCTIONS[name] = SymbolJoyType(name, [sec], _SYM_NUMS()) + if name in YIN_STACK_EFFECTS: + _log.info('Setting stack effect for Yin function %s := %s', F.name, doc_from_stack_effect(*sec)) -FUNCTIONS.update( - (name, SymbolJoyType(name, [_functions[name]], _SYM_NUMS())) - for name in sorted(_functions) - ) for name, primitive in getmembers(genlib, isfunction): inscribe(SimpleFunctionWrapper(primitive)) diff --git a/joy/utils/types.py b/joy/utils/types.py index 0eec8d8..287407e 100644 --- a/joy/utils/types.py +++ b/joy/utils/types.py @@ -435,7 +435,7 @@ def compilable(f): return isinstance(f, tuple) and all(imap(compilable, f)) or _stacky(f) -def doc_from_stack_effect(inputs, outputs): +def doc_from_stack_effect(inputs, outputs=('??', ())): ''' Return a crude string representation of a stack effect. ''' @@ -670,10 +670,12 @@ def stack_effect(*inputs): def _stack_effect(*outputs): def _apply_to(function): i, o = _functions[function.name] = __(*inputs), __(*outputs) + d = doc_from_stack_effect(i, o) function.__doc__ += ( '\nStack effect::\n\n ' # '::' for Sphinx docs. - + doc_from_stack_effect(i, o) + + d ) + _log.info('Setting stack effect for %s := %s', function.name, d) return function return _apply_to return _stack_effect @@ -688,8 +690,10 @@ def ef(*inputs): def combinator_effect(number, *expect): def _combinator_effect(c): e = __(*expect) if expect else None - C = FUNCTIONS[c.name] = CombinatorJoyType(c.name, [c], number, e) - if expect: C.expect = __(*expect) + FUNCTIONS[c.name] = CombinatorJoyType(c.name, [c], number, e) + if e: + sec = doc_from_stack_effect(e) + _log.info('Setting stack EXPECT for combinator %s := %s', c.name, sec) return c return _combinator_effect @@ -713,13 +717,12 @@ def generate_library_code(DEFS, f=None): print >> f -##if __name__ == '__main__': -## show() - def poly_combinator_effect(number, effect_funcs, *expect): def _poly_combinator_effect(c): e = __(*expect) if expect else None FUNCTIONS[c.name] = CombinatorJoyType(c.name, effect_funcs, number, e) + if e: + _log.info('Setting stack EXPECT for combinator %s := %s', c.name, e) return c return _poly_combinator_effect