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
|
<!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 - 020run.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; }
.traceContains { color: #008000; }
.SalientComment { color: #00ffff; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.PreProc { color: #800080; }
.CommentedCode { color: #6c6c6c; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Phase 3: Start running a loaded and transformed recipe.</span>
<span class="Comment">//:</span>
<span class="Comment">//: So far we've seen recipes as lists of instructions, and instructions point</span>
<span class="Comment">//: at other recipes. To kick things off mu needs to know how to run certain</span>
<span class="Comment">//: 'primitive' recipes. That will then give the ability to run recipes</span>
<span class="Comment">//: containing these primitives.</span>
<span class="Comment">//:</span>
<span class="Comment">//: This layer defines a skeleton with just two primitive recipes: IDLE which</span>
<span class="Comment">//: does nothing, and COPY, which can copy numbers from one memory location to</span>
<span class="Comment">//: another. Later layers will add more primitives.</span>
<span class="Delimiter">:(scenario copy_literal)</span>
def main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+run: {1: "number"} <- copy {23: "literal"}</span>
<span class="traceContains">+mem: storing 23 in location 1</span>
<span class="Delimiter">:(scenario copy)</span>
def main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">23</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:number
]
<span class="traceContains">+run: {2: "number"} <- copy {1: "number"}</span>
<span class="traceContains">+mem: location 1 is 23</span>
<span class="traceContains">+mem: storing 23 in location 2</span>
<span class="Delimiter">:(scenario copy_multiple)</span>
def main [
<span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">23</span><span class="Delimiter">,</span> <span class="Constant">24</span>
]
<span class="traceContains">+mem: storing 23 in location 1</span>
<span class="traceContains">+mem: storing 24 in location 2</span>
<span class="Delimiter">:(before "End Types")</span>
<span class="Comment">// Book-keeping while running a recipe.</span>
<span class="Comment">//: Later layers will replace this to support running multiple routines at once.</span>
<span class="Normal">struct</span> routine <span class="Delimiter">{</span>
recipe_ordinal running_recipe<span class="Delimiter">;</span>
<span class="Normal">int</span> running_step_index<span class="Delimiter">;</span>
routine<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> :running_recipe<span class="Delimiter">(</span>r<span class="Delimiter">),</span> running_step_index<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{}</span>
<span class="Normal">bool</span> completed<span class="Delimiter">()</span> <span class="Normal">const</span><span class="Delimiter">;</span>
<span class="Normal">const</span> vector<instruction>& steps<span class="Delimiter">()</span> <span class="Normal">const</span><span class="Delimiter">;</span>
<span class="Delimiter">};</span>
<span class="Delimiter">:(before "End Globals")</span>
routine* Current_routine = <span class="Constant">NULL</span><span class="Delimiter">;</span>
map<string<span class="Delimiter">,</span> <span class="Normal">int</span>> Instructions_running<span class="Delimiter">;</span>
map<string<span class="Delimiter">,</span> <span class="Normal">int</span>> Locations_read<span class="Delimiter">;</span>
map<string<span class="Delimiter">,</span> <span class="Normal">int</span>> Locations_read_by_instruction<span class="Delimiter">;</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> run<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
routine rr<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
Current_routine = &rr<span class="Delimiter">;</span>
run_current_routine<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> run_current_routine<span class="Delimiter">()</span>
<span class="Delimiter">{</span> <span class="Comment">// curly on a separate line, because later layers will modify function header</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-></span>completed<span class="Delimiter">())</span> <span class="Comment">// later layers will modify condition</span>
<span class="Delimiter">{</span>
<span class="Comment">// Running One Instruction</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>is_label<span class="Delimiter">)</span> <span class="Delimiter">{</span> ++current_step_index<span class="Delimiter">();</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Delimiter">}</span>
trace<span class="Delimiter">(</span>Initial_callstack_depth + Trace_stream<span class="Delimiter">-></span>callstack_depth<span class="Delimiter">,</span> <span class="Constant">"run"</span><span class="Delimiter">)</span> << to_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">())</span> << end<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">)</span> != <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"something wrote to location 0; this should never happen</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">// read all ingredients from memory, each potentially spanning multiple locations</span>
vector<vector<<span class="Normal">double</span>> > ingredients<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>should_copy_ingredients<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 < SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>read_memory<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)));</span>
<span class="Delimiter">}</span>
<span class="Comment">// instructions below will write to 'products'</span>
vector<vector<<span class="Normal">double</span>> > products<span class="Delimiter">;</span>
<span class="Normal">switch</span> <span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>operation<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// Primitive Recipe Implementations</span>
<span class="Normal">case</span> COPY: <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>products<span class="Delimiter">,</span> products<span class="Delimiter">.</span>begin<span class="Delimiter">()));</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// End Primitive Recipe Implementations</span>
<span class="Normal">default</span>: <span class="Delimiter">{</span>
cout << <span class="Constant">"not a primitive op: "</span> << current_instruction<span class="Delimiter">().</span>operation << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// Write Products of Instruction</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>products<span class="Delimiter">)</span> < SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise << SIZE<span class="Delimiter">(</span>products<span class="Delimiter">)</span> << <span class="Constant">" vs "</span> << SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">)</span> << <span class="Constant">": failed to write to all products! "</span> << to_original_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">())</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">else</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 < SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
write_memory<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Comment">// End Write Products of Instruction</span>
<span class="Comment">// End Running One Instruction</span>
<span class="Normal">finish_instruction</span>:<span class="Delimiter">;</span>
++current_step_index<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">stop_running_current_routine</span>:<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> should_copy_ingredients<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Comment">// End should_copy_ingredients Special-cases</span>
<span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Some helpers.</span>
<span class="Comment">//: We'll need to override these later as we change the definition of routine.</span>
<span class="Comment">//: Important that they return referrences into the routine.</span>
<span class="Normal">int</span>& current_step_index<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> Current_routine<span class="Delimiter">-></span>running_step_index<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">const</span> string& current_recipe_name<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-></span>running_recipe<span class="Delimiter">).</span>name<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">const</span> instruction& current_instruction<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-></span>running_recipe<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>running_step_index<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> routine::completed<span class="Delimiter">()</span> <span class="Normal">const</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> running_step_index >= SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> running_recipe<span class="Delimiter">).</span>steps<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">const</span> vector<instruction>& routine::steps<span class="Delimiter">()</span> <span class="Normal">const</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> running_recipe<span class="Delimiter">).</span>steps<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: Startup flow</span>
<span class="Comment">//: Step 1: load all .mu files with numeric prefixes (in order)</span>
<span class="Delimiter">:(before "End Load Recipes")</span>
<span class="Comment">// Load .mu Core</span>
<span class="CommentedCode">//? Save_trace = true;</span>
<span class="CommentedCode">//? START_TRACING_UNTIL_END_OF_SCOPE;</span>
load_file_or_directory<span class="Delimiter">(</span><span class="Constant">"core.mu"</span><span class="Delimiter">);</span>
<span class="CommentedCode">//? DUMP("");</span>
<span class="CommentedCode">//? exit(0);</span>
<span class="Comment">//: Step 2: load any .mu files provided at the commandline</span>
<span class="Delimiter">:(before "End Commandline Parsing")</span>
<span class="Comment">// Check For .mu Files</span>
<span class="CommentedCode">//? START_TRACING_UNTIL_END_OF_SCOPE</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>argc > <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// skip argv[0]</span>
++argv<span class="Delimiter">;</span>
--argc<span class="Delimiter">;</span>
<span class="Comment">// ignore argv past '--'; that's commandline args for 'main'</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>argc > <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>string<span class="Delimiter">(</span>*argv<span class="Delimiter">)</span> == <span class="Constant">"--"</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
load_file_or_directory<span class="Delimiter">(</span>*argv<span class="Delimiter">);</span>
--argc<span class="Delimiter">;</span>
++argv<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Run_tests<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> <span class="Constant">"main"</span><span class="Delimiter">));</span>
<span class="Delimiter">}</span>
transform_all<span class="Delimiter">();</span>
<span class="CommentedCode">//? DUMP("");</span>
<span class="CommentedCode">//? exit(0);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Trace_errors<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">1</span><span class="Delimiter">;</span>
save_snapshots<span class="Delimiter">();</span>
<span class="Comment">//: Step 3: if we aren't running tests, locate a recipe called 'main' and</span>
<span class="Comment">//: start running it.</span>
<span class="Delimiter">:(before "End Main")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!Run_tests && contains_key<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"main"</span><span class="Delimiter">)</span> && contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"main"</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
<span class="Comment">// Running Main</span>
setup<span class="Delimiter">();</span>
<span class="CommentedCode">//? Save_trace = true;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Trace_main<span class="Delimiter">)</span> Trace_stream = <span class="Normal">new</span> trace_stream<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9990</span><span class="Delimiter">,</span> <span class="Constant">"run"</span><span class="Delimiter">)</span> << <span class="Constant">"=== Starting to run"</span> << end<span class="Delimiter">();</span>
assert<span class="Delimiter">(</span>Num_calls_to_transform_all == <span class="Constant">1</span><span class="Delimiter">);</span>
run_main<span class="Delimiter">(</span>argc<span class="Delimiter">,</span> argv<span class="Delimiter">);</span>
teardown<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> run_main<span class="Delimiter">(</span><span class="Normal">int</span> argc<span class="Delimiter">,</span> <span class="Normal">char</span>* argv[]<span class="Delimiter">)</span> <span class="Delimiter">{</span>
recipe_ordinal r = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"main"</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>r<span class="Delimiter">)</span> run<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">//: By default we don't maintain the trace while running main because its</span>
<span class="Comment">//: overheads can grow rapidly. However, it's useful when debugging.</span>
<span class="Delimiter">:(before "End Globals")</span>
<span class="Normal">bool</span> Trace_main = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Commandline Options(*arg)")</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>is_equal<span class="Delimiter">(</span>*arg<span class="Delimiter">,</span> <span class="Constant">"--trace"</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
Trace_main = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> dump_profile<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>map<string<span class="Delimiter">,</span> <span class="Normal">int</span>>::iterator p = Instructions_running<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Instructions_running<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
cerr << p<span class="Delimiter">-></span>first << <span class="Constant">": "</span> << p<span class="Delimiter">-></span>second << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
cerr << <span class="Constant">"== locations read</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>map<string<span class="Delimiter">,</span> <span class="Normal">int</span>>::iterator p = Locations_read<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Locations_read<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
cerr << p<span class="Delimiter">-></span>first << <span class="Constant">": "</span> << p<span class="Delimiter">-></span>second << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
cerr << <span class="Constant">"== locations read by instruction</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>map<string<span class="Delimiter">,</span> <span class="Normal">int</span>>::iterator p = Locations_read_by_instruction<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Locations_read_by_instruction<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
cerr << p<span class="Delimiter">-></span>first << <span class="Constant">": "</span> << p<span class="Delimiter">-></span>second << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End One-time Setup")</span>
<span class="CommentedCode">//? atexit(dump_profile);</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> cleanup_main<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Save_trace && Trace_stream<span class="Delimiter">)</span> <span class="Delimiter">{</span>
ofstream fout<span class="Delimiter">(</span><span class="Constant">"interactive"</span><span class="Delimiter">);</span>
fout << Trace_stream<span class="Delimiter">-></span>readable_contents<span class="Delimiter">(</span><span class="Constant">""</span><span class="Delimiter">);</span>
fout<span class="Delimiter">.</span>close<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Trace_stream<span class="Delimiter">)</span> <span class="Normal">delete</span> Trace_stream<span class="Delimiter">,</span> Trace_stream = <span class="Constant">NULL</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End One-time Setup")</span>
atexit<span class="Delimiter">(</span>cleanup_main<span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> load_file_or_directory<span class="Delimiter">(</span>string filename<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_directory<span class="Delimiter">(</span>filename<span class="Delimiter">))</span> <span class="Delimiter">{</span>
load_all<span class="Delimiter">(</span>filename<span class="Delimiter">);</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
ifstream fin<span class="Delimiter">(</span>filename<span class="Delimiter">.</span>c_str<span class="Delimiter">());</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!fin<span class="Delimiter">)</span> <span class="Delimiter">{</span>
cerr << <span class="Constant">"no such file '"</span> << filename << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span> <span class="Comment">// don't raise, just warn. just in case it's just a name for a scenario to run.</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
trace<span class="Delimiter">(</span><span class="Constant">9990</span><span class="Delimiter">,</span> <span class="Constant">"load"</span><span class="Delimiter">)</span> << <span class="Constant">"=== "</span> << filename << end<span class="Delimiter">();</span>
load<span class="Delimiter">(</span>fin<span class="Delimiter">);</span>
fin<span class="Delimiter">.</span>close<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_directory<span class="Delimiter">(</span>string path<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">struct</span> stat info<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>stat<span class="Delimiter">(</span>path<span class="Delimiter">.</span>c_str<span class="Delimiter">(),</span> &info<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Comment">// error</span>
<span class="Identifier">return</span> info<span class="Delimiter">.</span>st_mode & S_IFDIR<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> load_all<span class="Delimiter">(</span>string dir<span class="Delimiter">)</span> <span class="Delimiter">{</span>
dirent** files<span class="Delimiter">;</span>
<span class="Normal">int</span> num_files = scandir<span class="Delimiter">(</span>dir<span class="Delimiter">.</span>c_str<span class="Delimiter">(),</span> &files<span class="Delimiter">,</span> <span class="Constant">NULL</span><span class="Delimiter">,</span> alphasort<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 < num_files<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
string curr_file = files[i]<span class="Delimiter">-></span>d_name<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>isdigit<span class="Delimiter">(</span>curr_file<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span>
load_file_or_directory<span class="Delimiter">(</span>dir+<span class="Constant">'/'</span>+curr_file<span class="Delimiter">);</span>
free<span class="Delimiter">(</span>files[i]<span class="Delimiter">);</span>
files[i] = <span class="Constant">NULL</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
free<span class="Delimiter">(</span>files<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Includes")</span>
<span class="PreProc">#include </span><span class="Constant"><dirent.h></span>
<span class="PreProc">#include </span><span class="Constant"><sys/stat.h></span>
<span class="SalientComment">//:: Reading from memory, writing to memory.</span>
<span class="Delimiter">:(code)</span>
vector<<span class="Normal">double</span>> read_memory<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// Begin Preprocess read_memory(x)</span>
vector<<span class="Normal">double</span>> result<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="Delimiter">{</span>
result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>x<span class="Delimiter">.</span>value<span class="Delimiter">);</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// End Preprocess read_memory(x)</span>
<span class="Normal">int</span> size = size_of<span class="Delimiter">(</span>x<span class="Delimiter">);</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> offset = <span class="Constant">0</span><span class="Delimiter">;</span> offset < size<span class="Delimiter">;</span> ++offset<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">double</span> val = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> x<span class="Delimiter">.</span>value+offset<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"location "</span> << x<span class="Delimiter">.</span>value+offset << <span class="Constant">" is "</span> << no_scientific<span class="Delimiter">(</span>val<span class="Delimiter">)</span> << end<span class="Delimiter">();</span>
result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>val<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> write_memory<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> x<span class="Delimiter">,</span> <span class="Normal">const</span> vector<<span class="Normal">double</span>>& data<span class="Delimiter">)</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">);</span> <span class="Comment">// run-time only</span>
<span class="Comment">// Begin Preprocess write_memory(x, data)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"can't write to '"</span> << to_string<span class="Delimiter">(</span>x<span class="Delimiter">)</span> << <span class="Constant">"'; no type</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>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_dummy<span class="Delimiter">(</span>x<span class="Delimiter">))</span> <span class="Identifier">return</span><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">return</span><span class="Delimiter">;</span>
<span class="Comment">// End Preprocess write_memory(x, data)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>value == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"can't write to location 0 in '"</span> << to_original_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">())</span> << <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>
<span class="Normal">if</span> <span class="Delimiter">(</span>size_mismatch<span class="Delimiter">(</span>x<span class="Delimiter">,</span> data<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">())</span> << <span class="Constant">"size mismatch in storing to '"</span> << x<span class="Delimiter">.</span>original_string << <span class="Constant">"' ("</span> << size_of<span class="Delimiter">(</span>x<span class="Delimiter">)</span> << <span class="Constant">" vs "</span> << SIZE<span class="Delimiter">(</span>data<span class="Delimiter">)</span> << <span class="Constant">") at '"</span> << to_original_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">())</span> << <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>
<span class="Comment">// End write_memory(x) Special-cases</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> offset = <span class="Constant">0</span><span class="Delimiter">;</span> offset < SIZE<span class="Delimiter">(</span>data<span class="Delimiter">);</span> ++offset<span class="Delimiter">)</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>x<span class="Delimiter">.</span>value+offset > <span class="Constant">0</span><span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing "</span> << no_scientific<span class="Delimiter">(</span>data<span class="Delimiter">.</span>at<span class="Delimiter">(</span>offset<span class="Delimiter">))</span> << <span class="Constant">" in location "</span> << x<span class="Delimiter">.</span>value+offset << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> x<span class="Delimiter">.</span>value+offset<span class="Delimiter">,</span> data<span class="Delimiter">.</span>at<span class="Delimiter">(</span>offset<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">int</span> size_of<span class="Delimiter">(</span><span class="Normal">const</span> reagent& r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!r<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Comment">// End size_of(reagent r) Cases</span>
<span class="Identifier">return</span> size_of<span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">int</span> size_of<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Comment">// End size_of(type) Cases</span>
<span class="Identifier">return</span> <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> size_mismatch<span class="Delimiter">(</span><span class="Normal">const</span> reagent& x<span class="Delimiter">,</span> <span class="Normal">const</span> vector<<span class="Normal">double</span>>& data<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Comment">// End size_mismatch(x) Cases</span>
<span class="CommentedCode">//? if (size_of(x) != SIZE(data)) cerr << size_of(x) << " vs " << SIZE(data) << '\n';</span>
<span class="Identifier">return</span> size_of<span class="Delimiter">(</span>x<span class="Delimiter">)</span> != SIZE<span class="Delimiter">(</span>data<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_literal<span class="Delimiter">(</span><span class="Normal">const</span> reagent& r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> is_literal<span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_literal<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">-></span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Identifier">return</span> type<span class="Delimiter">-></span>value == <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> scalar<span class="Delimiter">(</span><span class="Normal">const</span> vector<<span class="Normal">int</span>>& x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> SIZE<span class="Delimiter">(</span>x<span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> scalar<span class="Delimiter">(</span><span class="Normal">const</span> vector<<span class="Normal">double</span>>& x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> SIZE<span class="Delimiter">(</span>x<span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// helper for tests</span>
<span class="Normal">void</span> run<span class="Delimiter">(</span><span class="Normal">const</span> string& form<span class="Delimiter">)</span> <span class="Delimiter">{</span>
vector<recipe_ordinal> tmp = load<span class="Delimiter">(</span>form<span class="Delimiter">);</span>
transform_all<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>tmp<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>trace_count<span class="Delimiter">(</span><span class="Constant">"error"</span><span class="Delimiter">)</span> > <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Comment">// if a test defines main, it probably wants to start there regardless of</span>
<span class="Comment">// definition order</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"main"</span><span class="Delimiter">)))</span>
run<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"main"</span><span class="Delimiter">));</span>
<span class="Normal">else</span>
run<span class="Delimiter">(</span>tmp<span class="Delimiter">.</span>front<span class="Delimiter">());</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario run_label)</span>
def main [
+foo
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">23</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:number
]
<span class="traceContains">+run: {1: "number"} <- copy {23: "literal"}</span>
<span class="traceContains">+run: {2: "number"} <- copy {1: "number"}</span>
<span class="traceAbsent">-run: +foo</span>
<span class="Delimiter">:(scenario run_dummy)</span>
def main [
_<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: _ <- copy {0: "literal"}</span>
<span class="Delimiter">:(scenario write_to_0_disallowed)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
<span class="Constant">0</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
<span class="traceAbsent">-mem: storing 34 in location 0</span>
<span class="Comment">//: mu is robust to various combinations of commas and spaces. You just have</span>
<span class="Comment">//: to put spaces around the '<-'.</span>
<span class="Delimiter">:(scenario comma_without_space)</span>
def main [
<span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">2</span><span class="Delimiter">,</span><span class="Constant">2</span>
]
<span class="traceContains">+mem: storing 2 in location 1</span>
<span class="Delimiter">:(scenario space_without_comma)</span>
def main [
<span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">2</span> <span class="Constant">2</span>
]
<span class="traceContains">+mem: storing 2 in location 1</span>
<span class="Delimiter">:(scenario comma_before_space)</span>
def main [
<span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">2</span>
]
<span class="traceContains">+mem: storing 2 in location 1</span>
<span class="Delimiter">:(scenario comma_after_space)</span>
def main [
<span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">2</span> <span class="Delimiter">,</span><span class="Constant">2</span>
]
<span class="traceContains">+mem: storing 2 in location 1</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|