//: Arrays contain a variable number of elements of the same type. Their value //: starts with the length of the array. //: //: You can create arrays of containers, but containers can only contain //: elements of a fixed size, so you can't create containers containing arrays. //: Create containers containing addresses to arrays instead. //: You can create arrays using 'create-array'. :(scenario create_array) def main [ # create an array occupying locations 1 (for the size) and 2-4 (for the elements) 1:array:num:3 <- create-array ] +run: creating array from 4 locations :(before "End Primitive Recipe Declarations") CREATE_ARRAY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "create-array", CREATE_ARRAY); :(before "End Primitive Recipe Checks") case CREATE_ARRAY: { if (inst.products.empty()) { raise << maybe(get(Recipe, r).name) << "'create-array' needs one product and no ingredients but got '" << to_original_string(inst) << '\n' << end(); break; } reagent/*copy*/ product = inst.products.at(0); // Update CREATE_ARRAY product in Check if (!is_mu_array(product)) { raise << maybe(get(Recipe, r).name) << "'create-array' cannot create non-array '" << product.original_string << "'\n" << end(); break; } if (!product.type->right) { raise << maybe(get(Recipe, r).name) << "create array of what? '" << to_original_string(inst) << "'\n" << end(); break; } // 'create-array' will need to check properties rather than types type_tree* array_length_from_type = product.type->right->right; if (!array_length_from_type) { raise << maybe(get(Recipe, r).name) << "create array of what size? '" << to_original_string(inst) << "'\n" << end(); break; } if (!product.type->right->right->atom) array_length_from_type = array_length_from_type->left; if (!is_integer(array_length_from_type->name)) { raise << maybe(get(Recipe, r).name) << "'create-array' product should specify size of array after its element type, but got '" << product.type->right->right->name << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case CREATE_ARRAY: { reagent/*copy*/ product = current_instruction().products.at(0); // Update CREATE_ARRAY product in Run int base_address = product.value; type_tree* array_length_from_type = product.type->right->right; if (!product.type->right->right->atom) array_length_from_type = array_length_from_type->left; int array_length = to_integer(array_length_from_type->name); // initialize array length, so that size_of will work trace("mem") << "storing " << array_length << " in location " << base_address << end(); put(Memory, base_address, array_length); // in array elements int size = size_of(product); // in locations trace(9998, "run") << "creating array from " << size << " locations" << end(); // initialize array for (int i = 1; i <= size_of(product); ++i) put(Memory, base_address+i, 0); // no need to update product write_products = false; break; } :(scenario copy_array) # Arrays can be copied around with a single instruction just like numbers, # no matter how large they are. # You don't need to pass the size around, since each array variable stores its # size in memory at run-time. We'll call a variable with an explicit size a # 'static' array, and one without a 'dynamic' array since it can contain # arrays of many different sizes. def main [ 1:array:num:3 <- create-array 2:num <- copy 14 3:num <- copy 15 4:num <- copy 16 5:array:num <- copy 1:array:num:3 ] +mem: storing 3 in location 5 +mem: storing 14 in location 6 +mem: storing 15 in location 7 +mem: storing 16 in location 8 :(scenario stash_array) def main [
discard """
tfile: "tnot.nim"
tline: 14
errormsg: "type mismatch"
"""
# BUG: following compiles, but should not:
proc nodeOfDegree(x: int): bool =
result = false
proc main =
for j in 0..2:
for i in 0..10:
if not nodeOfDegree(1) >= 0: #ERROR_MSG type mismatch
echo "Yes"
else:
echo "No"
main()