<!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 - 053recipe_header.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.traceAbsent { color: #c00000; }
.traceContains { color: #008000; }
.muRecipe { color: #ff8700; }
.cSpecial { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Identifier { color: #c0a020; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Error { color: #ffffff; background-color: #ff6060; padding-bottom: 1px; }
.SalientComment { color: #00ffff; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Advanced notation for the common/easy case where a recipe takes some fixed</span>
<span class="Comment">//: number of ingredients and yields some fixed number of products.</span>
<span class="Delimiter">:(scenario recipe_with_header)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>z:num [
local-scope
load-ingredients
<span class="Normal">z</span>:num<span class="Special"> <- </span>add x<span class="Delimiter">,</span> y
<span class="Identifier">return</span> z
]
<span class="traceContains">+mem: storing 8 in location 1</span>
<span class="Comment">//: When loading recipes save any header.</span>
<span class="Delimiter">:(before "End recipe Fields")</span>
<span class="Normal">bool</span> has_header<span class="Delimiter">;</span>
vector<reagent> ingredients<span class="Delimiter">;</span>
vector<reagent> products<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End recipe Constructor")</span>
has_header = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Recipe Refinements")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="Constant">'['</span><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">"parse"</span><span class="Delimiter">)</span> << <span class="Constant">"recipe has a header; parsing"</span> << end<span class="Delimiter">();</span>
load_recipe_header<span class="Delimiter">(</span>in<span class="Delimiter">,</span> result<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> load_recipe_header<span class="Delimiter">(</span>istream& in<span class="Delimiter">,</span> recipe& result<span class="Delimiter">)</span> <span class="Delimiter">{</span>
result<span class="Delimiter">.</span>has_header = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> && in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="Constant">'['</span> && in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
string s = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">));</span>
raise << <span class="Constant">"incomplete recipe header at end of file (0)</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">"<-"</span><span class="Delimiter">)</span>
raise << <span class="Constant">"recipe "</span> << result<span class="Delimiter">.</span>name << <span class="Constant">" should say '->' and not '<-'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">"->"</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
result<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span>s<span class="Delimiter">));</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"parse"</span><span class="Delimiter">)</span> << <span class="Constant">"header ingredient: "</span> << result<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>back<span class="Delimiter">().</span>original_string << end<span class="Delimiter">();</span>
skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> && in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="Constant">'['</span> && in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
string s = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">));</span>
raise << <span class="Constant">"incomplete recipe header at end of file (1)</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
result<span class="Delimiter">.</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span>s<span class="Delimiter">));</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"parse"</span><span class="Delimiter">)</span> << <span class="Constant">"header product: "</span> << result<span class="Delimiter">.</span>products<span class="Delimiter">.</span>back<span class="Delimiter">().</span>original_string << end<span class="Delimiter">();</span>
skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">// End Load Recipe Header(result)</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario recipe_handles_stray_comma)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>z:num<span class="Delimiter">,</span> [
local-scope
load-ingredients
<span class="Normal">z</span>:num<span class="Special"> <- </span>add x<span class="Delimiter">,</span> y
<span class="Identifier">return</span> z
]
<span class="traceContains">+mem: storing 8 in location 1</span>
<span class="Delimiter">:(scenario recipe_handles_stray_comma_2)</span>
<span class="muRecipe">def</span> main [
foo
]
<span class="muRecipe">def</span> foo<span class="Delimiter">,</span> [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">2</span>
]
<span class="muRecipe">def</span> bar [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">3</span>
]
<span class="traceContains">+mem: storing 4 in location 1</span>
<span class="Delimiter">:(scenario recipe_handles_wrong_arrow)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> foo a:num<span class="Special"> <- </span>b:num [
]
<span class="traceContains">+error: recipe foo should say '->' and not '<-'</span>
<span class="Delimiter">:(scenario recipe_handles_missing_bracket)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main
<span class="Error">]</span>
<span class="traceContains">+error: main: recipe body must begin with '['</span>
<span class="Delimiter">:(scenario recipe_handles_missing_bracket_2)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main
local-scope
<span class="Delimiter">{</span>
<span class="Delimiter">}</span>
<span class="Error">]</span>
<span class="Comment"># doesn't overflow line when reading header</span>
<span class="traceAbsent">-parse: header ingredient: local-scope</span>
<span class="traceContains">+error: main: recipe body must begin with '['</span>
<span class="Delimiter">:(scenario recipe_handles_missing_bracket_3)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main <span class="Comment"># comment</span>
local-scope
<span class="Delimiter">{</span>
<span class="Delimiter">}</span>
<span class="Error">]</span>
<span class="Comment"># doesn't overflow line when reading header</span>
<span class="traceAbsent">-parse: header ingredient: local-scope</span>
<span class="traceContains">+error: main: recipe body must begin with '['</span>
<span class="Delimiter">:(after "Begin debug_string(recipe x)")</span>
out << <span class="Constant">"ingredients:</span><span class="cSpecial">\n</span><span class="Constant">"</span><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>x<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
out << <span class="Constant">" "</span> << debug_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
out << <span class="Constant">"products:</span><span class="cSpecial">\n</span><span class="Constant">"</span><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>x<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
out << <span class="Constant">" "</span> << debug_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Comment">//: If a recipe never mentions any ingredients or products, assume it has a header.</span>
<span class="Delimiter">:(scenario recipe_without_ingredients_or_products_has_header)</span>
<span class="muRecipe">def</span> test [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+parse: recipe test has a header</span>
<span class="Delimiter">:(before "End Recipe Body(result)")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!result<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Delimiter">{</span>
result<span class="Delimiter">.</span>has_header = <span class="Constant">true</span><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>result<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& inst = result<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>inst<span class="Delimiter">.</span>name == <span class="Constant">"reply"</span> && !inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
|| <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"return"</span> && !inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
|| inst<span class="Delimiter">.</span>name == <span class="Constant">"next-ingredient"</span>
|| inst<span class="Delimiter">.</span>name == <span class="Constant">"ingredient"</span>
|| inst<span class="Delimiter">.</span>name == <span class="Constant">"rewind-ingredients"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
result<span class="Delimiter">.</span>has_header = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>has_header<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">"parse"</span><span class="Delimiter">)</span> << <span class="Constant">"recipe "</span> << result<span class="Delimiter">.</span>name << <span class="Constant">" has a header"</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Support type abbreviations in headers.</span>
<span class="Delimiter">:(scenario type_abbreviations_in_recipe_headers)</span>
<span class="muRecipe">def</span> main [
local-scope
<span class="Normal">a</span>:text<span class="Special"> <- </span>foo
<span class="Constant">1</span>:<span class="Normal">char</span>/<span class="Special">raw <- </span>index *a<span class="Delimiter">,</span> <span class="Constant">0</span>
]
<span class="muRecipe">def</span> foo<span class="muRecipe"> -> </span>a:text [ <span class="Comment"># 'text' is an abbreviation</span>
local-scope
load-ingredients
a<span class="Special"> <- </span><span class="Normal">new</span> [abc]
]
<span class="traceContains">+mem: storing 97 in location 1</span>
<span class="Delimiter">:(before "End Expand Type Abbreviations(caller)")</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
expand_type_abbreviations<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">);</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
expand_type_abbreviations<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">);</span>
<span class="Comment">//: Rewrite 'load-ingredients' to instructions to create all reagents in the header.</span>
<span class="Delimiter">:(before "End Rewrite Instruction(curr, recipe result)")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"load-ingredients"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
recipe_ordinal op = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"next-ingredient-without-typechecking"</span><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>result<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
curr<span class="Delimiter">.</span>operation = op<span class="Delimiter">;</span>
curr<span class="Delimiter">.</span>name = <span class="Constant">"next-ingredient-without-typechecking"</span><span class="Delimiter">;</span>
curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<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>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"next-ingredient-without-typechecking"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>result<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"never call 'next-ingredient-without-typechecking' directly</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Comment">//: internal version of next-ingredient; don't call this directly</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
NEXT_INGREDIENT_WITHOUT_TYPECHECKING<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">"next-ingredient-without-typechecking"</span><span class="Delimiter">,</span> NEXT_INGREDIENT_WITHOUT_TYPECHECKING<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> NEXT_INGREDIENT_WITHOUT_TYPECHECKING: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> NEXT_INGREDIENT_WITHOUT_TYPECHECKING: <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>next_ingredient_to_process < SIZE<span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>ingredient_atoms<span class="Delimiter">))</span> <span class="Delimiter">{</span>
products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>
current_call<span class="Delimiter">().</span>ingredient_atoms<span class="Delimiter">.</span>at<span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>next_ingredient_to_process<span class="Delimiter">));</span>
assert<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>products<span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">);</span> products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">);</span> <span class="Comment">// push a new vector</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
++current_call<span class="Delimiter">().</span>next_ingredient_to_process<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">);</span>
<span class="Comment">// pad the first product with sufficient zeros to match its type</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>resize<span class="Delimiter">(</span>size_of<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: Check all calls against headers.</span>
<span class="Delimiter">:(scenario show_clear_error_on_bad_call)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>foo <span class="Constant">34</span>
]
<span class="muRecipe">def</span> foo x:point<span class="muRecipe"> -> </span>y:num [
local-scope
load-ingredients
<span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="traceContains">+error: main: ingredient 0 has the wrong type at '1:num <- foo 34'</span>
<span class="Delimiter">:(scenario show_clear_error_on_bad_call_2)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:point<span class="Special"> <- </span>foo <span class="Constant">34</span>
]
<span class="muRecipe">def</span> foo x:num<span class="muRecipe"> -> </span>y:num [
local-scope
load-ingredients
<span class="Identifier">return</span> x
]
<span class="traceContains">+error: main: product 0 has the wrong type at '1:point <- foo 34'</span>
<span class="Delimiter">:(after "Transform.push_back(check_instruction)")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_calls_against_header<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_calls_against_header<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> recipe& caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- type-check calls inside recipe "</span> << caller<span class="Delimiter">.</span>name << end<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>caller<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& inst = caller<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>inst<span class="Delimiter">.</span>operation < MAX_PRIMITIVE_RECIPES<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">const</span> recipe& callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!callee<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < min<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">),</span> SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">));</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// ingredients coerced from call to callee</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!types_coercible<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"ingredient "</span> << i << <span class="Constant">" has the wrong type at '"</span> << inst<span class="Delimiter">.</span>original_string << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < min<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">),</span> SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>products<span class="Delimiter">));</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_dummy<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Comment">// products coerced from callee to call</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!types_coercible<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> callee<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"product "</span> << i << <span class="Constant">" has the wrong type at '"</span> << inst<span class="Delimiter">.</span>original_string << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: Check types going in and out of all recipes with headers.</span>
<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario recipe_headers_are_checked)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>z:num [
local-scope
load-ingredients
<span class="Normal">z</span>:&:num<span class="Special"> <- </span>copy <span class="Constant">0</span>/unsafe
<span class="Identifier">return</span> z
]
<span class="traceContains">+error: add2: replied with the wrong type at 'return z'</span>
<span class="Delimiter">:(before "End Checks")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_reply_instructions_against_header<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_reply_instructions_against_header<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> recipe& caller_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!caller_recipe<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- checking reply instructions against header for "</span> << caller_recipe<span class="Delimiter">.</span>name << end<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>caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& inst = caller_recipe<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>inst<span class="Delimiter">.</span>name != <span class="Constant">"reply"</span> && inst<span class="Delimiter">.</span>name != <span class="Constant">"return"</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">)</span> != SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"replied with the wrong number of products at '"</span> << inst<span class="Delimiter">.</span>original_string << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<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>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!types_match<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span>
raise << maybe<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"replied with the wrong type at '"</span> << inst<span class="Delimiter">.</span>original_string << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario recipe_headers_are_checked_2)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num [
local-scope
load-ingredients
<span class="Normal">z</span>:&:num<span class="Special"> <- </span>copy <span class="Constant">0</span>/unsafe
<span class="Identifier">return</span> z
]
<span class="traceContains">+error: add2: replied with the wrong number of products at 'return z'</span>
<span class="Delimiter">:(scenario recipe_headers_check_for_duplicate_names)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> x:num<span class="muRecipe"> -> </span>z:num [
local-scope
load-ingredients
<span class="Identifier">return</span> z
]
<span class="traceContains">+error: add2: 'x' can't repeat in the ingredients</span>
<span class="Delimiter">:(before "End recipe Fields")</span>
map<string<span class="Delimiter">,</span> <span class="Normal">int</span>> ingredient_index<span class="Delimiter">;</span>
<span class="Delimiter">:(after "Begin Instruction Modifying Transforms")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_header_ingredients<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_header_ingredients<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
recipe& caller_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- checking reply instructions against header for "</span> << caller_recipe<span class="Delimiter">.</span>name << end<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>caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">))</span>
raise << maybe<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"'"</span> << caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name << <span class="Constant">"' can't repeat in the ingredients</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">,</span> i<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Deduce types from the header if possible.</span>
<span class="Delimiter">:(scenarios run)</span>
<span class="Delimiter">:(scenario deduce_instruction_types_from_recipe_header)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>z:num [
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> z
]
<span class="traceContains">+mem: storing 8 in location 1</span>
<span class="Delimiter">:(after "Begin Type Modifying Transforms")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>deduce_types_from_header<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> deduce_types_from_header<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
recipe& caller_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- deduce types from header for "</span> << caller_recipe<span class="Delimiter">.</span>name << end<span class="Delimiter">();</span>
map<string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*> header_type<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>caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// error handled elsewhere</span>
put<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"type of "</span> << caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name << <span class="Constant">" is "</span> << names_to_string<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> << end<span class="Delimiter">();</span>
<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>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// error handled elsewhere</span>
put<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"type of "</span> << caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name << <span class="Constant">" is "</span> << names_to_string<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> << end<span class="Delimiter">();</span>
<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>caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
instruction& inst = caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"instruction: "</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << end<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>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>header_type<span class="Delimiter">.</span>find<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">)</span> == header_type<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// error handled elsewhere</span>
inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*get<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">));</span>
trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"type of "</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name << <span class="Constant">" is "</span> << names_to_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> << end<span class="Delimiter">();</span>
<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>inst<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">" product: "</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> << end<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>header_type<span class="Delimiter">.</span>find<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">)</span> == header_type<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// error handled elsewhere</span>
inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*get<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">));</span>
trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"type of "</span> << inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name << <span class="Constant">" is "</span> << names_to_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<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 return_based_on_header)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>z:num [
local-scope
load-ingredients
z<span class="Special"> <- </span>add x<span class="Delimiter">,</span> y
<span class="Identifier">return</span>
]
<span class="traceContains">+mem: storing 8 in location 1</span>
<span class="Delimiter">:(after "Transform.push_back(check_header_ingredients)")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>fill_in_reply_ingredients<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> fill_in_reply_ingredients<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
recipe& caller_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!caller_recipe<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- fill in reply ingredients from header for recipe "</span> << caller_recipe<span class="Delimiter">.</span>name << end<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>caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
instruction& inst = caller_recipe<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>inst<span class="Delimiter">.</span>name == <span class="Constant">"reply"</span> || inst<span class="Delimiter">.</span>name == <span class="Constant">"return"</span><span class="Delimiter">)</span>
add_header_products<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">// fall through reply</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Comment">// error will be raised elsewhere if there's a product in the header; just give up</span>
<span class="Normal">const</span> instruction& final_instruction = caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">)</span>-<span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>final_instruction<span class="Delimiter">.</span>name != <span class="Constant">"reply"</span> && final_instruction<span class="Delimiter">.</span>name != <span class="Constant">"return"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
instruction inst<span class="Delimiter">;</span>
inst<span class="Delimiter">.</span>name = <span class="Constant">"reply"</span><span class="Delimiter">;</span>
add_header_products<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">);</span>
caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>inst<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> add_header_products<span class="Delimiter">(</span>instruction& inst<span class="Delimiter">,</span> <span class="Normal">const</span> recipe& caller_recipe<span class="Delimiter">)</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"reply"</span> || inst<span class="Delimiter">.</span>name == <span class="Constant">"return"</span><span class="Delimiter">);</span>
<span class="Comment">// collect any products with the same names as ingredients</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>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// if the ingredient is missing, add it from the header</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> == i<span class="Delimiter">)</span>
inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
<span class="Comment">// if it's missing /same_as_ingredient, try to fill it in</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">)</span> && !has_property<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> <span class="Constant">"same_as_ingredient"</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
ostringstream same_as_ingredient<span class="Delimiter">;</span>
same_as_ingredient << get<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">);</span>
inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>properties<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair<string<span class="Delimiter">,</span> string_tree*><span class="Delimiter">(</span><span class="Constant">"same-as-ingredient"</span><span class="Delimiter">,</span> <span class="Normal">new</span> string_tree<span class="Delimiter">(</span>same_as_ingredient<span class="Delimiter">.</span>str<span class="Delimiter">())));</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario explicit_reply_ignores_header)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">2</span>:num/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="muRecipe">def</span> add2 a:num<span class="Delimiter">,</span> b:num<span class="muRecipe"> -> </span>y:num<span class="Delimiter">,</span> z:num [
local-scope
load-ingredients
y<span class="Special"> <- </span>add a<span class="Delimiter">,</span> b
z<span class="Special"> <- </span>subtract a<span class="Delimiter">,</span> b
<span class="Identifier">return</span> a<span class="Delimiter">,</span> z
]
<span class="traceContains">+mem: storing 3 in location 1</span>
<span class="traceContains">+mem: storing -2 in location 2</span>
<span class="Delimiter">:(scenario return_on_fallthrough_based_on_header)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>z:num [
local-scope
load-ingredients
z<span class="Special"> <- </span>add x<span class="Delimiter">,</span> y
]
<span class="traceContains">+transform: instruction: reply {z: "number"}</span>
<span class="traceContains">+mem: storing 8 in location 1</span>
<span class="Delimiter">:(scenario return_on_fallthrough_already_exists)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>z:num [
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> z
]
<span class="traceContains">+transform: instruction: return {z: ()}</span>
<span class="traceAbsent">-transform: instruction: reply z:num</span>
<span class="traceContains">+mem: storing 8 in location 1</span>
<span class="Delimiter">:(scenario return_after_conditional_reply_based_on_header)</span>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>z:num [
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>
<span class="muRecipe">def</span> main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">34</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">34</span>
<span class="Constant">3</span>:num<span class="Special"> <- </span>add2 <span class="Constant">1</span>:num<span class="Delimiter">,</span> <span class="Constant">2</span>:num
]
<span class="muRecipe">def</span> add2 x:num<span class="Delimiter">,</span> y:num<span class="muRecipe"> -> </span>x:num [
local-scope
load-ingredients
]
<span class="traceContains">+error: main: '3:num <- add2 1:num, 2:num' should write to '1:num' rather than '3:num'</span>
<span class="Delimiter">:(before "End Includes")</span>
<span class="Normal">using</span> std::min<span class="Delimiter">;</span>
<span class="Normal">using</span> std::max<span class="Delimiter">;</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->