|
|
<!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 - 040brace.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<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-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.cSpecial { color: #008000; }
.Constant { color: #00a0a0; }
.CommentedCode { color: #6c6c6c; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Comment { color: #9090ff; }
.Delimiter { color: #a04060; }
.Special { color: #ff6060; }
.Identifier { color: #804000; }
.traceContains { color: #008000; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Structured programming</span>
<span class="Comment">//:</span>
<span class="Comment">//: Our jump recipes are quite inconvenient to use, so mu provides a</span>
<span class="Comment">//: lightweight tool called 'transform_braces' to work in a slightly more</span>
<span class="Comment">//: convenient format with nested braces:</span>
<span class="Comment">//:</span>
<span class="Comment">//: {</span>
<span class="Comment">//: some instructions</span>
<span class="Comment">//: {</span>
<span class="Comment">//: more instructions</span>
<span class="Comment">//: }</span>
<span class="Comment">//: }</span>
<span class="Comment">//:</span>
<span class="Comment">//: Braces are just labels, they require no special parsing. The pseudo</span>
<span class="Comment">//: recipes 'loop' and 'break' jump to just after the enclosing '{' and '}'</span>
<span class="Comment">//: respectively.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Conditional and unconditional 'loop' and 'break' should give us 80% of the</span>
<span class="Comment">//: benefits of the control-flow primitives we're used to in other languages,</span>
<span class="Comment">//: like 'if', 'while', 'for', etc.</span>
<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario brace_conversion)</span>
recipe main [
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: jump 1:offset</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="Comment">//: one-time setup</span>
<span class="Delimiter">:(after "int main")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>transform_braces<span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> transform_braces<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? cout << "AAA transform_braces\n"; //? 1</span>
<span class="CommentedCode">//? exit(0); //? 1</span>
<span class="Normal">const</span> <span class="Normal">int</span> OPEN = <span class="Constant">0</span><span class="Delimiter">,</span> CLOSE = <span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Comment">// use signed integer for step index because we'll be doing arithmetic on it</span>
list<pair<<span class="Normal">int</span><span class="Comment">/*</span><span class="Comment">OPEN/CLOSE</span><span class="Comment">*/</span><span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">step</span><span class="Comment">*/</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> > braces<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> index = <span class="Constant">0</span><span class="Delimiter">;</span> index < SIZE<span class="Delimiter">(</span>Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& inst = Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label == <span class="Constant">"{"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">"brace"</span><span class="Delimiter">)</span> << r << <span class="Constant">": push (open, "</span> << index << <span class="Constant">")"</span> << end<span class="Delimiter">();</span>
braces<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair<<span class="Normal">int</span><span class="Delimiter">,</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>><span class="Delimiter">(</span>OPEN<span class="Delimiter">,</span> index<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label == <span class="Constant">"}"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">"brace"</span><span class="Delimiter">)</span> << <span class="Constant">"push (close, "</span> << index << <span class="Constant">")"</span> << end<span class="Delimiter">();</span>
braces<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair<<span class="Normal">int</span><span class="Delimiter">,</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>><span class="Delimiter">(</span>CLOSE<span class="Delimiter">,</span> index<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
stack<<span class="Comment">/*</span><span class="Comment">step</span><span class="Comment">*/</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> open_braces<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">"after-brace"</span><span class="Delimiter">)</span> << <span class="Constant">"recipe "</span> << Recipe[r]<span class="Delimiter">.</span>name << end<span class="Delimiter">();</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> index = <span class="Constant">0</span><span class="Delimiter">;</span> index < SIZE<span class="Delimiter">(</span>Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
instruction& inst = Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label == <span class="Constant">"{"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
open_braces<span class="Delimiter">.</span>push<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>label == <span class="Constant">"}"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
open_braces<span class="Delimiter">.</span>pop<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>is_label<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation != Recipe_ordinal[<span class="Constant">"loop"</span>]
&& inst<span class="Delimiter">.</span>operation != Recipe_ordinal[<span class="Constant">"loop-if"</span>]
&& inst<span class="Delimiter">.</span>operation != Recipe_ordinal[<span class="Constant">"loop-unless"</span>]
&& inst<span class="Delimiter">.</span>operation != Recipe_ordinal[<span class="Constant">"break"</span>]
&& inst<span class="Delimiter">.</span>operation != Recipe_ordinal[<span class="Constant">"break-if"</span>]
&& inst<span class="Delimiter">.</span>operation != Recipe_ordinal[<span class="Constant">"break-unless"</span>]<span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">"after-brace"</span><span class="Delimiter">)</span> << inst<span class="Delimiter">.</span>name << <span class="Constant">" ..."</span> << end<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// update instruction operation</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-if"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span>
inst<span class="Delimiter">.</span>operation = Recipe_ordinal[<span class="Constant">"jump-if"</span>]<span class="Delimiter">;</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-unless"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span>
inst<span class="Delimiter">.</span>operation = Recipe_ordinal[<span class="Constant">"jump-unless"</span>]<span class="Delimiter">;</span>
<span class="Normal">else</span>
inst<span class="Delimiter">.</span>operation = Recipe_ordinal[<span class="Constant">"jump"</span>]<span class="Delimiter">;</span>
<span class="Comment">// check for explicitly provided targets</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-if"</span><span class="Delimiter">)</span> != string::npos || inst<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-unless"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// conditional branches check arg 1</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</span> && is_literal<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">"after-brace"</span><span class="Delimiter">)</span> << <span class="Constant">"jump "</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>name << <span class="Constant">":offset"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Comment">// unconditional branches check arg 0</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> && is_literal<span class="Delimiter">(</span>inst<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>
trace<span class="Delimiter">(</span><span class="Constant">"after-brace"</span><span class="Delimiter">)</span> << <span class="Constant">"jump "</span> << inst<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="Constant">":offset"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// if implicit, compute target</span>
reagent target<span class="Delimiter">;</span>
target<span class="Delimiter">.</span>types<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>Type_ordinal[<span class="Constant">"offset"</span>]<span class="Delimiter">);</span>
target<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>open_braces<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
raise << inst<span class="Delimiter">.</span>name << <span class="Constant">" needs a '{' before</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"loop"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span>
target<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>open_braces<span class="Delimiter">.</span>top<span class="Delimiter">()</span>-index<span class="Delimiter">);</span>
<span class="Normal">else</span> <span class="Comment">// break instruction</span>
target<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>matching_brace<span class="Delimiter">(</span>open_braces<span class="Delimiter">.</span>top<span class="Delimiter">(),</span> braces<span class="Delimiter">,</span> r<span class="Delimiter">)</span> - index - <span class="Constant">1</span><span class="Delimiter">);</span>
inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>target<span class="Delimiter">);</span>
<span class="Comment">// log computed target</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-if"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span>
trace<span class="Delimiter">(</span><span class="Constant">"after-brace"</span><span class="Delimiter">)</span> << <span class="Constant">"jump-if "</span> << inst<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="Constant">", "</span> << target<span class="Delimiter">.</span>value << <span class="Constant">":offset"</span> << end<span class="Delimiter">();</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"-unless"</span><span class="Delimiter">)</span> != string::npos<span class="Delimiter">)</span>
trace<span class="Delimiter">(</span><span class="Constant">"after-brace"</span><span class="Delimiter">)</span> << <span class="Constant">"jump-unless "</span> << inst<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="Constant">", "</span> << target<span class="Delimiter">.</span>value << <span class="Constant">":offset"</span> << end<span class="Delimiter">();</span>
<span class="Normal">else</span>
trace<span class="Delimiter">(</span><span class="Constant">"after-brace"</span><span class="Delimiter">)</span> << <span class="Constant">"jump "</span> << target<span class="Delimiter">.</span>value << <span class="Constant">":offset"</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// returns a signed integer not just so that we can return -1 but also to</span>
<span class="Comment">// enable future signed arithmetic</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> matching_brace<span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> index<span class="Delimiter">,</span> <span class="Normal">const</span> list<pair<<span class="Normal">int</span><span class="Delimiter">,</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> >& braces<span class="Delimiter">,</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">int</span> stacksize = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>list<pair<<span class="Normal">int</span><span class="Delimiter">,</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> >::const_iterator p = braces<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != braces<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>p<span class="Delimiter">-></span>second < index<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
stacksize += <span class="Delimiter">(</span>p<span class="Delimiter">-></span>first ? <span class="Constant">1</span> : -<span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>stacksize == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> p<span class="Delimiter">-></span>second<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
raise << Recipe[r]<span class="Delimiter">.</span>name << <span class="Constant">": unbalanced '{'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span> SIZE<span class="Delimiter">(</span>Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> <span class="Comment">// exit current routine</span>
<span class="Delimiter">}</span>
<span class="Comment">// temporarily suppress run</span>
<span class="Normal">void</span> transform<span class="Delimiter">(</span>string form<span class="Delimiter">)</span> <span class="Delimiter">{</span>
load<span class="Delimiter">(</span>form<span class="Delimiter">);</span>
transform_all<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.</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
BREAK<span class="Delimiter">,</span>
BREAK_IF<span class="Delimiter">,</span>
BREAK_UNLESS<span class="Delimiter">,</span>
LOOP<span class="Delimiter">,</span>
LOOP_IF<span class="Delimiter">,</span>
LOOP_UNLESS<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_ordinal[<span class="Constant">"break"</span>] = BREAK<span class="Delimiter">;</span>
Recipe_ordinal[<span class="Constant">"break-if"</span>] = BREAK_IF<span class="Delimiter">;</span>
Recipe_ordinal[<span class="Constant">"break-unless"</span>] = BREAK_UNLESS<span class="Delimiter">;</span>
Recipe_ordinal[<span class="Constant">"loop"</span>] = LOOP<span class="Delimiter">;</span>
Recipe_ordinal[<span class="Constant">"loop-if"</span>] = LOOP_IF<span class="Delimiter">;</span>
Recipe_ordinal[<span class="Constant">"loop-unless"</span>] = LOOP_UNLESS<span class="Delimiter">;</span>
<span class="Delimiter">:(scenario loop)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
loop
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump -2:offset</span>
<span class="Delimiter">:(scenario break_empty_block)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump 0:offset</span>
<span class="Delimiter">:(scenario break_cascading)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump 0:offset</span>
<span class="traceContains">+after-brace: jump 0:offset</span>
<span class="Delimiter">:(scenario break_cascading2)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump 1:offset</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump 0:offset</span>
<span class="Delimiter">:(scenario break_if)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Identifier">break</span>-<span class="Normal">if</span> <span class="Constant">2</span>:number
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump-if 2, 1:offset</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump 0:offset</span>
<span class="Delimiter">:(scenario break_nested)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Identifier">break</span>
<span class="Delimiter">{</span>
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
<span class="Constant">4</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: jump 4:offset</span>
<span class="Delimiter">:(scenario break_nested_degenerate)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Identifier">break</span>
<span class="Delimiter">{</span>
<span class="Delimiter">}</span>
<span class="Constant">4</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: jump 3:offset</span>
<span class="Delimiter">:(scenario break_nested_degenerate2)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Identifier">break</span>
<span class="Delimiter">{</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: jump 2:offset</span>
<span class="Delimiter">:(scenario break_label)</span>
<span class="Special">% Hide_warnings = true;</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Identifier">break</span> +foo:offset
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: jump +foo:offset</span>
<span class="Delimiter">:(scenario break_unless)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Identifier">break</span>-unless <span class="Constant">2</span>:number
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump-unless 2, 1:offset</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="Delimiter">:(scenario loop_unless)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
loop-unless <span class="Constant">2</span>:number
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: jump-unless 2, -1:offset</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="Delimiter">:(scenario loop_nested)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">{</span>
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
loop-<span class="Normal">if</span> <span class="Constant">4</span>:boolean
<span class="Constant">5</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
<span class="Delimiter">}</span>
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: jump-if 4, -5:offset</span>
<span class="Delimiter">:(scenario loop_label)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
+foo
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
]
<span class="traceContains">+after-brace: recipe main</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="traceContains">+after-brace: copy ...</span>
<span class="Comment">//: test how things actually run</span>
<span class="Delimiter">:(scenarios run)</span>
<span class="Delimiter">:(scenario brace_conversion_and_run)</span>
<span class="CommentedCode">#? % Trace_stream->dump_layer = "run";</span>
recipe test-factorial [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">5</span>:literal
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:literal
<span class="Delimiter">{</span>
<span class="Constant">3</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">1</span>:literal
<span class="Identifier">break</span>-<span class="Normal">if</span> <span class="Constant">3</span>:boolean
<span class="Comment"># $print 1:number</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>multiply <span class="Constant">2</span>:number<span class="Delimiter">,</span> <span class="Constant">1</span>:number
<span class="Constant">1</span>:number<span class="Special"> <- </span>subtract <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">1</span>:literal
loop
<span class="Delimiter">}</span>
<span class="Constant">4</span>:number<span class="Special"> <- </span>copy <span class="Constant">2</span>:number <span class="Comment"># trigger a read</span>
]
<span class="traceContains">+mem: location 2 is 120</span>
<span class="Delimiter">:(scenario break_outside_braces_warns)</span>
<span class="Special">% Hide_warnings = true;</span>
recipe main [
<span class="Identifier">break</span>
]
<span class="traceContains">+warn: break needs a '{' before</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|