mu/054static_dispatch.cc

689 lines
22 KiB
C++
Raw Normal View History

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)
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;
:(before "End One-time Setup")
put(Recipe_variants, "main", vector<recipe_ordinal>()); // since we manually added main to Recipe_ordinal
:(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)")
// there can only ever be one variant for main
if (result.name != "main" && contains_key(Recipe_ordinal, result.name)) {
const recipe_ordinal r = get(Recipe_ordinal, result.name);
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));
}
trace(9999, "load") << "switching " << result.name << " to " << new_name << end();
result.name = new_name;
2016-05-27 16:50:39 +00:00
result.is_autogenerated = true;
}
}
else {
// save first variant
put(Recipe_ordinal, result.name, Next_recipe_ordinal++);
get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, result.name));
2015-10-30 04:23:48 +00:00
}
:(code)
string matching_variant_name(const recipe& rr) {
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) {
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
}
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) {
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);
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;
}
:(before "End Globals")
set<string> Literal_type_names;
:(before "End One-time Setup")
Literal_type_names.insert("number");
Literal_type_names.insert("character");
:(code)
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
}
bool deeply_equal_type_names(const type_tree* a, const type_tree* b) {
if (!a) return !b;
if (!b) return !a;
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)
&& 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)
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)
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)
check_or_set_invalid_types(caller.products.at(i).type, maybe(caller.name), "recipe header product");
2015-11-19 18:29:55 +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;
//: after filling in all missing types (because we'll be introducing 'blank' types in this transform in a later layer, for shape-shifting recipes)
:(after "Transform.push_back(transform_names)")
Transform.push_back(resolve_ambiguous_calls); // idempotent
2015-10-30 04:23:48 +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")
list<call> Resolve_stack;
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) {
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) {
instruction& inst = caller_recipe.steps.at(index);
2015-10-30 04:23:48 +00:00
if (inst.is_label) continue;
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();
Resolve_stack.push_front(call(r));
Resolve_stack.front().running_step_index = index;
string new_name = best_variant(inst, caller_recipe);
if (!new_name.empty())
inst.name = new_name;
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
}
}
string best_variant(instruction& inst, const recipe& caller_recipe) {
const vector<recipe_ordinal>& variants = get(Recipe_variants, inst.name);
vector<recipe_ordinal> candidates;
// Static Dispatch Phase 1
candidates = strictly_matching_variants(inst, variants);
if (!candidates.empty()) return best_variant(inst, candidates).name;
// Static Dispatch Phase 2
2017-03-12 19:42:33 +00:00
candidates = strictly_matching_variants_except_literal_against_address_or_boolean(inst, variants);
if (!candidates.empty()) return best_variant(inst, candidates).name;
// Static Dispatch Phase 3
//: (shape-shifting recipes in a later layer)
// End Static Dispatch Phase 3
// Static Dispatch Phase 4
candidates = matching_variants(inst, variants);
if (!candidates.empty()) return best_variant(inst, candidates).name;
// error messages
if (!is_primitive(get(Recipe_ordinal, inst.name))) { // we currently don't check types for primitive variants
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();
raise << " which tries to call '" << original_header_label(get(Recipe, variants.at(0))) << "'\n" << end();
}
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();
raise << " available variants are:\n" << end();
for (int i = 0; i < SIZE(variants); ++i)
raise << " " << original_header_label(get(Recipe, variants.at(i))) << '\n' << end();
}
for (list<call>::iterator p = /*skip*/++Resolve_stack.begin(); p != Resolve_stack.end(); ++p) {
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();
else
2017-05-26 23:43:18 +00:00
raise << " (from '" << to_original_string(specializer_inst) << "')\n" << end();
// 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();
else
2017-05-26 23:43:18 +00:00
raise << " (part of '" << to_original_string(stash_inst) << "')\n" << end();
}
}
}
}
return "";
}
// phase 1
vector<recipe_ordinal> strictly_matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
vector<recipe_ordinal> result;
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(variants); ++i) {
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));
}
return result;
2015-10-30 04:23:48 +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) {
if (!types_strictly_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
trace(9993, "transform") << "strict match failed: ingredient " << i << end();
return false;
}
}
2016-10-20 05:10:35 +00:00
for (int i = 0; i < min(SIZE(inst.products), SIZE(variant.products)); ++i) {
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
}
}
return true;
}
// phase 2
vector<recipe_ordinal> strictly_matching_variants_except_literal_against_address_or_boolean(const instruction& inst, const vector<recipe_ordinal>& variants) {
vector<recipe_ordinal> result;
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(variants); ++i) {
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();
if (all_header_reagents_strictly_match_except_literal_against_address_or_boolean(inst, get(Recipe, variants.at(i))))
result.push_back(variants.at(i));
}
return result;
}
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) {
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();
return false;
}
}
2016-10-20 05:10:35 +00:00
for (int i = 0; i < min(SIZE(variant.products), SIZE(inst.products)); ++i) {
if (is_dummy(inst.products.at(i))) continue;
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();
return false;
}
}
return true;
}
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);
}
2017-03-12 19:42:33 +00:00
// phase 4
vector<recipe_ordinal> matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
vector<recipe_ordinal> result;
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(variants); ++i) {
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) {
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();
return false;
}
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) {
if (is_dummy(inst.products.at(i))) continue;
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();
return false;
}
}
return true;
}
// tie-breaker for each phase
const recipe& best_variant(const instruction& inst, vector<recipe_ordinal>& candidates) {
assert(!candidates.empty());
if (SIZE(candidates) == 1) return get(Recipe, candidates.at(0));
int min_score = 999;
int min_index = 0;
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(candidates); ++i) {
const recipe& candidate = get(Recipe, candidates.at(i));
// prefer functions without extra or missing ingredients or products
int score = abs(SIZE(candidate.products)-SIZE(inst.products))
+ abs(SIZE(candidate.ingredients)-SIZE(inst.ingredients));
// 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)))
++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)))
++score;
}
assert(score < 999);
if (score < min_score) {
min_score = score;
min_index = i;
}
}
return get(Recipe, candidates.at(min_index));
}
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)
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);
int index = c.running_step_index;
2016-10-20 05:10:35 +00:00
for (++index; index < SIZE(specializer_recipe.steps); ++index) {
const instruction& inst = specializer_recipe.steps.at(index);
if (inst.name == "stash") {
*stash_inst = inst;
return true;
}
2015-10-30 04:23:48 +00:00
}
return false;
2015-10-30 04:23:48 +00:00
}
:(scenario static_dispatch_disabled_in_recipe_without_variants)
def main [
2016-09-17 17:28:25 +00:00
1:num <- test 3
]
def test [
2016-09-17 17:28:25 +00:00
2:num <- next-ingredient # ensure no header
return 34
]
+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
]
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;
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
:(scenario static_dispatch_on_primitive_names)
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
]
# temporarily hardcode number equality to always fail
2016-09-17 17:28:25 +00:00
def equal x:num, y:num -> z:bool [
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
:(scenario static_dispatch_works_with_dummy_results_for_containers)
def main [
_ <- test 3, 4
]
2016-09-17 17:28:25 +00:00
def test a:num -> z:point [
local-scope
load-ingredients
z <- merge a, 0
]
2016-09-17 17:28:25 +00:00
def test a:num, b:num -> z:point [
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)
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)
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
:(scenario static_dispatch_prefers_literals_to_be_numbers_rather_than_addresses)
def main [
2016-09-17 17:28:25 +00:00
1:num <- foo 0
]
2016-09-17 19:55:10 +00:00
def foo x:&:num -> y:num [
return 34
]
2016-09-17 17:28:25 +00:00
def foo x:num -> y:num [
return 35
]
+mem: storing 35 in location 1
:(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
:(scenario static_dispatch_on_non_literal_character_ignores_variant_with_numbers)
% Hide_errors = true;
def main [
local-scope
x:char <- copy 10/newline
2016-09-17 17:28:25 +00:00
1:num/raw <- foo x
]
2016-09-17 17:28:25 +00:00
def foo x:num -> y:num [
load-ingredients
return 34
]
2016-09-17 17:28:25 +00:00
+error: main: ingredient 0 has the wrong type at '1:num/raw <- foo x'
-mem: storing 34 in location 1
:(scenario static_dispatch_dispatches_literal_to_boolean_before_character)
def main [
2016-09-17 17:28:25 +00:00
1:num/raw <- foo 0 # valid literal for boolean
]
2016-09-17 17:28:25 +00:00
def foo x:char -> y:num [
local-scope
load-ingredients
return 34
]
2016-09-17 17:28:25 +00:00
def foo x:bool -> y:num [
local-scope
load-ingredients
return 35
]
# boolean variant is preferred
+mem: storing 35 in location 1
:(scenario static_dispatch_dispatches_literal_to_character_when_out_of_boolean_range)
def main [
2016-09-17 17:28:25 +00:00
1:num/raw <- foo 97 # not a valid literal for boolean
]
2016-09-17 17:28:25 +00:00
def foo x:char -> y:num [
local-scope
load-ingredients
return 34
]
2016-09-17 17:28:25 +00:00
def foo x:bool -> y:num [
local-scope
load-ingredients
return 35
]
# character variant is preferred
+mem: storing 34 in location 1
:(scenario static_dispatch_dispatches_literal_to_number_if_at_all_possible)
def main [
2016-09-17 17:28:25 +00:00
1:num/raw <- foo 97
]
2016-09-17 17:28:25 +00:00
def foo x:char -> y:num [
local-scope
load-ingredients
return 34
]
2016-09-17 17:28:25 +00:00
def foo x:num -> y:num [
local-scope
load-ingredients
return 35
]
# number variant is preferred
+mem: storing 35 in location 1
:(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));
}
:(code)
2016-05-25 01:58:23 +00:00
string header_label(const recipe& caller) {
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)
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)
out << ' ' << to_string(caller.products.at(i));
return out.str();
}
string original_header_label(const recipe& caller) {
ostringstream out;
out << "recipe " << caller.original_name;
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();
}
:(scenario reload_variant_retains_other_variants)
def main [
2016-09-17 17:28:25 +00:00
1:num <- copy 34
2:num <- foo 1:num
]
2016-09-17 17:28:25 +00:00
def foo x:num -> y:num [
local-scope
load-ingredients
return 34
]
2016-09-17 19:55:10 +00:00
def foo x:&:num -> y:num [
local-scope
load-ingredients
return 35
]
2016-09-17 19:55:10 +00:00
def! foo x:&:num -> y:num [
local-scope
load-ingredients
return 36
]
+mem: storing 34 in location 2
$error: 0
:(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-09-17 17:28:25 +00:00
def foo a:num -> b:num [
local-scope
load-ingredients
return 34
]
2016-09-17 17:28:25 +00:00
def foo a:bool -> b:num [
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'
:(scenario override_methods_with_type_abbreviations)
def main [
2016-09-14 08:56:41 +00:00
local-scope
s:text <- new [abc]
2016-09-17 17:28:25 +00:00
1:num/raw <- foo s
]
2016-09-17 21:43:13 +00:00
def foo a:address:array:character -> result:number [
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 [
return 35
]
+mem: storing 35 in location 1
:(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
]
+error: main: types don't match in call for 'foo x'
+error: which tries to call 'recipe foo x:&:char'
:(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:
+error: recipe foo x:&:char
+error: recipe foo x:&:bool
:(before "End Includes")
using std::abs;