2015-10-31 17:54:48 +00:00
|
|
|
//:: Like container definitions, recipes too can contain type parameters.
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario shape_shifting_recipe)
|
2015-10-31 17:54:48 +00:00
|
|
|
recipe main [
|
|
|
|
10:point <- merge 14, 15
|
|
|
|
11:point <- foo 10:point
|
|
|
|
]
|
|
|
|
# non-matching variant
|
|
|
|
recipe foo a:number -> result:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
result <- copy 34
|
|
|
|
]
|
2015-11-11 04:24:03 +00:00
|
|
|
# matching shape-shifting variant
|
2015-10-31 17:54:48 +00:00
|
|
|
recipe foo a:_t -> result:_t [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
result <- copy a
|
|
|
|
]
|
|
|
|
+mem: storing 14 in location 11
|
|
|
|
+mem: storing 15 in location 12
|
|
|
|
|
2015-11-15 06:59:09 +00:00
|
|
|
//: Before anything else, disable transforms for shape-shifting recipes and
|
|
|
|
//: make sure we never try to actually run a shape-shifting recipe. We should
|
|
|
|
//: be rewriting such instructions to *specializations* with the type
|
|
|
|
//: ingredients filled in.
|
2015-11-05 07:44:46 +00:00
|
|
|
|
2015-11-08 06:56:06 +00:00
|
|
|
:(before "End Transform Checks")
|
|
|
|
if (any_type_ingredient_in_header(/*recipe_ordinal*/p->first)) continue;
|
2015-11-05 07:44:46 +00:00
|
|
|
|
2015-11-15 06:59:09 +00:00
|
|
|
:(after "Running One Instruction")
|
|
|
|
if (Current_routine->calls.front().running_step_index == 0
|
|
|
|
&& any_type_ingredient_in_header(Current_routine->calls.front().running_recipe)) {
|
2015-11-15 20:35:59 +00:00
|
|
|
//? DUMP("");
|
2015-11-15 06:59:09 +00:00
|
|
|
raise_error << "ran into unspecialized shape-shifting recipe " << current_recipe_name() << '\n' << end();
|
|
|
|
}
|
|
|
|
|
2015-11-15 08:37:29 +00:00
|
|
|
//: Make sure we don't match up literals with type ingredients without
|
|
|
|
//: specialization.
|
2016-01-31 06:22:52 +00:00
|
|
|
:(before "End Matching Types For Literal(to)")
|
|
|
|
if (contains_type_ingredient_name(to)) return false;
|
2015-11-15 08:37:29 +00:00
|
|
|
|
2015-11-07 21:33:26 +00:00
|
|
|
//: We'll be creating recipes without loading them from anywhere by
|
2015-11-20 07:44:11 +00:00
|
|
|
//: *specializing* existing recipes.
|
|
|
|
//:
|
|
|
|
//: Keep track of these new recipes in a separate variable in addition to
|
2015-12-14 04:01:42 +00:00
|
|
|
//: Recently_added_recipes, so that edit/ can clear them before reloading to
|
2015-11-20 07:44:11 +00:00
|
|
|
//: regenerate errors.
|
|
|
|
:(before "End Globals")
|
2015-12-14 04:01:42 +00:00
|
|
|
vector<recipe_ordinal> Recently_added_shape_shifting_recipes;
|
2015-11-20 07:44:11 +00:00
|
|
|
:(before "End Setup")
|
|
|
|
//? cerr << "setup: clearing recently-added shape-shifting recipes\n";
|
2015-12-14 04:01:42 +00:00
|
|
|
Recently_added_shape_shifting_recipes.clear();
|
2015-11-20 07:44:11 +00:00
|
|
|
|
|
|
|
//: make sure we don't clear any of these recipes when we start running tests
|
2015-11-07 21:33:26 +00:00
|
|
|
:(before "End Loading .mu Files")
|
2015-12-14 04:01:42 +00:00
|
|
|
Recently_added_recipes.clear();
|
|
|
|
Recently_added_types.clear();
|
2015-11-20 07:44:11 +00:00
|
|
|
//? cerr << "clearing recently-added shape-shifting recipes\n";
|
2015-12-14 04:01:42 +00:00
|
|
|
Recently_added_shape_shifting_recipes.clear();
|
2015-11-07 21:33:26 +00:00
|
|
|
|
2015-11-28 02:01:55 +00:00
|
|
|
//: save original name of specialized recipes
|
|
|
|
:(before "End recipe Fields")
|
|
|
|
string original_name;
|
|
|
|
//: original name is only set during load
|
2016-02-12 01:42:20 +00:00
|
|
|
:(before "End Load Recipe Name")
|
2015-11-28 02:01:55 +00:00
|
|
|
result.original_name = result.name;
|
|
|
|
|
2016-02-12 00:13:10 +00:00
|
|
|
:(after "Static Dispatch Phase 2")
|
|
|
|
candidates = strictly_matching_shape_shifting_variants(inst, variants);
|
|
|
|
if (!candidates.empty()) {
|
|
|
|
recipe_ordinal exemplar = best_shape_shifting_variant(inst, candidates);
|
|
|
|
trace(9992, "transform") << "found variant to specialize: " << exemplar << ' ' << get(Recipe, exemplar).name << end();
|
|
|
|
recipe_ordinal new_recipe_ordinal = new_variant(exemplar, inst, caller_recipe);
|
|
|
|
if (new_recipe_ordinal == 0) goto skip_shape_shifting_variants;
|
|
|
|
variants.push_back(new_recipe_ordinal); // side-effect
|
|
|
|
recipe& variant = get(Recipe, new_recipe_ordinal);
|
|
|
|
// perform all transforms on the new specialization
|
|
|
|
if (!variant.steps.empty()) {
|
|
|
|
trace(9992, "transform") << "transforming new specialization: " << variant.name << end();
|
|
|
|
for (long long int t = 0; t < SIZE(Transform); ++t) {
|
|
|
|
(*Transform.at(t))(new_recipe_ordinal);
|
2015-11-28 01:54:34 +00:00
|
|
|
}
|
2015-10-31 17:54:48 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
variant.transformed_until = SIZE(Transform)-1;
|
|
|
|
trace(9992, "transform") << "new specialization: " << variant.name << end();
|
|
|
|
return variant.name;
|
2015-10-31 17:54:48 +00:00
|
|
|
}
|
2016-02-12 00:13:10 +00:00
|
|
|
skip_shape_shifting_variants:;
|
2015-10-31 17:54:48 +00:00
|
|
|
|
2015-11-19 07:55:54 +00:00
|
|
|
//: make sure we have no unspecialized shape-shifting recipes being called
|
|
|
|
//: before running mu programs
|
|
|
|
|
|
|
|
:(before "End Instruction Operation Checks")
|
2015-12-14 09:30:56 +00:00
|
|
|
if (contains_key(Recipe, inst.operation) && inst.operation >= MAX_PRIMITIVE_RECIPES
|
2015-11-19 07:55:54 +00:00
|
|
|
&& any_type_ingredient_in_header(inst.operation)) {
|
|
|
|
raise_error << maybe(caller.name) << "instruction " << inst.name << " has no valid specialization\n" << end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-31 17:54:48 +00:00
|
|
|
:(code)
|
2016-02-12 00:13:10 +00:00
|
|
|
// phase 2 of static dispatch
|
|
|
|
vector<recipe_ordinal> strictly_matching_shape_shifting_variants(const instruction& inst, vector<recipe_ordinal>& variants) {
|
|
|
|
vector<recipe_ordinal> result;
|
|
|
|
for (long long int i = 0; i < SIZE(variants); ++i) {
|
|
|
|
if (variants.at(i) == -1) continue;
|
|
|
|
if (!any_type_ingredient_in_header(variants.at(i))) continue;
|
|
|
|
if (all_concrete_header_reagents_strictly_match(inst, get(Recipe, variants.at(i))))
|
|
|
|
result.push_back(variants.at(i));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool all_concrete_header_reagents_strictly_match(const instruction& inst, const recipe& variant) {
|
|
|
|
if (SIZE(inst.ingredients) < SIZE(variant.ingredients)) {
|
|
|
|
trace(9993, "transform") << "too few ingredients" << end();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (SIZE(variant.products) < SIZE(inst.products)) {
|
|
|
|
trace(9993, "transform") << "too few products" << end();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (long long int i = 0; i < SIZE(variant.ingredients); ++i) {
|
|
|
|
if (!concrete_types_strictly_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
|
|
|
|
trace(9993, "transform") << "concrete-type match failed: ingredient " << i << end();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (long long int i = 0; i < SIZE(inst.products); ++i) {
|
|
|
|
if (is_dummy(inst.products.at(i))) continue;
|
|
|
|
if (!concrete_types_strictly_match(variant.products.at(i), inst.products.at(i))) {
|
|
|
|
trace(9993, "transform") << "strict match failed: product " << i << end();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tie-breaker for phase 2
|
|
|
|
recipe_ordinal best_shape_shifting_variant(const instruction& inst, vector<recipe_ordinal>& candidates) {
|
|
|
|
assert(!candidates.empty());
|
|
|
|
// primary score
|
|
|
|
long long int max_score = -1;
|
|
|
|
for (long long int i = 0; i < SIZE(candidates); ++i) {
|
|
|
|
long long int score = number_of_concrete_types(candidates.at(i));
|
|
|
|
assert(score > -1);
|
|
|
|
if (score > max_score) max_score = score;
|
|
|
|
}
|
|
|
|
// break any ties at max_score by a secondary score
|
|
|
|
long long int min_score2 = 999;
|
|
|
|
long long int best_index = 0;
|
|
|
|
for (long long int i = 0; i < SIZE(candidates); ++i) {
|
|
|
|
long long int score1 = number_of_concrete_types(candidates.at(i));
|
|
|
|
assert(score1 <= max_score);
|
|
|
|
if (score1 != max_score) continue;
|
|
|
|
const recipe& candidate = get(Recipe, candidates.at(i));
|
|
|
|
long long int score2 = (SIZE(candidate.products)-SIZE(inst.products))
|
|
|
|
+ (SIZE(inst.ingredients)-SIZE(candidate.ingredients));
|
|
|
|
assert(score2 < 999);
|
|
|
|
if (score2 < min_score2) {
|
|
|
|
min_score2 = score2;
|
|
|
|
best_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return candidates.at(best_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-14 09:30:56 +00:00
|
|
|
string header(const recipe& caller) {
|
|
|
|
if (!caller.has_header) return maybe(caller.name);
|
|
|
|
ostringstream out;
|
|
|
|
out << caller.name;
|
|
|
|
for (long long int i = 0; i < SIZE(caller.ingredients); ++i) {
|
|
|
|
if (i > 0) out << ',';
|
|
|
|
out << ' ' << debug_string(caller.ingredients.at(i));
|
|
|
|
}
|
|
|
|
if (!caller.products.empty()) {
|
|
|
|
out << " ->";
|
|
|
|
for (long long int i = 0; i < SIZE(caller.products); ++i) {
|
|
|
|
if (i > 0) out << ',';
|
|
|
|
out << ' ' << debug_string(caller.products.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out << ": ";
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2015-10-31 17:54:48 +00:00
|
|
|
bool any_type_ingredient_in_header(recipe_ordinal variant) {
|
2015-11-28 08:34:34 +00:00
|
|
|
const recipe& caller = get(Recipe, variant);
|
|
|
|
for (long long int i = 0; i < SIZE(caller.ingredients); ++i) {
|
|
|
|
if (contains_type_ingredient_name(caller.ingredients.at(i)))
|
2015-10-31 17:54:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-28 08:34:34 +00:00
|
|
|
for (long long int i = 0; i < SIZE(caller.products); ++i) {
|
|
|
|
if (contains_type_ingredient_name(caller.products.at(i)))
|
2015-11-10 05:31:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-31 17:54:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-12 00:13:10 +00:00
|
|
|
bool concrete_types_strictly_match(reagent to, reagent from) {
|
2016-01-31 06:22:52 +00:00
|
|
|
canonize_type(to);
|
|
|
|
canonize_type(from);
|
2016-02-12 00:13:10 +00:00
|
|
|
return concrete_types_strictly_match(to.properties.at(0).second, from.properties.at(0).second, from);
|
2015-11-15 20:35:59 +00:00
|
|
|
}
|
|
|
|
|
2015-11-28 08:34:34 +00:00
|
|
|
long long int number_of_concrete_types(recipe_ordinal r) {
|
|
|
|
const recipe& caller = get(Recipe, r);
|
|
|
|
long long int result = 0;
|
|
|
|
for (long long int i = 0; i < SIZE(caller.ingredients); ++i)
|
|
|
|
result += number_of_concrete_types(caller.ingredients.at(i));
|
|
|
|
for (long long int i = 0; i < SIZE(caller.products); ++i)
|
|
|
|
result += number_of_concrete_types(caller.products.at(i));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long int number_of_concrete_types(const reagent& r) {
|
|
|
|
return number_of_concrete_types(r.properties.at(0).second);
|
|
|
|
}
|
|
|
|
|
|
|
|
long long int number_of_concrete_types(const string_tree* type) {
|
|
|
|
if (!type) return 0;
|
|
|
|
long long int result = 0;
|
|
|
|
if (!type->value.empty() && !is_type_ingredient_name(type->value))
|
|
|
|
result++;
|
|
|
|
result += number_of_concrete_types(type->left);
|
|
|
|
result += number_of_concrete_types(type->right);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-02-12 00:13:10 +00:00
|
|
|
bool concrete_types_strictly_match(const string_tree* to, const string_tree* from, const reagent& rhs_reagent) {
|
2016-01-31 06:22:52 +00:00
|
|
|
if (!to) return !from;
|
|
|
|
if (!from) return !to;
|
|
|
|
if (is_type_ingredient_name(to->value)) return true; // type ingredient matches anything
|
|
|
|
if (to->value == "literal" && from->value == "literal")
|
2015-11-28 05:38:09 +00:00
|
|
|
return true;
|
2016-01-31 06:22:52 +00:00
|
|
|
if (to->value == "literal"
|
|
|
|
&& Literal_type_names.find(from->value) != Literal_type_names.end())
|
2015-11-28 05:38:09 +00:00
|
|
|
return true;
|
2016-01-31 06:22:52 +00:00
|
|
|
if (from->value == "literal"
|
|
|
|
&& Literal_type_names.find(to->value) != Literal_type_names.end())
|
2015-11-28 05:38:09 +00:00
|
|
|
return true;
|
2016-01-31 06:22:52 +00:00
|
|
|
if (from->value == "literal" && to->value == "address")
|
2015-11-15 20:35:59 +00:00
|
|
|
return rhs_reagent.name == "0";
|
2016-01-31 06:22:52 +00:00
|
|
|
//? cerr << to->value << " vs " << from->value << '\n';
|
|
|
|
return to->value == from->value
|
2016-02-12 00:13:10 +00:00
|
|
|
&& concrete_types_strictly_match(to->left, from->left, rhs_reagent)
|
|
|
|
&& concrete_types_strictly_match(to->right, from->right, rhs_reagent);
|
2015-10-31 17:54:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 03:42:49 +00:00
|
|
|
bool contains_type_ingredient_name(const reagent& x) {
|
|
|
|
return contains_type_ingredient_name(x.properties.at(0).second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains_type_ingredient_name(const string_tree* type) {
|
|
|
|
if (!type) return false;
|
|
|
|
if (is_type_ingredient_name(type->value)) return true;
|
|
|
|
return contains_type_ingredient_name(type->left) || contains_type_ingredient_name(type->right);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_type_ingredient_name(const string& type) {
|
|
|
|
return !type.empty() && type.at(0) == '_';
|
|
|
|
}
|
|
|
|
|
2015-11-11 03:56:41 +00:00
|
|
|
recipe_ordinal new_variant(recipe_ordinal exemplar, const instruction& inst, const recipe& caller_recipe) {
|
2015-10-31 17:54:48 +00:00
|
|
|
string new_name = next_unused_recipe_name(inst.name);
|
2015-11-06 21:22:16 +00:00
|
|
|
assert(!contains_key(Recipe_ordinal, new_name));
|
2015-11-08 06:56:06 +00:00
|
|
|
recipe_ordinal new_recipe_ordinal = put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
|
2015-10-31 17:54:48 +00:00
|
|
|
// make a copy
|
2015-11-06 21:22:16 +00:00
|
|
|
assert(contains_key(Recipe, exemplar));
|
2015-11-08 06:56:06 +00:00
|
|
|
assert(!contains_key(Recipe, new_recipe_ordinal));
|
2015-12-14 04:01:42 +00:00
|
|
|
Recently_added_recipes.push_back(new_recipe_ordinal);
|
|
|
|
Recently_added_shape_shifting_recipes.push_back(new_recipe_ordinal);
|
2015-11-08 06:56:06 +00:00
|
|
|
put(Recipe, new_recipe_ordinal, get(Recipe, exemplar));
|
|
|
|
recipe& new_recipe = get(Recipe, new_recipe_ordinal);
|
2015-11-28 02:01:55 +00:00
|
|
|
new_recipe.name = new_name;
|
2016-02-12 02:12:57 +00:00
|
|
|
trace(9993, "transform") << "switching " << inst.name << " to specialized " << header_label(new_recipe_ordinal) << end();
|
2015-11-08 06:56:06 +00:00
|
|
|
// Since the exemplar never ran any transforms, we have to redo some of the
|
|
|
|
// work of the check_types_by_name transform while supporting type-ingredients.
|
|
|
|
compute_type_names(new_recipe);
|
|
|
|
// that gives enough information to replace type-ingredients with concrete types
|
2015-11-10 07:02:23 +00:00
|
|
|
{
|
|
|
|
map<string, const string_tree*> mappings;
|
2015-11-11 03:56:41 +00:00
|
|
|
bool error = false;
|
|
|
|
compute_type_ingredient_mappings(get(Recipe, exemplar), inst, mappings, caller_recipe, &error);
|
|
|
|
if (!error) replace_type_ingredients(new_recipe, mappings);
|
2015-11-10 07:02:23 +00:00
|
|
|
for (map<string, const string_tree*>::iterator p = mappings.begin(); p != mappings.end(); ++p)
|
|
|
|
delete p->second;
|
2015-12-28 21:02:06 +00:00
|
|
|
if (error) return 0; // todo: delete new_recipe_ordinal from Recipes and other global state
|
2015-11-10 07:02:23 +00:00
|
|
|
}
|
2015-11-15 09:16:51 +00:00
|
|
|
ensure_all_concrete_types(new_recipe, get(Recipe, exemplar));
|
2015-11-08 06:56:06 +00:00
|
|
|
return new_recipe_ordinal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void compute_type_names(recipe& variant) {
|
2015-11-08 21:58:40 +00:00
|
|
|
trace(9993, "transform") << "compute type names: " << variant.name << end();
|
2015-11-08 06:56:06 +00:00
|
|
|
map<string, string_tree*> type_names;
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int i = 0; i < SIZE(variant.ingredients); ++i)
|
2015-11-17 18:47:11 +00:00
|
|
|
save_or_deduce_type_name(variant.ingredients.at(i), type_names, variant);
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int i = 0; i < SIZE(variant.products); ++i)
|
2015-11-17 18:47:11 +00:00
|
|
|
save_or_deduce_type_name(variant.products.at(i), type_names, variant);
|
2015-11-08 06:56:06 +00:00
|
|
|
for (long long int i = 0; i < SIZE(variant.steps); ++i) {
|
|
|
|
instruction& inst = variant.steps.at(i);
|
2015-11-08 21:58:40 +00:00
|
|
|
trace(9993, "transform") << " instruction: " << inst.to_string() << end();
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int in = 0; in < SIZE(inst.ingredients); ++in)
|
2015-11-17 18:47:11 +00:00
|
|
|
save_or_deduce_type_name(inst.ingredients.at(in), type_names, variant);
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int out = 0; out < SIZE(inst.products); ++out)
|
2015-11-17 18:47:11 +00:00
|
|
|
save_or_deduce_type_name(inst.products.at(out), type_names, variant);
|
2015-11-08 06:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 18:47:11 +00:00
|
|
|
void save_or_deduce_type_name(reagent& x, map<string, string_tree*>& type_name, const recipe& variant) {
|
2015-11-08 22:46:48 +00:00
|
|
|
trace(9994, "transform") << " checking " << x.to_string() << ": " << debug_string(x.properties.at(0).second) << end();
|
2015-11-08 06:56:06 +00:00
|
|
|
if (!x.properties.at(0).second && contains_key(type_name, x.name)) {
|
|
|
|
x.properties.at(0).second = new string_tree(*get(type_name, x.name));
|
2015-11-08 22:46:48 +00:00
|
|
|
trace(9994, "transform") << " deducing type to " << debug_string(x.properties.at(0).second) << end();
|
2015-11-08 06:56:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!x.properties.at(0).second) {
|
2015-11-28 02:01:55 +00:00
|
|
|
raise_error << maybe(variant.original_name) << "unknown type for " << x.original_string << " (check the name for typos)\n" << end();
|
2015-11-08 06:56:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (contains_key(type_name, x.name)) return;
|
|
|
|
if (x.properties.at(0).second->value == "offset" || x.properties.at(0).second->value == "variant") return; // special-case for container-access instructions
|
|
|
|
put(type_name, x.name, x.properties.at(0).second);
|
2015-11-08 22:46:48 +00:00
|
|
|
trace(9993, "transform") << "type of " << x.name << " is " << debug_string(x.properties.at(0).second) << end();
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 03:56:41 +00:00
|
|
|
void compute_type_ingredient_mappings(const recipe& exemplar, const instruction& inst, map<string, const string_tree*>& mappings, const recipe& caller_recipe, bool* error) {
|
2015-11-15 08:37:29 +00:00
|
|
|
long long int limit = min(SIZE(inst.ingredients), SIZE(exemplar.ingredients));
|
|
|
|
for (long long int i = 0; i < limit; ++i) {
|
2015-11-11 04:14:50 +00:00
|
|
|
const reagent& exemplar_reagent = exemplar.ingredients.at(i);
|
2015-11-07 21:33:26 +00:00
|
|
|
reagent ingredient = inst.ingredients.at(i);
|
2015-11-08 06:56:06 +00:00
|
|
|
assert(ingredient.properties.at(0).second);
|
2015-11-07 21:33:26 +00:00
|
|
|
canonize_type(ingredient);
|
2015-11-15 09:02:53 +00:00
|
|
|
if (is_mu_address(exemplar_reagent) && ingredient.name == "0") continue; // assume it matches
|
2015-11-11 04:14:50 +00:00
|
|
|
accumulate_type_ingredients(exemplar_reagent, ingredient, mappings, exemplar, inst, caller_recipe, error);
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
2015-11-15 08:37:29 +00:00
|
|
|
limit = min(SIZE(inst.products), SIZE(exemplar.products));
|
|
|
|
for (long long int i = 0; i < limit; ++i) {
|
2015-11-11 04:14:50 +00:00
|
|
|
const reagent& exemplar_reagent = exemplar.products.at(i);
|
2015-11-07 21:33:26 +00:00
|
|
|
reagent product = inst.products.at(i);
|
2015-11-08 06:56:06 +00:00
|
|
|
assert(product.properties.at(0).second);
|
2015-11-07 21:33:26 +00:00
|
|
|
canonize_type(product);
|
2015-11-11 04:14:50 +00:00
|
|
|
accumulate_type_ingredients(exemplar_reagent, product, mappings, exemplar, inst, caller_recipe, error);
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 08:37:29 +00:00
|
|
|
inline long long int min(long long int a, long long int b) {
|
|
|
|
return (a < b) ? a : b;
|
|
|
|
}
|
|
|
|
|
2015-11-11 04:14:50 +00:00
|
|
|
void accumulate_type_ingredients(const reagent& exemplar_reagent, reagent& refinement, map<string, const string_tree*>& mappings, const recipe& exemplar, const instruction& call_instruction, const recipe& caller_recipe, bool* error) {
|
2015-11-05 08:36:02 +00:00
|
|
|
assert(refinement.properties.at(0).second);
|
2015-11-11 04:14:50 +00:00
|
|
|
accumulate_type_ingredients(exemplar_reagent.properties.at(0).second, refinement.properties.at(0).second, mappings, exemplar, exemplar_reagent, call_instruction, caller_recipe, error);
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 04:14:50 +00:00
|
|
|
void accumulate_type_ingredients(const string_tree* exemplar_type, const string_tree* refinement_type, map<string, const string_tree*>& mappings, const recipe& exemplar, const reagent& exemplar_reagent, const instruction& call_instruction, const recipe& caller_recipe, bool* error) {
|
|
|
|
if (!exemplar_type) return;
|
|
|
|
if (!refinement_type) {
|
2015-11-16 01:34:13 +00:00
|
|
|
// todo: make this smarter; only warn if exemplar_type contains some *new* type ingredient
|
2015-11-11 04:14:50 +00:00
|
|
|
raise_error << maybe(exemplar.name) << "missing type ingredient in " << exemplar_reagent.original_string << '\n' << end();
|
2015-11-05 08:57:23 +00:00
|
|
|
return;
|
2015-11-05 08:36:02 +00:00
|
|
|
}
|
2015-11-11 04:14:50 +00:00
|
|
|
if (!exemplar_type->value.empty() && exemplar_type->value.at(0) == '_') {
|
|
|
|
assert(!refinement_type->value.empty());
|
|
|
|
if (exemplar_type->right) {
|
2015-11-08 08:10:56 +00:00
|
|
|
raise_error << "type_ingredients in non-last position not currently supported\n" << end();
|
|
|
|
return;
|
|
|
|
}
|
2015-11-11 04:14:50 +00:00
|
|
|
if (!contains_key(mappings, exemplar_type->value)) {
|
|
|
|
trace(9993, "transform") << "adding mapping from " << exemplar_type->value << " to " << debug_string(refinement_type) << end();
|
2015-11-28 05:38:09 +00:00
|
|
|
put(mappings, exemplar_type->value, new string_tree(*refinement_type));
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-11-15 08:37:29 +00:00
|
|
|
if (!deeply_equal_types(get(mappings, exemplar_type->value), refinement_type)) {
|
2015-11-11 03:56:41 +00:00
|
|
|
raise_error << maybe(caller_recipe.name) << "no call found for '" << call_instruction.to_string() << "'\n" << end();
|
2015-11-15 09:44:58 +00:00
|
|
|
//? cerr << exemplar_type->value << ": " << debug_string(get(mappings, exemplar_type->value)) << " vs " << debug_string(refinement_type) << '\n';
|
2015-11-11 03:56:41 +00:00
|
|
|
*error = true;
|
|
|
|
return;
|
|
|
|
}
|
2015-11-28 05:38:09 +00:00
|
|
|
//? cerr << exemplar_type->value << ": " << debug_string(get(mappings, exemplar_type->value)) << " <= " << debug_string(refinement_type) << '\n';
|
|
|
|
if (get(mappings, exemplar_type->value)->value == "literal") {
|
|
|
|
delete get(mappings, exemplar_type->value);
|
|
|
|
put(mappings, exemplar_type->value, new string_tree(*refinement_type));
|
|
|
|
}
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-11-11 04:14:50 +00:00
|
|
|
accumulate_type_ingredients(exemplar_type->left, refinement_type->left, mappings, exemplar, exemplar_reagent, call_instruction, caller_recipe, error);
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
2015-11-11 04:14:50 +00:00
|
|
|
accumulate_type_ingredients(exemplar_type->right, refinement_type->right, mappings, exemplar, exemplar_reagent, call_instruction, caller_recipe, error);
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 08:10:56 +00:00
|
|
|
void replace_type_ingredients(recipe& new_recipe, const map<string, const string_tree*>& mappings) {
|
2015-10-31 17:54:48 +00:00
|
|
|
// update its header
|
2015-11-02 03:42:49 +00:00
|
|
|
if (mappings.empty()) return;
|
|
|
|
trace(9993, "transform") << "replacing in recipe header ingredients" << end();
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int i = 0; i < SIZE(new_recipe.ingredients); ++i)
|
2015-11-17 18:47:11 +00:00
|
|
|
replace_type_ingredients(new_recipe.ingredients.at(i), mappings, new_recipe);
|
2015-11-02 03:42:49 +00:00
|
|
|
trace(9993, "transform") << "replacing in recipe header products" << end();
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int i = 0; i < SIZE(new_recipe.products); ++i)
|
2015-11-17 18:47:11 +00:00
|
|
|
replace_type_ingredients(new_recipe.products.at(i), mappings, new_recipe);
|
2015-10-31 17:54:48 +00:00
|
|
|
// update its body
|
|
|
|
for (long long int i = 0; i < SIZE(new_recipe.steps); ++i) {
|
|
|
|
instruction& inst = new_recipe.steps.at(i);
|
2015-11-02 03:42:49 +00:00
|
|
|
trace(9993, "transform") << "replacing in instruction '" << inst.to_string() << "'" << end();
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int j = 0; j < SIZE(inst.ingredients); ++j)
|
2015-11-17 18:47:11 +00:00
|
|
|
replace_type_ingredients(inst.ingredients.at(j), mappings, new_recipe);
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int j = 0; j < SIZE(inst.products); ++j)
|
2015-11-17 18:47:11 +00:00
|
|
|
replace_type_ingredients(inst.products.at(j), mappings, new_recipe);
|
2015-11-08 06:56:06 +00:00
|
|
|
// special-case for new: replace type ingredient in first ingredient *value*
|
2015-11-28 05:38:09 +00:00
|
|
|
if (inst.name == "new" && inst.ingredients.at(0).properties.at(0).second->value != "literal-string") {
|
2015-11-08 06:56:06 +00:00
|
|
|
string_tree* type_name = parse_string_tree(inst.ingredients.at(0).name);
|
|
|
|
replace_type_ingredients(type_name, mappings);
|
2015-11-09 06:42:21 +00:00
|
|
|
inst.ingredients.at(0).name = type_name->to_string();
|
2015-11-08 06:56:06 +00:00
|
|
|
delete type_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 18:47:11 +00:00
|
|
|
void replace_type_ingredients(reagent& x, const map<string, const string_tree*>& mappings, const recipe& caller) {
|
2015-11-02 03:42:49 +00:00
|
|
|
trace(9993, "transform") << "replacing in ingredient " << x.original_string << end();
|
|
|
|
// replace properties
|
2015-11-17 18:47:11 +00:00
|
|
|
if (!x.properties.at(0).second) {
|
2015-11-28 02:01:55 +00:00
|
|
|
raise_error << "specializing " << caller.original_name << ": missing type for " << x.original_string << '\n' << end();
|
2015-11-17 18:47:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-02 03:42:49 +00:00
|
|
|
replace_type_ingredients(x.properties.at(0).second, mappings);
|
|
|
|
// refresh types from properties
|
|
|
|
delete x.type;
|
|
|
|
x.type = new_type_tree(x.properties.at(0).second);
|
|
|
|
if (x.type)
|
2015-11-08 22:46:48 +00:00
|
|
|
trace(9993, "transform") << " after: " << debug_string(x.type) << end();
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 08:10:56 +00:00
|
|
|
void replace_type_ingredients(string_tree* type, const map<string, const string_tree*>& mappings) {
|
2015-11-02 03:42:49 +00:00
|
|
|
if (!type) return;
|
2015-11-06 21:22:16 +00:00
|
|
|
if (is_type_ingredient_name(type->value) && contains_key(mappings, type->value)) {
|
2015-11-08 08:10:56 +00:00
|
|
|
const string_tree* replacement = get(mappings, type->value);
|
2015-11-08 22:46:48 +00:00
|
|
|
trace(9993, "transform") << type->value << " => " << debug_string(replacement) << end();
|
2015-11-28 05:38:09 +00:00
|
|
|
if (replacement->value == "literal")
|
|
|
|
type->value = "number";
|
|
|
|
else
|
|
|
|
type->value = replacement->value;
|
2015-11-08 08:10:56 +00:00
|
|
|
if (replacement->left) type->left = new string_tree(*replacement->left);
|
|
|
|
if (replacement->right) type->right = new string_tree(*replacement->right);
|
2015-11-02 03:42:49 +00:00
|
|
|
}
|
|
|
|
replace_type_ingredients(type->left, mappings);
|
|
|
|
replace_type_ingredients(type->right, mappings);
|
2015-10-31 17:54:48 +00:00
|
|
|
}
|
2015-11-01 00:18:59 +00:00
|
|
|
|
2015-11-15 09:16:51 +00:00
|
|
|
void ensure_all_concrete_types(/*const*/ recipe& new_recipe, const recipe& exemplar) {
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int i = 0; i < SIZE(new_recipe.ingredients); ++i)
|
2015-11-15 09:16:51 +00:00
|
|
|
ensure_all_concrete_types(new_recipe.ingredients.at(i), exemplar);
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int i = 0; i < SIZE(new_recipe.products); ++i)
|
2015-11-15 09:16:51 +00:00
|
|
|
ensure_all_concrete_types(new_recipe.products.at(i), exemplar);
|
2015-11-08 06:56:06 +00:00
|
|
|
for (long long int i = 0; i < SIZE(new_recipe.steps); ++i) {
|
2015-11-15 09:16:51 +00:00
|
|
|
instruction& inst = new_recipe.steps.at(i);
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int j = 0; j < SIZE(inst.ingredients); ++j)
|
2015-11-15 09:16:51 +00:00
|
|
|
ensure_all_concrete_types(inst.ingredients.at(j), exemplar);
|
2015-11-09 07:36:37 +00:00
|
|
|
for (long long int j = 0; j < SIZE(inst.products); ++j)
|
2015-11-15 09:16:51 +00:00
|
|
|
ensure_all_concrete_types(inst.products.at(j), exemplar);
|
2015-11-08 06:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 09:16:51 +00:00
|
|
|
void ensure_all_concrete_types(/*const*/ reagent& x, const recipe& exemplar) {
|
|
|
|
if (!x.type) {
|
|
|
|
raise_error << maybe(exemplar.name) << "failed to map a type to " << x.original_string << '\n' << end();
|
|
|
|
x.type = new type_tree(0); // just to prevent crashes later
|
2015-11-08 06:56:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-15 09:16:51 +00:00
|
|
|
if (x.type->value == -1) {
|
|
|
|
raise_error << maybe(exemplar.name) << "failed to map a type to the unknown " << x.original_string << '\n' << end();
|
2015-11-08 06:56:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario shape_shifting_recipe_2)
|
2015-11-01 00:18:59 +00:00
|
|
|
recipe main [
|
|
|
|
10:point <- merge 14, 15
|
|
|
|
11:point <- foo 10:point
|
|
|
|
]
|
2015-11-11 04:24:03 +00:00
|
|
|
# non-matching shape-shifting variant
|
2015-11-01 00:18:59 +00:00
|
|
|
recipe foo a:_t, b:_t -> result:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
result <- copy 34
|
|
|
|
]
|
2015-11-11 04:24:03 +00:00
|
|
|
# matching shape-shifting variant
|
2015-11-01 00:18:59 +00:00
|
|
|
recipe foo a:_t -> result:_t [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
result <- copy a
|
|
|
|
]
|
|
|
|
+mem: storing 14 in location 11
|
|
|
|
+mem: storing 15 in location 12
|
2015-11-02 03:42:49 +00:00
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario shape_shifting_recipe_nonroot)
|
2015-11-02 03:42:49 +00:00
|
|
|
recipe main [
|
|
|
|
10:foo:point <- merge 14, 15, 16
|
|
|
|
20:point/raw <- bar 10:foo:point
|
|
|
|
]
|
2015-11-11 04:24:03 +00:00
|
|
|
# shape-shifting recipe with type ingredient following some other type
|
2015-11-02 03:42:49 +00:00
|
|
|
recipe bar a:foo:_t -> result:_t [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
result <- get a, x:offset
|
|
|
|
]
|
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
+mem: storing 14 in location 20
|
|
|
|
+mem: storing 15 in location 21
|
2015-11-09 23:19:51 +00:00
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario shape_shifting_recipe_type_deduction_ignores_offsets)
|
2015-11-09 23:19:51 +00:00
|
|
|
recipe main [
|
|
|
|
10:foo:point <- merge 14, 15, 16
|
|
|
|
20:point/raw <- bar 10:foo:point
|
|
|
|
]
|
|
|
|
recipe bar a:foo:_t -> result:_t [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
x:number <- copy 1
|
|
|
|
result <- get a, x:offset # shouldn't collide with other variable
|
|
|
|
]
|
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
+mem: storing 14 in location 20
|
|
|
|
+mem: storing 15 in location 21
|
2015-11-10 05:31:21 +00:00
|
|
|
|
2016-01-31 06:30:08 +00:00
|
|
|
:(scenario shape_shifting_recipe_empty)
|
|
|
|
recipe main [
|
|
|
|
foo 1
|
|
|
|
]
|
|
|
|
# shape-shifting recipe with no body
|
|
|
|
recipe foo a:_t [
|
|
|
|
]
|
|
|
|
# shouldn't crash
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario shape_shifting_recipe_handles_shape_shifting_new_ingredient)
|
2015-11-10 05:31:21 +00:00
|
|
|
recipe main [
|
2016-01-20 07:18:03 +00:00
|
|
|
1:address:shared:foo:point <- bar 3
|
|
|
|
11:foo:point <- copy *1:address:shared:foo:point
|
2015-11-10 05:31:21 +00:00
|
|
|
]
|
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
2016-01-20 07:18:03 +00:00
|
|
|
recipe bar x:number -> result:address:shared:foo:_t [
|
2015-11-10 05:31:21 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
# new refers to _t in its ingredient *value*
|
|
|
|
result <- new {(foo _t) : type}
|
|
|
|
]
|
|
|
|
+mem: storing 0 in location 11
|
|
|
|
+mem: storing 0 in location 12
|
|
|
|
+mem: storing 0 in location 13
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario shape_shifting_recipe_handles_shape_shifting_new_ingredient_2)
|
2015-11-10 07:02:23 +00:00
|
|
|
recipe main [
|
2016-01-20 07:18:03 +00:00
|
|
|
1:address:shared:foo:point <- bar 3
|
|
|
|
11:foo:point <- copy *1:address:shared:foo:point
|
2015-11-10 07:02:23 +00:00
|
|
|
]
|
2016-01-20 07:18:03 +00:00
|
|
|
recipe bar x:number -> result:address:shared:foo:_t [
|
2015-11-10 07:02:23 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
# new refers to _t in its ingredient *value*
|
|
|
|
result <- new {(foo _t) : type}
|
|
|
|
]
|
|
|
|
# container defined after use
|
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
+mem: storing 0 in location 11
|
|
|
|
+mem: storing 0 in location 12
|
|
|
|
+mem: storing 0 in location 13
|
2015-11-10 07:26:15 +00:00
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario shape_shifting_recipe_supports_compound_types)
|
2015-11-10 07:26:15 +00:00
|
|
|
recipe main [
|
2016-01-20 07:18:03 +00:00
|
|
|
1:address:shared:point <- new point:type
|
|
|
|
2:address:number <- get-address *1:address:shared:point, y:offset
|
2015-11-10 07:26:15 +00:00
|
|
|
*2:address:number <- copy 34
|
2016-01-20 07:18:03 +00:00
|
|
|
3:address:shared:point <- bar 1:address:shared:point # specialize _t to address:shared:point
|
|
|
|
4:point <- copy *3:address:shared:point
|
2015-11-10 07:26:15 +00:00
|
|
|
]
|
|
|
|
recipe bar a:_t -> result:_t [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
result <- copy a
|
|
|
|
]
|
|
|
|
+mem: storing 34 in location 5
|
2015-11-11 03:56:41 +00:00
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario shape_shifting_recipe_error)
|
2015-11-11 03:56:41 +00:00
|
|
|
% Hide_errors = true;
|
|
|
|
recipe main [
|
|
|
|
a:number <- copy 3
|
2016-01-23 22:13:32 +00:00
|
|
|
b:address:shared:number <- foo a
|
2015-11-11 03:56:41 +00:00
|
|
|
]
|
|
|
|
recipe foo a:_t -> b:_t [
|
|
|
|
load-ingredients
|
|
|
|
b <- copy a
|
|
|
|
]
|
2016-01-23 22:13:32 +00:00
|
|
|
+error: main: no call found for 'b:address:shared:number <- foo a'
|
2015-11-15 06:59:09 +00:00
|
|
|
|
|
|
|
:(scenario specialize_inside_recipe_without_header)
|
|
|
|
recipe main [
|
|
|
|
foo 3
|
|
|
|
]
|
|
|
|
recipe foo [
|
|
|
|
local-scope
|
|
|
|
x:number <- next-ingredient # ensure no header
|
|
|
|
1:number/raw <- bar x # call a shape-shifting recipe
|
|
|
|
]
|
|
|
|
recipe bar x:_elem -> y:_elem [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
y <- add x, 1
|
|
|
|
]
|
|
|
|
+mem: storing 4 in location 1
|
2015-11-15 08:37:29 +00:00
|
|
|
|
|
|
|
:(scenario specialize_with_literal)
|
|
|
|
recipe main [
|
|
|
|
local-scope
|
|
|
|
# permit literal to map to number
|
|
|
|
1:number/raw <- foo 3
|
|
|
|
]
|
|
|
|
recipe foo x:_elem -> y:_elem [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
y <- add x, 1
|
|
|
|
]
|
|
|
|
+mem: storing 4 in location 1
|
|
|
|
|
|
|
|
:(scenario specialize_with_literal_2)
|
|
|
|
recipe main [
|
|
|
|
local-scope
|
|
|
|
# permit literal to map to character
|
|
|
|
1:character/raw <- foo 3
|
|
|
|
]
|
|
|
|
recipe foo x:_elem -> y:_elem [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
y <- add x, 1
|
|
|
|
]
|
|
|
|
+mem: storing 4 in location 1
|
2015-11-15 09:02:53 +00:00
|
|
|
|
|
|
|
:(scenario specialize_with_literal_3)
|
|
|
|
% Hide_errors = true;
|
|
|
|
recipe main [
|
|
|
|
local-scope
|
|
|
|
# permit '0' to map to address to shape-shifting type-ingredient
|
2016-01-23 22:13:32 +00:00
|
|
|
1:address:shared:character/raw <- foo 0
|
2015-11-15 09:02:53 +00:00
|
|
|
]
|
|
|
|
recipe foo x:address:_elem -> y:address:_elem [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
y <- copy x
|
|
|
|
]
|
|
|
|
+mem: storing 0 in location 1
|
|
|
|
$error: 0
|
2015-11-15 09:16:51 +00:00
|
|
|
|
|
|
|
:(scenario specialize_with_literal_4)
|
|
|
|
% Hide_errors = true;
|
|
|
|
recipe main [
|
|
|
|
local-scope
|
|
|
|
# ambiguous call: what's the type of its ingredient?!
|
|
|
|
foo 0
|
|
|
|
]
|
|
|
|
recipe foo x:address:_elem -> y:address:_elem [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
y <- copy x
|
|
|
|
]
|
2015-11-28 06:14:05 +00:00
|
|
|
+error: foo: failed to map a type to x
|
|
|
|
+error: foo: failed to map a type to y
|
2015-11-15 09:44:58 +00:00
|
|
|
|
|
|
|
:(scenario specialize_with_literal_5)
|
|
|
|
recipe main [
|
|
|
|
foo 3, 4 # recipe mapping two variables to literals
|
|
|
|
]
|
|
|
|
recipe foo x:_elem, y:_elem [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
1:number/raw <- add x, y
|
|
|
|
]
|
|
|
|
+mem: storing 7 in location 1
|
2015-11-15 20:35:59 +00:00
|
|
|
|
|
|
|
:(scenario multiple_shape_shifting_variants)
|
|
|
|
# try to call two different shape-shifting recipes with the same name
|
|
|
|
recipe main [
|
|
|
|
e1:d1:number <- merge 3
|
|
|
|
e2:d2:number <- merge 4, 5
|
|
|
|
1:number/raw <- foo e1
|
|
|
|
2:number/raw <- foo e2
|
|
|
|
]
|
|
|
|
# the two shape-shifting definitions
|
|
|
|
recipe foo a:d1:_elem -> b:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 34
|
|
|
|
]
|
|
|
|
recipe foo a:d2:_elem -> b:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 35
|
|
|
|
]
|
|
|
|
# the shape-shifting containers they use
|
|
|
|
container d1:_elem [
|
|
|
|
x:_elem
|
|
|
|
]
|
|
|
|
container d2:_elem [
|
|
|
|
x:number
|
|
|
|
y:_elem
|
|
|
|
]
|
|
|
|
+mem: storing 34 in location 1
|
|
|
|
+mem: storing 35 in location 2
|
|
|
|
|
|
|
|
:(scenario multiple_shape_shifting_variants_2)
|
|
|
|
# static dispatch between shape-shifting variants, _including pointer lookups_
|
|
|
|
recipe main [
|
|
|
|
e1:d1:number <- merge 3
|
2016-01-20 07:18:03 +00:00
|
|
|
e2:address:shared:d2:number <- new {(d2 number): type}
|
2015-11-15 20:35:59 +00:00
|
|
|
1:number/raw <- foo e1
|
|
|
|
2:number/raw <- foo *e2 # different from previous scenario
|
|
|
|
]
|
|
|
|
recipe foo a:d1:_elem -> b:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 34
|
|
|
|
]
|
|
|
|
recipe foo a:d2:_elem -> b:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 35
|
|
|
|
]
|
|
|
|
container d1:_elem [
|
|
|
|
x:_elem
|
|
|
|
]
|
|
|
|
container d2:_elem [
|
|
|
|
x:number
|
|
|
|
y:_elem
|
|
|
|
]
|
|
|
|
+mem: storing 34 in location 1
|
|
|
|
+mem: storing 35 in location 2
|
2015-11-17 18:47:11 +00:00
|
|
|
|
|
|
|
:(scenario missing_type_in_shape_shifting_recipe)
|
|
|
|
% Hide_errors = true;
|
|
|
|
recipe main [
|
|
|
|
a:d1:number <- merge 3
|
|
|
|
foo a
|
|
|
|
]
|
|
|
|
recipe foo a:d1:_elem -> b:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
copy e # no such variable
|
|
|
|
reply 34
|
|
|
|
]
|
|
|
|
container d1:_elem [
|
|
|
|
x:_elem
|
|
|
|
]
|
|
|
|
+error: foo: unknown type for e (check the name for typos)
|
|
|
|
+error: specializing foo: missing type for e
|
|
|
|
# and it doesn't crash
|
2015-11-17 19:25:00 +00:00
|
|
|
|
|
|
|
:(scenario missing_type_in_shape_shifting_recipe_2)
|
|
|
|
% Hide_errors = true;
|
|
|
|
recipe main [
|
|
|
|
a:d1:number <- merge 3
|
|
|
|
foo a
|
|
|
|
]
|
|
|
|
recipe foo a:d1:_elem -> b:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
get e, x:offset # unknown variable in a 'get', which does some extra checking
|
|
|
|
reply 34
|
|
|
|
]
|
|
|
|
container d1:_elem [
|
|
|
|
x:_elem
|
|
|
|
]
|
|
|
|
+error: foo: unknown type for e (check the name for typos)
|
|
|
|
+error: specializing foo: missing type for e
|
|
|
|
# and it doesn't crash
|
2015-11-28 01:54:34 +00:00
|
|
|
|
|
|
|
:(scenarios transform)
|
|
|
|
:(scenario specialize_recursive_shape_shifting_recipe)
|
|
|
|
recipe main [
|
|
|
|
1:number <- copy 34
|
|
|
|
2:number <- foo 1:number
|
|
|
|
]
|
|
|
|
recipe foo x:_elem -> y:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
{
|
|
|
|
break
|
|
|
|
y:number <- foo x
|
|
|
|
}
|
|
|
|
reply y
|
|
|
|
]
|
2015-11-28 02:01:55 +00:00
|
|
|
+transform: new specialization: foo_2
|
2015-11-28 01:54:34 +00:00
|
|
|
# transform terminates
|
2015-11-28 08:34:34 +00:00
|
|
|
|
|
|
|
:(scenarios run)
|
|
|
|
:(scenario specialize_most_similar_variant)
|
|
|
|
recipe main [
|
2016-01-20 07:18:03 +00:00
|
|
|
1:address:shared:number <- new number:type
|
|
|
|
2:number <- foo 1:address:shared:number
|
2015-11-28 08:34:34 +00:00
|
|
|
]
|
|
|
|
recipe foo x:_elem -> y:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 34
|
|
|
|
]
|
2016-01-20 07:18:03 +00:00
|
|
|
recipe foo x:address:shared:_elem -> y:number [
|
2015-11-28 08:34:34 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 35
|
|
|
|
]
|
|
|
|
+mem: storing 35 in location 2
|
2016-02-12 01:36:58 +00:00
|
|
|
|
|
|
|
:(scenario specialize_most_similar_variant_2)
|
|
|
|
# version with headers padded with lots of unrelated concrete types
|
|
|
|
recipe main [
|
|
|
|
1:number <- copy 23
|
|
|
|
2:address:shared:array:number <- copy 0
|
|
|
|
3:number <- foo 2:address:shared:array:number, 1:number
|
|
|
|
]
|
|
|
|
# variant with concrete type
|
|
|
|
recipe foo dummy:address:shared:array:number, x:number -> y:number, dummy:address:shared:array:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 34
|
|
|
|
]
|
|
|
|
# shape-shifting variant
|
|
|
|
recipe foo dummy:address:shared:array:number, x:_elem -> y:number, dummy:address:shared:array:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 35
|
|
|
|
]
|
|
|
|
# prefer the concrete variant
|
|
|
|
+mem: storing 34 in location 3
|
|
|
|
|
|
|
|
:(scenario specialize_most_similar_variant_3)
|
|
|
|
recipe main [
|
|
|
|
1:address:shared:array:character <- new [abc]
|
|
|
|
foo 1:address:shared:array:character
|
|
|
|
]
|
|
|
|
recipe foo x:address:shared:array:character [
|
|
|
|
2:number <- copy 34
|
|
|
|
]
|
|
|
|
recipe foo x:address:_elem [
|
|
|
|
2:number <- copy 35
|
|
|
|
]
|
|
|
|
# make sure the more precise version was used
|
|
|
|
+mem: storing 34 in location 2
|
|
|
|
|
|
|
|
:(scenario specialize_literal_as_number)
|
|
|
|
recipe main [
|
|
|
|
1:number <- foo 23
|
|
|
|
]
|
|
|
|
recipe foo x:_elem -> y:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 34
|
|
|
|
]
|
|
|
|
recipe foo x:character -> y:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 35
|
|
|
|
]
|
|
|
|
+mem: storing 34 in location 1
|
|
|
|
|
|
|
|
:(scenario specialize_literal_as_number_2)
|
|
|
|
# version calling with literal
|
|
|
|
recipe main [
|
|
|
|
1:number <- foo 0
|
|
|
|
]
|
|
|
|
# variant with concrete type
|
|
|
|
recipe foo x:number -> y:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 34
|
|
|
|
]
|
|
|
|
# shape-shifting variant
|
|
|
|
recipe foo x:address:shared:_elem -> y:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
reply 35
|
|
|
|
]
|
|
|
|
# prefer the concrete variant, ignore concrete types in scoring the shape-shifting variant
|
|
|
|
+mem: storing 34 in location 1
|