//: Advanced notation for the common/easy case where a recipe takes some fixed //: number of ingredients and yields some fixed number of products. void test_recipe_with_header() { run( "def main [\n" " 1:num/raw <- add2 3, 5\n" "]\n" "def add2 x:num, y:num -> z:num [\n" " local-scope\n" " load-ingredients\n" " z:num <- add x, y\n" " return z\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 8 in location 1\n" ); } //: When loading recipes save any header. :(before "End recipe Fields") bool has_header; vector ingredients; vector products; :(before "End recipe Constructor") has_header = false; :(before "End Recipe Refinements") if (in.peek() != '[') { trace(101, "parse") << "recipe has a header; parsing" << end(); load_recipe_header(in, result); } :(code) void load_recipe_header(istream& in, recipe& result) { result.has_header = true; while (has_data(in) && in.peek() != '[' && in.peek() != '\n') { string s = next_word(in); if (s.empty()) { assert(!has_data(in)); raise << "incomplete recipe header at end of file (0)\n" << end(); return; } if (s == "<-") raise << "recipe " << result.name << " should say '->' and not '<-'\n" << end(); if (s == "->") break; result.ingredients.push_back(reagent(s)); trace(101, "parse") << "header ingredient: " << result.ingredients.back().original_string << end(); skip_whitespace_but_not_newline(in); } while (has_data(in) && in.peek() != '[' && in.peek() != '\n') { string s = next_word(in); if (s.empty()) { assert(!has_data(in)); raise << "incomplete recipe header at end of file (1)\n" << end(); return; } result.products.push_back(reagent(s)); trace(101, "parse") << "header product: " << result.products.back().original_string << end(); skip_whitespace_but_not_newline(in); } // End Load Recipe Header(result) } void test_recipe_handles_stray_comma() { run( "def main [\n" " 1:num/raw <- add2 3, 5\n" "]\n" "def add2 x:num, y:num -> z:num, [\n" " local-scope\n" " load-ingredients\n" " z:num <- add x, y\n" " return z\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 8 in location 1\n" ); } void test_recipe_handles_stray_comma_2() { run( "def main [\n" " foo\n" "]\n" "def foo, [\n" " 1:num/raw <- add 2, 2\n" "]\n" "def bar [\n" " 1:num/raw <- add 2, 3\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 4 in location 1\n" ); } void test_recipe_handles_wrong_arrow() { Hide_errors = true; run( "def foo a:num <- b:num [\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: recipe foo should say '->' and not '<-'\n" ); } void test_recipe_handles_missing_bracket() { Hide_errors = true; run( "def main\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: recipe body must begin with '['\n" ); } void test_recipe_handles_missing_bracket_2() { Hide_errors = true; run( "def main\n" " local-scope\n" " {\n" " }\n" "]\n" ); // doesn't overflow line when reading header CHECK_TRACE_DOESNT_CONTAIN("parse: header ingredient: local-scope"); CHECK_TRACE_CONTENTS( "error: main: recipe body must begin with '['\n" ); } void test_recipe_handles_missing_bracket_3() { Hide_errors = true; run( "def main # comment\n" " local-scope\n" " {\n" " }\n" "]\n" ); // doesn't overflow line when reading header CHECK_TRACE_DOESNT_CONTAIN("parse: header ingredient: local-scope"); CHECK_TRACE_CONTENTS( "error: main: recipe body must begin with '['\n" ); } :(after "Begin debug_string(recipe x)") out << "ingredients:\n"; for (int i = 0; i < SIZE(x.ingredients); ++i) out << " " << debug_string(x.ingredients.at(i)) << '\n'; out << "products:\n"; for (int i = 0; i < SIZE(x.products); ++i) out << " " << debug_string(x.products.at(i)) << '\n'; //: If a recipe never mentions any ingredients or products, assume it has a header. :(code) void test_recipe_without_ingredients_or_products_has_header() { run( "def test [\n" " 1:num <- copy 34\n" "]\n" ); CHECK_TRACE_CONTENTS( "parse: recipe test has a header\n" ); } :(before "End Recipe
discard """
  file: "tmultim2.nim"
  output: '''collide: unit, thing
collide: unit, thing
collide: thing, unit
collide: thing, thing'''
"""
# Test multi methods

type
  TThing = object {.inheritable.}
  TUnit = object of TThing
    x: int
  TParticle = object of TThing
    a, b: int
    
method collide(a, b: TThing) {.inline.} =
  echo "collide: thing, thing"
  
method collide(a: TThing, b: TUnit) {.inline.} =
  echo "collide: thing, unit"

method collide(a: TUnit, b: TThing) {.inline.} =
  echo "collide: unit, thing"

proc test(a, b: TThing) {.inline.} =
  collide(a, b)

proc staticCollide(a, b: TThing) {.inline.} =
  procCall collide(a, b)


var
  a: TThing
  b, c: TUnit
collide(b, c) # ambiguous (unit, thing) or (thing, unit)? -> prefer unit, thing!
test(b, c)
collide(a, b)
staticCollide(a, b)
m.push_back(deduce_types_from_header); // idempotent :(code) void deduce_types_from_header(const recipe_ordinal r) { recipe& caller_recipe = get(Recipe, r); if (caller_recipe.products.empty()) return; trace(101, "transform") << "--- deduce types from header for " << caller_recipe.name << end(); map header_type; for (int i = 0; i < SIZE(caller_recipe.ingredients); ++i) { if (!caller_recipe.ingredients.at(i).type) continue; // error handled elsewhere put(header_type, caller_recipe.ingredients.at(i).name, caller_recipe.ingredients.at(i).type); trace(103, "transform") << "type of " << caller_recipe.ingredients.at(i).name << " is " << names_to_string(caller_recipe.ingredients.at(i).type) << end(); } for (int i = 0; i < SIZE(caller_recipe.products); ++i) { if (!caller_recipe.products.at(i).type) continue; // error handled elsewhere put(header_type, caller_recipe.products.at(i).name, caller_recipe.products.at(i).type); trace(103, "transform") << "type of " << caller_recipe.products.at(i).name << " is " << names_to_string(caller_recipe.products.at(i).type) << end(); } for (int i = 0; i < SIZE(caller_recipe.steps); ++i) { instruction& inst = caller_recipe.steps.at(i); trace(102, "transform") << "instruction: " << to_string(inst) << end(); for (int i = 0; i < SIZE(inst.ingredients); ++i) { if (inst.ingredients.at(i).type) continue; if (header_type.find(inst.ingredients.at(i).name) == header_type.end()) continue; if (!contains_key(header_type, inst.ingredients.at(i).name)) continue; // error handled elsewhere inst.ingredients.at(i).type = new type_tree(*get(header_type, inst.ingredients.at(i).name)); trace(103, "transform") << "type of " << inst.ingredients.at(i).name << " is " << names_to_string(inst.ingredients.at(i).type) << end(); } for (int i = 0; i < SIZE(inst.products); ++i) { trace(103, "transform") << " product: " << to_string(inst.products.at(i)) << end(); if (inst.products.at(i).type) continue; if (header_type.find(inst.products.at(i).name) == header_type.end()) continue; if (!contains_key(header_type, inst.products.at(i).name)) continue; // error handled elsewhere inst.products.at(i).type = new type_tree(*get(header_type, inst.products.at(i).name)); trace(103, "transform") << "type of " << inst.products.at(i).name << " is " << names_to_string(inst.products.at(i).type) << end(); } } } //: One final convenience: no need to say what to return if the information is //: in the header. void test_return_based_on_header() { run( "def main [\n" " 1:num/raw <- add2 3, 5\n" "]\n" "def add2 x:num, y:num -> z:num [\n" " local-scope\n" " load-ingredients\n" " z <- add x, y\n" " return\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 8 in location 1\n" ); } :(after "Transform.push_back(check_header_ingredients)") Transform.push_back(fill_in_return_ingredients); // idempotent :(code) void fill_in_return_ingredients(const recipe_ordinal r) { recipe& caller_recipe = get(Recipe, r); trace(101, "transform") << "--- fill in return ingredients from header for recipe " << caller_recipe.name << end(); if (!caller_recipe.has_header) return; for (int i = 0; i < SIZE(caller_recipe.steps); ++i) { instruction& inst = caller_recipe.steps.at(i); if (inst.name == "reply" || inst.name == "return") add_header_products(inst, caller_recipe); } // fall through return if (!caller_recipe.steps.empty()) { const instruction& final_instruction = caller_recipe.steps.at(SIZE(caller_recipe.steps)-1); if (final_instruction.name == "reply" || final_instruction.name == "return") return; } instruction inst; inst.name = "return"; add_header_products(inst, caller_recipe); caller_recipe.steps.push_back(inst); } void add_header_products(instruction& inst, const recipe& caller_recipe) { assert(inst.name == "reply" || inst.name == "return"); // collect any products with the same names as ingredients for (int i = 0; i < SIZE(caller_recipe.products); ++i) { // if the ingredient is missing, add it from the header if (SIZE(inst.ingredients) == i) inst.ingredients.push_back(caller_recipe.products.at(i)); // if it's missing /same_as_ingredient, try to fill it in if (contains_key(caller_recipe.ingredient_index, caller_recipe.products.at(i).name) && !has_property(inst.ingredients.at(i), "same_as_ingredient")) { ostringstream same_as_ingredient; same_as_ingredient << get(caller_recipe.ingredient_index, caller_recipe.products.at(i).name); inst.ingredients.at(i).properties.push_back(pair("same-as-ingredient", new string_tree(same_as_ingredient.str()))); } } } void test_explicit_return_ignores_header() { run( "def main [\n" " 1:num/raw, 2:num/raw <- add2 3, 5\n" "]\n" "def add2 a:num, b:num -> y:num, z:num [\n" " local-scope\n" " load-ingredients\n" " y <- add a, b\n" " z <- subtract a, b\n" " return a, z\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 3 in location 1\n" "mem: storing -2 in location 2\n" ); } void test_return_on_fallthrough_based_on_header() { run( "def main [\n" " 1:num/raw <- add2 3, 5\n" "]\n" "def add2 x:num, y:num -> z:num [\n" " local-scope\n" " load-ingredients\n" " z <- add x, y\n" "]\n" ); CHECK_TRACE_CONTENTS( "transform: instruction: return {z: \"number\"}\n" "mem: storing 8 in location 1\n" ); } void test_return_on_fallthrough_already_exists() { run( "def main [\n" " 1:num/raw <- add2 3, 5\n" "]\n" "def add2 x:num, y:num -> z:num [\n" " local-scope\n" " load-ingredients\n" " z <- add x, y # no type for z\n" " return z\n" "]\n" ); CHECK_TRACE_CONTENTS( "transform: instruction: return {z: ()}\n" ); CHECK_TRACE_DOESNT_CONTAIN("transform: instruction: return z:num"); CHECK_TRACE_CONTENTS( "mem: storing 8 in location 1\n" ); } void test_return_causes_error_in_empty_recipe() { Hide_errors = true; run( "def foo -> x:num [\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: foo: tried to read ingredient 'x' in 'return x:num' but it hasn't been written to yet\n" ); } void test_return_after_conditional_return_based_on_header() { run( "def main [\n" " 1:num/raw <- add2 3, 5\n" "]\n" "def add2 x:num, y:num -> z:num [\n" " local-scope\n" " load-ingredients\n" " z <- add x, y\n" // no type for z " return-if false, 34\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 8 in location 1\n" ); } void test_recipe_headers_perform_same_ingredient_check() { Hide_errors = true; run( "def main [\n" " 1:num <- copy 34\n" " 2:num <- copy 34\n" " 3:num <- add2 1:num, 2:num\n" "]\n" "def add2 x:num, y:num -> x:num [\n" " local-scope\n" " load-ingredients\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: '3:num <- add2 1:num, 2:num' should write to '1:num' rather than '3:num'\n" ); } //: One special-case is recipe 'main'. Make sure it's only ever taking in text //: ingredients, and returning a single number. void test_recipe_header_ingredients_constrained_for_main() { Hide_errors = true; run( "def main x:num [\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: ingredients of recipe 'main' must all be text (address:array:character)\n" ); } void test_recipe_header_products_constrained_for_main() { Hide_errors = true; run( "def main -> x:text [\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: recipe 'main' must return at most a single product, a number\n" ); } void test_recipe_header_products_constrained_for_main_2() { Hide_errors = true; run( "def main -> x:num, y:num [\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: recipe 'main' must return at most a single product, a number\n" ); } :(after "Transform.push_back(expand_type_abbreviations)") Transform.push_back(check_recipe_header_constraints); :(code) void check_recipe_header_constraints(const recipe_ordinal r) { const recipe& caller = get(Recipe, r); if (caller.name != "main") return; trace(102, "transform") << "check recipe header constraints for recipe " << caller.name << end(); if (!caller.has_header) return; reagent/*local*/ expected_ingredient("x:address:array:character"); for (int i = 0; i < SIZE(caller.ingredients); ++i) { if (!types_strictly_match(expected_ingredient, caller.ingredients.at(i))) { raise << "ingredients of recipe 'main' must all be text (address:array:character)\n" << end(); break; } } int nprod = SIZE(caller.products); reagent/*local*/ expected_product("x:number"); if (nprod > 1 || (nprod == 1 && !types_strictly_match(expected_product, caller.products.at(0)))) { raise << "recipe 'main' must return at most a single product, a number\n" << end(); } }