From 805d58c6aeeeba3e4989c0eed6781b3861e8fae0 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 25 Jan 2018 22:39:31 -0800 Subject: 4199 --- html/072recipe.cc.html | 760 ++++++++++++++++++++++++------------------------- 1 file changed, 378 insertions(+), 382 deletions(-) (limited to 'html/072recipe.cc.html') diff --git a/html/072recipe.cc.html b/html/072recipe.cc.html index aa256245..30bfd731 100644 --- a/html/072recipe.cc.html +++ b/html/072recipe.cc.html @@ -15,19 +15,18 @@ body { font-size: 12pt; font-family: monospace; color: #aaaaaa; background-color a { color:#eeeeee; text-decoration: none; } a:hover { text-decoration: underline; } * { font-size: 12pt; font-size: 1em; } -.Conceal { color: #4e4e4e; } -.traceContains { color: #008000; } -.LineNr { color: #444444; } .SalientComment { color: #00ffff; } -.Identifier { color: #c0a020; } +.LineNr { color: #444444; } .Constant { color: #00a0a0; } +.muRecipe { color: #ff8700; } +.Delimiter { color: #800080; } +.Special { color: #c00000; } +.Identifier { color: #c0a020; } .Normal { color: #aaaaaa; background-color: #080808; padding-bottom: 1px; } .Comment { color: #9090ff; } .Comment a { color:#0000ee; text-decoration:underline; } -.Delimiter { color: #800080; } -.Special { color: #c00000; } .cSpecial { color: #008000; } -.muRecipe { color: #ff8700; } +.traceContains { color: #008000; } --> @@ -101,11 +100,11 @@ if ('onhashchange' in window) { 37 } 38 bool contains_reagent_with_non_recipe_literal_type(const recipe& caller, const string& name) { 39 for (int i = 0; i < SIZE(caller.steps); ++i) { - 40 ¦ const instruction& inst = caller.steps.at(i); - 41 ¦ for (int i = 0; i < SIZE(inst.ingredients); ++i) - 42 ¦ ¦ if (is_matching_non_recipe_literal(inst.ingredients.at(i), name)) return true; - 43 ¦ for (int i = 0; i < SIZE(inst.products); ++i) - 44 ¦ ¦ if (is_matching_non_recipe_literal(inst.products.at(i), name)) return true; + 40 const instruction& inst = caller.steps.at(i); + 41 for (int i = 0; i < SIZE(inst.ingredients); ++i) + 42 if (is_matching_non_recipe_literal(inst.ingredients.at(i), name)) return true; + 43 for (int i = 0; i < SIZE(inst.products); ++i) + 44 if (is_matching_non_recipe_literal(inst.products.at(i), name)) return true; 45 } 46 return false; 47 } @@ -133,12 +132,12 @@ if ('onhashchange' in window) { 69 :(before "End Primitive Recipe Checks") 70 case CALL: { 71 if (inst.ingredients.empty()) { - 72 ¦ raise << maybe(get(Recipe, r).name) << "'call' requires at least one ingredient (the recipe to call)\n" << end(); - 73 ¦ break; + 72 raise << maybe(get(Recipe, r).name) << "'call' requires at least one ingredient (the recipe to call)\n" << end(); + 73 break; 74 } 75 if (!is_mu_recipe(inst.ingredients.at(0))) { - 76 ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'call' should be a recipe, but got '" << inst.ingredients.at(0).original_string << "'\n" << end(); - 77 ¦ break; + 76 raise << maybe(get(Recipe, r).name) << "first ingredient of 'call' should be a recipe, but got '" << inst.ingredients.at(0).original_string << "'\n" << end(); + 77 break; 78 } 79 break; 80 } @@ -146,13 +145,13 @@ if ('onhashchange' in window) { 82 case CALL: { 83 // Begin Call 84 if (Trace_stream) { - 85 ¦ ++Trace_stream->callstack_depth; - 86 ¦ trace("trace") << "indirect 'call': incrementing callstack depth to " << Trace_stream->callstack_depth << end(); - 87 ¦ assert(Trace_stream->callstack_depth < 9000); // 9998-101 plus cushion + 85 ++Trace_stream->callstack_depth; + 86 trace("trace") << "indirect 'call': incrementing callstack depth to " << Trace_stream->callstack_depth << end(); + 87 assert(Trace_stream->callstack_depth < 9000); // 9998-101 plus cushion 88 } 89 if (!ingredients.at(0).at(0)) { - 90 ¦ raise << maybe(current_recipe_name()) << "tried to call empty recipe in '" << to_string(current_instruction()) << "'" << end(); - 91 ¦ break; + 90 raise << maybe(current_recipe_name()) << "tried to call empty recipe in '" << to_string(current_instruction()) << "'" << end(); + 91 break; 92 } 93 const call& caller_frame = current_call(); 94 instruction/*copy*/ call_instruction = to_instruction(caller_frame); @@ -161,369 +160,366 @@ if ('onhashchange' in window) { 97 Current_routine->calls.push_front(call(ingredients.at(0).at(0))); 98 ingredients.erase(ingredients.begin()); // drop the callee 99 finish_call_housekeeping(call_instruction, ingredients); -100 Num_refcount_updates[caller_frame.running_recipe][caller_frame.running_step_index] -101 ¦ ¦ += (Total_refcount_updates - initial_num_refcount_updates); -102 initial_num_refcount_updates = Total_refcount_updates; -103 // not done with caller -104 write_products = false; -105 fall_through_to_next_instruction = false; -106 break; -107 } -108 -109 :(scenario call_variable) -110 def main [ -111 {1: (recipe number -> number)} <- copy f -112 2:num <- call {1: (recipe number -> number)}, 34 -113 ] -114 def f x:num -> y:num [ -115 local-scope -116 load-ingredients -117 y <- copy x -118 ] -119 +mem: storing 34 in location 2 -120 -121 :(scenario call_literal_recipe_repeatedly) -122 def main [ -123 1:num <- call f, 34 -124 1:num <- call f, 35 -125 ] -126 def f x:num -> y:num [ -127 local-scope -128 load-ingredients -129 y <- copy x -130 ] -131 +mem: storing 34 in location 1 -132 +mem: storing 35 in location 1 -133 -134 :(scenario call_shape_shifting_recipe) -135 def main [ -136 1:num <- call f, 34 -137 ] -138 def f x:_elem -> y:_elem [ -139 local-scope -140 load-ingredients -141 y <- copy x -142 ] -143 +mem: storing 34 in location 1 -144 -145 :(scenario call_shape_shifting_recipe_inside_shape_shifting_recipe) -146 def main [ -147 1:num <- f 34 -148 ] -149 def f x:_elem -> y:_elem [ -150 local-scope -151 load-ingredients -152 y <- call g x -153 ] -154 def g x:_elem -> y:_elem [ -155 local-scope -156 load-ingredients -157 y <- copy x -158 ] -159 +mem: storing 34 in location 1 -160 -161 :(scenario call_shape_shifting_recipe_repeatedly_inside_shape_shifting_recipe) -162 def main [ -163 1:num <- f 34 -164 ] -165 def f x:_elem -> y:_elem [ -166 local-scope -167 load-ingredients -168 y <- call g x -169 y <- call g x -170 ] -171 def g x:_elem -> y:_elem [ -172 local-scope -173 load-ingredients -174 y <- copy x -175 ] -176 +mem: storing 34 in location 1 -177 -178 //:: check types for 'call' instructions -179 -180 :(scenario call_check_literal_recipe) -181 % Hide_errors = true; -182 def main [ -183 1:num <- call f, 34 -184 ] -185 def f x:point -> y:point [ -186 local-scope -187 load-ingredients -188 y <- copy x -189 ] -190 +error: main: ingredient 0 has the wrong type at '1:num <- call f, 34' -191 +error: main: product 0 has the wrong type at '1:num <- call f, 34' -192 -193 :(scenario call_check_variable_recipe) -194 % Hide_errors = true; -195 def main [ -196 {1: (recipe point -> point)} <- copy f -197 2:num <- call {1: (recipe point -> point)}, 34 -198 ] -199 def f x:point -> y:point [ -200 local-scope -201 load-ingredients -202 y <- copy x -203 ] -204 +error: main: ingredient 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34' -205 +error: main: product 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34' -206 -207 :(before "End resolve_ambiguous_call(r, index, inst, caller_recipe) Special-cases") -208 if (inst.name == "call" && !inst.ingredients.empty() && is_recipe_literal(inst.ingredients.at(0))) { -209 resolve_indirect_ambiguous_call(r, index, inst, caller_recipe); -210 return; -211 } -212 :(code) -213 bool is_recipe_literal(const reagent& x) { -214 return x.type && x.type->atom && x.type->name == "recipe-literal"; -215 } -216 void resolve_indirect_ambiguous_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) { -217 instruction inst2; -218 inst2.name = inst.ingredients.at(0).name; -219 for (int i = /*skip recipe*/1; i < SIZE(inst.ingredients); ++i) -220 ¦ inst2.ingredients.push_back(inst.ingredients.at(i)); -221 for (int i = 0; i < SIZE(inst.products); ++i) -222 ¦ inst2.products.push_back(inst.products.at(i)); -223 resolve_ambiguous_call(r, index, inst2, caller_recipe); -224 inst.ingredients.at(0).name = inst2.name; -225 inst.ingredients.at(0).set_value(get(Recipe_ordinal, inst2.name)); -226 } -227 -228 :(after "Transform.push_back(check_instruction)") -229 Transform.push_back(check_indirect_calls_against_header); // idempotent -230 :(code) -231 void check_indirect_calls_against_header(const recipe_ordinal r) { -232 trace(9991, "transform") << "--- type-check 'call' instructions inside recipe " << get(Recipe, r).name << end(); -233 const recipe& caller = get(Recipe, r); -234 for (int i = 0; i < SIZE(caller.steps); ++i) { -235 ¦ const instruction& inst = caller.steps.at(i); -236 ¦ if (!is_indirect_call(inst.operation)) continue; -237 ¦ if (inst.ingredients.empty()) continue; // error raised above -238 ¦ const reagent& callee = inst.ingredients.at(0); -239 ¦ if (!is_mu_recipe(callee)) continue; // error raised above -240 ¦ const recipe callee_header = is_literal(callee) ? get(Recipe, callee.value) : from_reagent(inst.ingredients.at(0)); -241 ¦ if (!callee_header.has_header) continue; -242 ¦ if (is_indirect_call_with_ingredients(inst.operation)) { -243 ¦ ¦ for (long int i = /*skip callee*/1; i < min(SIZE(inst.ingredients), SIZE(callee_header.ingredients)+/*skip callee*/1); ++i) { -244 ¦ ¦ ¦ if (!types_coercible(callee_header.ingredients.at(i-/*skip callee*/1), inst.ingredients.at(i))) -245 ¦ ¦ ¦ ¦ raise << maybe(caller.name) << "ingredient " << i-/*skip callee*/1 << " has the wrong type at '" << to_original_string(inst) << "'\n" << end(); -246 ¦ ¦ } -247 ¦ } -248 ¦ if (is_indirect_call_with_products(inst.operation)) { -249 ¦ ¦ for (long int i = 0; i < min(SIZE(inst.products), SIZE(callee_header.products)); ++i) { -250 ¦ ¦ ¦ if (is_dummy(inst.products.at(i))) continue; -251 ¦ ¦ ¦ if (!types_coercible(callee_header.products.at(i), inst.products.at(i))) -252 ¦ ¦ ¦ ¦ raise << maybe(caller.name) << "product " << i << " has the wrong type at '" << to_original_string(inst) << "'\n" << end(); -253 ¦ ¦ } -254 ¦ } -255 } -256 } -257 -258 bool is_indirect_call(const recipe_ordinal r) { -259 return is_indirect_call_with_ingredients(r) || is_indirect_call_with_products(r); -260 } -261 -262 bool is_indirect_call_with_ingredients(const recipe_ordinal r) { -263 if (r == CALL) return true; -264 // End is_indirect_call_with_ingredients Special-cases -265 return false; -266 } -267 bool is_indirect_call_with_products(const recipe_ordinal r) { -268 if (r == CALL) return true; -269 // End is_indirect_call_with_products Special-cases -270 return false; -271 } -272 -273 recipe from_reagent(const reagent& r) { -274 assert(r.type); -275 recipe result_header; // will contain only ingredients and products, nothing else -276 result_header.has_header = true; -277 // Begin Reagent->Recipe(r, recipe_header) -278 if (r.type->atom) { -279 ¦ assert(r.type->name == "recipe"); -280 ¦ return result_header; -281 } -282 const type_tree* root_type = r.type->atom ? r.type : r.type->left; -283 assert(root_type->atom); -284 assert(root_type->name == "recipe"); -285 const type_tree* curr = r.type->right; -286 for (/*nada*/; curr && !curr->atom; curr = curr->right) { -287 ¦ if (curr->left->atom && curr->left->name == "->") { -288 ¦ ¦ curr = curr->right; // skip delimiter -289 ¦ ¦ goto read_products; -290 ¦ } -291 ¦ result_header.ingredients.push_back(next_recipe_reagent(curr->left)); -292 } -293 if (curr) { -294 ¦ assert(curr->atom); -295 ¦ result_header.ingredients.push_back(next_recipe_reagent(curr)); -296 ¦ return result_header; // no products -297 } -298 read_products: -299 for (/*nada*/; curr && !curr->atom; curr = curr->right) -300 ¦ result_header.products.push_back(next_recipe_reagent(curr->left)); -301 if (curr) { -302 ¦ assert(curr->atom); -303 ¦ result_header.products.push_back(next_recipe_reagent(curr)); -304 } -305 return result_header; -306 } -307 -308 :(before "End Unit Tests") -309 void test_from_reagent_atomic() { -310 reagent a("{f: recipe}"); -311 recipe r_header = from_reagent(a); -312 CHECK(r_header.ingredients.empty()); -313 CHECK(r_header.products.empty()); -314 } -315 void test_from_reagent_non_atomic() { -316 reagent a("{f: (recipe number -> number)}"); -317 recipe r_header = from_reagent(a); -318 CHECK_EQ(SIZE(r_header.ingredients), 1); -319 CHECK_EQ(SIZE(r_header.products), 1); -320 } -321 void test_from_reagent_reads_ingredient_at_end() { -322 reagent a("{f: (recipe number number)}"); -323 recipe r_header = from_reagent(a); -324 CHECK_EQ(SIZE(r_header.ingredients), 2); -325 CHECK(r_header.products.empty()); -326 } -327 void test_from_reagent_reads_sole_ingredient_at_end() { -328 reagent a("{f: (recipe number)}"); -329 recipe r_header = from_reagent(a); -330 CHECK_EQ(SIZE(r_header.ingredients), 1); -331 CHECK(r_header.products.empty()); -332 } -333 -334 :(code) -335 reagent next_recipe_reagent(const type_tree* curr) { -336 if (!curr->left) return reagent("recipe:"+curr->name); -337 reagent result; -338 result.name = "recipe"; -339 result.type = new type_tree(*curr); -340 return result; -341 } -342 -343 bool is_mu_recipe(const reagent& r) { -344 if (!r.type) return false; -345 if (r.type->atom) { -346 ¦ // End is_mu_recipe Atom Cases(r) -347 ¦ return r.type->name == "recipe-literal"; -348 } -349 return r.type->left->atom && r.type->left->name == "recipe"; -350 } -351 -352 :(scenario copy_typecheck_recipe_variable) -353 % Hide_errors = true; -354 def main [ -355 3:num <- copy 34 # abc def -356 {1: (recipe number -> number)} <- copy f # store literal in a matching variable -357 {2: (recipe boolean -> boolean)} <- copy {1: (recipe number -> number)} # mismatch between recipe variables -358 ] -359 def f x:num -> y:num [ -360 local-scope -361 load-ingredients -362 y <- copy x -363 ] -364 +error: main: can't copy '{1: (recipe number -> number)}' to '{2: (recipe boolean -> boolean)}'; types don't match -365 -366 :(scenario copy_typecheck_recipe_variable_2) -367 % Hide_errors = true; -368 def main [ -369 {1: (recipe number -> number)} <- copy f # mismatch with a recipe literal -370 ] -371 def f x:bool -> y:bool [ -372 local-scope -373 load-ingredients -374 y <- copy x -375 ] -376 +error: main: can't copy 'f' to '{1: (recipe number -> number)}'; types don't match -377 -378 :(before "End Matching Types For Literal(to)") -379 if (is_mu_recipe(to)) { -380 if (!contains_key(Recipe, from.value)) { -381 ¦ raise << "trying to store recipe " << from.name << " into " << to_string(to) << " but there's no such recipe\n" << end(); -382 ¦ return false; -383 } -384 const recipe& rrhs = get(Recipe, from.value); -385 const recipe& rlhs = from_reagent(to); -386 for (long int i = 0; i < min(SIZE(rlhs.ingredients), SIZE(rrhs.ingredients)); ++i) { -387 ¦ if (!types_match(rlhs.ingredients.at(i), rrhs.ingredients.at(i))) -388 ¦ ¦ return false; -389 } -390 for (long int i = 0; i < min(SIZE(rlhs.products), SIZE(rrhs.products)); ++i) { -391 ¦ if (!types_match(rlhs.products.at(i), rrhs.products.at(i))) -392 ¦ ¦ return false; -393 } -394 return true; -395 } -396 -397 :(scenario call_variable_compound_ingredient) -398 def main [ -399 {1: (recipe (address number) -> number)} <- copy f -400 2:&:num <- copy 0 -401 3:num <- call {1: (recipe (address number) -> number)}, 2:&:num -402 ] -403 def f x:&:num -> y:num [ -404 local-scope -405 load-ingredients -406 y <- copy x -407 ] -408 $error: 0 -409 -410 //: make sure we don't accidentally break on a recipe literal -411 :(scenario jump_forbidden_on_recipe_literals) -412 % Hide_errors = true; -413 def foo [ +100 // not done with caller +101 write_products = false; +102 fall_through_to_next_instruction = false; +103 break; +104 } +105 +106 :(scenario call_variable) +107 def main [ +108 {1: (recipe number -> number)} <- copy f +109 2:num <- call {1: (recipe number -> number)}, 34 +110 ] +111 def f x:num -> y:num [ +112 local-scope +113 load-ingredients +114 y <- copy x +115 ] +116 +mem: storing 34 in location 2 +117 +118 :(scenario call_literal_recipe_repeatedly) +119 def main [ +120 1:num <- call f, 34 +121 1:num <- call f, 35 +122 ] +123 def f x:num -> y:num [ +124 local-scope +125 load-ingredients +126 y <- copy x +127 ] +128 +mem: storing 34 in location 1 +129 +mem: storing 35 in location 1 +130 +131 :(scenario call_shape_shifting_recipe) +132 def main [ +133 1:num <- call f, 34 +134 ] +135 def f x:_elem -> y:_elem [ +136 local-scope +137 load-ingredients +138 y <- copy x +139 ] +140 +mem: storing 34 in location 1 +141 +142 :(scenario call_shape_shifting_recipe_inside_shape_shifting_recipe) +143 def main [ +144 1:num <- f 34 +145 ] +146 def f x:_elem -> y:_elem [ +147 local-scope +148 load-ingredients +149 y <- call g x +150 ] +151 def g x:_elem -> y:_elem [ +152 local-scope +153 load-ingredients +154 y <- copy x +155 ] +156 +mem: storing 34 in location 1 +157 +158 :(scenario call_shape_shifting_recipe_repeatedly_inside_shape_shifting_recipe) +159 def main [ +160 1:num <- f 34 +161 ] +162 def f x:_elem -> y:_elem [ +163 local-scope +164 load-ingredients +165 y <- call g x +166 y <- call g x +167 ] +168 def g x:_elem -> y:_elem [ +169 local-scope +170 load-ingredients +171 y <- copy x +172 ] +173 +mem: storing 34 in location 1 +174 +175 //:: check types for 'call' instructions +176 +177 :(scenario call_check_literal_recipe) +178 % Hide_errors = true; +179 def main [ +180 1:num <- call f, 34 +181 ] +182 def f x:point -> y:point [ +183 local-scope +184 load-ingredients +185 y <- copy x +186 ] +187 +error: main: ingredient 0 has the wrong type at '1:num <- call f, 34' +188 +error: main: product 0 has the wrong type at '1:num <- call f, 34' +189 +190 :(scenario call_check_variable_recipe) +191 % Hide_errors = true; +192 def main [ +193 {1: (recipe point -> point)} <- copy f +194 2:num <- call {1: (recipe point -> point)}, 34 +195 ] +196 def f x:point -> y:point [ +197 local-scope +198 load-ingredients +199 y <- copy x +200 ] +201 +error: main: ingredient 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34' +202 +error: main: product 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34' +203 +204 :(before "End resolve_ambiguous_call(r, index, inst, caller_recipe) Special-cases") +205 if (inst.name == "call" && !inst.ingredients.empty() && is_recipe_literal(inst.ingredients.at(0))) { +206 resolve_indirect_ambiguous_call(r, index, inst, caller_recipe); +207 return; +208 } +209 :(code) +210 bool is_recipe_literal(const reagent& x) { +211 return x.type && x.type->atom && x.type->name == "recipe-literal"; +212 } +213 void resolve_indirect_ambiguous_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) { +214 instruction inst2; +215 inst2.name = inst.ingredients.at(0).name; +216 for (int i = /*skip recipe*/1; i < SIZE(inst.ingredients); ++i) +217 inst2.ingredients.push_back(inst.ingredients.at(i)); +218 for (int i = 0; i < SIZE(inst.products); ++i) +219 inst2.products.push_back(inst.products.at(i)); +220 resolve_ambiguous_call(r, index, inst2, caller_recipe); +221 inst.ingredients.at(0).name = inst2.name; +222 inst.ingredients.at(0).set_value(get(Recipe_ordinal, inst2.name)); +223 } +224 +225 :(after "Transform.push_back(check_instruction)") +226 Transform.push_back(check_indirect_calls_against_header); // idempotent +227 :(code) +228 void check_indirect_calls_against_header(const recipe_ordinal r) { +229 trace(9991, "transform") << "--- type-check 'call' instructions inside recipe " << get(Recipe, r).name << end(); +230 const recipe& caller = get(Recipe, r); +231 for (int i = 0; i < SIZE(caller.steps); ++i) { +232 const instruction& inst = caller.steps.at(i); +233 if (!is_indirect_call(inst.operation)) continue; +234 if (inst.ingredients.empty()) continue; // error raised above +235 const reagent& callee = inst.ingredients.at(0); +236 if (!is_mu_recipe(callee)) continue; // error raised above +237 const recipe callee_header = is_literal(callee) ? get(Recipe, callee.value) : from_reagent(inst.ingredients.at(0)); +238 if (!callee_header.has_header) continue; +239 if (is_indirect_call_with_ingredients(inst.operation)) { +240 for (long int i = /*skip callee*/1; i < min(SIZE(inst.ingredients), SIZE(callee_header.ingredients)+/*skip callee*/1); ++i) { +241 if (!types_coercible(callee_header.ingredients.at(i-/*skip callee*/1), inst.ingredients.at(i))) +242 raise << maybe(caller.name) << "ingredient " << i-/*skip callee*/1 << " has the wrong type at '" << to_original_string(inst) << "'\n" << end(); +243 } +244 } +245 if (is_indirect_call_with_products(inst.operation)) { +246 for (long int i = 0; i < min(SIZE(inst.products), SIZE(callee_header.products)); ++i) { +247 if (is_dummy(inst.products.at(i))) continue; +248 if (!types_coercible(callee_header.products.at(i), inst.products.at(i))) +249 raise << maybe(caller.name) << "product " << i << " has the wrong type at '" << to_original_string(inst) << "'\n" << end(); +250 } +251 } +252 } +253 } +254 +255 bool is_indirect_call(const recipe_ordinal r) { +256 return is_indirect_call_with_ingredients(r) || is_indirect_call_with_products(r); +257 } +258 +259 bool is_indirect_call_with_ingredients(const recipe_ordinal r) { +260 if (r == CALL) return true; +261 // End is_indirect_call_with_ingredients Special-cases +262 return false; +263 } +264 bool is_indirect_call_with_products(const recipe_ordinal r) { +265 if (r == CALL) return true; +266 // End is_indirect_call_with_products Special-cases +267 return false; +268 } +269 +270 recipe from_reagent(const reagent& r) { +271 assert(r.type); +272 recipe result_header; // will contain only ingredients and products, nothing else +273 result_header.has_header = true; +274 // Begin Reagent->Recipe(r, recipe_header) +275 if (r.type->atom) { +276 assert(r.type->name == "recipe"); +277 return result_header; +278 } +279 const type_tree* root_type = r.type->atom ? r.type : r.type->left; +280 assert(root_type->atom); +281 assert(root_type->name == "recipe"); +282 const type_tree* curr = r.type->right; +283 for (/*nada*/; curr && !curr->atom; curr = curr->right) { +284 if (curr->left->atom && curr->left->name == "->") { +285 curr = curr->right; // skip delimiter +286 goto read_products; +287 } +288 result_header.ingredients.push_back(next_recipe_reagent(curr->left)); +289 } +290 if (curr) { +291 assert(curr->atom); +292 result_header.ingredients.push_back(next_recipe_reagent(curr)); +293 return result_header; // no products +294 } +295 read_products: +296 for (/*nada*/; curr && !curr->atom; curr = curr->right) +297 result_header.products.push_back(next_recipe_reagent(curr->left)); +298 if (curr) { +299 assert(curr->atom); +300 result_header.products.push_back(next_recipe_reagent(curr)); +301 } +302 return result_header; +303 } +304 +305 :(before "End Unit Tests") +306 void test_from_reagent_atomic() { +307 reagent a("{f: recipe}"); +308 recipe r_header = from_reagent(a); +309 CHECK(r_header.ingredients.empty()); +310 CHECK(r_header.products.empty()); +311 } +312 void test_from_reagent_non_atomic() { +313 reagent a("{f: (recipe number -> number)}"); +314 recipe r_header = from_reagent(a); +315 CHECK_EQ(SIZE(r_header.ingredients), 1); +316 CHECK_EQ(SIZE(r_header.products), 1); +317 } +318 void test_from_reagent_reads_ingredient_at_end() { +319 reagent a("{f: (recipe number number)}"); +320 recipe r_header = from_reagent(a); +321 CHECK_EQ(SIZE(r_header.ingredients), 2); +322 CHECK(r_header.products.empty()); +323 } +324 void test_from_reagent_reads_sole_ingredient_at_end() { +325 reagent a("{f: (recipe number)}"); +326 recipe r_header = from_reagent(a); +327 CHECK_EQ(SIZE(r_header.ingredients), 1); +328 CHECK(r_header.products.empty()); +329 } +330 +331 :(code) +332 reagent next_recipe_reagent(const type_tree* curr) { +333 if (!curr->left) return reagent("recipe:"+curr->name); +334 reagent result; +335 result.name = "recipe"; +336 result.type = new type_tree(*curr); +337 return result; +338 } +339 +340 bool is_mu_recipe(const reagent& r) { +341 if (!r.type) return false; +342 if (r.type->atom) { +343 // End is_mu_recipe Atom Cases(r) +344 return r.type->name == "recipe-literal"; +345 } +346 return r.type->left->atom && r.type->left->name == "recipe"; +347 } +348 +349 :(scenario copy_typecheck_recipe_variable) +350 % Hide_errors = true; +351 def main [ +352 3:num <- copy 34 # abc def +353 {1: (recipe number -> number)} <- copy f # store literal in a matching variable +354 {2: (recipe boolean -> boolean)} <- copy {1: (recipe number -> number)} # mismatch between recipe variables +355 ] +356 def f x:num -> y:num [ +357 local-scope +358 load-ingredients +359 y <- copy x +360 ] +361 +error: main: can't copy '{1: (recipe number -> number)}' to '{2: (recipe boolean -> boolean)}'; types don't match +362 +363 :(scenario copy_typecheck_recipe_variable_2) +364 % Hide_errors = true; +365 def main [ +366 {1: (recipe number -> number)} <- copy f # mismatch with a recipe literal +367 ] +368 def f x:bool -> y:bool [ +369 local-scope +370 load-ingredients +371 y <- copy x +372 ] +373 +error: main: can't copy 'f' to '{1: (recipe number -> number)}'; types don't match +374 +375 :(before "End Matching Types For Literal(to)") +376 if (is_mu_recipe(to)) { +377 if (!contains_key(Recipe, from.value)) { +378 raise << "trying to store recipe " << from.name << " into " << to_string(to) << " but there's no such recipe\n" << end(); +379 return false; +380 } +381 const recipe& rrhs = get(Recipe, from.value); +382 const recipe& rlhs = from_reagent(to); +383 for (long int i = 0; i < min(SIZE(rlhs.ingredients), SIZE(rrhs.ingredients)); ++i) { +384 if (!types_match(rlhs.ingredients.at(i), rrhs.ingredients.at(i))) +385 return false; +386 } +387 for (long int i = 0; i < min(SIZE(rlhs.products), SIZE(rrhs.products)); ++i) { +388 if (!types_match(rlhs.products.at(i), rrhs.products.at(i))) +389 return false; +390 } +391 return true; +392 } +393 +394 :(scenario call_variable_compound_ingredient) +395 def main [ +396 {1: (recipe (address number) -> number)} <- copy f +397 2:&:num <- copy 0 +398 3:num <- call {1: (recipe (address number) -> number)}, 2:&:num +399 ] +400 def f x:&:num -> y:num [ +401 local-scope +402 load-ingredients +403 y <- copy x +404 ] +405 $error: 0 +406 +407 //: make sure we don't accidentally break on a recipe literal +408 :(scenario jump_forbidden_on_recipe_literals) +409 % Hide_errors = true; +410 def foo [ +411 local-scope +412 ] +413 def main [ 414 local-scope -415 ] -416 def main [ -417 local-scope -418 { -419 ¦ break-if foo -420 } -421 ] -422 # error should be as if foo is not a recipe -423 +error: main: missing type for 'foo' in 'break-if foo' -424 -425 :(before "End JUMP_IF Checks") -426 check_for_recipe_literals(inst, get(Recipe, r)); -427 :(before "End JUMP_UNLESS Checks") -428 check_for_recipe_literals(inst, get(Recipe, r)); -429 :(code) -430 void check_for_recipe_literals(const instruction& inst, const recipe& caller) { -431 for (int i = 0; i < SIZE(inst.ingredients); ++i) { -432 ¦ if (is_mu_recipe(inst.ingredients.at(i))) { -433 ¦ ¦ raise << maybe(caller.name) << "missing type for '" << inst.ingredients.at(i).original_string << "' in '" << to_original_string(inst) << "'\n" << end(); -434 ¦ ¦ if (is_present_in_ingredients(caller, inst.ingredients.at(i).name)) -435 ¦ ¦ ¦ raise << " did you forget 'load-ingredients'?\n" << end(); -436 ¦ } -437 } -438 } -439 -440 :(scenario load_ingredients_missing_error_3) -441 % Hide_errors = true; -442 def foo {f: (recipe num -> num)} [ -443 local-scope -444 b:num <- call f, 1 -445 ] -446 +error: foo: missing type for 'f' in 'b:num <- call f, 1' -447 +error: did you forget 'load-ingredients'? +415 { +416 break-if foo +417 } +418 ] +419 # error should be as if foo is not a recipe +420 +error: main: missing type for 'foo' in 'break-if foo' +421 +422 :(before "End JUMP_IF Checks") +423 check_for_recipe_literals(inst, get(Recipe, r)); +424 :(before "End JUMP_UNLESS Checks") +425 check_for_recipe_literals(inst, get(Recipe, r)); +426 :(code) +427 void check_for_recipe_literals(const instruction& inst, const recipe& caller) { +428 for (int i = 0; i < SIZE(inst.ingredients); ++i) { +429 if (is_mu_recipe(inst.ingredients.at(i))) { +430 raise << maybe(caller.name) << "missing type for '" << inst.ingredients.at(i).original_string << "' in '" << to_original_string(inst) << "'\n" << end(); +431 if (is_present_in_ingredients(caller, inst.ingredients.at(i).name)) +432 raise << " did you forget 'load-ingredients'?\n" << end(); +433 } +434 } +435 } +436 +437 :(scenario load_ingredients_missing_error_3) +438 % Hide_errors = true; +439 def foo {f: (recipe num -> num)} [ +440 local-scope +441 b:num <- call f, 1 +442 ] +443 +error: foo: missing type for 'f' in 'b:num <- call f, 1' +444 +error: did you forget 'load-ingredients'? +445 +446 :(before "End Mu Types Initialization") +447 put(Type_abbreviations, "function", new_type_tree("recipe")); 448 -449 :(before "End Mu Types Initialization") -450 put(Type_abbreviations, "function", new_type_tree("recipe")); -451 -452 :(scenario call_function) -453 def main [ -454 {1: (function number -> number)} <- copy f -455 2:num <- call {1: (function number -> number)}, 34 -456 ] -457 def f x:num -> y:num [ -458 local-scope -459 load-ingredients -460 y <- copy x -461 ] -462 +mem: storing 34 in location 2 +449 :(scenario call_function) +450 def main [ +451 {1: (function number -> number)} <- copy f +452 2:num <- call {1: (function number -> number)}, 34 +453 ] +454 def f x:num -> y:num [ +455 local-scope +456 load-ingredients +457 y <- copy x +458 ] +459 +mem: storing 34 in location 2 -- cgit 1.4.1-2-gfad0