summary refs log blame commit diff stats
path: root/doc/uml/134530.diagram
blob: 64cb30aea052c1c923d6dcd1118d95296673cec9 (plain) (tree)
an>* type) { assert(type->right); if (type->right->atom) { return type->right; } else if (!type->right->right) { return type->right->left; } // hack: support array:num:3 without requiring extra parens else if (type->right->right->left && type->right->right->left->atom && is_integer(type->right->right->left->name)) { assert(!type->right->right->right); return type->right->left; } return type->right; } int array_length(const reagent& x) { // x should already be canonized. // hack: look for length in type if (!x.type->atom && x.type->right && !x.type->right->atom && x.type->right->right && !x.type->right->right->atom && !x.type->right->right->right // exactly 3 types && x.type->right->right->left && x.type->right->right->left->atom && is_integer(x.type->right->right->left->name)) { // third 'type' is a number // get size from type return to_integer(x.type->right->right->left->name); } // this should never happen at transform time return get_or_insert(Memory, x.value); } :(before "End Unit Tests") void test_array_length_compound() { put(Memory, 1, 3); put(Memory, 2, 14); put(Memory, 3, 15); put(Memory, 4, 16); reagent x("1:array:address:num"); // 3 types, but not a static array populate_value(x); CHECK_EQ(array_length(x), 3); } void test_array_length_static() { reagent x("1:array:num:3"); CHECK_EQ(array_length(x), 3); } void test_index_truncates() { run( "def main [\n" " 1:array:num:3 <- create-array\n" " 2:num <- copy 14\n" " 3:num <- copy 15\n" " 4:num <- copy 16\n" " 10:num <- index 1:array:num:3, 1.5\n" // non-whole number "]\n" ); CHECK_TRACE_CONTENTS( // fraction is truncated away "mem: storing 15 in location 10\n" ); } void test_index_out_of_bounds() { Hide_errors = true; run( "def main [\n" " 1:array:point:3 <- create-array\n" " index 1:array:point:3, 4\n" // less than size of array in locations, but larger than its length in elements "]\n" ); CHECK_TRACE_CONTENTS( "error: main: invalid index 4 in 'index 1:array:point:3, 4'\n" ); } void test_index_out_of_bounds_2() { Hide_errors = true; run( "def main [\n" " 1:array:num:3 <- create-array\n" " index 1:array:num, -1\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: invalid index -1 in 'index 1:array:num, -1'\n" ); } void test_index_product_type_mismatch() { Hide_errors = true; run( "def main [\n" " 1:array:point:3 <- create-array\n" " 10:num <- index 1:array:point, 0\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: 'index' on '1:array:point' can't be saved in '10:num'; type should be 'point'\n" ); } //: we might want to call 'index' without saving the results, say in a sandbox void test_index_without_product() { run( "def main [\n" " 1:array:num:3 <- create-array\n" " index 1:array:num:3, 0\n" "]\n" ); // just don't die } //:: To write to elements of arrays, use 'put'. void test_put_index() { run( "def main [\n" " 1:array:num:3 <- create-array\n" " 1:array:num <- put-index 1:array:num, 1, 34\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 34 in location 3\n" ); } :(before "End Primitive Recipe Declarations") PUT_INDEX, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "put-index", PUT_INDEX); :(before "End Primitive Recipe Checks") case PUT_INDEX: { if (SIZE(inst.ingredients) != 3) { raise << maybe(get(Recipe, r).name) << "'put-index' expects exactly 3 ingredients in '" << to_original_string(inst) << "'\n" << end(); break; } reagent/*copy*/ base = inst.ingredients.at(0); // Update PUT_INDEX base in Check if (!is_mu_array(base)) { raise << maybe(get(Recipe, r).name) << "'put-index' on a non-array '" << base.original_string << "'\n" << end(); break; } reagent/*copy*/ index = inst.ingredients.at(1); // Update PUT_INDEX index in Check if (!is_mu_number(index)) { raise << maybe(get(Recipe, r).name) << "second ingredient of 'put-index' should have type 'number', but got '" << inst.ingredients.at(1).original_string << "'\n" << end(); break; } reagent/*copy*/ value = inst.ingredients.at(2); // Update PUT_INDEX value in Check reagent/*local*/ element(copy_array_element(base.type)); if (!types_coercible(element, value)) { raise << maybe(get(Recipe, r).name) << "'put-index " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should store " << names_to_string_without_quotes(element.type) << " but '" << value.name << "' has type " << names_to_string_without_quotes(value.type) << '\n' << end(); break; } if (inst.products.empty()) break; // no more checks necessary if (inst.products.at(0).name != inst.ingredients.at(0).name) { raise << maybe(get(Recipe, r).name) << "product of 'put-index' must be first ingredient '" << inst.ingredients.at(0).original_string << "', but got '" << inst.products.at(0).original_string << "'\n" << end(); break; } // End PUT_INDEX Product Checks break; } :(before "End Primitive Recipe Implementations") case PUT_INDEX: { reagent/*copy*/ base = current_instruction().ingredients.at(0); // Update PUT_INDEX base in Run int base_address = base.value; if (base_address == 0) { raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end(); break; } reagent/*copy*/ index = current_instruction().ingredients.at(1); // Update PUT_INDEX index in Run vector<double> index_val(read_memory(index)); if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) { raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end(); break; } int address = base_address + /*skip length*/1 + index_val.at(0)*size_of(array_element(base.type)); trace(Callstack_depth+1, "run") << "address to copy to is " << address << end(); // optimization: directly write the element rather than updating 'product' // and writing the entire array write_products = false; vector<double> value = read_memory(current_instruction().ingredients.at(2)); // Write Memory in PUT_INDEX in Run for (int i = 0; i < SIZE(value); ++i) { trace(Callstack_depth+1, "mem") << "storing " << no_scientific(value.at(i)) << " in location " << address+i << end(); put(Memory, address+i, value.at(i)); } break; } :(code) void test_put_index_out_of_bounds() { Hide_errors = true; run( "def main [\n" " 1:array:point:3 <- create-array\n" " 8:point <- merge 34, 35\n" " 1:array:point <- put-index 1:array:point, 4, 8:point\n" // '4' is less than size of array in locations, but larger than its length in elements "]\n" ); CHECK_TRACE_CONTENTS( "error: main: invalid index 4 in '1:array:point <- put-index 1:array:point, 4, 8:point'\n" ); } void test_put_index_out_of_bounds_2() { Hide_errors = true; run( "def main [\n" " 1:array:point:3 <- create-array\n" " 10:point <- merge 34, 35\n" " 1:array:point <- put-index 1:array:point, -1, 10:point\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: invalid index -1 in '1:array:point <- put-index 1:array:point, -1, 10:point'\n" ); } void test_put_index_product_error() { Hide_errors = true; run( "def main [\n" " 1:array:num:3 <- create-array\n" " 4:array:num:3 <- put-index 1:array:num:3, 0, 34\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: product of 'put-index' must be first ingredient '1:array:num:3', but got '4:array:num:3'\n" ); } //:: compute the length of an array void test_array_length() { run( "def main [\n" " 1:array:num:3 <- create-array\n" " 10:num <- length 1:array:num\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 3 in location 10\n" ); } :(before "End Primitive Recipe Declarations") LENGTH, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "length", LENGTH); :(before "End Primitive Recipe Checks") case LENGTH: { if (SIZE(inst.ingredients) != 1) { raise << maybe(get(Recipe, r).name) << "'length' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end(); break; } reagent/*copy*/ array = inst.ingredients.at(0); // Update LENGTH array in Check if (!is_mu_array(array)) { raise << "tried to calculate length of non-array '" << array.original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case LENGTH: { reagent/*copy*/ array = current_instruction().ingredients.at(0); // Update LENGTH array in Run if (array.value == 0) { raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end(); break; } products.resize(1); products.at(0).push_back(get_or_insert(Memory, array.value)); break; } //: optimization: none of the instructions in this layer use 'ingredients' so //: stop copying potentially huge arrays into it. :(before "End should_copy_ingredients Special-cases") recipe_ordinal r = current_instruction().operation; if (r == CREATE_ARRAY || r == INDEX || r == PUT_INDEX || r == LENGTH) return false;