OSDN Git Service

Update Square_Spiral notebook to use Joy kernel
[joypy/Thun.git] / debugger.py
1 '''
2 In order to debug the problem I'm having with definitions breaking a
3 zipper expression I need to load a system, load the defs into a dictionary,
4 and then run the expression and see if it raises an exception, substituting
5 definitions one-at-a-time until I find the one that breaks it.
6 '''
7
8 from joy.library import default_defs, initialize, inscribe, Def
9 from joy.joy import joy
10 from joy.parser import text_to_expression
11 from joy.utils.pretty_print import trace
12 from joy.utils.stack import stack_to_string
13
14
15 inscribe(trace)
16 dictionary = initialize()
17 defs = {}
18 default_defs(defs)
19
20
21 expression = text_to_expression(
22     '[1 [2 [3 4 25 6] 7] 8]'
23     '[dup mul]'
24     '[dip dip infra dip infra dip infra]'
25     '[[] ccons] step i'
26 ##    '[[[] ccons] step i]'
27 ##    'trace'
28     )
29
30 step_d = {d:defs[d] for d in defs if 'step' in d}
31 for name in ('?', 'dupdipd', 'popopop'):
32     step_d[name] = defs[name]
33
34 def test_expr(ds):
35     D = dictionary.copy()
36     D.update(ds)
37     try:
38         stack, _, _ = joy((), expression, D)
39     except Exception as err:
40         return err
41     return stack_to_string(stack)
42
43 res = test_expr(step_d)
44 if res:
45     print(res)
46
47 ##for def_name in defs:
48 ##    D = dictionary.copy()
49 ##    D[def_name] = defs[def_name]
50 ##    try:
51 ##        stack, _, d = joy((), expression, D)
52 ##    except:
53 ##        print(def_name, 'failed!')
54 ##    else:
55 ##        print(stack_to_string(stack), def_name, 'pass')
56 ##