//: A big convenience high-level languages provide is the ability to name memory //: locations. In mu, a transform called 'transform_names' provides this //: convenience. :(scenario convert_names) recipe main [ x:number <- copy 0:literal ] +name: assign x 1 +mem: storing 0 in location 1 :(scenario convert_names_warns) % Hide_warnings = true; recipe main [ x:number <- copy y:number ] +warn: use before set: y in main :(after "int main") Transform.push_back(transform_names); :(before "End Globals") map > Name; :(after "Clear Other State For recently_added_recipes") for (long long int i = 0; i < SIZE(recently_added_recipes); ++i) { Name.erase(recently_added_recipes.at(i)); } :(code) void transform_names(const recipe_number r) { bool names_used = false; bool numeric_locations_used = false; map& names = Name[r]; map > metadata; // store the indices 'used' so far in the map long long int& curr_idx = names[""]; ++curr_idx; // avoid using index 0, benign skip in some other cases for (long long int i = 0; i < SIZE(Recipe[r].steps); ++i) { instruction& inst = Recipe[r].steps.at(i); // Per-recipe Transforms // map names to addresses for (long long int in = 0; in < SIZE(inst.ingredients); ++in) { check_metadata(metadata, inst.ingredients.at(in), r); if (is_numeric_location(inst.ingredients.at(in))) numeric_locations_used = true; if (is_named_location(inst.ingredients.at(in))) names_used = true; if (disqualified(inst.ingredients.at(in))) continue; if (!already_transformed(inst.ingredients.at(in), names)) { raise << "use before set: " << inst.ingredients.at(in).name << " in " << Recipe[r].name << '\n'; } inst.ingredients.at(in).set_value(lookup_name(inst.ingredients.at(in), r)); } for (long long int out = 0; out < SIZE(inst.products); ++out) { check_metadata(metadata, inst.products.at(out), r); if (is_numeric_location(inst.products.at(out))) numeric_locations_used = true; if (is_named_location(inst.products.at(out))) names_used = true; if (disqualified(inst.products.at(out))) continue; if (names.find(inst.products.at(out).name) == names.end()) { trace("name") << "assign " << inst.products.at(out).name << " " << curr_idx; names[inst.products.at(out).name] = curr_idx; curr_idx += size_of(inst.products.at(out)); } inst.products.at(out).set_value(lookup_name(inst.products.at(out), r)); } } if (names_used && numeric_locations_used && r != Recipe_number["interactive"]) raise << "mixing variable names and numeric addresses in " << Recipe[r].name << '\n'; } void check_metadata(map >& metadata, const reagent& x, const recipe_number r) { if (isa_literal(x)) return; if (is_raw(x)) return; if (metadata.find(x.name) == metadata.end()) metadata[x.name] = x.types; if (metadata[x.name] != x.types) raise << x.name << " used with multiple types in " << Recipe[r].name << '\n'; } bool disqualified(/*mutable*/ reagent& x) { if (x.types.empty()) raise << "missing type in " << x.to_string() << '\n'; assert(!x.types.empty()); if (is_raw(x)) return true; if (isa_literal(x)) return true; if (is_integer(x.name)) return true; // End Disqualified Reagents if (x.initialized) return true; return false; } bool already_transformed(const reagent& r, const map& names) { return names.find(r.name) != names.end(); } long long int lookup_name(const reagent& r, const recipe_number default_recipe) { return Name[default_recipe][r.name]; } type_number skip_addresses(const vector& types) { for (long long int i = 0; i < SIZE(types); ++i) { if (types.at(i) != Type_number["address"]) return types.at(i); } raise << "expected a container" << '\n' << die(); return -1; } int find_element_name(const type_number t, const string& name) { const type_info& container = Type[t]; //? cout << "looking for element " << name << " in type " << container.name << " with " << SIZE(container.element_names) << " elements\n"; //? 1 for (long long int i = 0; i < SIZE(container.element_names); ++i) { if (container.element_names.at(i) == name) return i; } raise << "unknown element " << name << " in container " << t << '\n' << die(); return -1; } bool is_numeric_location(const reagent& x) { if (isa_literal(x)) return false; if (is_raw(x)) return false; if (x.name == "0") return false; // used for chaining lexical scopes return is_integer(x.name); } bool is_named_location(const reagent& x) { if (isa_literal(x)) return false; if (is_raw(x)) return false; if (is_special_name(x.name)) return false; return !is_integer(x.name); } bool is_raw(const reagent& r