1 //: Construct types out of their constituent fields.
  2 
  3 :(scenario merge)
  4 container foo [
  5   x:num
  6   y:num
  7 ]
  8 def main [
  9   1:foo <- merge 3, 4
 10 ]
 11 +mem: storing 3 in location 1
 12 +mem: storing 4 in location 2
 13 
 14 :(before "End Primitive Recipe Declarations")
 15 MERGE,
 16 :(before "End Primitive Recipe Numbers")
 17 put(Recipe_ordinal, "merge", MERGE);
 18 :(before "End Primitive Recipe Checks")
 19 case MERGE: {
 20   // type-checking in a separate transform below
 21   break;
 22 }
 23 :(before "End Primitive Recipe Implementations")
 24 case MERGE: {
 25   products.resize(1);
 26   for (int i = 0;  i < SIZE(ingredients);  ++i)
 27     for (int j = 0;  j < SIZE(ingredients.at(i));  ++j)
 28       products.at(0).push_back(ingredients.at(i).at(j));
 29   break;
 30 }
 31 
 32 //: type-check 'merge' to avoid interpreting numbers as addresses
 33 
 34 :(scenario merge_check)
 35 def main [
 36   1:point <- merge 3, 4
 37 ]
 38 $error: 0
 39 
 40 :(scenario merge_check_missing_element)
 41 % Hide_errors = true;
 42 def main [
 43   1:point <- merge 3
 44 ]
 45 +error: main: too few ingredients in '1:point <- merge 3'
 46 
 47 :(scenario merge_check_extra_element)
 48 % Hide_errors = true;
 49 def main [
 50   1:point <- merge 3, 4, 5
 51 ]
 52 +error: main: too many ingredients in '1:point <- merge 3, 4, 5'
 53 
 54 //: We want to avoid causing memory corruption, but other than that we want to
 55 //: be flexible in how we construct containers of containers. It should be
 56 //: equally easy to define a container out of primitives or intermediate
 57 //: container fields.
 58 
 59 :(scenario merge_check_recursive_containers)
 60 def main [
 61   1:point <- merge 3, 4
 62   1:point-number <- merge 1:point, 5
 63 ]
 64 $error: 0
 65 
 66 :(scenario merge_check_recursive_containers_2)
 67 % Hide_errors = true;
 68 def main [
 69   1:point <- merge 3, 4
 70   2:point-number <- merge 1:point
 71 ]
 72 +error: main: too few ingredients in '2:point-number <- merge 1:point'
 73 
 74 :(scenario merge_check_recursive_containers_3)
 75 def main [
 76   1:point-number <- merge 3, 4, 5
 77 ]
 78 $error: 0
 79 
 80 :(scenario merge_check_recursive_containers_4)
 81 % Hide_errors = true;
 82 def main [
 83   1:point-number <- merge 3, 4
 84 ]
 85 +error: main: too few ingredients in '1:point-number <- merge 3, 4'
 86 
 87 :(scenario merge_check_reflexive)
 88 % Hide_errors = true;
 89 def main [
 90   1:point <- merge 3, 4
 91   2:point <- merge 1:point
 92 ]
 93 $error: 0
 94 
 95 //: Since a container can be merged in several ways, we need to be able to
 96 //: backtrack through different possibilities. Later we'll allow creating
 97 //: exclusive containers which contain just one of rather than all of their
 98 //: elements. That will also require backtracking capabilities. Here's the
 99 //: state we need to maintain for backtracking:
100 
101 :(before "End Types")
102 struct merge_check_point {
103   reagent container;
104   int container_element_index;
105   merge_check_point(const reagent& c, int i) :container(c), container_element_index(i) {}
106 };
107 
108 struct merge_check_state {
109   stack<merge_check_point> data;
110 };
111 
112 :(before "End Checks")
113 Transform.push_back(check_merge_calls);  // idempotent
114 :(code)
115 void check_merge_calls(const recipe_ordinal r) {
116   const recipe& caller = get(Recipe, r);
117   trace(9991, "transform") << "--- type-check merge instructions in recipe " << caller.name << end();
118   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
119     const instruction& inst = caller.steps.at(i);
120     if (inst.name != "merge") continue;
121     if (SIZE(inst.products) != 1) {
122       raise << maybe(caller.name) << "'merge' should yield a single product in '" << inst.original_string << "'\n" << end();
123       continue;
124     }
125     reagent/*copy*/ product = inst.products.at(0);
126     // Update product While Type-checking Merge
127     const type_tree* product_base_type = product.type->atom ? product.type : product.type->left;
128     assert(product_base_type->atom);
129     if (product_base_type->value == 0 || !contains_key(Type, product_base_type->value)) {
130       raise << maybe(caller.name) << "'merge' should yield a container in '" << inst.original_string << "'\n" << end();
131       continue;
132     }
133     const type_info& info = get(Type, product_base_type->value);
134     if (info.kind != CONTAINER && info.kind != EXCLUSIVE_CONTAINER) {
135       raise << maybe(caller.name) << "'merge' should yield a container in '" << inst.original_string << "'\n" << end();
136       continue;
137     }
138     check_merge_call(inst.ingredients, product, caller, inst);
139   }
140 }
141 
142 void check_merge_call(const vector<reagent>& ingredients, const reagent& product, const recipe& caller, const instruction& inst) {
143   int ingredient_index = 0;
144   merge_check_state state;
145   state.data.push(merge_check_point(product, 0));
146   while (true) {
147     assert(!state.data.empty());
148     trace(9999, "transform") << ingredient_index << " vs " << SIZE(ingredients) << end();
149     if (ingredient_index >= SIZE(ingredientspre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<!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 - 311decimal-int.subx</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-dark">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #a8a8a8; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #a8a8a8; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.SpecialChar { color: #d70000; }
.subxComment { color: #005faf; }
.subxS1Comment { color: #0000af; }
.LineNr { }
.subxH1Comment { color: #005faf; text-decoration: underline; }
.subxMinorFunction { color: #875f5f; }
.subxTest { color: #5f8700; }
.subxFunction { color: #af5f00; text-decoration: underline; }
.Constant { color: #008787; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  var lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'