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
|
<!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 - 026call.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; }
.cSpecial { color: #008000; }
.muRecipe { color: #ff8700; }
.SalientComment { color: #00ffff; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.traceContains { color: #008000; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Identifier { color: #fcb165; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: So far the recipes we define can't run each other. Let's fix that.</span>
<span class="Delimiter">:(scenario calling_recipe)</span>
<span class="muRecipe">def</span> main [
f
]
<span class="muRecipe">def</span> f [
<span class="Constant">3</span>:num<span class="Special"> <- </span>add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">2</span>
]
<span class="traceContains">+mem: storing 4 in location 3</span>
<span class="Delimiter">:(scenario return_on_fallthrough)</span>
<span class="muRecipe">def</span> main [
f
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">3</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">def</span> f [
<span class="Constant">4</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">5</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: f</span>
<span class="Comment"># running f</span>
<span class="traceContains">+run: {4: "number"} <- copy {0: "literal"}</span>
<span class="traceContains">+run: {5: "number"} <- copy {0: "literal"}</span>
<span class="Comment"># back out to main</span>
<span class="traceContains">+run: {1: "number"} <- copy {0: "literal"}</span>
<span class="traceContains">+run: {2: "number"} <- copy {0: "literal"}</span>
<span class="traceContains">+run: {3: "number"} <- copy {0: "literal"}</span>
<span class="Delimiter">:(before "struct routine {")</span>
<span class="Comment">// Everytime a recipe runs another, we interrupt it and start running the new</span>
<span class="Comment">// recipe. When that finishes, we continue this one where we left off.</span>
<span class="Comment">// This requires maintaining a 'stack' of interrupted recipes or 'calls'.</span>
<span class="Normal">struct</span> call <span class="Delimiter">{</span>
recipe_ordinal running_recipe<span class="Delimiter">;</span>
<span class="Normal">int</span> running_step_index<span class="Delimiter">;</span>
<span class="Comment">// End call Fields</span>
call<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
running_recipe = r<span class="Delimiter">;</span>
running_step_index = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Comment">// End call Constructor</span>
<span class="Delimiter">}</span>
~call<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Comment">// End call Destructor</span>
<span class="Delimiter">}</span>
<span class="Delimiter">};</span>
<span class="Normal">typedef</span> list<call> call_stack<span class="Delimiter">;</span>
<span class="Delimiter">:(replace{} "struct routine")</span>
<span class="Normal">struct</span> routine <span class="Delimiter">{</span>
call_stack calls<span class="Delimiter">;</span>
<span class="Comment">// End routine Fields</span>
routine<span class="Delimiter">(</span>recipe_ordinal r<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">:(code)</span>
routine::routine<span class="Delimiter">(</span>recipe_ordinal r<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="Delimiter">{</span>
++Trace_stream<span class="Delimiter">-></span>callstack_depth<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"trace"</span><span class="Delimiter">)</span> << <span class="Constant">"new routine; incrementing callstack depth to "</span> << Trace_stream<span class="Delimiter">-></span>callstack_depth << end<span class="Delimiter">();</span>
assert<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-></span>callstack_depth < <span class="Constant">9000</span><span class="Delimiter">);</span> <span class="Comment">// 9998-101 plus cushion</span>
<span class="Delimiter">}</span>
calls<span class="Delimiter">.</span>push_front<span class="Delimiter">(</span>call<span class="Delimiter">(</span>r<span class="Delimiter">));</span>
<span class="Comment">// End routine Constructor</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
call& current_call<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>front<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: now update routine's helpers</span>
<span class="Delimiter">:(replace{} "int& current_step_index()")</span>
<span class="Normal">int</span>& current_step_index<span class="Delimiter">()</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
<span class="Identifier">return</span> current_call<span class="Delimiter">().</span>running_step_index<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(replace{} "const string& current_recipe_name()")</span>
<span class="Normal">const</span> string& current_recipe_name<span class="Delimiter">()</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
<span class="Identifier">return</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> current_call<span class="Delimiter">().</span>running_recipe<span class="Delimiter">).</span>name<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(replace{} "const recipe& current_recipe()")</span>
<span class="Normal">const</span> recipe& current_recipe<span class="Delimiter">()</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
<span class="Identifier">return</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> current_call<span class="Delimiter">().</span>running_recipe<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(replace{} "const instruction& current_instruction()")</span>
<span class="Normal">const</span> instruction& current_instruction<span class="Delimiter">()</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
<span class="Identifier">return</span> to_instruction<span class="Delimiter">(</span>current_call<span class="Delimiter">());</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">const</span> instruction& to_instruction<span class="Delimiter">(</span><span class="Normal">const</span> call& call<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> call<span class="Delimiter">.</span>running_recipe<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>call<span class="Delimiter">.</span>running_step_index<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(after "Defined Recipe Checks")</span>
<span class="Comment">// not a primitive; check that it's present in the book of recipes</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"undefined operation in '"</span> << inst<span class="Delimiter">.</span>original_string << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(replace{} "default:" following "End Primitive Recipe Implementations")</span>
<span class="Normal">default</span>: <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& call_instruction = current_instruction<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Recipe<span class="Delimiter">.</span>find<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>operation<span class="Delimiter">)</span> == Recipe<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> <span class="Comment">// duplicate from Checks</span>
<span class="Comment">// stop running this instruction immediately</span>
++current_step_index<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// not a primitive; look up the book of recipes</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Trace_stream<span class="Delimiter">)</span> <span class="Delimiter">{</span>
++Trace_stream<span class="Delimiter">-></span>callstack_depth<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"trace"</span><span class="Delimiter">)</span> << <span class="Constant">"incrementing callstack depth to "</span> << Trace_stream<span class="Delimiter">-></span>callstack_depth << end<span class="Delimiter">();</span>
assert<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-></span>callstack_depth < <span class="Constant">9000</span><span class="Delimiter">);</span> <span class="Comment">// 9998-101 plus cushion</span>
<span class="Delimiter">}</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>push_front<span class="Delimiter">(</span>call<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>operation<span class="Delimiter">));</span>
finish_call_housekeeping<span class="Delimiter">(</span>call_instruction<span class="Delimiter">,</span> ingredients<span class="Delimiter">);</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// not done with caller; don't increment step_index of caller</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> finish_call_housekeeping<span class="Delimiter">(</span><span class="Normal">const</span> instruction& call_instruction<span class="Delimiter">,</span> <span class="Normal">const</span> vector<vector<<span class="Normal">double</span>> >& ingredients<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// End Call Housekeeping</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario calling_undefined_recipe_fails)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
foo
]
<span class="traceContains">+error: main: undefined operation in 'foo '</span>
<span class="Delimiter">:(scenario calling_undefined_recipe_handles_missing_result)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
<span class="Normal">x</span>:num<span class="Special"> <- </span>foo
]
<span class="traceContains">+error: main: undefined operation in 'x:num <- foo '</span>
<span class="SalientComment">//:: finally, we need to fix the termination conditions for the run loop</span>
<span class="Delimiter">:(replace{} "bool routine::completed() const")</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> calls<span class="Delimiter">.</span>empty<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(replace{} "const vector<instruction>& routine::steps() const")</span>
<span class="Normal">const</span> vector<instruction>& routine::steps<span class="Delimiter">()</span> <span class="Normal">const</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>!calls<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
<span class="Identifier">return</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>running_recipe<span class="Delimiter">).</span>steps<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "Running One Instruction")</span>
<span class="Comment">// when we reach the end of one call, we may reach the end of the one below</span>
<span class="Comment">// it, and the one below that, and so on</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>current_step_index<span class="Delimiter">()</span> >= SIZE<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>steps<span class="Delimiter">()))</span> <span class="Delimiter">{</span>
<span class="Comment">// Falling Through End Of Recipe</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Trace_stream<span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"trace"</span><span class="Delimiter">)</span> << <span class="Constant">"fall-through: exiting "</span> << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">"; decrementing callstack depth from "</span> << Trace_stream<span class="Delimiter">-></span>callstack_depth << end<span class="Delimiter">();</span>
--Trace_stream<span class="Delimiter">-></span>callstack_depth<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-></span>callstack_depth >= <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>pop_front<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">goto</span> stop_running_current_routine<span class="Delimiter">;</span>
<span class="Comment">// Complete Call Fallthrough</span>
<span class="Comment">// todo: fail if no products returned</span>
++current_step_index<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|