about summary refs log tree commit diff stats
path: root/html/026call.cc.html
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-04-25 22:27:19 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-04-25 22:27:19 -0700
commit32b8fac2799ac7cec613e84a3eb9c009141b6a3a (patch)
tree11f56c1a235abf7b626ea8983fff3d2edb1fcf98 /html/026call.cc.html
parent224972ee9871fcb06ee285fa5f3d9528c034d414 (diff)
downloadmu-32b8fac2799ac7cec613e84a3eb9c009141b6a3a.tar.gz
2866
Diffstat (limited to 'html/026call.cc.html')
-rw-r--r--html/026call.cc.html207
1 files changed, 207 insertions, 0 deletions
diff --git a/html/026call.cc.html b/html/026call.cc.html
new file mode 100644
index 00000000..229f0fc8
--- /dev/null
+++ b/html/026call.cc.html
@@ -0,0 +1,207 @@
+<!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; }
+.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; }
+-->
+</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>
+def main [
+  f
+]
+def f [
+  <span class="Constant">3</span>:number<span class="Special"> &lt;- </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>
+def main [
+  f
+  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
+  <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
+  <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
+]
+def f [
+  <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
+  <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
+]
+<span class="traceContains">+run: f</span>
+<span class="Comment"># running f</span>
+<span class="traceContains">+run: {4: &quot;number&quot;} &lt;- copy {0: &quot;literal&quot;}</span>
+<span class="traceContains">+run: {5: &quot;number&quot;} &lt;- copy {0: &quot;literal&quot;}</span>
+<span class="Comment"># back out to main</span>
+<span class="traceContains">+run: {1: &quot;number&quot;} &lt;- copy {0: &quot;literal&quot;}</span>
+<span class="traceContains">+run: {2: &quot;number&quot;} &lt;- copy {0: &quot;literal&quot;}</span>
+<span class="traceContains">+run: {3: &quot;number&quot;} &lt;- copy {0: &quot;literal&quot;}</span>
+
+<span class="Delimiter">:(before &quot;struct routine {&quot;)</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&lt;call&gt; call_stack<span class="Delimiter">;</span>
+
+<span class="Delimiter">:(replace{} &quot;struct routine&quot;)</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&lt;instruction&gt;&amp; 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">-&gt;</span>callstack_depth<span class="Delimiter">;</span>
+    trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;trace&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;new routine; incrementing callstack depth to &quot;</span> &lt;&lt; Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt;&lt; end<span class="Delimiter">();</span>
+    assert<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt; <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>
+<span class="Normal">inline</span> call&amp; current_call<span class="Delimiter">()</span> <span class="Delimiter">{</span>
+  <span class="Identifier">return</span> Current_routine<span class="Delimiter">-&gt;</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{} &quot;inline int&amp; current_step_index()&quot;)</span>
+<span class="Normal">inline</span> <span class="Normal">int</span>&amp; current_step_index<span class="Delimiter">()</span> <span class="Delimiter">{</span>
+  assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-&gt;</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{} &quot;inline const string&amp; current_recipe_name()&quot;)</span>
+<span class="Normal">inline</span> <span class="Normal">const</span> string&amp; current_recipe_name<span class="Delimiter">()</span> <span class="Delimiter">{</span>
+  assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-&gt;</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{} &quot;inline const instruction&amp; current_instruction()&quot;)</span>
+<span class="Normal">inline</span> <span class="Normal">const</span> instruction&amp; current_instruction<span class="Delimiter">()</span> <span class="Delimiter">{</span>
+  assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-&gt;</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">inline</span> <span class="Normal">const</span> instruction&amp; to_instruction<span class="Delimiter">(</span><span class="Normal">const</span> call&amp; 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 &quot;Defined Recipe Checks&quot;)</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 &lt;&lt; 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> &lt;&lt; <span class="Constant">&quot;undefined operation in '&quot;</span> &lt;&lt; to_original_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &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">break</span><span class="Delimiter">;</span>
+<span class="Delimiter">}</span>
+<span class="Delimiter">:(replace{} &quot;default:&quot; following &quot;End Primitive Recipe Implementations&quot;)</span>
+<span class="Normal">default</span>: <span class="Delimiter">{</span>
+  <span class="Normal">const</span> instruction&amp; 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">-&gt;</span>callstack_depth<span class="Delimiter">;</span>
+    trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;trace&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;incrementing callstack depth to &quot;</span> &lt;&lt; Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt;&lt; end<span class="Delimiter">();</span>
+    assert<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt; <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">-&gt;</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&amp; call_instruction<span class="Delimiter">,</span> <span class="Normal">const</span> vector&lt;vector&lt;<span class="Normal">double</span>&gt; &gt;&amp; 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>
+def 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>
+def main [
+  <span class="Normal">x</span>:number<span class="Special"> &lt;- </span>foo
+]
+<span class="traceContains">+error: main: undefined operation in 'x:number &lt;- foo '</span>
+
+<span class="SalientComment">//:: finally, we need to fix the termination conditions for the run loop</span>
+
+<span class="Delimiter">:(replace{} &quot;inline bool routine::completed() const&quot;)</span>
+<span class="Normal">inline</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{} &quot;inline const vector&lt;instruction&gt;&amp; routine::steps() const&quot;)</span>
+<span class="Normal">inline</span> <span class="Normal">const</span> vector&lt;instruction&gt;&amp; 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 &quot;Running One Instruction&quot;)</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> &gt;= SIZE<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</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">&quot;trace&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;fall-through: exiting &quot;</span> &lt;&lt; current_recipe_name<span class="Delimiter">()</span> &lt;&lt; <span class="Constant">&quot;; decrementing callstack depth from &quot;</span> &lt;&lt; Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt;&lt; end<span class="Delimiter">();</span>
+    --Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth<span class="Delimiter">;</span>
+    assert<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &gt;= <span class="Constant">0</span><span class="Delimiter">);</span>
+  <span class="Delimiter">}</span>
+  Current_routine<span class="Delimiter">-&gt;</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">-&gt;</span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><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 : -->