about summary refs log tree commit diff stats
path: root/html/052tangle.cc.html
blob: b3106faa0e3116e0f3fb036895fefc5f6996302f (plain) (blame)
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
<!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 - 052tangle.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; }
.traceAbsent { color: #c00000; }
.cSpecial { color: #008000; }
.muRecipe { color: #ff8700; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.traceContains { color: #008000; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Identifier { color: #fcb165; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Allow code for recipes to be pulled in from multiple places and inserted</span>
<span class="Comment">//: at special labels called 'waypoints'. Unlike jump targets, a recipe can</span>
<span class="Comment">//: have multiple ambiguous waypoints with the same name. Any 'before' and</span>
<span class="Comment">//: 'after' fragments will simply be inserted at all applicable waypoints.</span>
<span class="Comment">//: Waypoints are always surrounded by '&lt;&gt;', e.g. &lt;handle-request&gt;.</span>
<span class="Comment">//:</span>
<span class="Comment">//: todo: switch recipe.steps to a more efficient data structure.</span>

<span class="Delimiter">:(scenario tangle_before)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  &lt;label1&gt;
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+mem: storing 0 in location 1</span>
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">3</span>

<span class="Comment">//: while loading recipes, load before/after fragments</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
map&lt;string <span class="Comment">/*</span><span class="Comment">label</span><span class="Comment">*/</span><span class="Delimiter">,</span> recipe&gt; Before_fragments<span class="Delimiter">,</span> After_fragments<span class="Delimiter">;</span>
set&lt;string <span class="Comment">/*</span><span class="Comment">label</span><span class="Comment">*/</span>&gt; Fragments_used<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Setup&quot;)</span>
Before_fragments<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
After_fragments<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Fragments_used<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>

<span class="Delimiter">:(before &quot;End Command Handlers&quot;)</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>command == <span class="Constant">&quot;before&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  string label = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  recipe tmp<span class="Delimiter">;</span>
  slurp_body<span class="Delimiter">(</span>in<span class="Delimiter">,</span> tmp<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_waypoint<span class="Delimiter">(</span>label<span class="Delimiter">))</span>
    Before_fragments[label]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>Before_fragments[label]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> tmp<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> tmp<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>end<span class="Delimiter">());</span>
  <span class="Normal">else</span>
    raise &lt;&lt; <span class="Constant">&quot;can't tangle before label &quot;</span> &lt;&lt; label &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Comment">// End before Command Handler</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>command == <span class="Constant">&quot;after&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  string label = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  recipe tmp<span class="Delimiter">;</span>
  slurp_body<span class="Delimiter">(</span>in<span class="Delimiter">,</span> tmp<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_waypoint<span class="Delimiter">(</span>label<span class="Delimiter">))</span>
    After_fragments[label]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>After_fragments[label]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> tmp<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> tmp<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>end<span class="Delimiter">());</span>
  <span class="Normal">else</span>
    raise &lt;&lt; <span class="Constant">&quot;can't tangle after label &quot;</span> &lt;&lt; label &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Comment">// End after Command Handler</span>
<span class="Delimiter">}</span>

<span class="Comment">//: after all recipes are loaded, insert fragments at appropriate labels.</span>

<span class="Delimiter">:(after &quot;Begin Instruction Inserting/Deleting Transforms&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>insert_fragments<span class="Delimiter">);</span>  <span class="Comment">// NOT idempotent</span>

<span class="Comment">//: We might need to perform multiple passes, in case inserted fragments</span>
<span class="Comment">//: include more labels that need further insertions. Track which labels we've</span>
<span class="Comment">//: already processed using an extra field.</span>
<span class="Delimiter">:(before &quot;End instruction Fields&quot;)</span>
<span class="Normal">mutable</span> <span class="Normal">bool</span> tangle_done<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End instruction Constructor&quot;)</span>
tangle_done = <span class="Constant">false</span><span class="Delimiter">;</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> insert_fragments<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  insert_fragments<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">));</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> insert_fragments<span class="Delimiter">(</span>recipe&amp; r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- insert fragments into recipe &quot;</span> &lt;&lt; r<span class="Delimiter">.</span>name &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">bool</span> made_progress = <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">int</span> pass = <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>made_progress<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    made_progress = <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Comment">// create a new vector because insertions invalidate iterators</span>
    vector&lt;instruction&gt; result<span class="Delimiter">;</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>r<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Normal">const</span> instruction&amp; inst = r<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>!inst<span class="Delimiter">.</span>is_label || !is_waypoint<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label<span class="Delimiter">)</span> || inst<span class="Delimiter">.</span>tangle_done<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>inst<span class="Delimiter">);</span>
        <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Delimiter">}</span>
      inst<span class="Delimiter">.</span>tangle_done = <span class="Constant">true</span><span class="Delimiter">;</span>
      made_progress = <span class="Constant">true</span><span class="Delimiter">;</span>
      Fragments_used<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label<span class="Delimiter">);</span>
      ostringstream prefix<span class="Delimiter">;</span>
      prefix &lt;&lt; <span class="Constant">'+'</span> &lt;&lt; r<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">'_'</span> &lt;&lt; pass &lt;&lt; <span class="Constant">'_'</span> &lt;&lt; i<span class="Delimiter">;</span>
      <span class="Comment">// ok to use contains_key even though Before_fragments uses [],</span>
      <span class="Comment">// because appending an empty recipe is a noop</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Before_fragments<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>label<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">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;insert fragments before label &quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>label &lt;&lt; end<span class="Delimiter">();</span>
        append_fragment<span class="Delimiter">(</span>result<span class="Delimiter">,</span> Before_fragments[inst<span class="Delimiter">.</span>label]<span class="Delimiter">.</span>steps<span class="Delimiter">,</span> prefix<span class="Delimiter">.</span>str<span class="Delimiter">());</span>
      <span class="Delimiter">}</span>
      result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>inst<span class="Delimiter">);</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>After_fragments<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>label<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">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;insert fragments after label &quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>label &lt;&lt; end<span class="Delimiter">();</span>
        append_fragment<span class="Delimiter">(</span>result<span class="Delimiter">,</span> After_fragments[inst<span class="Delimiter">.</span>label]<span class="Delimiter">.</span>steps<span class="Delimiter">,</span> prefix<span class="Delimiter">.</span>str<span class="Delimiter">());</span>
      <span class="Delimiter">}</span>
    <span class="Delimiter">}</span>
    r<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>swap<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
    ++pass<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> append_fragment<span class="Delimiter">(</span>vector&lt;instruction&gt;&amp; base<span class="Delimiter">,</span> <span class="Normal">const</span> vector&lt;instruction&gt;&amp; patch<span class="Delimiter">,</span> <span class="Normal">const</span> string prefix<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// append 'patch' to 'base' while keeping 'base' oblivious to any new jump</span>
  <span class="Comment">// targets in 'patch' oblivious to 'base' by prepending 'prefix' to them.</span>
  <span class="Comment">// we might tangle the same fragment at multiple points in a single recipe,</span>
  <span class="Comment">// and we need to avoid duplicate jump targets.</span>
  <span class="Comment">// so we'll keep jump targets local to the specific before/after fragment</span>
  <span class="Comment">// that introduces them.</span>
  set&lt;string&gt; jump_targets<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>patch<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> instruction&amp; inst = patch<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>is_label &amp;&amp; is_jump_target<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label<span class="Delimiter">))</span>
      jump_targets<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>patch<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    instruction inst = patch<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<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="Delimiter">{</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>jump_targets<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>label<span class="Delimiter">))</span>
        inst<span class="Delimiter">.</span>label = prefix+inst<span class="Delimiter">.</span>label<span class="Delimiter">;</span>
      base<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>inst<span class="Delimiter">);</span>
      <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> j = <span class="Constant">0</span><span class="Delimiter">;</span> j &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++j<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      reagent&amp; x = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">);</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>!is_literal<span class="Delimiter">(</span>x<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;label&quot;</span> &amp;&amp; contains_key<span class="Delimiter">(</span>jump_targets<span class="Delimiter">,</span> x<span class="Delimiter">.</span>name<span class="Delimiter">))</span>
        x<span class="Delimiter">.</span>name = prefix+x<span class="Delimiter">.</span>name<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    base<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>inst<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> is_waypoint<span class="Delimiter">(</span>string label<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> *label<span class="Delimiter">.</span>begin<span class="Delimiter">()</span> == <span class="Constant">'&lt;'</span> &amp;&amp; *label<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> == <span class="Constant">'&gt;'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">//: complain about unapplied fragments</span>
<span class="Comment">//: This can't run during transform because later (shape-shifting recipes)</span>
<span class="Comment">//: we'll encounter situations where fragments might get used long after</span>
<span class="Comment">//: they're loaded, and we might run transform_all in between. To avoid</span>
<span class="Comment">//: spurious errors, run this check right at the end, after all code is</span>
<span class="Comment">//: loaded, right before we run main.</span>
<span class="Delimiter">:(before &quot;End Commandline Parsing&quot;)</span>
check_insert_fragments<span class="Delimiter">();</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_insert_fragments<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>map&lt;string<span class="Delimiter">,</span> recipe&gt;::iterator p = Before_fragments<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Before_fragments<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>!contains_key<span class="Delimiter">(</span>Fragments_used<span class="Delimiter">,</span> p<span class="Delimiter">-&gt;</span>first<span class="Delimiter">))</span>
      raise &lt;&lt; <span class="Constant">&quot;could not locate insert before label &quot;</span> &lt;&lt; p<span class="Delimiter">-&gt;</span>first &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Delimiter">}</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>map&lt;string<span class="Delimiter">,</span> recipe&gt;::iterator p = After_fragments<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != After_fragments<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>!contains_key<span class="Delimiter">(</span>Fragments_used<span class="Delimiter">,</span> p<span class="Delimiter">-&gt;</span>first<span class="Delimiter">))</span>
      raise &lt;&lt; <span class="Constant">&quot;could not locate insert after label &quot;</span> &lt;&lt; p<span class="Delimiter">-&gt;</span>first &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario tangle_before_and_after)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  &lt;label1&gt;
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+mem: storing 0 in location 1</span>
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="traceContains">+mem: storing 0 in location 4</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">4</span>

<span class="Delimiter">:(scenario tangle_ignores_jump_target)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  +label1
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">before</span> +label1 [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+error: can't tangle before label +label1</span>

<span class="Delimiter">:(scenario tangle_keeps_labels_separate)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  &lt;label1&gt;
  &lt;label2&gt;
  <span class="Constant">6</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">before</span> &lt;label2&gt; [
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">after</span> &lt;label2&gt; [
  <span class="Constant">5</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+mem: storing 0 in location 1</span>
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="Comment"># 'after' fragments for earlier label always go before 'before' fragments for later label</span>
<span class="traceContains">+mem: storing 0 in location 4</span>
<span class="Comment"># label2</span>
<span class="traceContains">+mem: storing 0 in location 5</span>
<span class="traceContains">+mem: storing 0 in location 6</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">6</span>

<span class="Delimiter">:(scenario tangle_stacks_multiple_fragments)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  &lt;label1&gt;
  <span class="Constant">6</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Constant">5</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+mem: storing 0 in location 1</span>
<span class="Comment"># 'before' fragments stack in order</span>
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="traceContains">+mem: storing 0 in location 4</span>
<span class="Comment"># label1</span>
<span class="Comment"># 'after' fragments stack in reverse order</span>
<span class="traceContains">+mem: storing 0 in location 5</span>
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="traceContains">+mem: storing 0 in location 6</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">6</span>

<span class="Delimiter">:(scenario tangle_supports_fragments_with_multiple_instructions)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  &lt;label1&gt;
  <span class="Constant">6</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Constant">5</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+mem: storing 0 in location 1</span>
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 0 in location 4</span>
<span class="traceContains">+mem: storing 0 in location 5</span>
<span class="traceContains">+mem: storing 0 in location 6</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">6</span>

<span class="Delimiter">:(scenario tangle_tangles_into_all_labels_with_same_name)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
  &lt;label1&gt;
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
  recipe2
]
<span class="muRecipe">def</span> recipe2 [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">11</span>
  &lt;label1&gt;
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">11</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="traceContains">+mem: storing 10 in location 1</span>
<span class="traceContains">+mem: storing 12 in location 2</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 12 in location 3</span>
<span class="traceContains">+mem: storing 10 in location 4</span>
<span class="Comment"># recipe2</span>
<span class="traceContains">+mem: storing 11 in location 1</span>
<span class="traceContains">+mem: storing 12 in location 2</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 12 in location 3</span>
<span class="traceContains">+mem: storing 11 in location 4</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">8</span>

<span class="Delimiter">:(scenario tangle_tangles_into_all_labels_with_same_name_2)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
  &lt;label1&gt;
  &lt;label1&gt;
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="traceContains">+mem: storing 10 in location 1</span>
<span class="traceContains">+mem: storing 12 in location 2</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 12 in location 3</span>
<span class="traceContains">+mem: storing 12 in location 2</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 12 in location 3</span>
<span class="traceContains">+mem: storing 10 in location 4</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">6</span>

<span class="Delimiter">:(scenario tangle_tangles_into_all_labels_with_same_name_3)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
  &lt;label1&gt;
  &lt;foo&gt;
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="muRecipe">after</span> &lt;foo&gt; [
  &lt;label1&gt;
]
<span class="traceContains">+mem: storing 10 in location 1</span>
<span class="traceContains">+mem: storing 12 in location 2</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 12 in location 3</span>
<span class="traceContains">+mem: storing 12 in location 2</span>
<span class="Comment"># foo/label1</span>
<span class="traceContains">+mem: storing 12 in location 3</span>
<span class="traceContains">+mem: storing 10 in location 4</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">6</span>

<span class="Delimiter">:(scenario tangle_handles_jump_target_inside_fragment)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
  &lt;label1&gt;
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  jump <span class="Constant">+label2:label</span>
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
  +label2
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="traceContains">+mem: storing 10 in location 1</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 12 in location 3</span>
<span class="traceContains">+mem: storing 10 in location 4</span>
<span class="Comment"># ignored by jump</span>
<span class="traceAbsent">-mem: storing 12 in label 2</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">3</span>

<span class="Delimiter">:(scenario tangle_renames_jump_target)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
  &lt;label1&gt;
  +label2
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  jump <span class="Constant">+label2:label</span>
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
  +label2  <span class="Comment"># renamed</span>
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="traceContains">+mem: storing 10 in location 1</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 12 in location 3</span>
<span class="traceContains">+mem: storing 10 in location 4</span>
<span class="Comment"># ignored by jump</span>
<span class="traceAbsent">-mem: storing 12 in label 2</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">3</span>

<span class="Delimiter">:(scenario tangle_jump_to_base_recipe)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
  &lt;label1&gt;
  +label2
  <span class="Constant">4</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>
]
<span class="muRecipe">before</span> &lt;label1&gt; [
  jump <span class="Constant">+label2:label</span>
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
]
<span class="traceContains">+mem: storing 10 in location 1</span>
<span class="Comment"># label1</span>
<span class="traceContains">+mem: storing 10 in location 4</span>
<span class="Comment"># ignored by jump</span>
<span class="traceAbsent">-mem: storing 12 in label 2</span>
<span class="traceAbsent">-mem: storing 12 in location 3</span>
<span class="Comment"># nothing else</span>
$mem: <span class="Constant">2</span>

<span class="Comment">//: ensure that there are no new fragments created for a label after it's already been inserted to</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> test_new_fragment_after_tangle<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Comment">// define a recipe</span>
  load<span class="Delimiter">(</span><span class="Constant">&quot;def foo [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;  local-scope</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;  &lt;label&gt;</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;after &lt;label&gt; [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;  1:num/raw &lt;- copy 34</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">);</span>
  transform_all<span class="Delimiter">();</span>
  CHECK_TRACE_DOESNT_CONTAIN_ERROR<span class="Delimiter">();</span>
  Hide_errors = <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Comment">// try to tangle into recipe foo after transform</span>
  load<span class="Delimiter">(</span><span class="Constant">&quot;before &lt;label&gt; [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;  2:num/raw &lt;- copy 35</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">);</span>
  CHECK_TRACE_CONTAINS_ERROR<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End before Command Handler&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Fragments_used<span class="Delimiter">,</span> label<span class="Delimiter">))</span>
  raise &lt;&lt; <span class="Constant">&quot;we've already tangled some code at label &quot;</span> &lt;&lt; label &lt;&lt; <span class="Constant">&quot; in a previous call to transform_all(). Those locations won't be updated.</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
<span class="Delimiter">:(before &quot;End after Command Handler&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Fragments_used<span class="Delimiter">,</span> label<span class="Delimiter">))</span>
  raise &lt;&lt; <span class="Constant">&quot;we've already tangled some code at label &quot;</span> &lt;&lt; label &lt;&lt; <span class="Constant">&quot; in a previous call to transform_all(). Those locations won't be updated.</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->