diff options
-rw-r--r-- | html/028call_reply.cc.html | 92 | ||||
-rw-r--r-- | html/030container.cc.html | 2 | ||||
-rw-r--r-- | html/033address.cc.html | 2 | ||||
-rw-r--r-- | html/040brace.cc.html | 85 | ||||
-rw-r--r-- | html/052tangle.cc.html | 15 | ||||
-rw-r--r-- | html/055recipe_header.cc.html | 18 | ||||
-rw-r--r-- | html/057shape_shifting_container.cc.html | 2 | ||||
-rw-r--r-- | html/072channel.mu.html | 125 | ||||
-rw-r--r-- | html/084console.mu.html | 1 | ||||
-rw-r--r-- | html/chessboard.mu.html | 5 |
10 files changed, 212 insertions, 135 deletions
diff --git a/html/028call_reply.cc.html b/html/028call_reply.cc.html index 39ecff7c..396e7332 100644 --- a/html/028call_reply.cc.html +++ b/html/028call_reply.cc.html @@ -34,7 +34,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color <pre id='vimCodeElement'> <span class="Comment">//: Calls can also generate products, using 'reply' or 'return'.</span> -<span class="Delimiter">:(scenario reply)</span> +<span class="Delimiter">:(scenario return)</span> def main [ <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> <- </span>f <span class="Constant">34</span> ] @@ -47,16 +47,16 @@ def f [ <span class="traceContains">+mem: storing 35 in location 2</span> <span class="Delimiter">:(before "End Primitive Recipe Declarations")</span> -REPLY<span class="Delimiter">,</span> +RETURN<span class="Delimiter">,</span> <span class="Delimiter">:(before "End Primitive Recipe Numbers")</span> -put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"reply"</span><span class="Delimiter">,</span> REPLY<span class="Delimiter">);</span> -put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"return"</span><span class="Delimiter">,</span> REPLY<span class="Delimiter">);</span> +put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"return"</span><span class="Delimiter">,</span> RETURN<span class="Delimiter">);</span> +put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"reply"</span><span class="Delimiter">,</span> RETURN<span class="Delimiter">);</span> <span class="Comment">// synonym while teaching</span> <span class="Delimiter">:(before "End Primitive Recipe Checks")</span> -<span class="Normal">case</span> REPLY: <span class="Delimiter">{</span> +<span class="Normal">case</span> RETURN: <span class="Delimiter">{</span> <span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Comment">// checks will be performed by a transform below</span> <span class="Delimiter">}</span> <span class="Delimiter">:(before "End Primitive Recipe Implementations")</span> -<span class="Normal">case</span> REPLY: <span class="Delimiter">{</span> +<span class="Normal">case</span> RETURN: <span class="Delimiter">{</span> <span class="Comment">// Starting Reply</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">"reply: decrementing callstack depth from "</span> << Trace_stream<span class="Delimiter">-></span>callstack_depth << end<span class="Delimiter">();</span> @@ -95,7 +95,7 @@ Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</spa <span class="Normal">const</span> recipe& callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> caller_instruction<span class="Delimiter">.</span>operation<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>callee<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Normal">const</span> instruction& reply_inst = callee<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span> - <span class="Normal">if</span> <span class="Delimiter">(</span>reply_inst<span class="Delimiter">.</span>operation != REPLY<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>reply_inst<span class="Delimiter">.</span>operation != RETURN<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// check types with the caller</span> <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">)</span> > SIZE<span class="Delimiter">(</span>reply_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span> <span class="Delimiter">{</span> raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"too few values replied from "</span> << callee<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> @@ -104,7 +104,7 @@ Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</spa <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>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span> reagent lhs = reply_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span> reagent rhs = caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span> - <span class="Comment">// End Check REPLY Copy(lhs, rhs)</span> + <span class="Comment">// End Check RETURN Copy(lhs, rhs)</span> <span class="Normal">if</span> <span class="Delimiter">(</span>!types_coercible<span class="Delimiter">(</span>rhs<span class="Delimiter">,</span> lhs<span class="Delimiter">))</span> <span class="Delimiter">{</span> raise << maybe<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << reply_inst<span class="Delimiter">.</span>name << <span class="Constant">" ingredient "</span> << lhs<span class="Delimiter">.</span>original_string << <span class="Constant">" can't be saved in "</span> << rhs<span class="Delimiter">.</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> raise << to_string<span class="Delimiter">(</span>lhs<span class="Delimiter">.</span>type<span class="Delimiter">)</span> << <span class="Constant">" vs "</span> << to_string<span class="Delimiter">(</span>rhs<span class="Delimiter">.</span>type<span class="Delimiter">)</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> @@ -135,7 +135,7 @@ Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</spa <span class="Delimiter">}</span> <span class="Delimiter">}</span> -<span class="Delimiter">:(scenario reply_type_mismatch)</span> +<span class="Delimiter">:(scenario return_type_mismatch)</span> <span class="Special">% Hide_errors = true;</span> def main [ <span class="Constant">3</span>:number<span class="Special"> <- </span>f <span class="Constant">2</span> @@ -153,7 +153,7 @@ def f [ <span class="Comment">//: the recipe's 'reply' will help catch accidental misuse of such</span> <span class="Comment">//: 'ingredient-products' (sometimes called in-out parameters in other languages).</span> -<span class="Delimiter">:(scenario reply_same_as_ingredient)</span> +<span class="Delimiter">:(scenario return_same_as_ingredient)</span> <span class="Special">% Hide_errors = true;</span> def main [ <span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span> @@ -165,7 +165,7 @@ def test1 [ ] <span class="traceContains">+error: main: '2:number <- test1 1:number' should write to 1:number rather than 2:number</span> -<span class="Delimiter">:(scenario reply_same_as_ingredient_dummy)</span> +<span class="Delimiter">:(scenario return_same_as_ingredient_dummy)</span> def main [ <span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span> _<span class="Special"> <- </span>test1 <span class="Constant">1</span>:number <span class="Comment"># call with different ingredient and product</span> @@ -192,76 +192,6 @@ string to_string<span class="Delimiter">(</span><span class="Normal">const</span out << <span class="Constant">"]"</span><span class="Delimiter">;</span> <span class="Identifier">return</span> out<span class="Delimiter">.</span>str<span class="Delimiter">();</span> <span class="Delimiter">}</span> - -<span class="Comment">//: Conditional reply.</span> - -<span class="Delimiter">:(scenario reply_if)</span> -def main [ - <span class="Constant">1</span>:number<span class="Special"> <- </span>test1 -] -def test1 [ - <span class="Identifier">return</span>-<span class="Normal">if</span> <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">34</span> - <span class="Identifier">return</span> <span class="Constant">35</span> -] -<span class="traceContains">+mem: storing 35 in location 1</span> - -<span class="Delimiter">:(scenario reply_if_2)</span> -def main [ - <span class="Constant">1</span>:number<span class="Special"> <- </span>test1 -] -def test1 [ - <span class="Identifier">return</span>-<span class="Normal">if</span> <span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">34</span> - <span class="Identifier">return</span> <span class="Constant">35</span> -] -<span class="traceContains">+mem: storing 34 in location 1</span> - -<span class="Delimiter">:(before "End Rewrite Instruction(curr, recipe result)")</span> -<span class="Comment">// rewrite `reply-if a, b, c, ...` to</span> -<span class="Comment">// ```</span> -<span class="Comment">// jump-unless a, 1:offset</span> -<span class="Comment">// reply b, c, ...</span> -<span class="Comment">// ```</span> -<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"reply-if"</span> || curr<span class="Delimiter">.</span>name == <span class="Constant">"return-if"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> - <span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span> - curr<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"jump-unless"</span><span class="Delimiter">);</span> - curr<span class="Delimiter">.</span>name = <span class="Constant">"jump-unless"</span><span class="Delimiter">;</span> - vector<reagent> results<span class="Delimiter">;</span> - copy<span class="Delimiter">(</span>++curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> inserter<span class="Delimiter">(</span>results<span class="Delimiter">,</span> results<span class="Delimiter">.</span>end<span class="Delimiter">()));</span> - curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span> - curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span><span class="Constant">"1:offset"</span><span class="Delimiter">));</span> - result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>curr<span class="Delimiter">);</span> - curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> - curr<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"reply"</span><span class="Delimiter">);</span> - curr<span class="Delimiter">.</span>name = <span class="Constant">"reply"</span><span class="Delimiter">;</span> - curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>swap<span class="Delimiter">(</span>results<span class="Delimiter">);</span> - <span class="Delimiter">}</span> - <span class="Normal">else</span> <span class="Delimiter">{</span> - raise << <span class="Constant">"'"</span> << curr<span class="Delimiter">.</span>name << <span class="Constant">"' never yields any products</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span> - <span class="Delimiter">}</span> -<span class="Delimiter">}</span> -<span class="Comment">// rewrite `reply-unless a, b, c, ...` to</span> -<span class="Comment">// ```</span> -<span class="Comment">// jump-if a, 1:offset</span> -<span class="Comment">// reply b, c, ...</span> -<span class="Comment">// ```</span> -<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"reply-unless"</span> || curr<span class="Delimiter">.</span>name == <span class="Constant">"return-unless"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> - <span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span> - curr<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"jump-if"</span><span class="Delimiter">);</span> - curr<span class="Delimiter">.</span>name = <span class="Constant">"jump-if"</span><span class="Delimiter">;</span> - vector<reagent> results<span class="Delimiter">;</span> - copy<span class="Delimiter">(</span>++curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> inserter<span class="Delimiter">(</span>results<span class="Delimiter">,</span> results<span class="Delimiter">.</span>end<span class="Delimiter">()));</span> - curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span> - curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span><span class="Constant">"1:offset"</span><span class="Delimiter">));</span> - result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>curr<span class="Delimiter">);</span> - curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> - curr<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"reply"</span><span class="Delimiter">);</span> - curr<span class="Delimiter">.</span>name = <span class="Constant">"reply"</span><span class="Delimiter">;</span> - curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>swap<span class="Delimiter">(</span>results<span class="Delimiter">);</span> - <span class="Delimiter">}</span> - <span class="Normal">else</span> <span class="Delimiter">{</span> - raise << <span class="Constant">"'"</span> << curr<span class="Delimiter">.</span>name << <span class="Constant">"' never yields any products</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span> - <span class="Delimiter">}</span> -<span class="Delimiter">}</span> </pre> </body> </html> diff --git a/html/030container.cc.html b/html/030container.cc.html index 311631ea..716db06e 100644 --- a/html/030container.cc.html +++ b/html/030container.cc.html @@ -89,7 +89,7 @@ def main [ <span class="traceContains">+mem: storing 36 in location 17</span> <span class="Comment">//: products of recipes can include containers</span> -<span class="Delimiter">:(scenario reply_container)</span> +<span class="Delimiter">:(scenario return_container)</span> def main [ <span class="Constant">3</span>:point<span class="Special"> <- </span>f <span class="Constant">2</span> ] diff --git a/html/033address.cc.html b/html/033address.cc.html index 04791321..f71ee22a 100644 --- a/html/033address.cc.html +++ b/html/033address.cc.html @@ -533,7 +533,7 @@ def main [ canonize_type<span class="Delimiter">(</span>ingredient<span class="Delimiter">);</span> <span class="Delimiter">:(before "End Preprocess NEXT_INGREDIENT product")</span> canonize_type<span class="Delimiter">(</span>product<span class="Delimiter">);</span> -<span class="Delimiter">:(before "End Check REPLY Copy(lhs, rhs)</span> +<span class="Delimiter">:(before "End Check RETURN Copy(lhs, rhs)</span> canonize_type<span class="Delimiter">(</span>lhs<span class="Delimiter">);</span> canonize_type<span class="Delimiter">(</span>rhs<span class="Delimiter">);</span> diff --git a/html/040brace.cc.html b/html/040brace.cc.html index 6077150d..b0912d1e 100644 --- a/html/040brace.cc.html +++ b/html/040brace.cc.html @@ -396,6 +396,91 @@ def main [ ] <span class="traceContains">+error: break-if expects 1 or 2 ingredients, but got none</span> +<span class="Comment">//: Using break we can now implement conditional returns.</span> + +<span class="Delimiter">:(scenario return_if)</span> +def main [ + <span class="Constant">1</span>:number<span class="Special"> <- </span>test1 +] +def test1 [ + <span class="Identifier">return</span>-<span class="Normal">if</span> <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">34</span> + <span class="Identifier">return</span> <span class="Constant">35</span> +] +<span class="traceContains">+mem: storing 35 in location 1</span> + +<span class="Delimiter">:(scenario return_if_2)</span> +def main [ + <span class="Constant">1</span>:number<span class="Special"> <- </span>test1 +] +def test1 [ + <span class="Identifier">return</span>-<span class="Normal">if</span> <span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">34</span> + <span class="Identifier">return</span> <span class="Constant">35</span> +] +<span class="traceContains">+mem: storing 34 in location 1</span> + +<span class="Delimiter">:(before "End Rewrite Instruction(curr, recipe result)")</span> +<span class="Comment">// rewrite `return-if a, b, c, ...` to</span> +<span class="Comment">// ```</span> +<span class="Comment">// {</span> +<span class="Comment">// break-unless a</span> +<span class="Comment">// return b, c, ...</span> +<span class="Comment">// }</span> +<span class="Comment">// ```</span> +<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"return-if"</span> || curr<span class="Delimiter">.</span>name == <span class="Constant">"reply-if"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span> + emit_return_block<span class="Delimiter">(</span>result<span class="Delimiter">,</span> <span class="Constant">"break-unless"</span><span class="Delimiter">,</span> curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> + curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> + <span class="Delimiter">}</span> + <span class="Normal">else</span> <span class="Delimiter">{</span> + raise << <span class="Constant">"'"</span> << curr<span class="Delimiter">.</span>name << <span class="Constant">"' never yields any products</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span> + <span class="Delimiter">}</span> +<span class="Delimiter">}</span> +<span class="Comment">// rewrite `return-unless a, b, c, ...` to</span> +<span class="Comment">// ```</span> +<span class="Comment">// {</span> +<span class="Comment">// break-if a</span> +<span class="Comment">// return b, c, ...</span> +<span class="Comment">// }</span> +<span class="Comment">// ```</span> +<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"return-unless"</span> || curr<span class="Delimiter">.</span>name == <span class="Constant">"reply-unless"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span> + emit_return_block<span class="Delimiter">(</span>result<span class="Delimiter">,</span> <span class="Constant">"break-if"</span><span class="Delimiter">,</span> curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> + curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> + <span class="Delimiter">}</span> + <span class="Normal">else</span> <span class="Delimiter">{</span> + raise << <span class="Constant">"'"</span> << curr<span class="Delimiter">.</span>name << <span class="Constant">"' never yields any products</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span> + <span class="Delimiter">}</span> +<span class="Delimiter">}</span> + +<span class="Delimiter">:(code)</span> +<span class="Normal">void</span> emit_return_block<span class="Delimiter">(</span>recipe& out<span class="Delimiter">,</span> <span class="Normal">const</span> string& break_command<span class="Delimiter">,</span> <span class="Normal">const</span> vector<reagent>& ingredients<span class="Delimiter">)</span> <span class="Delimiter">{</span> + reagent condition = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> + vector<reagent> return_ingredients<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>return_ingredients<span class="Delimiter">,</span> return_ingredients<span class="Delimiter">.</span>end<span class="Delimiter">()));</span> + + <span class="Comment">// {</span> + instruction open_label<span class="Delimiter">;</span> open_label<span class="Delimiter">.</span>is_label=<span class="Constant">true</span><span class="Delimiter">;</span> open_label<span class="Delimiter">.</span>label = <span class="Constant">"{"</span><span class="Delimiter">;</span> + out<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>open_label<span class="Delimiter">);</span> + + <span class="Comment">// <break command> <condition></span> + instruction break_inst<span class="Delimiter">;</span> + break_inst<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> break_command<span class="Delimiter">);</span> + break_inst<span class="Delimiter">.</span>name = break_inst<span class="Delimiter">.</span>old_name = break_command<span class="Delimiter">;</span> + break_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>condition<span class="Delimiter">);</span> + out<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>break_inst<span class="Delimiter">);</span> + + <span class="Comment">// return <return ingredients></span> + instruction return_inst<span class="Delimiter">;</span> + return_inst<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"return"</span><span class="Delimiter">);</span> + return_inst<span class="Delimiter">.</span>name = <span class="Constant">"return"</span><span class="Delimiter">;</span> + return_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>swap<span class="Delimiter">(</span>return_ingredients<span class="Delimiter">);</span> + out<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>return_inst<span class="Delimiter">);</span> + + <span class="Comment">// }</span> + instruction close_label<span class="Delimiter">;</span> close_label<span class="Delimiter">.</span>is_label=<span class="Constant">true</span><span class="Delimiter">;</span> close_label<span class="Delimiter">.</span>label = <span class="Constant">"}"</span><span class="Delimiter">;</span> + out<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>close_label<span class="Delimiter">);</span> +<span class="Delimiter">}</span> + <span class="Comment">//: Make sure these pseudo recipes get consistent numbers in all tests, even</span> <span class="Comment">//: though they aren't implemented. Allows greater flexibility in ordering</span> <span class="Comment">//: transforms.</span> diff --git a/html/052tangle.cc.html b/html/052tangle.cc.html index 4ffcaebf..6356cd68 100644 --- a/html/052tangle.cc.html +++ b/html/052tangle.cc.html @@ -168,14 +168,15 @@ tangle_done = <span class="Constant">false</span><span class="Delimiter">;</span <span class="Delimiter">}</span> <span class="Comment">//: complain about unapplied fragments</span> -<span class="Delimiter">:(before "End Globals")</span> -<span class="Normal">bool</span> Transform_check_insert_fragments_Ran = <span class="Constant">false</span><span class="Delimiter">;</span> -<span class="Delimiter">:(after "Transform.push_back(insert_fragments)")</span> -Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_insert_fragments<span class="Delimiter">);</span> <span class="Comment">// idempotent</span> +<span class="Comment">//: This can't run during transform because later (shape-shifting recipes)</span> +<span class="Comment">//: we'll encounter situations where fragments might get used long after</span> +<span class="Comment">//: they're loaded, and we might run transform_all in between. To avoid</span> +<span class="Comment">//: spurious errors, run this check right at the end, after all code is</span> +<span class="Comment">//: loaded, right before we run main.</span> +<span class="Delimiter">:(before "End Commandline Parsing")</span> +check_insert_fragments<span class="Delimiter">();</span> <span class="Delimiter">:(code)</span> -<span class="Normal">void</span> check_insert_fragments<span class="Delimiter">(</span>unused recipe_ordinal<span class="Delimiter">)</span> <span class="Delimiter">{</span> - <span class="Normal">if</span> <span class="Delimiter">(</span>Transform_check_insert_fragments_Ran<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span> - Transform_check_insert_fragments_Ran = <span class="Constant">true</span><span class="Delimiter">;</span> +<span class="Normal">void</span> check_insert_fragments<span class="Delimiter">()</span> <span class="Delimiter">{</span> <span class="Normal">for</span> <span class="Delimiter">(</span>map<string<span class="Delimiter">,</span> recipe>::iterator p = Before_fragments<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Before_fragments<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Fragments_used<span class="Delimiter">,</span> p<span class="Delimiter">-></span>first<span class="Delimiter">))</span> raise << <span class="Constant">"could not locate insert before "</span> << p<span class="Delimiter">-></span>first << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> diff --git a/html/055recipe_header.cc.html b/html/055recipe_header.cc.html index 93acb6c8..ccdee487 100644 --- a/html/055recipe_header.cc.html +++ b/html/055recipe_header.cc.html @@ -398,7 +398,7 @@ Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</spa <span class="Comment">//: One final convenience: no need to say what to return if the information is</span> <span class="Comment">//: in the header.</span> -<span class="Delimiter">:(scenario reply_based_on_header)</span> +<span class="Delimiter">:(scenario return_based_on_header)</span> def main [ <span class="Constant">1</span>:number/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span> ] @@ -463,7 +463,7 @@ def add2 a:number<span class="Delimiter">,</span> b:number <span class="Delimite <span class="traceContains">+mem: storing 3 in location 1</span> <span class="traceContains">+mem: storing -2 in location 2</span> -<span class="Delimiter">:(scenario reply_on_fallthrough_based_on_header)</span> +<span class="Delimiter">:(scenario return_on_fallthrough_based_on_header)</span> def main [ <span class="Constant">1</span>:number/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span> ] @@ -475,7 +475,7 @@ def add2 x:number<span class="Delimiter">,</span> y:number <span class="Delimite <span class="traceContains">+transform: instruction: reply {z: "number"}</span> <span class="traceContains">+mem: storing 8 in location 1</span> -<span class="Delimiter">:(scenario reply_on_fallthrough_already_exists)</span> +<span class="Delimiter">:(scenario return_on_fallthrough_already_exists)</span> def main [ <span class="Constant">1</span>:number/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span> ] @@ -489,6 +489,18 @@ def add2 x:number<span class="Delimiter">,</span> y:number <span class="Delimite <span class="traceAbsent">-transform: instruction: reply z:number</span> <span class="traceContains">+mem: storing 8 in location 1</span> +<span class="Delimiter">:(scenario return_after_conditional_reply_based_on_header)</span> +def main [ + <span class="Constant">1</span>:number/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span> +] +def add2 x:number<span class="Delimiter">,</span> y:number <span class="Delimiter">-></span> z:number [ + local-scope + load-ingredients + z<span class="Special"> <- </span>add x<span class="Delimiter">,</span> y <span class="Comment"># no type for z</span> + <span class="Identifier">return</span>-<span class="Normal">if</span> <span class="Constant">0</span>/<span class="Constant">false</span><span class="Delimiter">,</span> <span class="Constant">34</span> +] +<span class="traceContains">+mem: storing 8 in location 1</span> + <span class="Delimiter">:(scenario recipe_headers_perform_same_ingredient_check)</span> <span class="Special">% Hide_errors = true;</span> def main [ diff --git a/html/057shape_shifting_container.cc.html b/html/057shape_shifting_container.cc.html index 7c8b2826..1dbf543b 100644 --- a/html/057shape_shifting_container.cc.html +++ b/html/057shape_shifting_container.cc.html @@ -120,7 +120,7 @@ map<string<span class="Delimiter">,</span> type_ordinal> type_ingredient_n <span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Delimiter">{</span> string curr = slurp_until<span class="Delimiter">(</span>in<span class="Delimiter">,</span> <span class="Constant">':'</span><span class="Delimiter">);</span> <span class="Normal">if</span> <span class="Delimiter">(</span>info<span class="Delimiter">.</span>type_ingredient_names<span class="Delimiter">.</span>find<span class="Delimiter">(</span>curr<span class="Delimiter">)</span> != info<span class="Delimiter">.</span>type_ingredient_names<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> - raise << <span class="Constant">"can't repeat type ingredient names in a single container definition</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span> + raise << <span class="Constant">"can't repeat type ingredient names in a single container definition: "</span> << curr << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> put<span class="Delimiter">(</span>info<span class="Delimiter">.</span>type_ingredient_names<span class="Delimiter">,</span> curr<span class="Delimiter">,</span> next_type_ordinal++<span class="Delimiter">);</span> diff --git a/html/072channel.mu.html b/html/072channel.mu.html index afda2035..c710e692 100644 --- a/html/072channel.mu.html +++ b/html/072channel.mu.html @@ -96,6 +96,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color <span class="Constant">local-scope</span> <span class="Constant">load-ingredients</span> chan:address:channel:_elem<span class="Special"> <- </span>get *out, <span class="Constant">chan:offset</span> +<span class="Constant"> <channel-write-initial></span> <span class="Delimiter">{</span> <span class="Comment"># block if chan is full</span> full:boolean<span class="Special"> <- </span>channel-full? chan @@ -129,6 +130,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color <span class="Comment"># block if chan is empty</span> empty?:boolean<span class="Special"> <- </span>channel-empty? chan <span class="muControl">break-unless</span> empty? +<span class="Constant"> <channel-read-empty></span> free-address:location<span class="Special"> <- </span>get-location *chan, <span class="Constant">first-free:offset</span> wait-for-location free-address <span class="Delimiter">}</span> @@ -229,45 +231,6 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color ] ] -<span class="SalientComment">## helpers</span> - -<span class="Comment"># An empty channel has first-empty and first-full both at the same value.</span> -<span class="muRecipe">def</span> channel-empty? chan:address:channel:_elem<span class="muRecipe"> -> </span>result:boolean [ - <span class="Constant">local-scope</span> - <span class="Constant">load-ingredients</span> - <span class="Comment"># return chan.first-full == chan.first-free</span> - full:number<span class="Special"> <- </span>get *chan, <span class="Constant">first-full:offset</span> - free:number<span class="Special"> <- </span>get *chan, <span class="Constant">first-free:offset</span> - result<span class="Special"> <- </span>equal full, free -] - -<span class="Comment"># A full channel has first-empty just before first-full, wasting one slot.</span> -<span class="Comment"># (Other alternatives: <a href="https://en.wikipedia.org/wiki/Circular_buffer#Full_.2F_Empty_Buffer_Distinction)">https://en.wikipedia.org/wiki/Circular_buffer#Full_.2F_Empty_Buffer_Distinction)</a></span> -<span class="muRecipe">def</span> channel-full? chan:address:channel:_elem<span class="muRecipe"> -> </span>result:boolean [ - <span class="Constant">local-scope</span> - <span class="Constant">load-ingredients</span> - <span class="Comment"># tmp = chan.first-free + 1</span> - tmp:number<span class="Special"> <- </span>get *chan, <span class="Constant">first-free:offset</span> - tmp<span class="Special"> <- </span>add tmp, <span class="Constant">1</span> - <span class="Delimiter">{</span> - <span class="Comment"># if tmp == chan.capacity, tmp = 0</span> - len:number<span class="Special"> <- </span>capacity chan - at-end?:boolean<span class="Special"> <- </span>greater-or-equal tmp, len - <span class="muControl">break-unless</span> at-end? - tmp<span class="Special"> <- </span>copy <span class="Constant">0</span> - <span class="Delimiter">}</span> - <span class="Comment"># return chan.first-full == tmp</span> - full:number<span class="Special"> <- </span>get *chan, <span class="Constant">first-full:offset</span> - result<span class="Special"> <- </span>equal full, tmp -] - -<span class="muRecipe">def</span> capacity chan:address:channel:_elem<span class="muRecipe"> -> </span>result:number [ - <span class="Constant">local-scope</span> - <span class="Constant">load-ingredients</span> - q:address:array:_elem<span class="Special"> <- </span>get *chan, <span class="Constant">data:offset</span> - result<span class="Special"> <- </span>length *q -] - <span class="muScenario">scenario</span> channel-new-empty-not-full [ run [ <span class="Constant">1</span>:address:source:number, <span class="Constant">2</span>:address:sink:number<span class="Special"> <- </span>new-channel <span class="Constant">3/capacity</span> @@ -324,6 +287,85 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color ] ] +<span class="SalientComment">## cancelling channels</span> + +<span class="Comment"># every channel comes with a boolean signifying if it's been closed</span> +<span class="Comment"># initially this boolean is false</span> +<span class="Comment"># todo: can't yet include type ingredients when extending containers</span> +<span class="muData">container</span> channel [ + closed?:boolean +] + +<span class="Comment"># a channel can be closed from either the source or the sink</span> +<span class="Comment"># both threads can modify it, but they can only set it, so this is a benign race</span> +<span class="muRecipe">def</span> close x:address:source:_elem<span class="muRecipe"> -> </span>x:address:source:_elem [ + <span class="Constant">local-scope</span> + <span class="Constant">load-ingredients</span> + chan:address:channel:_elem<span class="Special"> <- </span>get *x, <span class="Constant">chan:offset</span> + *chan<span class="Special"> <- </span>put *chan, <span class="Constant">closed?:offset</span>, <span class="Constant">1/true</span> +] +<span class="muRecipe">def</span> close x:address:sink:_elem<span class="muRecipe"> -> </span>x:address:sink:_elem [ + <span class="Constant">local-scope</span> + <span class="Constant">load-ingredients</span> + chan:address:channel:_elem<span class="Special"> <- </span>get *x, <span class="Constant">chan:offset</span> + *chan<span class="Special"> <- </span>put *chan, <span class="Constant">closed?:offset</span>, <span class="Constant">1/true</span> +] + +<span class="Comment"># once a channel is closed from one side, no further operations are expected from that side</span> +<span class="Comment"># if a channel is closed for reading,</span> +<span class="Comment"># no further writes will be let through</span> +<span class="Comment"># if a channel is closed for writing,</span> +<span class="Comment"># future reads continue until the channel empties,</span> +<span class="Comment"># then the channel is also closed for reading</span> +<span class="muRecipe">after</span> <span class="Constant"><channel-write-initial></span> [ + closed?:boolean<span class="Special"> <- </span>get *chan, <span class="Constant">closed?:offset</span> + <span class="muControl">return-if</span> closed? +] + +<span class="muRecipe">after</span> <span class="Constant"><channel-read-empty></span> [ + closed?:boolean<span class="Special"> <- </span>get *chan, <span class="Constant">closed?:offset</span> + <span class="muControl">return-if</span> closed?, <span class="Constant">0/hack</span> <span class="Comment"># only scalar _elems supported in channels; need to support construction of arbitrary empty containers</span> +] + +<span class="SalientComment">## helpers</span> + +<span class="Comment"># An empty channel has first-empty and first-full both at the same value.</span> +<span class="muRecipe">def</span> channel-empty? chan:address:channel:_elem<span class="muRecipe"> -> </span>result:boolean [ + <span class="Constant">local-scope</span> + <span class="Constant">load-ingredients</span> + <span class="Comment"># return chan.first-full == chan.first-free</span> + full:number<span class="Special"> <- </span>get *chan, <span class="Constant">first-full:offset</span> + free:number<span class="Special"> <- </span>get *chan, <span class="Constant">first-free:offset</span> + result<span class="Special"> <- </span>equal full, free +] + +<span class="Comment"># A full channel has first-empty just before first-full, wasting one slot.</span> +<span class="Comment"># (Other alternatives: <a href="https://en.wikipedia.org/wiki/Circular_buffer#Full_.2F_Empty_Buffer_Distinction)">https://en.wikipedia.org/wiki/Circular_buffer#Full_.2F_Empty_Buffer_Distinction)</a></span> +<span class="muRecipe">def</span> channel-full? chan:address:channel:_elem<span class="muRecipe"> -> </span>result:boolean [ + <span class="Constant">local-scope</span> + <span class="Constant">load-ingredients</span> + <span class="Comment"># tmp = chan.first-free + 1</span> + tmp:number<span class="Special"> <- </span>get *chan, <span class="Constant">first-free:offset</span> + tmp<span class="Special"> <- </span>add tmp, <span class="Constant">1</span> + <span class="Delimiter">{</span> + <span class="Comment"># if tmp == chan.capacity, tmp = 0</span> + len:number<span class="Special"> <- </span>capacity chan + at-end?:boolean<span class="Special"> <- </span>greater-or-equal tmp, len + <span class="muControl">break-unless</span> at-end? + tmp<span class="Special"> <- </span>copy <span class="Constant">0</span> + <span class="Delimiter">}</span> + <span class="Comment"># return chan.first-full == tmp</span> + full:number<span class="Special"> <- </span>get *chan, <span class="Constant">first-full:offset</span> + result<span class="Special"> <- </span>equal full, tmp +] + +<span class="muRecipe">def</span> capacity chan:address:channel:_elem<span class="muRecipe"> -> </span>result:number [ + <span class="Constant">local-scope</span> + <span class="Constant">load-ingredients</span> + q:address:array:_elem<span class="Special"> <- </span>get *chan, <span class="Constant">data:offset</span> + result<span class="Special"> <- </span>length *q +] + <span class="Comment"># helper for channels of characters in particular</span> <span class="muRecipe">def</span> buffer-lines in:address:source:character, buffered-out:address:sink:character<span class="muRecipe"> -> </span>buffered-out:address:sink:character, in:address:source:character [ <span class="Constant">local-scope</span> @@ -372,6 +414,11 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color i<span class="Special"> <- </span>add i, <span class="Constant">1</span> <span class="muControl">loop</span> <span class="Delimiter">}</span> + <span class="Delimiter">{</span> + <span class="muControl">break-unless</span> eof? + buffered-out<span class="Special"> <- </span>close buffered-out + <span class="muControl">return</span> + <span class="Delimiter">}</span> <span class="muControl">loop</span> <span class="Delimiter">}</span> ] diff --git a/html/084console.mu.html b/html/084console.mu.html index 157e7504..fb6df826 100644 --- a/html/084console.mu.html +++ b/html/084console.mu.html @@ -115,6 +115,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color chan<span class="Special"> <- </span>write chan, c <span class="muControl">loop</span> <span class="Delimiter">}</span> + chan<span class="Special"> <- </span>close chan ] <span class="muRecipe">def</span> wait-for-event console:address:console<span class="muRecipe"> -> </span>console:address:console [ diff --git a/html/chessboard.mu.html b/html/chessboard.mu.html index 357c33ff..d4241248 100644 --- a/html/chessboard.mu.html +++ b/html/chessboard.mu.html @@ -71,7 +71,8 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color run [ screen:address:screen, console:address:console<span class="Special"> <- </span>chessboard screen:address:screen, console:address:console <span class="Comment"># icon for the cursor</span> - screen<span class="Special"> <- </span>print screen, <span class="Constant">9251/␣</span> + <span class="Constant">1</span>:character/cursor-icon<span class="Special"> <- </span>copy <span class="Constant">9251/␣</span> + screen<span class="Special"> <- </span>print screen, <span class="Constant">1</span>:character/cursor-icon ] screen-should-contain [ <span class="Comment"># 1 2 3 4 5 6 7 8 9 10 11</span> @@ -291,7 +292,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color to-rank:number, quit?, error?<span class="Special"> <- </span>read-rank stdin, screen <span class="muControl">return-if</span> quit?, <span class="Constant">0/dummy</span> <span class="muControl">return-if</span> error?, <span class="Constant">0/dummy</span> - *result<span class="Special"> <- </span>put *result, <span class="Constant">to-rank:offset</span>, to-file + *result<span class="Special"> <- </span>put *result, <span class="Constant">to-rank:offset</span>, to-rank error?<span class="Special"> <- </span>expect-from-channel stdin, <span class="Constant">10/newline</span>, screen <span class="muControl">return-if</span> error?, <span class="Constant">0/dummy</span>, <span class="Constant">0/quit</span> ] |