1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 040brace.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.traceContains { color: #008000; }
.muRecipe { color: #ff8700; }
.cSpecial { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.CommentedCode { color: #6c6c6c; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Structured programming</span>
<span class="Comment">//:</span>
<span class="Comment">//: Our jump recipes are quite inconvenient to use, so mu provides a</span>
<span class="Comment">//: lightweight tool called 'transform_braces' to work in a slightly more</span>
<span class="Comment">//: convenient format with nested braces:</span>
<span class="Comment">//:</span>
<span class="Comment">//: {</span>
<span class="Comment">//: some instructions</span>
<span class="Comment">//: {</span>
<span class="Comment">//: more instructions</span>
<span class="Comment">//: }</span>
<span class="Comment">//: }</span>
<span class="Comment">//:</span>
<span class="Comment">//: Braces are just labels, they require no special parsing. The pseudo</span>
<span class="Comment">//: recipes 'loop' and 'break' jump to just after the enclosing '{' and '}'</span>
<span class="Comment">//: respectively.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Conditional and unconditional 'loop' and 'break' should give us 80% of the</span>
<span class="Comment">//: benefits of the control-flow primitives we're used to in other languages,</span>
<span class="Comment">//: like 'if', 'while', 'for', etc.</span>
<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario brace_conversion)</span>
<span class="muRecipe">def</span> main [
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: jump 1:offset</span>
<span class="traceContains">+transform: copy ...</span>
<span class="Delimiter">:(before "End Instruction Modifying Transforms")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>transform_braces<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> transform_braces<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> <span class="Normal">int</span> OPEN = <span class="Constant">0</span><span class="Delimiter">,</span> CLOSE = <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Comment">// use signed integer for step index because we'll be doing arithmetic on it</span>
list<pair<<span class="Normal">int</span><span class="Comment">/*</span><span class="Comment">OPEN/CLOSE</span><span class="Comment">*/</span><span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">step</span><span class="Comment">*/</span><span class="Normal">int</span>> > braces<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- transform braces for recipe "</span> << get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name << end<span class="Delimiter">();</span>
<span class="CommentedCode">//? cerr << "--- transform braces for recipe " << get(Recipe, r).name << '\n';</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> index = <span class="Constant">0</span><span class="Delimiter">;</span> index < SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& inst = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label == <span class="Constant">"{"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"push (open, "</span> << index << <span class="Constant">")"</span> << end<span class="Delimiter">();</span>
braces<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair<<span class="Normal">int</span><span class="Delimiter">,</span><span class="Normal">int</span>><span class="Delimiter">(</span>OPEN<span class="Delimiter">,</span> index<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label == <span class="Constant">"}"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"push (close, "</span> << index << <span class="Constant">")"</span> << end<span class="Delimiter">();</span>
braces<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair<<span class="Normal">int</span><span class="Delimiter">,</span><span class="Normal">int</span>><span class="Delimiter">(</span>CLOSE<span class="Delimiter">,</span> index<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
stack<<span class="Comment">/*</span><span class="Comment">step</span><span class="Comment">*/</span><span class="Normal">int</span>> open_braces<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> index = <span class="Constant">0</span><span class="Delimiter">;</span> index < SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
instruction& inst = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label == <span class="Constant">"{"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
open_braces<span class="Delimiter">.</span>push<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label == <span class="Constant">"}"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>open_braces<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"missing '{' in '"</span> << get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
open_braces<span class="Delimiter">.</span>pop<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>is_label<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>old_name != <span class="Constant">"loop"</span>
&& inst<span class="Delimiter">.</span>old_name != <span class="Constant">"loop-if"</span>
&& inst<span class="Delimiter">.</span>old_name != <span class="Constant">"loop-unless"</span>
&& inst<span class="Delimiter">.</span>old_name != <span class="Constant">"break"</span>
&& inst<span class="Delimiter">.</span>old_name != <span class="Constant">"break-if"</span>
&& inst<span class="Delimiter">.</span>old_name != <span class="Constant">"break-unless"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << inst<span class="Delimiter">.</span>old_name << <span class="Constant">" ..."</span> << end<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// check for errors</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>old_name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-if"</span><span class="Delimiter">)</span> != string::npos || inst<span class="Delimiter">.</span>old_name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-unless"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"'"</span> << inst<span class="Delimiter">.</span>old_name << <span class="Constant">"' expects 1 or 2 ingredients, but got none</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// update instruction operation</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>old_name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-if"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
inst<span class="Delimiter">.</span>name = <span class="Constant">"jump-if"</span><span class="Delimiter">;</span>
inst<span class="Delimiter">.</span>operation = JUMP_IF<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>old_name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-unless"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
inst<span class="Delimiter">.</span>name = <span class="Constant">"jump-unless"</span><span class="Delimiter">;</span>
inst<span class="Delimiter">.</span>operation = JUMP_UNLESS<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
inst<span class="Delimiter">.</span>name = <span class="Constant">"jump"</span><span class="Delimiter">;</span>
inst<span class="Delimiter">.</span>operation = JUMP<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// check for explicitly provided targets</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>old_name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-if"</span><span class="Delimiter">)</span> != string::npos || inst<span class="Delimiter">.</span>old_name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-unless"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// conditional branches check arg 1</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</span> && is_literal<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << inst<span class="Delimiter">.</span>name << <span class="Constant">' '</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>name << <span class="Constant">":offset</span><span class="Constant">" << end();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Comment">// unconditional branches check arg 0</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> && is_literal<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"jump "</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name << <span class="Constant">":offset</span><span class="Constant">" << end();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// if implicit, compute target</span>
reagent target<span class="Delimiter">;</span>
target<span class="Delimiter">.</span>type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">"offset"</span><span class="Delimiter">);</span>
target<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>open_braces<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
raise << <span class="Constant">"'"</span> << inst<span class="Delimiter">.</span>old_name << <span class="Constant">"' needs a '{' before</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>old_name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"loop"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span>
target<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>open_braces<span class="Delimiter">.</span>top<span class="Delimiter">()</span>-index<span class="Delimiter">);</span>
<span class="Normal">else</span> <span class="Comment">// break instruction</span>
target<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>matching_brace<span class="Delimiter">(</span>open_braces<span class="Delimiter">.</span>top<span class="Delimiter">(),</span> braces<span class="Delimiter">,</span> r<span class="Delimiter">)</span> - index - <span class="Constant">1</span><span class="Delimiter">);</span>
inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>target<span class="Delimiter">);</span>
<span class="Comment">// log computed target</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"jump"</span><span class="Delimiter">)</span>
trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"jump "</span> << no_scientific<span class="Delimiter">(</span>target<span class="Delimiter">.</span>value<span class="Delimiter">)</span> << <span class="Constant">":offset</span><span class="Constant">" << end();</span>
<span class="Normal">else</span>
trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << inst<span class="Delimiter">.</span>name << <span class="Constant">' '</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name << <span class="Constant">", "</span> << no_scientific<span class="Delimiter">(</span>target<span class="Delimiter">.</span>value<span class="Delimiter">)</span> << <span class="Constant">":offset</span><span class="Constant">" << end();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// returns a signed integer not just so that we can return -1 but also to</span>
<span class="Comment">// enable future signed arithmetic</span>
<span class="Normal">int</span> matching_brace<span class="Delimiter">(</span><span class="Normal">int</span> index<span class="Delimiter">,</span> <span class="Normal">const</span> list<pair<<span class="Normal">int</span><span class="Delimiter">,</span> <span class="Normal">int</span>> >& braces<span class="Delimiter">,</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">int</span> stacksize = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>list<pair<<span class="Normal">int</span><span class="Delimiter">,</span> <span class="Normal">int</span>> >::const_iterator p = braces<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != braces<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>p<span class="Delimiter">-></span>second < index<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
stacksize += <span class="Delimiter">(</span>p<span class="Delimiter">-></span>first ? <span class="Constant">1</span> : -<span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>stacksize == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> p<span class="Delimiter">-></span>second<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"unbalanced '{'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span> SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> <span class="Comment">// exit current routine</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario loop)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Constant">3</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
loop
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump -2:offset</span>
<span class="Delimiter">:(scenario break_empty_block)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump 0:offset</span>
<span class="Delimiter">:(scenario break_cascading)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump 0:offset</span>
<span class="traceContains">+transform: jump 0:offset</span>
<span class="Delimiter">:(scenario break_cascading_2)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Constant">3</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump 1:offset</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump 0:offset</span>
<span class="Delimiter">:(scenario break_if)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>-<span class="Normal">if</span> <span class="Constant">2</span>:num
<span class="Constant">3</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump-if 2, 1:offset</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump 0:offset</span>
<span class="Delimiter">:(scenario break_nested)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Identifier">break</span>
<span class="Delimiter">{</span>
<span class="Constant">3</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Constant">4</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: jump 4:offset</span>
<span class="Delimiter">:(scenario break_nested_degenerate)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Identifier">break</span>
<span class="Delimiter">{</span>
<span class="Delimiter">}</span>
<span class="Constant">4</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: jump 3:offset</span>
<span class="Delimiter">:(scenario break_nested_degenerate_2)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Identifier">break</span>
<span class="Delimiter">{</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: jump 2:offset</span>
<span class="Delimiter">:(scenario break_label)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span> <span class="Constant">+foo:offset</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: jump +foo:offset</span>
<span class="Delimiter">:(scenario break_unless)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>-unless <span class="Constant">2</span>:num
<span class="Constant">3</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump-unless 2, 1:offset</span>
<span class="traceContains">+transform: copy ...</span>
<span class="Delimiter">:(scenario loop_unless)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
loop-unless <span class="Constant">2</span>:num
<span class="Constant">3</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: jump-unless 2, -1:offset</span>
<span class="traceContains">+transform: copy ...</span>
<span class="Delimiter">:(scenario loop_nested)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
<span class="Constant">3</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
loop-<span class="Normal">if</span> <span class="Constant">4</span>:<span class="Normal">bool</span>
<span class="Constant">5</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: jump-if 4, -5:offset</span>
<span class="Delimiter">:(scenario loop_label)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
+foo
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+transform: --- transform braces for recipe main</span>
<span class="traceContains">+transform: copy ...</span>
<span class="traceContains">+transform: copy ...</span>
<span class="Comment">//: test how things actually run</span>
<span class="Delimiter">:(scenarios run)</span>
<span class="Delimiter">:(scenario brace_conversion_and_run)</span>
<span class="muRecipe">def</span> test-factorial [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">5</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">1</span>
<span class="Delimiter">{</span>
<span class="Constant">3</span>:<span class="Normal">bool</span><span class="Special"> <- </span>equal <span class="Constant">1</span>:num<span class="Delimiter">,</span> <span class="Constant">1</span>
<span class="Identifier">break</span>-<span class="Normal">if</span> <span class="Constant">3</span>:<span class="Normal">bool</span>
<span class="Comment"># $print 1:num</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>multiply <span class="Constant">2</span>:num<span class="Delimiter">,</span> <span class="Constant">1</span>:num
<span class="Constant">1</span>:num<span class="Special"> <- </span>subtract <span class="Constant">1</span>:num<span class="Delimiter">,</span> <span class="Constant">1</span>
loop
<span class="Delimiter">}</span>
<span class="Constant">4</span>:num<span class="Special"> <- </span>copy <span class="Constant">2</span>:num <span class="Comment"># trigger a read</span>
]
<span class="traceContains">+mem: location 2 is 120</span>
<span class="Delimiter">:(scenario break_outside_braces_fails)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
<span class="Identifier">break</span>
]
<span class="traceContains">+error: 'break' needs a '{' before</span>
<span class="Delimiter">:(scenario break_conditional_without_ingredient_fails)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
<span class="Delimiter">{</span>
<span class="Identifier">break</span>-<span class="Normal">if</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+error: 'break-if' expects 1 or 2 ingredients, but got none</span>
<span class="Comment">//: Using break we can now implement conditional returns.</span>
<span class="Delimiter">:(scenario return_if)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>test1
]
<span class="muRecipe">def</span> test1 [
<span class="Identifier">return</span>-<span class="Normal">if</span> <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">34</span>
<span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="traceContains">+mem: storing 35 in location 1</span>
<span class="Delimiter">:(scenario return_if_2)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>test1
]
<span class="muRecipe">def</span> test1 [
<span class="Identifier">return</span>-<span class="Normal">if</span> <span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">34</span>
<span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="traceContains">+mem: storing 34 in location 1</span>
<span class="Delimiter">:(before "End Rewrite Instruction(curr, recipe result)")</span>
<span class="Comment">// rewrite `return-if a, b, c, ...` to</span>
<span class="Comment">// ```</span>
<span class="Comment">// {</span>
<span class="Comment">// break-unless a</span>
<span class="Comment">// return b, c, ...</span>
<span class="Comment">// }</span>
<span class="Comment">// ```</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"return-if"</span> || curr<span class="Delimiter">.</span>name == <span class="Constant">"reply-if"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
emit_return_block<span class="Delimiter">(</span>result<span class="Delimiter">,</span> <span class="Constant">"break-unless"</span><span class="Delimiter">,</span> curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span>
curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"'"</span> << curr<span class="Delimiter">.</span>name << <span class="Constant">"' never yields any products</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// rewrite `return-unless a, b, c, ...` to</span>
<span class="Comment">// ```</span>
<span class="Comment">// {</span>
<span class="Comment">// break-if a</span>
<span class="Comment">// return b, c, ...</span>
<span class="Comment">// }</span>
<span class="Comment">// ```</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"return-unless"</span> || curr<span class="Delimiter">.</span>name == <span class="Constant">"reply-unless"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
emit_return_block<span class="Delimiter">(</span>result<span class="Delimiter">,</span> <span class="Constant">"break-if"</span><span class="Delimiter">,</span> curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span>
curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"'"</span> << curr<span class="Delimiter">.</span>name << <span class="Constant">"' never yields any products</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> emit_return_block<span class="Delimiter">(</span>recipe& out<span class="Delimiter">,</span> <span class="Normal">const</span> string& break_command<span class="Delimiter">,</span> <span class="Normal">const</span> vector<reagent>& ingredients<span class="Delimiter">)</span> <span class="Delimiter">{</span>
reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> condition = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
vector<reagent> return_ingredients<span class="Delimiter">;</span>
copy<span class="Delimiter">(</span>++ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> ingredients<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> inserter<span class="Delimiter">(</span>return_ingredients<span class="Delimiter">,</span> return_ingredients<span class="Delimiter">.</span>end<span class="Delimiter">()));</span>
<span class="Comment">// {</span>
instruction open_label<span class="Delimiter">;</span> open_label<span class="Delimiter">.</span>is_label=<span class="Constant">true</span><span class="Delimiter">;</span> open_label<span class="Delimiter">.</span>label = <span class="Constant">"{"</span><span class="Delimiter">;</span>
out<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>open_label<span class="Delimiter">);</span>
<span class="Comment">// <break command> <condition></span>
instruction break_inst<span class="Delimiter">;</span>
break_inst<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> break_command<span class="Delimiter">);</span>
break_inst<span class="Delimiter">.</span>name = break_inst<span class="Delimiter">.</span>old_name = break_command<span class="Delimiter">;</span>
break_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>condition<span class="Delimiter">);</span>
out<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>break_inst<span class="Delimiter">);</span>
<span class="Comment">// return <return ingredients></span>
instruction return_inst<span class="Delimiter">;</span>
return_inst<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"return"</span><span class="Delimiter">);</span>
return_inst<span class="Delimiter">.</span>name = <span class="Constant">"return"</span><span class="Delimiter">;</span>
return_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>swap<span class="Delimiter">(</span>return_ingredients<span class="Delimiter">);</span>
out<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>return_inst<span class="Delimiter">);</span>
<span class="Comment">// }</span>
instruction close_label<span class="Delimiter">;</span> close_label<span class="Delimiter">.</span>is_label=<span class="Constant">true</span><span class="Delimiter">;</span> close_label<span class="Delimiter">.</span>label = <span class="Constant">"}"</span><span class="Delimiter">;</span>
out<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>close_label<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Make sure these pseudo recipes get consistent numbers in all tests, even</span>
<span class="Comment">//: though they aren't implemented. Allows greater flexibility in ordering</span>
<span class="Comment">//: transforms.</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
BREAK<span class="Delimiter">,</span>
BREAK_IF<span class="Delimiter">,</span>
BREAK_UNLESS<span class="Delimiter">,</span>
LOOP<span class="Delimiter">,</span>
LOOP_IF<span class="Delimiter">,</span>
LOOP_UNLESS<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"break"</span><span class="Delimiter">,</span> BREAK<span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"break-if"</span><span class="Delimiter">,</span> BREAK_IF<span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"break-unless"</span><span class="Delimiter">,</span> BREAK_UNLESS<span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"loop"</span><span class="Delimiter">,</span> LOOP<span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"loop-if"</span><span class="Delimiter">,</span> LOOP_IF<span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"loop-unless"</span><span class="Delimiter">,</span> LOOP_UNLESS<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> BREAK: <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Normal">case</span> BREAK_IF: <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Normal">case</span> BREAK_UNLESS: <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Normal">case</span> LOOP: <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Normal">case</span> LOOP_IF: <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Normal">case</span> LOOP_UNLESS: <span class="Identifier">break</span><span class="Delimiter">;</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|