OSDN Git Service

Working on bug #15
[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
17 dictionary = initialize()
18
19 defs = {}
20 default_defs(defs)
21
22
23 expression = text_to_expression(
24     '[1 [2 [3 4 25 6] 7] 8]'
25     '[dup mul]'
26     '[dip dip infra dip infra dip infra]'
27 ##    '[[] ccons] step i'
28
29     # to trace replace last line above with:
30     '[[[] ccons] step i]'
31     'trace'
32     )
33
34 expected_result = '[1 [2 [3 4 625 6] 7] 8]'
35 expected_result_as_stack = text_to_expression(expected_result)
36
37
38 def test_expr(ds):
39     '''
40     Run the test expression with the defs in ds.
41     Return the resulting stack as a string or the
42     exception raised if any.
43     '''
44     D = dictionary.copy()
45     D.update(ds)
46     try:
47         stack, _, _ = joy((), expression, D)
48     except Exception as err:
49         return err
50     return stack_to_string(stack)
51
52
53 # The problem is that it works with the built-ins:
54
55 ##print(test_expr({}))
56
57 # Results:
58 #   [1 [2 [3 4 625 6] 7] 8]
59 #
60 # But not with the definitions:
61
62 ##print(test_expr(defs))
63
64 # Results:
65 #   not enough values to unpack (expected 2, got 0)
66 #
67 # This obviously sucks and is bad. :(
68
69 # First, because it's easy, let's try adding single defs
70 # one-at-a-time to the dictionary and see if any one of
71 # them breaks it.
72
73 # Only the defs that shadow the built-ins could be the problem:
74 candidates = set(dictionary) & set(defs)
75
76 ##for def_name in candidates:
77 ##    stack_str = test_expr({def_name: defs[def_name]})
78 ##    if stack_str != expected_result:
79 ##        print(def_name, 'failed!')
80 ##        print(stack_str)
81
82 # Results:
83 #   step failed!
84 #   _step0
85
86 # Ah yes, step's definition has parts (and dependencies).
87 step_defs = {
88     d: defs[d]
89     for d in defs
90     if 'step' in d
91     }
92 for name in ('?', 'dupdipd', 'popopop'):
93     step_defs[name] = defs[name]
94 ##print(sorted(step_defs))
95 ##print(test_expr(step_defs))
96
97 # Results:
98 #   [1 [2 [3 4 625 6] 7] 8]
99 #
100 # So it's not step by itself, it's some combination of defintions
101 # that is causing the bug.
102
103 its_is_probably_not = set('''
104     dipd roll< uncons
105 '''.split())
106
107 sus_defs = {
108     def_name: defs[def_name]
109     for def_name in defs
110     if (def_name in candidates
111     or def_name in step_defs)
112     and def_name not in its_is_probably_not
113     }
114 ##print()
115 ##print(test_expr(sus_defs))
116
117 d = step_defs.copy()
118 d['uncons'] = defs['uncons']
119 d['cleave'] = defs['cleave']
120 d['fork'] = defs['fork']
121
122 ##print(test_expr(d))
123
124 CD = {
125     name: defs[name]
126     for name in candidates
127     }
128 CD.update(step_defs)
129 CD['codi'] = defs['codi']
130 CD['swapd'] = defs['swapd']
131 CD['cleave'] = defs['cleave']
132 CD['fork'] = defs['fork']
133 CD['grba'] = defs['grba']
134 CD['infrst'] = defs['infrst']
135
136 ##print(test_expr(CD))
137
138 ##print(sorted(CD))
139 # [++, --, '?', _step0, _step1, _stept, abs, app1, app2, app3, at, b, ccons, clear, 'cleave', 'codi', dipd, disenstacken, drop, dupd, dupdd, dupdip, 'dupdipd', 'fork', fourth, genrec, 'grba', ii, infra, 'infrst', map, mod, neg, not, pm, popd, popdd, popop, popopd, popopdd, 'popopop', rest, reverse, roll<, roll>, rolldown, rollup, rrest, second, shunt, step, step_zero, sum, 'swapd', swons, take, third, times, tuck, uncons, unit, unswons, x]
140
141 del CD['++']
142 del CD['--']
143 ##del CD['?']
144 ##del CD['_step0']
145 ##del CD['_step1']
146 ##del CD['_stept']
147 del CD['abs']
148 del CD['app1']
149 del CD['app2']
150 del CD['app3']
151 del CD['at']
152 del CD['b']
153 del CD['ccons']
154 del CD['clear']
155 ##del CD['cleave']  # <-- dep
156 del CD['codi']
157 del CD['dipd']
158 del CD['disenstacken']
159 del CD['drop']
160 del CD['dupd']
161 del CD['dupdd']
162 del CD['dupdip']
163 del CD['dupdipd']
164 ##del CD['fork']  # <-- dep
165 del CD['fourth']
166 del CD['genrec']
167 ##del CD['grba']  # <-- dep
168 del CD['ii']
169 del CD['infra']
170 ##del CD['infrst']  # <-- dep
171 del CD['map']
172 del CD['mod']
173 del CD['neg']
174 del CD['not']
175 del CD['pm']
176 del CD['popd']
177 ##del CD['popdd']  # <-- !!!!!
178 del CD['popop']
179 del CD['popopd']
180 del CD['popopdd']
181 del CD['popopop']
182 del CD['rest']
183 del CD['reverse']
184 del CD['roll<']
185 del CD['roll>']
186 del CD['rolldown']
187 del CD['rollup']
188 del CD['rrest']
189 del CD['second']
190 del CD['shunt']
191 ##del CD['step']  # <-- !!!!!
192 del CD['step_zero']
193 del CD['sum']
194 del CD['swapd']
195 del CD['swons']
196 del CD['take']
197 del CD['third']
198 del CD['times']
199 del CD['tuck']
200 ##del CD['uncons']  #  <-- popopop !?
201 del CD['unit']
202 del CD['unswons']
203 del CD['x']
204
205 print(test_expr(CD))
206 for n in sorted(CD):
207         print(n)
208 ##    ?
209 ##    _step0
210 ##    _step1
211 ##    _stept
212 ##    cleave
213 ##    fork
214 ##    grba
215 ##    infrst
216 ##    popdd
217 ##    step
218 ##    uncons
219
220
221
222
223
224 ##print()
225 ##print(set(dictionary) & set(defs))