about summary refs log tree commit diff stats
path: root/html/011load.cc.html
blob: 065fd49104a9412f48727aae99ee1a23cb328e17 (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
<!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 - 011load.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: #c0a020; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Phase 1 of running Mu code: load it from a textual representation.</span>
<span class="Comment">//:</span>
<span class="Comment">//: The process of running Mu code:</span>
<span class="Comment">//:   load -&gt; transform -&gt; run</span>

<span class="Delimiter">:(scenarios load)</span>  <span class="Comment">// use 'load' instead of 'run' in all scenarios in this layer</span>
<span class="Delimiter">:(scenario first_recipe)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>

<span class="Delimiter">:(code)</span>
vector&lt;recipe_ordinal&gt; load<span class="Delimiter">(</span>string form<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  istringstream in<span class="Delimiter">(</span>form<span class="Delimiter">);</span>
  in &gt;&gt; std::noskipws<span class="Delimiter">;</span>
  <span class="Identifier">return</span> load<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

vector&lt;recipe_ordinal&gt; load<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  in &gt;&gt; std::noskipws<span class="Delimiter">;</span>
  vector&lt;recipe_ordinal&gt; result<span class="Delimiter">;</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
    string command = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>command<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
      assert<span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">));</span>
      <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Comment">// Command Handlers</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>command == <span class="Constant">&quot;recipe&quot;</span> || command == <span class="Constant">&quot;def&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      recipe_ordinal r = slurp_recipe<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>r &gt; <span class="Constant">0</span><span class="Delimiter">)</span> result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
    <span class="Delimiter">}</span>
    <span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>command == <span class="Constant">&quot;recipe!&quot;</span> || command == <span class="Constant">&quot;def!&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      Disable_redefine_checks = <span class="Constant">true</span><span class="Delimiter">;</span>
      recipe_ordinal r = slurp_recipe<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>r &gt; <span class="Constant">0</span><span class="Delimiter">)</span> result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
      Disable_redefine_checks = <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Comment">// End Command Handlers</span>
    <span class="Normal">else</span> <span class="Delimiter">{</span>
      raise &lt;&lt; <span class="Constant">&quot;unknown top-level command: &quot;</span> &lt;&lt; command &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">// return the recipe ordinal slurped, or -1 if it failed</span>
<span class="Comment">// (later layers will cause failures)</span>
<span class="Normal">int</span> slurp_recipe<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  recipe result<span class="Delimiter">;</span>
  result<span class="Delimiter">.</span>name = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>name<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
    assert<span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">));</span>
    raise &lt;&lt; <span class="Constant">&quot;file ended with 'recipe'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span> -<span class="Constant">1</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Comment">// End Load Recipe Name</span>
  skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Comment">// End Recipe Refinements</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>name<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
    raise &lt;&lt; <span class="Constant">&quot;empty result.name</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- defining &quot;</span> &lt;&lt; result<span class="Delimiter">.</span>name &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> result<span class="Delimiter">.</span>name<span class="Delimiter">))</span>
    put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> result<span class="Delimiter">.</span>name<span class="Delimiter">,</span> Next_recipe_ordinal++<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>Recipe<span class="Delimiter">.</span>find<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> result<span class="Delimiter">.</span>name<span class="Delimiter">))</span> != Recipe<span class="Delimiter">.</span>end<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;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;already exists&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>should_check_for_redefine<span class="Delimiter">(</span>result<span class="Delimiter">.</span>name<span class="Delimiter">))</span>
      raise &lt;&lt; <span class="Constant">&quot;redefining recipe &quot;</span> &lt;&lt; result<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot;</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    Recipe<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> result<span class="Delimiter">.</span>name<span class="Delimiter">));</span>
  <span class="Delimiter">}</span>
  slurp_body<span class="Delimiter">(</span>in<span class="Delimiter">,</span> result<span class="Delimiter">);</span>
  <span class="Comment">// End Recipe Body(result)</span>
  put<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> result<span class="Delimiter">.</span>name<span class="Delimiter">),</span> result<span class="Delimiter">);</span>
  <span class="Identifier">return</span> get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> result<span class="Delimiter">.</span>name<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> slurp_body<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">,</span> recipe&amp; result<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  in &gt;&gt; std::noskipws<span class="Delimiter">;</span>
  skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>get<span class="Delimiter">()</span> != <span class="Constant">'['</span><span class="Delimiter">)</span>
    raise &lt;&lt; result<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot;: recipe body must begin with '['</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span>  <span class="Comment">// permit trailing comment after '['</span>
  instruction curr<span class="Delimiter">;</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>next_instruction<span class="Delimiter">(</span>in<span class="Delimiter">,</span> &amp;curr<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    curr<span class="Delimiter">.</span>original_string = to_original_string<span class="Delimiter">(</span>curr<span class="Delimiter">);</span>
    <span class="Comment">// End Rewrite Instruction(curr, recipe result)</span>
    trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">&quot;load&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;after rewriting: &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>curr<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!curr<span class="Delimiter">.</span>is_empty<span class="Delimiter">())</span> result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>curr<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> next_instruction<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">,</span> instruction* curr<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  curr<span class="Delimiter">-&gt;</span>clear<span class="Delimiter">();</span>
  skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    raise &lt;&lt; <span class="Constant">&quot;incomplete recipe at end of file (0)</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>

  vector&lt;string&gt; words<span class="Delimiter">;</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span>
      raise &lt;&lt; <span class="Constant">&quot;incomplete recipe at end of file (1)</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    string word = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>word<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
      assert<span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">));</span>
      raise &lt;&lt; <span class="Constant">&quot;incomplete recipe at end of file (2)</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    words<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>word<span class="Delimiter">);</span>
    skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>words<span class="Delimiter">)</span> == <span class="Constant">1</span> &amp;&amp; words<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">&quot;]&quot;</span><span class="Delimiter">)</span>
    <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>  <span class="Comment">// end of recipe</span>

  <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>words<span class="Delimiter">)</span> == <span class="Constant">1</span> &amp;&amp; is_label_word<span class="Delimiter">(</span>words<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
    curr<span class="Delimiter">-&gt;</span>is_label = <span class="Constant">true</span><span class="Delimiter">;</span>
    curr<span class="Delimiter">-&gt;</span>label = words<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
    trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;label: &quot;</span> &lt;&lt; curr<span class="Delimiter">-&gt;</span>label &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span>
      raise &lt;&lt; <span class="Constant">&quot;incomplete recipe at end of file (3)</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>

  vector&lt;string&gt;::iterator p = words<span class="Delimiter">.</span>begin<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>find<span class="Delimiter">(</span>words<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> words<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> <span class="Constant">&quot;&lt;-&quot;</span><span class="Delimiter">)</span> != words<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span>
    <span class="Normal">for</span> <span class="Delimiter">(;</span>  *p != <span class="Constant">&quot;&lt;-&quot;</span><span class="Delimiter">;</span>  ++p<span class="Delimiter">)</span>
      curr<span class="Delimiter">-&gt;</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span>*p<span class="Delimiter">));</span>
    ++p<span class="Delimiter">;</span>  <span class="Comment">// skip &lt;-</span>
  <span class="Delimiter">}</span>

  <span class="Normal">if</span> <span class="Delimiter">(</span>p == words<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span>
    raise &lt;&lt; <span class="Constant">&quot;instruction prematurely ended with '&lt;-'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  curr<span class="Delimiter">-&gt;</span>old_name = curr<span class="Delimiter">-&gt;</span>name = *p<span class="Delimiter">;</span>  ++p<span class="Delimiter">;</span>
  <span class="Comment">// curr-&gt;operation will be set in a later layer</span>

  <span class="Normal">for</span> <span class="Delimiter">(;</span>  p != words<span class="Delimiter">.</span>end<span class="Delimiter">();</span>  ++p<span class="Delimiter">)</span>
    curr<span class="Delimiter">-&gt;</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span>*p<span class="Delimiter">));</span>

  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;instruction: &quot;</span> &lt;&lt; curr<span class="Delimiter">-&gt;</span>name &lt;&lt; end<span class="Delimiter">();</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;  number of ingredients: &quot;</span> &lt;&lt; SIZE<span class="Delimiter">(</span>curr<span class="Delimiter">-&gt;</span>ingredients<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>vector&lt;reagent&gt;::iterator p = curr<span class="Delimiter">-&gt;</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">();</span>  p != curr<span class="Delimiter">-&gt;</span>ingredients<span class="Delimiter">.</span>end<span class="Delimiter">();</span>  ++p<span class="Delimiter">)</span>
    trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;  ingredient: &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>*p<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>vector&lt;reagent&gt;::iterator p = curr<span class="Delimiter">-&gt;</span>products<span class="Delimiter">.</span>begin<span class="Delimiter">();</span>  p != curr<span class="Delimiter">-&gt;</span>products<span class="Delimiter">.</span>end<span class="Delimiter">();</span>  ++p<span class="Delimiter">)</span>
    trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;  product: &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>*p<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    raise &lt;&lt; <span class="Constant">&quot;9: unbalanced '[' for recipe</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">// can return empty string -- only if `in` has no more data</span>
string next_word<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Comment">// End next_word Special-cases</span>
  ostringstream out<span class="Delimiter">;</span>
  slurp_word<span class="Delimiter">(</span>in<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
  skip_whitespace_and_comments_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  string result = out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>result != <span class="Constant">&quot;[&quot;</span> &amp;&amp; ends_with<span class="Delimiter">(</span>result<span class="Delimiter">,</span> <span class="Constant">'['</span><span class="Delimiter">))</span>
    raise &lt;&lt; <span class="Constant">&quot;insert a space before '[' in '&quot;</span> &lt;&lt; result &lt;&lt; <span class="Constant">&quot;'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> is_label_word<span class="Delimiter">(</span><span class="Normal">const</span> string&amp; word<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>!word<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
  <span class="Identifier">return</span> !isalnum<span class="Delimiter">(</span>word<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> &amp;&amp; string<span class="Delimiter">(</span><span class="Constant">&quot;$_*@&amp;,=-&quot;</span><span class="Delimiter">).</span>find<span class="Delimiter">(</span>word<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> == string::npos<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> ends_with<span class="Delimiter">(</span><span class="Normal">const</span> string&amp; s<span class="Delimiter">,</span> <span class="Normal">const</span> <span class="Normal">char</span> c<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>s<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> *s<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> == c<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
<span class="Comment">// word boundaries</span>
<span class="Normal">extern</span> <span class="Normal">const</span> string Terminators<span class="Delimiter">(</span><span class="Constant">&quot;(){}&quot;</span><span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> slurp_word<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">,</span> ostream&amp; out<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">char</span> c<span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> &amp;&amp; Terminators<span class="Delimiter">.</span>find<span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">())</span> != string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    in &gt;&gt; c<span class="Delimiter">;</span>
    out &lt;&lt; c<span class="Delimiter">;</span>
    <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>in &gt;&gt; c<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>isspace<span class="Delimiter">(</span>c<span class="Delimiter">)</span> || Terminators<span class="Delimiter">.</span>find<span class="Delimiter">(</span>c<span class="Delimiter">)</span> != string::npos || Ignore<span class="Delimiter">.</span>find<span class="Delimiter">(</span>c<span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      in<span class="Delimiter">.</span>putback<span class="Delimiter">(</span>c<span class="Delimiter">);</span>
      <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    out &lt;&lt; c<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> skip_whitespace_and_comments<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span><span class="Constant">true</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>isspace<span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()))</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
    <span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>Ignore<span class="Delimiter">.</span>find<span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">())</span> != string::npos<span class="Delimiter">)</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
    <span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">'#'</span><span class="Delimiter">)</span> skip_comment<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">else</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Comment">// confusing; move to the next line only to skip a comment, but never otherwise</span>
<span class="Normal">void</span> skip_whitespace_and_comments_but_not_newline<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span><span class="Constant">true</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>isspace<span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()))</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
    <span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>Ignore<span class="Delimiter">.</span>find<span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">())</span> != string::npos<span class="Delimiter">)</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
    <span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">'#'</span><span class="Delimiter">)</span> skip_comment<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">else</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> skip_comment<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">'#'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
    <span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario recipe_instead_of_def)</span>
<span class="muRecipe">recipe</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_comment_outside_recipe)</span>
<span class="Comment"># this comment will be dropped by the tangler, so we need a dummy recipe to stop that</span>
<span class="muRecipe">def</span> f1 [
]
<span class="Comment"># this comment will go through to 'load'</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_comment_amongst_instruction)</span>
<span class="muRecipe">def</span> main [
  <span class="Comment"># comment</span>
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_comment_amongst_instruction_2)</span>
<span class="muRecipe">def</span> main [
  <span class="Comment"># comment</span>
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
  <span class="Comment"># comment</span>
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_comment_amongst_instruction_3)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
  <span class="Comment"># comment</span>
  <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {2: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_comment_after_instruction)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>  <span class="Comment"># comment</span>
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_label)</span>
<span class="muRecipe">def</span> main [
  +foo
]
<span class="traceContains">+parse: label: +foo</span>

<span class="Delimiter">:(scenario parse_dollar_as_recipe_name)</span>
<span class="muRecipe">def</span> main [
  $foo
]
<span class="traceContains">+parse: instruction: $foo</span>

<span class="Delimiter">:(scenario parse_multiple_properties)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>/foo:bar:baz
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;, &quot;foo&quot;: (&quot;bar&quot; &quot;baz&quot;)}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_multiple_products)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>
<span class="traceContains">+parse:   product: {2: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_multiple_ingredients)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span><span class="Delimiter">,</span> <span class="Constant">4</span>:number
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   ingredient: {4: &quot;number&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>
<span class="traceContains">+parse:   product: {2: &quot;number&quot;}</span>

<span class="Delimiter">:(scenario parse_multiple_types)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:address:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span><span class="Delimiter">,</span> <span class="Constant">4</span>:number
]
<span class="traceContains">+parse: instruction: copy</span>
<span class="traceContains">+parse:   ingredient: {23: &quot;literal&quot;}</span>
<span class="traceContains">+parse:   ingredient: {4: &quot;number&quot;}</span>
<span class="traceContains">+parse:   product: {1: &quot;number&quot;}</span>
<span class="traceContains">+parse:   product: {2: (&quot;address&quot; &quot;number&quot;)}</span>

<span class="Delimiter">:(scenario parse_properties)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:address:number/lookup<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+parse:   product: {1: (&quot;address&quot; &quot;number&quot;), &quot;lookup&quot;: ()}</span>

<span class="Comment">//: this test we can't represent with a scenario</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> test_parse_comment_terminated_by_eof<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  load<span class="Delimiter">(</span><span class="Constant">&quot;recipe main [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
       <span class="Constant">&quot;  a:number &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="Constant">&quot;# abc&quot;</span><span class="Delimiter">);</span>  <span class="Comment">// no newline after comment</span>
  cerr &lt;&lt; <span class="Constant">&quot;.&quot;</span><span class="Delimiter">;</span>  <span class="Comment">// termination = success</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario warn_on_missing_space_before_bracket)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main[
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+error: insert a space before '[' in 'main['</span>

<span class="Comment">//: Warn if a recipe gets redefined, because large codebases can accidentally</span>
<span class="Comment">//: step on their own toes. But there'll be many occasions later where</span>
<span class="Comment">//: we'll want to disable the errors.</span>
<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
<span class="Normal">bool</span> Disable_redefine_checks = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Setup&quot;)</span>
Disable_redefine_checks = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> should_check_for_redefine<span class="Delimiter">(</span><span class="Normal">const</span> string&amp; recipe_name<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>Disable_redefine_checks<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario forbid_redefining_recipes)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">24</span>
]
<span class="traceContains">+error: redefining recipe main</span>

<span class="Delimiter">:(scenario permit_forcibly_redefining_recipes)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="muRecipe">def</span>! main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">24</span>
]
<span class="traceAbsent">-error: redefining recipe main</span>
$error: <span class="Constant">0</span>

<span class="Delimiter">:(code)</span>
<span class="Comment">// for debugging</span>
<span class="Normal">void</span> show_rest_of_stream<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  cerr &lt;&lt; <span class="Constant">'^'</span><span class="Delimiter">;</span>
  <span class="Normal">char</span> c<span class="Delimiter">;</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>in &gt;&gt; c<span class="Delimiter">)</span>
    cerr &lt;&lt; c<span class="Delimiter">;</span>
  cerr &lt;&lt; <span class="Constant">&quot;$</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
  exit<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->