git @ Cat's Eye Technologies Wanda / 9aed469
More tests for presence of only one $, and for tracing. Chris Pressey 6 years ago
2 changed file(s) with 48 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
1212
1313 -> Functionality "Run Wanda program" is implemented by
1414 -> shell command "python src/wanda.py %(test-body-file)"
15
16 -> Functionality "Trace Wanda program" is implemented by
17 -> shell command "python src/wanda.py --trace %(test-body-file) | head -n 15"
1518
1619 -> Tests for functionality "Run Wanda program"
1720
142145 ten
143146 ===> $ ten
144147
148 $
149 : $ $ ten -> $ 10 ;
150 ten
151 ===> $ ten
152
153 $
154 : $ ten -> $ $ 10 ;
155 ten
156 ===> $ ten
157
145158 Often the `$` will appear in the leftmost position in both the pattern
146159 and the replacement, as in the above examples, but this is not required.
147160
149162 ---------
150163
151164 If we include the name of a function in its definition, recursion ought to
152 happen. For example if we said
165 happen. And indeed, it does. For example if we said
153166
154167 : $ fact -> $ dup 1 - fact * ;
155168
156169 then
157170
158 4 $ fact
171 3 $ fact
159172
160173 would rewrite to
161174
162 4 $ dup 1 - fact *
175 3 $ dup 1 - fact *
163176
164177 which is fine, the next `fact` will get rewritten the same way in due course,
165178 all fine except for the troublesome matter of it never terminating because we
166 haven't given a base case.
179 haven't given a base case. Viewing the trace of execution for the first few
180 steps makes this clear:
181
182 -> Tests for functionality "Trace Wanda program"
183
184 3 $
185 : $ fact -> $ dup 1 - fact * ;
186 fact
187 ===> 3 $ fact
188 ===> 3 $ dup 1 - fact *
189 ===> 3 3 $ 1 - fact *
190 ===> 3 3 1 $ - fact *
191 ===> 3 2 $ fact *
192 ===> 3 2 $ dup 1 - fact * *
193 ===> 3 2 2 $ 1 - fact * *
194 ===> 3 2 2 1 $ - fact * *
195 ===> 3 2 1 $ fact * *
196 ===> 3 2 1 $ dup 1 - fact * * *
197 ===> 3 2 1 1 $ 1 - fact * * *
198 ===> 3 2 1 1 1 $ - fact * * *
199 ===> 3 2 1 0 $ fact * * *
200 ===> 3 2 1 0 $ dup 1 - fact * * * *
201 ===> 3 2 1 0 0 $ 1 - fact * * * *
202
203 -> Tests for functionality "Run Wanda program"
167204
168205 What would be great would be some way for `0 fact` to be immediately rewritten
169206 into `1` instead of recursing.
114114 if defn:
115115 rules.insert(0, defn)
116116
117 # if options.trace then
118 # local formatted_rule = fmt(match_info.pattern) .. " -> " .. fmt(match_info.replacement)
119 # print(":" .. formatted_rule .. "; => " .. fmt(redex))
117 if options.get('trace'):
118 print(fmt(redex))
120119
121120 start_index = 0
122121 else:
126125
127126
128127 def main(args):
128 options = {}
129 if args[0] == '--trace':
130 options['trace'] = True
131 args = args[1:]
129132 program = load_program(args[0])
130 result = run_wanda(program, {})
133 result = run_wanda(program, options)
131134 print(fmt(result))
132135
133136