diff options
Diffstat (limited to 'html/049continuation.cc.html')
-rw-r--r-- | html/049continuation.cc.html | 76 |
1 files changed, 44 insertions, 32 deletions
diff --git a/html/049continuation.cc.html b/html/049continuation.cc.html index 0eba6a43..12ac59a9 100644 --- a/html/049continuation.cc.html +++ b/html/049continuation.cc.html @@ -13,15 +13,17 @@ pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; } body { font-family: monospace; color: #eeeeee; background-color: #080808; } * { font-size: 1.05em; } -.traceContains { color: #008000; } -.Constant { color: #00a0a0; } +.cSpecial { color: #008000; } .SalientComment { color: #00ffff; } +.Constant { color: #00a0a0; } +.CommentedCode { color: #6c6c6c; } .traceAbsent { color: #c00000; } +.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; } .Comment { color: #9090ff; } .Delimiter { color: #a04060; } .Special { color: #ff6060; } .Identifier { color: #804000; } -.CommentedCode { color: #6c6c6c; } +.traceContains { color: #008000; } --> </style> @@ -42,8 +44,8 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; } <span class="Comment">//: todo: implement continuations in mu's memory</span> <span class="Delimiter">:(before "End Globals")</span> -map<long long int<span class="Delimiter">,</span> call_stack> Continuation<span class="Delimiter">;</span> -long long int Next_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span> +map<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span><span class="Delimiter">,</span> call_stack> Continuation<span class="Delimiter">;</span> +<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> Next_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span> <span class="Delimiter">:(before "End Setup")</span> Continuation<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> Next_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span> @@ -57,7 +59,7 @@ CURRENT_CONTINUATION<span class="Delimiter">,</span> <span class="Delimiter">:(before "End Primitive Recipe Numbers")</span> Recipe_ordinal[<span class="Constant">"current-continuation"</span>] = CURRENT_CONTINUATION<span class="Delimiter">;</span> <span class="Delimiter">:(before "End Primitive Recipe Implementations")</span> -case CURRENT_CONTINUATION: <span class="Delimiter">{</span> +<span class="Normal">case</span> CURRENT_CONTINUATION: <span class="Delimiter">{</span> <span class="Comment">// copy the current call stack</span> Continuation[Next_continuation_id] = Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">;</span> <span class="Comment">// deep copy because calls have no pointers</span> <span class="Comment">// make sure calling the copy doesn't spawn the same continuation again</span> @@ -65,7 +67,7 @@ case CURRENT_CONTINUATION: <span class="Delimiter">{</span> products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span> products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>Next_continuation_id<span class="Delimiter">);</span> ++Next_continuation_id<span class="Delimiter">;</span> - trace<span class="Delimiter">(</span><span class="Constant">"current-continuation"</span><span class="Delimiter">)</span> << <span class="Constant">"new continuation "</span> << Next_continuation_id<span class="Delimiter">;</span> + trace<span class="Delimiter">(</span><span class="Constant">"current-continuation"</span><span class="Delimiter">)</span> << <span class="Constant">"new continuation "</span> << Next_continuation_id << end<span class="Delimiter">();</span> <span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> @@ -74,9 +76,12 @@ CONTINUE_FROM<span class="Delimiter">,</span> <span class="Delimiter">:(before "End Primitive Recipe Numbers")</span> Recipe_ordinal[<span class="Constant">"continue-from"</span>] = CONTINUE_FROM<span class="Delimiter">;</span> <span class="Delimiter">:(before "End Primitive Recipe Implementations")</span> -case CONTINUE_FROM: <span class="Delimiter">{</span> - assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span> - long long int c = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> +<span class="Normal">case</span> CONTINUE_FROM: <span class="Delimiter">{</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>!scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span> + raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": first ingredient of 'continue-from' should be a continuation id generated by 'current-continuation', but got "</span> << current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> + <span class="Identifier">break</span><span class="Delimiter">;</span> + <span class="Delimiter">}</span> + <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> c = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> Current_routine<span class="Delimiter">-></span>calls = Continuation[c]<span class="Delimiter">;</span> <span class="Comment">// deep copy; calls have no pointers</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// skip rest of this instruction</span> <span class="Delimiter">}</span> @@ -88,8 +93,8 @@ recipe main [ <span class="Constant">2</span>:continuation<span class="Special"> <- </span>current-continuation <span class="Delimiter">{</span> <span class="CommentedCode">#? $print 1:number</span> - <span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-or-equal <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">3</span>:literal - <span class="Identifier">break</span>-if <span class="Constant">3</span>:boolean + <span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-<span class="Normal">or</span>-equal <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">3</span>:literal + <span class="Identifier">break</span>-<span class="Normal">if</span> <span class="Constant">3</span>:boolean <span class="Constant">1</span>:number<span class="Special"> <- </span>add <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">1</span>:literal <span class="Identifier">continue</span>-from <span class="Constant">2</span>:continuation <span class="Comment"># loop</span> <span class="Delimiter">}</span> @@ -107,8 +112,8 @@ recipe main [ <span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal <span class="Constant">2</span>:continuation<span class="Special"> <- </span>loop-body <span class="Delimiter">{</span> - <span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-or-equal <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">3</span>:literal - <span class="Identifier">break</span>-if <span class="Constant">3</span>:boolean + <span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-<span class="Normal">or</span>-equal <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">3</span>:literal + <span class="Identifier">break</span>-<span class="Normal">if</span> <span class="Constant">3</span>:boolean <span class="Identifier">continue</span>-from <span class="Constant">2</span>:continuation <span class="Comment"># loop</span> <span class="Delimiter">}</span> ] @@ -142,8 +147,8 @@ recipe main [ <span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">5</span>:literal <span class="Delimiter">{</span> <span class="Constant">2</span>:number<span class="Special"> <- </span>call <span class="Constant">1</span>:continuation<span class="Delimiter">,</span> <span class="Constant">2</span>:number <span class="Comment"># 2 is an argument to g, the 'top' of the continuation</span> - <span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-or-equal <span class="Constant">2</span>:number<span class="Delimiter">,</span> <span class="Constant">8</span>:literal - <span class="Identifier">break</span>-if <span class="Constant">3</span>:boolean + <span class="Constant">3</span>:boolean<span class="Special"> <- </span>greater-<span class="Normal">or</span>-equal <span class="Constant">2</span>:number<span class="Delimiter">,</span> <span class="Constant">8</span>:literal + <span class="Identifier">break</span>-<span class="Normal">if</span> <span class="Constant">3</span>:boolean loop <span class="Delimiter">}</span> ] @@ -191,7 +196,7 @@ recipe g [ <span class="Comment">//: the call stack</span> <span class="Delimiter">:(before "End call Fields")</span> -bool is_reset<span class="Delimiter">;</span> +<span class="Normal">bool</span> is_reset<span class="Delimiter">;</span> <span class="Delimiter">:(before "End call Constructor")</span> is_reset = <span class="Constant">false</span><span class="Delimiter">;</span> @@ -202,19 +207,19 @@ CREATE_DELIMITED_CONTINUATION<span class="Delimiter">,</span> <span class="Delimiter">:(before "End Primitive Recipe Numbers")</span> Recipe_ordinal[<span class="Constant">"create-delimited-continuation"</span>] = CREATE_DELIMITED_CONTINUATION<span class="Delimiter">;</span> <span class="Delimiter">:(before "End Primitive Recipe Implementations")</span> -case CREATE_DELIMITED_CONTINUATION: <span class="Delimiter">{</span> +<span class="Normal">case</span> CREATE_DELIMITED_CONTINUATION: <span class="Delimiter">{</span> Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>is_reset = <span class="Constant">true</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>Recipe_ordinal[current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name]<span class="Delimiter">));</span> ingredients<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">());</span> <span class="Comment">// drop the callee</span> - <span class="Identifier">goto</span> complete_call<span class="Delimiter">;</span> + <span class="Identifier">goto</span> call_housekeeping<span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Comment">//: save the slice of current call stack until the 'create-delimited-continuation'</span> <span class="Comment">//: call, and return it as the result.</span> <span class="Comment">//: todo: implement delimited continuations in mu's memory</span> <span class="Delimiter">:(before "End Globals")</span> -map<long long int<span class="Delimiter">,</span> call_stack> Delimited_continuation<span class="Delimiter">;</span> -long long int Next_delimited_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span> +map<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span><span class="Delimiter">,</span> call_stack> Delimited_continuation<span class="Delimiter">;</span> +<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> Next_delimited_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span> <span class="Delimiter">:(before "End Setup")</span> Delimited_continuation<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> Next_delimited_continuation_id = <span class="Constant">0</span><span class="Delimiter">;</span> @@ -224,7 +229,7 @@ REPLY_DELIMITED_CONTINUATION<span class="Delimiter">,</span> <span class="Delimiter">:(before "End Primitive Recipe Numbers")</span> Recipe_ordinal[<span class="Constant">"reply-delimited-continuation"</span>] = REPLY_DELIMITED_CONTINUATION<span class="Delimiter">;</span> <span class="Delimiter">:(before "End Primitive Recipe Implementations")</span> -case REPLY_DELIMITED_CONTINUATION: <span class="Delimiter">{</span> +<span class="Normal">case</span> REPLY_DELIMITED_CONTINUATION: <span class="Delimiter">{</span> <span class="Comment">// first clear any existing ingredients, to isolate the creation of the</span> <span class="Comment">// continuation from its calls</span> Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>ingredient_atoms<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> @@ -232,9 +237,12 @@ case REPLY_DELIMITED_CONTINUATION: <span class="Delimiter">{</span> <span class="Comment">// copy the current call stack until the most recent 'reset' call</span> call_stack::iterator find_reset<span class="Delimiter">(</span>call_stack& c<span class="Delimiter">);</span> <span class="Comment">// manual prototype containing '::'</span> call_stack::iterator reset = find_reset<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">);</span> - assert<span class="Delimiter">(</span>reset != Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>end<span class="Delimiter">());</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>reset == Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> + raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": couldn't find a 'reset' call to jump out to</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> Delimited_continuation[Next_delimited_continuation_id] = call_stack<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> reset<span class="Delimiter">);</span> - while <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>begin<span class="Delimiter">()</span> != reset<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Normal">while</span> <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>begin<span class="Delimiter">()</span> != reset<span class="Delimiter">)</span> <span class="Delimiter">{</span> --Callstack_depth<span class="Delimiter">;</span> Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>pop_front<span class="Delimiter">();</span> <span class="Delimiter">}</span> @@ -247,23 +255,27 @@ case REPLY_DELIMITED_CONTINUATION: <span class="Delimiter">{</span> <span class="Delimiter">:(code)</span> call_stack::iterator find_reset<span class="Delimiter">(</span>call_stack& c<span class="Delimiter">)</span> <span class="Delimiter">{</span> - for <span class="Delimiter">(</span>call_stack::iterator p = c<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != c<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> - if <span class="Delimiter">(</span>p<span class="Delimiter">-></span>is_reset<span class="Delimiter">)</span> <span class="Identifier">return</span> p<span class="Delimiter">;</span> + <span class="Normal">for</span> <span class="Delimiter">(</span>call_stack::iterator p = c<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != c<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>p<span class="Delimiter">-></span>is_reset<span class="Delimiter">)</span> <span class="Identifier">return</span> p<span class="Delimiter">;</span> <span class="Identifier">return</span> c<span class="Delimiter">.</span>end<span class="Delimiter">();</span> <span class="Delimiter">}</span> <span class="Comment">//: overload 'call' for continuations</span> -<span class="Delimiter">:(after "case CALL:")</span> - if <span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">"continuation"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> +<span class="Delimiter">:(after "Begin Call")</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>!current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> + && !current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> + && current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">"continuation"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Comment">// copy multiple calls on to current call stack</span> assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span> - assert<span class="Delimiter">(</span>Delimited_continuation<span class="Delimiter">.</span>find<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> != Delimited_continuation<span class="Delimiter">.</span>end<span class="Delimiter">());</span> - const call_stack& new_calls = Delimited_continuation[ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span>]<span class="Delimiter">;</span> - for <span class="Delimiter">(</span>call_stack::const_reverse_iterator p = new_calls<span class="Delimiter">.</span>rbegin<span class="Delimiter">();</span> p != new_calls<span class="Delimiter">.</span>rend<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>Delimited_continuation<span class="Delimiter">.</span>find<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> == Delimited_continuation<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> + raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": no such delimited continuation "</span> << current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> + <span class="Delimiter">}</span> + <span class="Normal">const</span> call_stack& new_calls = Delimited_continuation[ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span>]<span class="Delimiter">;</span> + <span class="Normal">for</span> <span class="Delimiter">(</span>call_stack::const_reverse_iterator p = new_calls<span class="Delimiter">.</span>rbegin<span class="Delimiter">();</span> p != new_calls<span class="Delimiter">.</span>rend<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>push_front<span class="Delimiter">(</span>*p<span class="Delimiter">);</span> ++current_step_index<span class="Delimiter">();</span> <span class="Comment">// skip past the reply-delimited-continuation</span> ingredients<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">());</span> <span class="Comment">// drop the callee</span> - <span class="Identifier">goto</span> complete_call<span class="Delimiter">;</span> + <span class="Identifier">goto</span> call_housekeeping<span class="Delimiter">;</span> <span class="Delimiter">}</span> </pre> </body> |