First, import what we need.
from joy.joy import run
from joy.library import initialize
from joy.utils.stack import stack_to_string
from joy.utils.pretty_print import TracePrinter
Define a dictionary, an initial stack, and two helper functions to run Joy code and print results for us.
D = initialize()
S = ()
def J(text):
print(stack_to_string(run(text, S, D)[0]))
def V(text):
tp = TracePrinter()
run(text, S, D, tp.viewer)
tp.print_()
J('23 18 +')
J('45 30 gcd')
A viewer
records each step of the evaluation of a Joy program. The TracePrinter
has a facility for printing out a trace of the evaluation, one line per step. Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression ("continuation") on the right. I find these traces beautiful, like a kind of art.
V('23 18 +')
V('45 30 gcd')
Here's a longer trace.
V('96 27 gcd')