//: Introduce a new transform to perform various checks in instructions before //: we start running them. It'll be extensible, so that we can add checks for //: new recipes as we extend 'run' to support them. //: //: Doing checking in a separate part complicates things, because the values //: of variables in memory and the processor (current_recipe_name, //: current_instruction) aren't available at checking time. If I had a more //: sophisticated layer system I'd introduce the simpler version first and //: transform it in a separate layer or set of layers. :(before "End Checks") Transform.push_back(check_instruction); // idempotent :(code) void check_instruction(const recipe_ordinal r) { trace(9991, "transform") << "--- perform checks for recipe " << get(Recipe, r).name << end(); map > metadata; for (int i = 0; i < SIZE(get(Recipe, r).steps); ++i) { instruction& inst = get(Recipe, r).steps.at(i); if (inst.is_label) continue; switch (inst.operation) { // Primitive Recipe Checks case COPY: { if (SIZE(inst.products) > SIZE(inst.ingredients)) { raise << maybe(get(Recipe, r).name) << "too many products in '" << to_original_string(inst) << "'\n" << end(); break; } for (int i = 0; i < SIZE(inst.products); ++i) { if (!types_coercible(inst.products.at(i), inst.ingredients.at(i))) { raise << maybe(get(Recipe, r).name) << "can't copy '" << inst.ingredients.at(i).original_string << "' to '" << inst.products.at(i).original_string << "'; types don't match\n" << end(); goto finish_checking_instruction; } } break; } // End Primitive Recipe Checks default: { // Defined Recipe Checks // End Defined Recipe Checks } } finish_checking_instruction:; } } :(scenario copy_checks_reagent_count) % Hide_errors = true; def main [ 1:num, 2:num <- copy 34 ] +error: main: too many products in '1:num, 2:num <- copy 34' :(scenario write_scalar_to_array_disallowed) % Hide_errors = true; def main [ 1:array:num <- copy 34 ] +error: main: can't copy '34' to '1:array:num'; types don't match :(scenario write_scalar_to_array_disallowed_2) % Hide_errors = true; def main [ 1:num, 2:array:num <- copy 34, 35 ] +error: main: can't copy '35' to '2:array:num'; types don't match :(scenario write_scalar_to_address_disallowed) % Hide_errors = true; def main [ 1:&:num <- copy 34 ] +error: main: can't copy '34' to '1:&:num'; types don't match :(scenario write_address_to_character_disallowed) % Hide_errors = true; def main [ 1:&:num <- copy 12/unsafe 2:char <- copy 1:&:num ] +error: main: can't copy '1:&:num' to '2:char'; types don't match :(scenario write_number_to_character_allowed) def main [ 1:num <- copy 97 2:char <- copy 1:num ] $error: 0 :(code) // types_match with some leniency bool types_coercible(reagent/*copy*/ to, reagent/*copy*/ from) { // Begin types_coercible(reagent to, reagent from) if (types_match_sub(to, from)) return true; if (is_real_mu_number(from) && is_mu_character(to)) return true; // End types_coercible Special-cases return false; } bool types_match_sub(const reagent& to, const reagent& from) { // to sidestep type-checking, use /unsafe in the source. // this will be highlighted in red inside vim. just for setting up some tests. if (is_unsafe(from)) return true; if (is_literal(from)) { if (is_mu_array(to)) return false; // End Matching Types For Literal(to) if (!to.type) return false; // allow writing null to any address if (is_mu_address(to)) return from.name == "null"; return size_of(to) == 1; // literals are always scalars } return types_strictly_match_sub(to, from); } // variant for others to call bool types_match(reagent/*copy*/ to, reagent/*copy*/ from) { // Begin types_match(reagent to, reagent from) return types_match_sub(to, from); } //