Trace command.
Don't print trace by default, instead a new `trace` combinator wirks like `i` and prints a trace.
This commit is contained in:
parent
4704799c37
commit
ddf063973c
|
|
@ -20,6 +20,8 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
from .library import initialize
|
from .library import initialize
|
||||||
from .joy import repl
|
from .joy import repl
|
||||||
|
from .utils import pretty_print # Inscribe trace command.
|
||||||
|
|
||||||
|
|
||||||
print('''\
|
print('''\
|
||||||
Thun - Copyright © 2017 Simon Forman
|
Thun - Copyright © 2017 Simon Forman
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ from builtins import input
|
||||||
from traceback import print_exc, format_exc
|
from traceback import print_exc, format_exc
|
||||||
from .parser import text_to_expression, ParseError, Symbol
|
from .parser import text_to_expression, ParseError, Symbol
|
||||||
from .utils.stack import stack_to_string
|
from .utils.stack import stack_to_string
|
||||||
from .utils.pretty_print import TracePrinter
|
|
||||||
|
|
||||||
|
|
||||||
def joy(stack, expression, dictionary, viewer=None):
|
def joy(stack, expression, dictionary, viewer=None):
|
||||||
|
|
@ -101,16 +100,11 @@ def repl(stack=(), dictionary=None):
|
||||||
text = input('joy? ')
|
text = input('joy? ')
|
||||||
except (EOFError, KeyboardInterrupt):
|
except (EOFError, KeyboardInterrupt):
|
||||||
break
|
break
|
||||||
viewer = TracePrinter()
|
|
||||||
try:
|
try:
|
||||||
stack, _, dictionary = run(text, stack, dictionary, viewer.viewer)
|
stack, _, dictionary = run(text, stack, dictionary)
|
||||||
except:
|
except:
|
||||||
exc = format_exc() # Capture the exception.
|
exc = format_exc() # Capture the exception.
|
||||||
viewer.print_() # Print the Joy trace.
|
|
||||||
print('-' * 73)
|
|
||||||
print(exc) # Print the original exception.
|
print(exc) # Print the original exception.
|
||||||
else:
|
|
||||||
viewer.print_()
|
|
||||||
except:
|
except:
|
||||||
print_exc()
|
print_exc()
|
||||||
print()
|
print()
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,35 @@ from __future__ import print_function
|
||||||
from builtins import object
|
from builtins import object
|
||||||
from traceback import print_exc
|
from traceback import print_exc
|
||||||
from .stack import expression_to_string, stack_to_string
|
from .stack import expression_to_string, stack_to_string
|
||||||
|
from ..joy import joy
|
||||||
|
from ..library import inscribe, FunctionWrapper
|
||||||
|
|
||||||
|
|
||||||
|
@inscribe
|
||||||
|
@FunctionWrapper
|
||||||
|
def trace(stack, expression, dictionary):
|
||||||
|
'''Evaluate a Joy expression on a stack and print a trace.
|
||||||
|
|
||||||
|
This function is just like the `i` combinator but it also prints a
|
||||||
|
trace of the evaluation
|
||||||
|
|
||||||
|
:param stack stack: The stack.
|
||||||
|
:param stack expression: The expression to evaluate.
|
||||||
|
:param dict dictionary: A ``dict`` mapping names to Joy functions.
|
||||||
|
:rtype: (stack, (), dictionary)
|
||||||
|
|
||||||
|
'''
|
||||||
|
tp = TracePrinter()
|
||||||
|
quote, stack = stack
|
||||||
|
try:
|
||||||
|
s, _, d = joy(stack, quote, dictionary, tp.viewer)
|
||||||
|
except:
|
||||||
|
tp.print_()
|
||||||
|
print('-' * 73)
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
tp.print_()
|
||||||
|
return s, expression, d
|
||||||
|
|
||||||
|
|
||||||
class TracePrinter(object):
|
class TracePrinter(object):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue