2015-10-30 04:23:48 +00:00
|
|
|
//: Transform to maintain multiple variants of a recipe depending on the
|
|
|
|
//: number and types of the ingredients and products. Allows us to use nice
|
|
|
|
//: names like 'print' or 'length' in many mutually extensible ways.
|
|
|
|
|
|
|
|
:(scenario static_dispatch)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
7:num/raw <- test 3
|
2015-10-30 04:23:48 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def test a:num -> z:num [
|
2015-10-30 04:23:48 +00:00
|
|
|
z <- copy 1
|
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def test a:num, b:num -> z:num [
|
2015-10-30 04:23:48 +00:00
|
|
|
z <- copy 2
|
|
|
|
]
|
|
|
|
+mem: storing 1 in location 7
|
|
|
|
|
|
|
|
//: When loading recipes, accumulate variants if headers don't collide, and
|
2016-02-25 16:24:14 +00:00
|
|
|
//: flag an error if headers collide.
|
2015-10-30 04:23:48 +00:00
|
|
|
|
|
|
|
:(before "End Globals")
|
|
|
|
map<string, vector<recipe_ordinal> > Recipe_variants;
|
2015-11-07 20:58:58 +00:00
|
|
|
:(before "End One-time Setup")
|
|
|
|
put(Recipe_variants, "main", vector<recipe_ordinal>()); // since we manually added main to Recipe_ordinal
|
2016-03-20 07:48:04 +00:00
|
|
|
|
|
|
|
:(before "End Globals")
|
|
|
|
map<string, vector<recipe_ordinal> > Recipe_variants_snapshot;
|
|
|
|
:(before "End save_snapshots")
|
|
|
|
Recipe_variants_snapshot = Recipe_variants;
|
|
|
|
:(before "End restore_snapshots")
|
|
|
|
Recipe_variants = Recipe_variants_snapshot;
|
2015-10-30 04:23:48 +00:00
|
|
|
|
|
|
|
:(before "End Load Recipe Header(result)")
|
2016-02-10 18:27:27 +00:00
|
|
|
// there can only ever be one variant for main
|
2016-01-26 03:32:03 +00:00
|
|
|
if (result.name != "main" && contains_key(Recipe_ordinal, result.name)) {
|
2015-11-06 22:15:37 +00:00
|
|
|
const recipe_ordinal r = get(Recipe_ordinal, result.name);
|
2015-12-14 09:30:56 +00:00
|
|
|
if (!contains_key(Recipe, r) || get(Recipe, r).has_header) {
|
|
|
|
string new_name = matching_variant_name(result);
|
|
|
|
if (new_name.empty()) {
|
|
|
|
// variant doesn't already exist
|
|
|
|
new_name = next_unused_recipe_name(result.name);
|
|
|
|
put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
|
|
|
|
get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, new_name));
|
|
|
|
}
|
2017-11-03 08:50:46 +00:00
|
|
|
trace("load") << "switching " << result.name << " to " << new_name << end();
|
2015-11-01 06:09:04 +00:00
|
|
|
result.name = new_name;
|
2016-05-27 16:50:39 +00:00
|
|
|
result.is_autogenerated = true;
|
2015-11-01 06:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// save first variant
|
2015-11-06 19:06:58 +00:00
|
|
|
put(Recipe_ordinal, result.name, Next_recipe_ordinal++);
|
2015-11-06 22:15:37 +00:00
|
|
|
get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, result.name));
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:(code)
|
2015-12-14 09:30:56 +00:00
|
|
|
string matching_variant_name(const recipe& rr) {
|
2015-11-22 09:29:05 +00:00
|
|
|
const vector<recipe_ordinal>& variants = get_or_insert(Recipe_variants, rr.name);
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(variants); ++i) {
|
2015-12-14 09:30:56 +00:00
|
|
|
if (!contains_key(Recipe, variants.at(i))) continue;
|
|
|
|
const recipe& candidate = get(Recipe, variants.at(i));
|
|
|
|
if (!all_reagents_match(rr, candidate)) continue;
|
|
|
|
return candidate.name;
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
2015-12-14 09:30:56 +00:00
|
|
|
return "";
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool all_reagents_match(const recipe& r1, const recipe& r2) {
|
|
|
|
if (SIZE(r1.ingredients) != SIZE(r2.ingredients)) return false;
|
|
|
|
if (SIZE(r1.products) != SIZE(r2.products)) return false;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(r1.ingredients); ++i) {
|
2016-09-14 07:33:40 +00:00
|
|
|
expand_type_abbreviations(r1.ingredients.at(i).type);
|
|
|
|
expand_type_abbreviations(r2.ingredients.at(i).type);
|
|
|
|
if (!deeply_equal_type_names(r1.ingredients.at(i), r2.ingredients.at(i)))
|
2015-10-30 04:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(r1.products); ++i) {
|
2016-09-14 08:56:41 +00:00
|
|
|
expand_type_abbreviations(r1.products.at(i).type);
|
|
|
|
expand_type_abbreviations(r2.products.at(i).type);
|
2016-09-14 07:33:40 +00:00
|
|
|
if (!deeply_equal_type_names(r1.products.at(i), r2.products.at(i)))
|
2015-10-30 04:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-28 06:23:54 +00:00
|
|
|
:(before "End Globals")
|
|
|
|
set<string> Literal_type_names;
|
|
|
|
:(before "End One-time Setup")
|
|
|
|
Literal_type_names.insert("number");
|
|
|
|
Literal_type_names.insert("character");
|
|
|
|
:(code)
|
2016-02-22 04:30:02 +00:00
|
|
|
bool deeply_equal_type_names(const reagent& a, const reagent& b) {
|
|
|
|
return deeply_equal_type_names(a.type, b.type);
|
2016-02-07 00:15:05 +00:00
|
|
|
}
|
2016-02-22 04:30:02 +00:00
|
|
|
bool deeply_equal_type_names(const type_tree* a, const type_tree* b) {
|
2015-11-28 06:23:54 +00:00
|
|
|
if (!a) return !b;
|
|
|
|
if (!b) return !a;
|
2016-11-11 02:45:13 +00:00
|
|
|
if (a->atom != b->atom) return false;
|
|
|
|
if (a->atom) {
|
|
|
|
if (a->name == "literal" && b->name == "literal")
|
|
|
|
return true;
|
|
|
|
if (a->name == "literal")
|
|
|
|
return Literal_type_names.find(b->name) != Literal_type_names.end();
|
|
|
|
if (b->name == "literal")
|
|
|
|
return Literal_type_names.find(a->name) != Literal_type_names.end();
|
|
|
|
return a->name == b->name;
|
|
|
|
}
|
|
|
|
return deeply_equal_type_names(a->left, b->left)
|
2016-02-22 04:30:02 +00:00
|
|
|
&& deeply_equal_type_names(a->right, b->right);
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string next_unused_recipe_name(const string& recipe_name) {
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 2; /*forever*/; ++i) {
|
2015-10-30 04:23:48 +00:00
|
|
|
ostringstream out;
|
|
|
|
out << recipe_name << '_' << i;
|
2015-11-14 06:27:59 +00:00
|
|
|
if (!contains_key(Recipe_ordinal, out.str()))
|
2015-10-30 04:23:48 +00:00
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//: Once all the recipes are loaded, transform their bodies to replace each
|
|
|
|
//: call with the most suitable variant.
|
|
|
|
|
|
|
|
:(scenario static_dispatch_picks_most_similar_variant)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
7:num/raw <- test 3, 4, 5
|
2015-10-30 04:23:48 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def test a:num -> z:num [
|
2015-10-30 04:23:48 +00:00
|
|
|
z <- copy 1
|
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def test a:num, b:num -> z:num [
|
2015-10-30 04:23:48 +00:00
|
|
|
z <- copy 2
|
|
|
|
]
|
|
|
|
+mem: storing 2 in location 7
|
|
|
|
|
2015-11-19 18:29:55 +00:00
|
|
|
//: support recipe headers in a previous transform to fill in missing types
|
|
|
|
:(before "End check_or_set_invalid_types")
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(caller.ingredients); ++i)
|
2016-02-22 04:30:02 +00:00
|
|
|
check_or_set_invalid_types(caller.ingredients.at(i).type, maybe(caller.name), "recipe header ingredient");
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(caller.products); ++i)
|
2016-02-22 04:30:02 +00:00
|
|
|
check_or_set_invalid_types(caller.products.at(i).type, maybe(caller.name), "recipe header product");
|
2015-11-19 18:29:55 +00:00
|
|
|
|
2017-04-21 00:02:35 +00:00
|
|
|
//: save original name of recipes before renaming them
|
|
|
|
:(before "End recipe Fields")
|
|
|
|
string original_name;
|
|
|
|
//: original name is only set during load
|
|
|
|
:(before "End Load Recipe Name")
|
|
|
|
result.original_name = result.name;
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
//: after filling in all missing types (because we'll be introducing 'blank' types in this transform in a later layer, for shape-shifting recipes)
|
2016-04-11 04:08:31 +00:00
|
|
|
:(after "Transform.push_back(transform_names)")
|
2015-11-07 01:29:52 +00:00
|
|
|
Transform.push_back(resolve_ambiguous_calls); // idempotent
|
2015-10-30 04:23:48 +00:00
|
|
|
|
2016-01-30 20:03:16 +00:00
|
|
|
//: In a later layer we'll introduce recursion in resolve_ambiguous_calls, by
|
|
|
|
//: having it generate code for shape-shifting recipes and then transform such
|
|
|
|
//: code. This data structure will help error messages be more useful.
|
|
|
|
//:
|
|
|
|
//: We're punning the 'call' data structure just because it has slots for
|
|
|
|
//: calling recipe and calling instruction.
|
|
|
|
:(before "End Globals")
|
2016-11-24 03:40:47 +00:00
|
|
|
list<call> Resolve_stack;
|
2016-01-30 20:03:16 +00:00
|
|
|
|
2015-10-30 04:23:48 +00:00
|
|
|
:(code)
|
2016-10-22 23:10:23 +00:00
|
|
|
void resolve_ambiguous_calls(const recipe_ordinal r) {
|
2015-11-11 03:56:41 +00:00
|
|
|
recipe& caller_recipe = get(Recipe, r);
|
|
|
|
trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << caller_recipe.name << end();
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int index = 0; index < SIZE(caller_recipe.steps); ++index) {
|
2015-11-11 03:56:41 +00:00
|
|
|
instruction& inst = caller_recipe.steps.at(index);
|
2015-10-30 04:23:48 +00:00
|
|
|
if (inst.is_label) continue;
|
2016-02-12 00:13:10 +00:00
|
|
|
if (non_ghost_size(get_or_insert(Recipe_variants, inst.name)) == 0) continue;
|
2017-05-26 23:43:18 +00:00
|
|
|
trace(9992, "transform") << "instruction " << to_original_string(inst) << end();
|
2016-11-24 03:40:47 +00:00
|
|
|
Resolve_stack.push_front(call(r));
|
|
|
|
Resolve_stack.front().running_step_index = index;
|
2016-02-12 00:13:10 +00:00
|
|
|
string new_name = best_variant(inst, caller_recipe);
|
|
|
|
if (!new_name.empty())
|
|
|
|
inst.name = new_name;
|
2016-11-24 03:40:47 +00:00
|
|
|
assert(Resolve_stack.front().running_recipe == r);
|
|
|
|
assert(Resolve_stack.front().running_step_index == index);
|
|
|
|
Resolve_stack.pop_front();
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 00:13:10 +00:00
|
|
|
string best_variant(instruction& inst, const recipe& caller_recipe) {
|
2017-10-30 09:39:11 +00:00
|
|
|
const vector<recipe_ordinal>& variants = get(Recipe_variants, inst.name);
|
2016-02-12 00:13:10 +00:00
|
|
|
vector<recipe_ordinal> candidates;
|
|
|
|
|
|
|
|
// Static Dispatch Phase 1
|
2017-10-30 20:45:17 +00:00
|
|
|
//? cerr << inst.name << " phase 1\n";
|
2016-02-12 00:13:10 +00:00
|
|
|
candidates = strictly_matching_variants(inst, variants);
|
|
|
|
if (!candidates.empty()) return best_variant(inst, candidates).name;
|
|
|
|
|
2016-04-28 05:28:55 +00:00
|
|
|
// Static Dispatch Phase 2
|
2017-10-30 20:45:17 +00:00
|
|
|
//? cerr << inst.name << " phase 2\n";
|
2017-03-12 19:42:33 +00:00
|
|
|
candidates = strictly_matching_variants_except_literal_against_address_or_boolean(inst, variants);
|
2016-05-06 04:47:59 +00:00
|
|
|
if (!candidates.empty()) return best_variant(inst, candidates).name;
|
2016-02-12 00:13:10 +00:00
|
|
|
|
2017-10-30 20:45:17 +00:00
|
|
|
//? cerr << inst.name << " phase 3\n";
|
2016-02-12 00:13:10 +00:00
|
|
|
// Static Dispatch Phase 3
|
2016-05-06 04:47:59 +00:00
|
|
|
//: (shape-shifting recipes in a later layer)
|
|
|
|
// End Static Dispatch Phase 3
|
2016-02-12 00:13:10 +00:00
|
|
|
|
|
|
|
// Static Dispatch Phase 4
|
2017-10-30 20:45:17 +00:00
|
|
|
//? cerr << inst.name << " phase 4\n";
|
2016-02-12 00:13:10 +00:00
|
|
|
candidates = matching_variants(inst, variants);
|
|
|
|
if (!candidates.empty()) return best_variant(inst, candidates).name;
|
|
|
|
|
|
|
|
// error messages
|
2017-10-19 03:08:05 +00:00
|
|
|
if (!is_primitive(get(Recipe_ordinal, inst.name))) { // we currently don't check types for primitive variants
|
2017-04-13 18:00:08 +00:00
|
|
|
if (SIZE(variants) == 1) {
|
2017-05-26 23:43:18 +00:00
|
|
|
raise << maybe(caller_recipe.name) << "types don't match in call for '" << to_original_string(inst) << "'\n" << end();
|
2017-04-20 17:09:14 +00:00
|
|
|
raise << " which tries to call '" << original_header_label(get(Recipe, variants.at(0))) << "'\n" << end();
|
2017-04-13 18:00:08 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-05-26 23:43:18 +00:00
|
|
|
raise << maybe(caller_recipe.name) << "failed to find a matching call for '" << to_original_string(inst) << "'\n" << end();
|
2017-04-13 18:00:08 +00:00
|
|
|
raise << " available variants are:\n" << end();
|
2017-04-20 17:09:14 +00:00
|
|
|
for (int i = 0; i < SIZE(variants); ++i)
|
|
|
|
raise << " " << original_header_label(get(Recipe, variants.at(i))) << '\n' << end();
|
2017-04-13 18:00:08 +00:00
|
|
|
}
|
2016-11-24 03:40:47 +00:00
|
|
|
for (list<call>::iterator p = /*skip*/++Resolve_stack.begin(); p != Resolve_stack.end(); ++p) {
|
2016-01-30 20:03:16 +00:00
|
|
|
const recipe& specializer_recipe = get(Recipe, p->running_recipe);
|
|
|
|
const instruction& specializer_inst = specializer_recipe.steps.at(p->running_step_index);
|
|
|
|
if (specializer_recipe.name != "interactive")
|
2017-05-26 23:43:18 +00:00
|
|
|
raise << " (from '" << to_original_string(specializer_inst) << "' in " << specializer_recipe.name << ")\n" << end();
|
2016-01-30 20:03:16 +00:00
|
|
|
else
|
2017-05-26 23:43:18 +00:00
|
|
|
raise << " (from '" << to_original_string(specializer_inst) << "')\n" << end();
|
2016-01-30 20:03:16 +00:00
|
|
|
// One special-case to help with the rewrite_stash transform. (cross-layer)
|
|
|
|
if (specializer_inst.products.at(0).name.find("stash_") == 0) {
|
|
|
|
instruction stash_inst;
|
|
|
|
if (next_stash(*p, &stash_inst)) {
|
|
|
|
if (specializer_recipe.name != "interactive")
|
2017-05-26 23:43:18 +00:00
|
|
|
raise << " (part of '" << to_original_string(stash_inst) << "' in " << specializer_recipe.name << ")\n" << end();
|
2016-01-30 20:03:16 +00:00
|
|
|
else
|
2017-05-26 23:43:18 +00:00
|
|
|
raise << " (part of '" << to_original_string(stash_inst) << "')\n" << end();
|
2016-01-30 20:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
return "";
|
2016-01-30 20:03:16 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 00:13:10 +00:00
|
|
|
// phase 1
|
2017-10-30 09:39:11 +00:00
|
|
|
vector<recipe_ordinal> strictly_matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
|
2016-02-12 00:13:10 +00:00
|
|
|
vector<recipe_ordinal> result;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(variants); ++i) {
|
2016-02-12 00:13:10 +00:00
|
|
|
if (variants.at(i) == -1) continue;
|
|
|
|
trace(9992, "transform") << "checking variant (strict) " << i << ": " << header_label(variants.at(i)) << end();
|
|
|
|
if (all_header_reagents_strictly_match(inst, get(Recipe, variants.at(i))))
|
|
|
|
result.push_back(variants.at(i));
|
2015-11-19 07:55:54 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
return result;
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 00:13:10 +00:00
|
|
|
bool all_header_reagents_strictly_match(const instruction& inst, const recipe& variant) {
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < min(SIZE(inst.ingredients), SIZE(variant.ingredients)); ++i) {
|
2016-02-12 00:13:10 +00:00
|
|
|
if (!types_strictly_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
|
|
|
|
trace(9993, "transform") << "strict match failed: ingredient " << i << end();
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-22 09:29:05 +00:00
|
|
|
}
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < min(SIZE(inst.products), SIZE(variant.products)); ++i) {
|
2016-02-12 00:13:10 +00:00
|
|
|
if (is_dummy(inst.products.at(i))) continue;
|
|
|
|
if (!types_strictly_match(variant.products.at(i), inst.products.at(i))) {
|
|
|
|
trace(9993, "transform") << "strict match failed: product " << i << end();
|
|
|
|
return false;
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:47:59 +00:00
|
|
|
// phase 2
|
2017-10-30 09:39:11 +00:00
|
|
|
vector<recipe_ordinal> strictly_matching_variants_except_literal_against_address_or_boolean(const instruction& inst, const vector<recipe_ordinal>& variants) {
|
2016-02-12 00:13:10 +00:00
|
|
|
vector<recipe_ordinal> result;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(variants); ++i) {
|
2016-02-12 00:13:10 +00:00
|
|
|
if (variants.at(i) == -1) continue;
|
2016-05-25 01:38:13 +00:00
|
|
|
trace(9992, "transform") << "checking variant (strict except literal-against-boolean) " << i << ": " << header_label(variants.at(i)) << end();
|
2016-05-06 04:47:59 +00:00
|
|
|
if (all_header_reagents_strictly_match_except_literal_against_address_or_boolean(inst, get(Recipe, variants.at(i))))
|
2016-02-12 00:13:10 +00:00
|
|
|
result.push_back(variants.at(i));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:47:59 +00:00
|
|
|
bool all_header_reagents_strictly_match_except_literal_against_address_or_boolean(const instruction& inst, const recipe& variant) {
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < min(SIZE(inst.ingredients), SIZE(variant.ingredients)); ++i) {
|
2016-05-06 04:47:59 +00:00
|
|
|
if (!types_strictly_match_except_literal_against_address_or_boolean(variant.ingredients.at(i), inst.ingredients.at(i))) {
|
2016-06-12 06:32:11 +00:00
|
|
|
trace(9993, "transform") << "match failed: ingredient " << i << end();
|
2016-02-12 00:13:10 +00:00
|
|
|
return false;
|
2015-11-28 02:10:15 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
}
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < min(SIZE(variant.products), SIZE(inst.products)); ++i) {
|
2016-02-12 00:13:10 +00:00
|
|
|
if (is_dummy(inst.products.at(i))) continue;
|
2016-05-06 04:47:59 +00:00
|
|
|
if (!types_strictly_match_except_literal_against_address_or_boolean(variant.products.at(i), inst.products.at(i))) {
|
2016-06-12 06:32:11 +00:00
|
|
|
trace(9993, "transform") << "match failed: product " << i << end();
|
2016-02-12 00:13:10 +00:00
|
|
|
return false;
|
2015-11-29 19:43:25 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:47:59 +00:00
|
|
|
bool types_strictly_match_except_literal_against_address_or_boolean(const reagent& to, const reagent& from) {
|
3309
Rip out everything to fix one failing unit test (commit 3290; type
abbreviations).
This commit does several things at once that I couldn't come up with a
clean way to unpack:
A. It moves to a new representation for type trees without changing
the actual definition of the `type_tree` struct.
B. It adds unit tests for our type metadata precomputation, so that
errors there show up early and in a simpler setting rather than dying
when we try to load Mu code.
C. It fixes a bug, guarding against infinite loops when precomputing
metadata for recursive shape-shifting containers. To do this it uses a
dumb way of comparing type_trees, comparing their string
representations instead. That is likely incredibly inefficient.
Perhaps due to C, this commit has made Mu incredibly slow. Running all
tests for the core and the edit/ app now takes 6.5 minutes rather than
3.5 minutes.
== more notes and details
I've been struggling for the past week now to back out of a bad design
decision, a premature optimization from the early days: storing atoms
directly in the 'value' slot of a cons cell rather than creating a
special 'atom' cons cell and storing it on the 'left' slot. In other
words, if a cons cell looks like this:
o
/ | \
left val right
..then the type_tree (a b c) used to look like this (before this
commit):
o
| \
a o
| \
b o
| \
c null
..rather than like this 'classic' approach to s-expressions which never
mixes val and right (which is what we now have):
o
/ \
o o
| / \
a o o
| / \
b o null
|
c
The old approach made several operations more complicated, most recently
the act of replacing a (possibly atom/leaf) sub-tree with another. That
was the final straw that got me to realize the contortions I was going
through to save a few type_tree nodes (cons cells).
Switching to the new approach was hard partly because I've been using
the old approach for so long and type_tree manipulations had pervaded
everything. Another issue I ran into was the realization that my layers
were not cleanly separated. Key parts of early layers (precomputing type
metadata) existed purely for far later ones (shape-shifting types).
Layers I got repeatedly stuck at:
1. the transform for precomputing type sizes (layer 30)
2. type-checks on merge instructions (layer 31)
3. the transform for precomputing address offsets in types (layer 36)
4. replace operations in supporting shape-shifting recipes (layer 55)
After much thrashing I finally noticed that it wasn't the entirety of
these layers that was giving me trouble, but just the type metadata
precomputation, which had bugs that weren't manifesting until 30 layers
later. Or, worse, when loading .mu files before any tests had had a
chance to run. A common failure mode was running into types at run time
that I hadn't precomputed metadata for at transform time.
Digging into these bugs got me to realize that what I had before wasn't
really very good, but a half-assed heuristic approach that did a whole
lot of extra work precomputing metadata for utterly meaningless types
like `((address number) 3)` which just happened to be part of a larger
type like `(array (address number) 3)`.
So, I redid it all. I switched the representation of types (because the
old representation made unit tests difficult to retrofit) and added unit
tests to the metadata precomputation. I also made layer 30 only do the
minimal metadata precomputation it needs for the concepts introduced
until then. In the process, I also made the precomputation more correct
than before, and added hooks in the right place so that I could augment
the logic when I introduced shape-shifting containers.
== lessons learned
There's several levels of hygiene when it comes to layers:
1. Every layer introduces precisely what it needs and in the simplest
way possible. If I was building an app until just that layer, nothing
would seem over-engineered.
2. Some layers are fore-shadowing features in future layers. Sometimes
this is ok. For example, layer 10 foreshadows containers and arrays and
so on without actually supporting them. That is a net win because it
lets me lay out the core of Mu's data structures out in one place. But
if the fore-shadowing gets too complex things get nasty. Not least
because it can be hard to write unit tests for features before you
provide the plumbing to visualize and manipulate them.
3. A layer is introducing features that are tested only in later layers.
4. A layer is introducing features with tests that are invalidated in
later layers. (This I knew from early on to be an obviously horrendous
idea.)
Summary: avoid Level 2 (foreshadowing layers) as much as possible.
Tolerate it indefinitely for small things where the code stays simple
over time, but become strict again when things start to get more
complex.
Level 3 is mostly a net lose, but sometimes it can be expedient (a real
case of the usually grossly over-applied term "technical debt"), and
it's better than the conventional baseline of no layers and no
scenarios. Just clean it up as soon as possible.
Definitely avoid layer 4 at any time.
== minor lessons
Avoid unit tests for trivial things, write scenarios in context as much as
possible. But within those margins unit tests are fine. Just introduce them
before any scenarios (commit 3297).
Reorganizing layers can be easy. Just merge layers for starters! Punt on
resplitting them in some new way until you've gotten them to work. This is the
wisdom of Refactoring: small steps.
What made it hard was not wanting to merge *everything* between layer 30
and 55. The eventual insight was realizing I just need to move those two
full-strength transforms and nothing else.
2016-09-10 01:32:52 +00:00
|
|
|
if (is_literal(from) && is_mu_boolean(to))
|
|
|
|
return from.name == "0" || from.name == "1";
|
2017-03-12 19:42:33 +00:00
|
|
|
// Match Literal Zero Against Address {
|
|
|
|
if (is_literal(from) && is_mu_address(to))
|
|
|
|
return from.name == "0";
|
|
|
|
// }
|
|
|
|
return types_strictly_match(to, from);
|
2016-05-06 04:47:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 19:42:33 +00:00
|
|
|
// phase 4
|
2017-10-30 09:39:11 +00:00
|
|
|
vector<recipe_ordinal> matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
|
2016-02-12 00:13:10 +00:00
|
|
|
vector<recipe_ordinal> result;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(variants); ++i) {
|
2016-02-12 00:13:10 +00:00
|
|
|
if (variants.at(i) == -1) continue;
|
|
|
|
trace(9992, "transform") << "checking variant " << i << ": " << header_label(variants.at(i)) << end();
|
|
|
|
if (all_header_reagents_match(inst, get(Recipe, variants.at(i))))
|
|
|
|
result.push_back(variants.at(i));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool all_header_reagents_match(const instruction& inst, const recipe& variant) {
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < min(SIZE(inst.ingredients), SIZE(variant.ingredients)); ++i) {
|
2016-02-12 00:13:10 +00:00
|
|
|
if (!types_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
|
2016-06-12 06:32:11 +00:00
|
|
|
trace(9993, "transform") << "match failed: ingredient " << i << end();
|
2016-02-12 00:13:10 +00:00
|
|
|
return false;
|
2015-11-28 02:10:15 +00:00
|
|
|
}
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < min(SIZE(variant.products), SIZE(inst.products)); ++i) {
|
2015-11-19 07:55:54 +00:00
|
|
|
if (is_dummy(inst.products.at(i))) continue;
|
2016-02-12 00:13:10 +00:00
|
|
|
if (!types_match(variant.products.at(i), inst.products.at(i))) {
|
2016-06-12 06:32:11 +00:00
|
|
|
trace(9993, "transform") << "match failed: product " << i << end();
|
2016-02-12 00:13:10 +00:00
|
|
|
return false;
|
2015-11-28 02:10:15 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tie-breaker for each phase
|
|
|
|
const recipe& best_variant(const instruction& inst, vector<recipe_ordinal>& candidates) {
|
|
|
|
assert(!candidates.empty());
|
2017-10-30 09:39:11 +00:00
|
|
|
if (SIZE(candidates) == 1) return get(Recipe, candidates.at(0));
|
2016-03-14 03:26:47 +00:00
|
|
|
int min_score = 999;
|
|
|
|
int min_index = 0;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(candidates); ++i) {
|
2016-02-12 00:13:10 +00:00
|
|
|
const recipe& candidate = get(Recipe, candidates.at(i));
|
2017-03-12 20:02:12 +00:00
|
|
|
// prefer functions without extra or missing ingredients or products
|
2016-03-14 03:26:47 +00:00
|
|
|
int score = abs(SIZE(candidate.products)-SIZE(inst.products))
|
2016-02-12 00:13:10 +00:00
|
|
|
+ abs(SIZE(candidate.ingredients)-SIZE(inst.ingredients));
|
2017-03-12 20:02:12 +00:00
|
|
|
// prefer functions with non-address ingredients or products
|
2017-04-05 02:43:57 +00:00
|
|
|
for (int j = 0; j < SIZE(candidate.ingredients); ++j) {
|
|
|
|
if (is_mu_address(candidate.ingredients.at(j)))
|
2017-03-12 20:02:12 +00:00
|
|
|
++score;
|
|
|
|
}
|
2017-04-05 02:43:57 +00:00
|
|
|
for (int j = 0; j < SIZE(candidate.products); ++j) {
|
|
|
|
if (is_mu_address(candidate.products.at(j)))
|
2017-03-12 20:02:12 +00:00
|
|
|
++score;
|
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
assert(score < 999);
|
|
|
|
if (score < min_score) {
|
|
|
|
min_score = score;
|
|
|
|
min_index = i;
|
2015-11-29 19:43:25 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
}
|
|
|
|
return get(Recipe, candidates.at(min_index));
|
|
|
|
}
|
|
|
|
|
2016-03-14 03:26:47 +00:00
|
|
|
int non_ghost_size(vector<recipe_ordinal>& variants) {
|
|
|
|
int result = 0;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(variants); ++i)
|
2016-02-12 00:13:10 +00:00
|
|
|
if (variants.at(i) != -1) ++result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool next_stash(const call& c, instruction* stash_inst) {
|
|
|
|
const recipe& specializer_recipe = get(Recipe, c.running_recipe);
|
2016-03-14 03:26:47 +00:00
|
|
|
int index = c.running_step_index;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (++index; index < SIZE(specializer_recipe.steps); ++index) {
|
2016-02-12 00:13:10 +00:00
|
|
|
const instruction& inst = specializer_recipe.steps.at(index);
|
|
|
|
if (inst.name == "stash") {
|
|
|
|
*stash_inst = inst;
|
|
|
|
return true;
|
2015-11-28 02:10:15 +00:00
|
|
|
}
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
return false;
|
2015-10-30 04:23:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 00:13:10 +00:00
|
|
|
:(scenario static_dispatch_disabled_in_recipe_without_variants)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num <- test 3
|
2016-02-12 00:13:10 +00:00
|
|
|
]
|
2016-03-08 09:30:14 +00:00
|
|
|
def test [
|
2016-09-17 17:28:25 +00:00
|
|
|
2:num <- next-ingredient # ensure no header
|
2016-03-08 09:30:14 +00:00
|
|
|
return 34
|
2016-02-12 00:13:10 +00:00
|
|
|
]
|
|
|
|
+mem: storing 34 in location 1
|
|
|
|
|
2015-10-30 04:23:48 +00:00
|
|
|
:(scenario static_dispatch_disabled_on_headerless_definition)
|
2016-02-25 15:58:09 +00:00
|
|
|
% Hide_errors = true;
|
2016-09-17 17:28:25 +00:00
|
|
|
def test a:num -> z:num [
|
2015-10-30 04:23:48 +00:00
|
|
|
z <- copy 1
|
|
|
|
]
|
2016-03-08 09:30:14 +00:00
|
|
|
def test [
|
|
|
|
return 34
|
2015-10-30 04:23:48 +00:00
|
|
|
]
|
2016-02-25 15:58:09 +00:00
|
|
|
+error: redefining recipe test
|
2015-10-30 04:23:48 +00:00
|
|
|
|
|
|
|
:(scenario static_dispatch_disabled_on_headerless_definition_2)
|
2016-02-25 15:58:09 +00:00
|
|
|
% Hide_errors = true;
|
2016-03-08 09:30:14 +00:00
|
|
|
def test [
|
|
|
|
return 34
|
2015-10-30 04:23:48 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def test a:num -> z:num [
|
2015-10-30 04:23:48 +00:00
|
|
|
z <- copy 1
|
|
|
|
]
|
2016-02-25 15:58:09 +00:00
|
|
|
+error: redefining recipe test
|
2015-11-22 09:29:05 +00:00
|
|
|
|
|
|
|
:(scenario static_dispatch_on_primitive_names)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num <- copy 34
|
|
|
|
2:num <- copy 34
|
|
|
|
3:bool <- equal 1:num, 2:num
|
2016-09-17 07:46:03 +00:00
|
|
|
4:bool <- copy 0/false
|
|
|
|
5:bool <- copy 0/false
|
|
|
|
6:bool <- equal 4:bool, 5:bool
|
2015-11-22 09:29:05 +00:00
|
|
|
]
|
|
|
|
# temporarily hardcode number equality to always fail
|
2016-09-17 17:28:25 +00:00
|
|
|
def equal x:num, y:num -> z:bool [
|
2015-11-22 09:29:05 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
z <- copy 0/false
|
|
|
|
]
|
|
|
|
# comparing numbers used overload
|
|
|
|
+mem: storing 0 in location 3
|
|
|
|
# comparing booleans continues to use primitive
|
|
|
|
+mem: storing 1 in location 6
|
2015-11-28 02:10:15 +00:00
|
|
|
|
2015-11-19 07:55:54 +00:00
|
|
|
:(scenario static_dispatch_works_with_dummy_results_for_containers)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2015-11-19 07:55:54 +00:00
|
|
|
_ <- test 3, 4
|
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def test a:num -> z:point [
|
2015-11-19 07:55:54 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
z <- merge a, 0
|
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def test a:num, b:num -> z:point [
|
2015-11-19 07:55:54 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
z <- merge a, b
|
|
|
|
]
|
|
|
|
$error: 0
|
|
|
|
|
2015-11-19 18:29:55 +00:00
|
|
|
:(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_first_use)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 19:55:10 +00:00
|
|
|
x:&:foo <- new foo:type
|
2015-11-19 18:29:55 +00:00
|
|
|
test x
|
|
|
|
]
|
|
|
|
container foo [
|
2016-09-17 17:28:25 +00:00
|
|
|
x:num
|
2015-11-19 18:29:55 +00:00
|
|
|
]
|
2016-09-17 19:55:10 +00:00
|
|
|
def test a:&:foo -> z:num [
|
2015-11-19 18:29:55 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-09-17 17:28:25 +00:00
|
|
|
z:num <- get *a, x:offset
|
2015-11-19 18:29:55 +00:00
|
|
|
]
|
|
|
|
$error: 0
|
|
|
|
|
|
|
|
:(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_second_use)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 19:55:10 +00:00
|
|
|
x:&:foo <- new foo:type
|
2015-11-19 18:29:55 +00:00
|
|
|
test x
|
|
|
|
]
|
2016-09-17 19:55:10 +00:00
|
|
|
def test a:&:foo -> z:num [
|
2015-11-19 18:29:55 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-09-17 17:28:25 +00:00
|
|
|
z:num <- get *a, x:offset
|
2015-11-19 18:29:55 +00:00
|
|
|
]
|
|
|
|
container foo [
|
2016-09-17 17:28:25 +00:00
|
|
|
x:num
|
2015-11-19 18:29:55 +00:00
|
|
|
]
|
|
|
|
$error: 0
|
|
|
|
|
2015-11-28 02:10:15 +00:00
|
|
|
:(scenario static_dispatch_prefers_literals_to_be_numbers_rather_than_addresses)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num <- foo 0
|
2015-11-28 02:10:15 +00:00
|
|
|
]
|
2016-09-17 19:55:10 +00:00
|
|
|
def foo x:&:num -> y:num [
|
2016-03-08 09:30:14 +00:00
|
|
|
return 34
|
2015-11-28 02:10:15 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:num -> y:num [
|
2016-03-08 09:30:14 +00:00
|
|
|
return 35
|
2015-11-28 02:10:15 +00:00
|
|
|
]
|
|
|
|
+mem: storing 35 in location 1
|
2015-11-28 05:38:09 +00:00
|
|
|
|
2017-03-12 20:02:12 +00:00
|
|
|
:(scenario static_dispatch_prefers_literals_to_be_numbers_rather_than_addresses_2)
|
|
|
|
def main [
|
|
|
|
1:num <- foo 0 0
|
|
|
|
]
|
|
|
|
# Both variants need to bind 0 to address in first ingredient.
|
|
|
|
# We still want to prefer the variant with a number rather than address for
|
|
|
|
# _subsequent_ ingredients.
|
|
|
|
def foo x:&:num y:&:num -> z:num [ # put the bad match before the good one
|
|
|
|
return 34
|
|
|
|
]
|
|
|
|
def foo x:&:num y:num -> z:num [
|
|
|
|
return 35
|
|
|
|
]
|
|
|
|
+mem: storing 35 in location 1
|
|
|
|
|
2015-11-28 05:38:09 +00:00
|
|
|
:(scenario static_dispatch_on_non_literal_character_ignores_variant_with_numbers)
|
|
|
|
% Hide_errors = true;
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2015-11-28 05:38:09 +00:00
|
|
|
local-scope
|
2016-09-17 07:31:55 +00:00
|
|
|
x:char <- copy 10/newline
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num/raw <- foo x
|
2015-11-28 05:38:09 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:num -> y:num [
|
2015-11-28 05:38:09 +00:00
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 34
|
2015-11-28 05:38:09 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
+error: main: ingredient 0 has the wrong type at '1:num/raw <- foo x'
|
2015-11-28 05:38:09 +00:00
|
|
|
-mem: storing 34 in location 1
|
2015-11-29 19:43:25 +00:00
|
|
|
|
|
|
|
:(scenario static_dispatch_dispatches_literal_to_boolean_before_character)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num/raw <- foo 0 # valid literal for boolean
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:char -> y:num [
|
2015-11-29 19:43:25 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 34
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:bool -> y:num [
|
2015-11-29 19:43:25 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 35
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
|
|
|
# boolean variant is preferred
|
|
|
|
+mem: storing 35 in location 1
|
|
|
|
|
|
|
|
:(scenario static_dispatch_dispatches_literal_to_character_when_out_of_boolean_range)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num/raw <- foo 97 # not a valid literal for boolean
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:char -> y:num [
|
2015-11-29 19:43:25 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 34
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:bool -> y:num [
|
2015-11-29 19:43:25 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 35
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
|
|
|
# character variant is preferred
|
|
|
|
+mem: storing 34 in location 1
|
|
|
|
|
|
|
|
:(scenario static_dispatch_dispatches_literal_to_number_if_at_all_possible)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num/raw <- foo 97
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:char -> y:num [
|
2015-11-29 19:43:25 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 34
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:num -> y:num [
|
2015-11-29 19:43:25 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 35
|
2015-11-29 19:43:25 +00:00
|
|
|
]
|
|
|
|
# number variant is preferred
|
|
|
|
+mem: storing 35 in location 1
|
2015-11-29 20:10:22 +00:00
|
|
|
|
2017-05-29 06:42:54 +00:00
|
|
|
:(replace{} "string header_label(const recipe_ordinal r)")
|
2016-10-22 23:10:23 +00:00
|
|
|
string header_label(const recipe_ordinal r) {
|
2016-05-25 01:58:23 +00:00
|
|
|
return header_label(get(Recipe, r));
|
|
|
|
}
|
2017-05-29 06:42:54 +00:00
|
|
|
:(code)
|
2016-05-25 01:58:23 +00:00
|
|
|
string header_label(const recipe& caller) {
|
2015-11-29 20:10:22 +00:00
|
|
|
ostringstream out;
|
2015-11-29 21:52:14 +00:00
|
|
|
out << "recipe " << caller.name;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(caller.ingredients); ++i)
|
2016-02-19 21:42:45 +00:00
|
|
|
out << ' ' << to_string(caller.ingredients.at(i));
|
2015-11-29 21:52:14 +00:00
|
|
|
if (!caller.products.empty()) out << " ->";
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int i = 0; i < SIZE(caller.products); ++i)
|
2016-02-19 21:42:45 +00:00
|
|
|
out << ' ' << to_string(caller.products.at(i));
|
2015-11-29 20:10:22 +00:00
|
|
|
return out.str();
|
|
|
|
}
|
2015-12-14 09:30:56 +00:00
|
|
|
|
2017-04-20 17:09:14 +00:00
|
|
|
string original_header_label(const recipe& caller) {
|
|
|
|
ostringstream out;
|
2017-04-21 00:02:35 +00:00
|
|
|
out << "recipe " << caller.original_name;
|
2017-04-20 17:09:14 +00:00
|
|
|
for (int i = 0; i < SIZE(caller.ingredients); ++i)
|
|
|
|
out << ' ' << caller.ingredients.at(i).original_string;
|
|
|
|
if (!caller.products.empty()) out << " ->";
|
|
|
|
for (int i = 0; i < SIZE(caller.products); ++i)
|
|
|
|
out << ' ' << caller.products.at(i).original_string;
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:30:56 +00:00
|
|
|
:(scenario reload_variant_retains_other_variants)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num <- copy 34
|
|
|
|
2:num <- foo 1:num
|
2015-12-14 09:30:56 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo x:num -> y:num [
|
2015-12-14 09:30:56 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 34
|
2015-12-14 09:30:56 +00:00
|
|
|
]
|
2016-09-17 19:55:10 +00:00
|
|
|
def foo x:&:num -> y:num [
|
2015-12-14 09:30:56 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 35
|
2015-12-14 09:30:56 +00:00
|
|
|
]
|
2016-09-17 19:55:10 +00:00
|
|
|
def! foo x:&:num -> y:num [
|
2015-12-14 09:30:56 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return 36
|
2015-12-14 09:30:56 +00:00
|
|
|
]
|
|
|
|
+mem: storing 34 in location 2
|
|
|
|
$error: 0
|
2015-12-19 20:41:09 +00:00
|
|
|
|
2016-04-11 04:08:31 +00:00
|
|
|
:(scenario dispatch_errors_come_after_unknown_name_errors)
|
|
|
|
% Hide_errors = true;
|
|
|
|
def main [
|
2016-09-17 17:28:25 +00:00
|
|
|
y:num <- foo x
|
2016-04-11 04:08:31 +00:00
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo a:num -> b:num [
|
2016-04-11 04:08:31 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
return 34
|
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
def foo a:bool -> b:num [
|
2016-04-11 04:08:31 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
return 35
|
|
|
|
]
|
2016-09-17 17:28:25 +00:00
|
|
|
+error: main: missing type for 'x' in 'y:num <- foo x'
|
|
|
|
+error: main: failed to find a matching call for 'y:num <- foo x'
|
2016-04-11 04:08:31 +00:00
|
|
|
|
2016-09-14 07:33:40 +00:00
|
|
|
:(scenario override_methods_with_type_abbreviations)
|
|
|
|
def main [
|
2016-09-14 08:56:41 +00:00
|
|
|
local-scope
|
2016-09-17 07:06:04 +00:00
|
|
|
s:text <- new [abc]
|
2016-09-17 17:28:25 +00:00
|
|
|
1:num/raw <- foo s
|
2016-09-14 07:33:40 +00:00
|
|
|
]
|
2016-09-17 21:43:13 +00:00
|
|
|
def foo a:address:array:character -> result:number [
|
2016-09-14 07:33:40 +00:00
|
|
|
return 34
|
|
|
|
]
|
2016-09-17 21:43:13 +00:00
|
|
|
# identical to previous variant once you take type abbreviations into account
|
|
|
|
def! foo a:text -> result:num [
|
2016-09-14 07:33:40 +00:00
|
|
|
return 35
|
|
|
|
]
|
|
|
|
+mem: storing 35 in location 1
|
|
|
|
|
2017-04-13 18:00:08 +00:00
|
|
|
:(scenario ignore_static_dispatch_in_type_errors_without_overloading)
|
|
|
|
% Hide_errors = true;
|
|
|
|
def main [
|
|
|
|
local-scope
|
|
|
|
x:&:num <- copy 0
|
|
|
|
foo x
|
|
|
|
]
|
|
|
|
def foo x:&:char [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
]
|
2017-04-20 17:09:14 +00:00
|
|
|
+error: main: types don't match in call for 'foo x'
|
|
|
|
+error: which tries to call 'recipe foo x:&:char'
|
2017-04-13 18:00:08 +00:00
|
|
|
|
|
|
|
:(scenario show_available_variants_in_dispatch_errors)
|
|
|
|
% Hide_errors = true;
|
|
|
|
def main [
|
|
|
|
local-scope
|
|
|
|
x:&:num <- copy 0
|
|
|
|
foo x
|
|
|
|
]
|
|
|
|
def foo x:&:char [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
]
|
|
|
|
def foo x:&:bool [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
]
|
|
|
|
+error: main: failed to find a matching call for 'foo x'
|
|
|
|
+error: available variants are:
|
2017-04-20 17:09:14 +00:00
|
|
|
+error: recipe foo x:&:char
|
|
|
|
+error: recipe foo x:&:bool
|
2017-04-13 18:00:08 +00:00
|
|
|
|
2016-01-18 05:20:05 +00:00
|
|
|
:(before "End Includes")
|
|
|
|
using std::abs;
|