1 //: Arrays contain a variable number of elements of the same type. Their value
  2 //: starts with the length of the array.
  3 //:
  4 //: You can create arrays of containers, but containers can only contain
  5 //: elements of a fixed size, so you can't create containers containing arrays.
  6 //: Create containers containing addresses to arrays instead.
  7 
  8 //: You can create arrays using 'create-array'.
  9 :(scenario create_array)
 10 def main [
 11   # create an array occupying locations 1 (for the size) and 2-4 (for the elements)
 12   1:array:num:3 <- create-array
 13 ]
 14 +run: creating array of size 4
 15 
 16 :(before "End Primitive Recipe Declarations")
 17 CREATE_ARRAY,
 18 :(before "End Primitive Recipe Numbers")
 19 put(Recipe_ordinal, "create-array", CREATE_ARRAY);
 20 :(before "End Primitive Recipe Checks")
 21 case CREATE_ARRAY: {
 22   if (inst.products.empty()) {
 23   ¦ raise << maybe(get(Recipe, r).name) << "'create-array' needs one product and no ingredients but got '" << to_original_string(inst) << '\n' << end();
 24   ¦ break;
 25   }
 26   reagent/*copy*/ product = inst.products.at(0);
 27   // Update CREATE_ARRAY product in Check
 28   if (!is_mu_array(product)) {
 29   ¦ raise << maybe(get(Recipe, r).name) << "'create-array' cannot create non-array '" << product.original_string << "'\n" << end();
 30   ¦ break;
 31   }
 32   if (!product.type->right) {
 33   ¦ raise << maybe(get(Recipe, r).name) << "create array of what? '" << to_original_string(inst) << "'\n" << end();
 34   ¦ break;
 35   }
 36   // 'create-array' will need to check properties rather than types
 37   type_tree* array_length_from_type = product.type->right->right;
 38   if (!array_length_from_type) {
 39   ¦ raise << maybe(get(Recipe, r).name) << "create array of what size? '" << to_original_string(inst) << "'\n" << end();
 40   ¦ break;
 41   }
 42   if (!product.type->right->right->atom)
 43   ¦ array_length_from_type = array_length_from_type->left;
 44   if (!is_integer(array_length_from_type->name)) {
 45   ¦ 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();
 46   ¦ break;
 47   }
 48   break;
 49 }
 50 :(before "End Primitive Recipe Implementations")
 51 case CREATE_ARRAY: {
 52   reagent/*copy*/ product = current_instruction().products.at(0);
 53   // Update CREATE_ARRAY product in Run
 54   int base_address = product.value;
 55   type_tree* array_length_from_type = product.type->right->right;
 56   if (!product.type->right->right->atom)
 57   ¦ array_length_from_type = array_length_from_type->left;
 58   int array_length = to_integer(array_length_from_type->name);
 59   // initialize array length, so that size_of will work
 60   trace(9999, "mem") << "storing " << array_length << " in location " << base_address << end();
 61   put(Memory, base_address, array_length);  // in array elements
 62   int size = size_of(product);  // in locations
 63   trace(9998, "run") << "creating array of size " << size << end();
 64   // initialize array
 65   for (int i = 1;  i <= size_of(product);  ++i) {
 66   ¦ put(Memory, base_address+i, 0);
 67   }
 68   // no need to update product
 69   write_products = false;
 70   break;
 71 }
 72 
 73 :(scenario copy_array)
 74 # Arrays can be copied around with a single instruction just like numbers,
 75 # no matter how large they are.
 76 # You don't need to pass the size around, since each array variable stores its
 77 # size in memory at run-time. We'll call a variable with an explicit size a
 78 # 'static' array, and one without a 'dynamic' array since it can contain
 79 # arrays of many different sizes.
 80 def main [
 81   1:array:num:3 <- create-array
 82   2:num <- copy 14
 83   3:num <- copy 15
 84   4:num <- copy 16
 85   5:array:num <- copy 1:array:num:3
 86 ]
 87 +mem: storing 3 in location 5
 88 +mem: storing 14 in location 6
 89 +mem: storing 15 in location 7
 90 +mem: storing 16 in location 8
 91 
 92 :(scenario stash_array)
 93 def main [
 94   1:array:num:3 <- create-array
 95   2:num <- copy 14
 96   3:num <- copy 15
 97   4:num <- copy 16
 98   stash [foo:], 1:array:num:3
 99 ]
100 +app: foo: 3 14 15 16
101 
102 :(before "End types_coercible Special-cases")
103 if (is_mu_array(from) && is_mu_array(to))
104   return types_strictly_match(array_element(from.type), array_element(to.type));
105 
106 :(before "End size_of(reagent r) Special-cases")
107 if (!r.type->atom && r.type->left->atom && r.type->left->value == get(Type_ordinal, "array")) {
108   if (!r.type->right) {
109   ¦ raise << maybe(current_recipe_name()) << "'" << r.original_string << "' is an array of what?\n" << end();
110   ¦ return 1;
111   }
112   return /*space for length*/1 + array_length(r)*size_of(array_element(r.type));
113 }
114 
115 :(before "End size_of(type) Non-atom Special-cases")
116 if (type->left->value == get(Type_ordinal, "array")) return static_array_length(type);
117 :(code)
118 int static_array_length(const type_tree* type) {
119   if (!type->atom && type->right && !type->right->atom && type->right->right && !type->right->right->atom && !type->right->right->right  // exactly 3 types
120   ¦ ¦ && type->right->right->left && type->right->right->left->atom && is_integer(type->right->right->left->name)) {  // third 'type' is a number
121   ¦ // get size from type
122   ¦ return to_integer(type->right->right->left->name);
123   }
124   cerr << to_string(type) << '\n';
125   assert(false);
126 }
127 
128 //: disable the size mismatch check for arrays since the destination array
129 //: need not be initialized
130 :(before "End size_mismatch(x) Special-cases")
131 if (x.type && !x.type->atom && x.type->left->value == get(Type_ordinal, "array")) return false;
132 
133 //:: arrays inside containers
134 //: arrays are disallowed inside containers unless their length is fixed in
135 //: advance
136 
137 :(scenario container_permits_static_array_element)
138 container foo [
139   x:array:num:3
140 ]
141 $error: 0
142 
143 :(before "End insert_container Special-cases")
144 else if (is_integer(type->name)) {  // sometimes types will contain non-type tags, like numbers for the size of an array
145   type->value = 0;
146 }
147 
148 :(scenario container_disallows_dynamic_array_element)
149 % Hide_errors = true;
150 container foo [
151   x:array:num
152 ]
153 +error: container 'foo' cannot determine size of element 'x'
154 
155 :(before "End Load Container Element Definition")
156 {
157   const type_tree* type = info.elements.back().type;
158   if (type && type->atom && type->name == "array") {
159   ¦ raise << "container '" << name << "' doesn't specify type of array elements for '" << info.elements.back().name << "'\n" << end();
160   ¦ continue;
161   }
162   if (type && !type->atom && type->left->atom && type->left->name == "array") {
163   ¦ if (!type->right) {
164   ¦ ¦ raise << "container '" << name << "' doesn't specify type of array elements for '" << info.elements.back().name << "'\n" << end();
165   ¦ ¦ continue;
166   ¦ }
167   ¦ if (!type->right->right || !is_integer(type->right->right->left->name)) {  // array has no length
168   ¦ ¦ raise << "container '" << name << "' cannot determine size of element '" << info.elements.back().name << "'\n" << end();
169   ¦ ¦ continue;
170   ¦ }
171   }
172 }
173 
174 //: disable the size mismatch check for 'merge' instructions since containers
175 //: can contain arrays, and since we already do plenty of checking for them
176 :(before "End size_mismatch(x) Special-cases")
177 if (current_call().running_step_index < SIZE(get(Recipe, current_call().running_recipe).steps)
178   ¦ && current_instruction().operation == MERGE) {
179   return false;
180 }
181 
182 :(scenario merge_static_array_into_container)
183 container foo [
184   x:num
185   y:array:num:3
186 ]
187 def main [
188   1:array:num:3 <- create-array
189   10:foo <- merge 34, 1:array:num:3
190 ]
191 # no errors
192 
193 :(scenario code_inside_container)
194 % Hide_errors = true;
195 container card [
196   rank:num <- next-ingredient
197 ]
198 def foo [
199   1:card <- merge 3
200   2:num <- get 1:card rank:offset
201 ]
202 # shouldn't die
203 
204 //:: containers inside arrays
205 //: make sure we compute container sizes inside arrays
206 
207 :(before "End compute_container_sizes Non-atom Special-cases")
208 else if (type->left->name == "array")
209   compute_container_sizes(array_element(type), pending_metadata, location_for_error_messages);
210 
211 :(before "End Unit Tests")
212 void test_container_sizes_from_array() {
213   // a container we don't have the size for
214   reagent container("x:point");
215   CHECK(!contains_key(Container_metadata, container.type));
216   // scanning an array of the container precomputes the size of the container
217   reagent r("x:array:point");
218   compute_container_sizes(r, "");
219   CHECK(contains_key(Container_metadata, container.type));
220   CHECK_EQ(get(Container_metadata, container.type).size, 2);
221 }
222 
223 void test_container_sizes_from_address_to_array() {
224   // a container we don't have the size for
225   reagent container("x:point");
226   CHECK(!contains_key(Container_metadata, container.type));
227   // scanning an address to an array of the container precomputes the size of the container
228   reagent r("x:address:array:point");
229   compute_container_sizes(r, "");
230   CHECK(contains_key(Container_metadata, container.type));
231   CHECK_EQ(get(Container_metadata, container.type).size, 2);
232 }
233 
234 void test_container_sizes_from_static_array() {
235   // a container we don't have the size for
236   reagent container("x:point");
237   int old_size = SIZE(Container_metadata);
238   // scanning an address to an array of the container precomputes the size of the container
239   reagent r("x:array:point:10");
240   compute_container_sizes(r, "");
241   CHECK(contains_key(Container_metadata, container.type));
242   CHECK_EQ(get(Container_metadata, container.type).size, 2);
243   // no non-container types precomputed
244   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
245 }
246 
247 void test_container_sizes_from_address_to_static_array() {
248   // a container we don't have the size for
249   reagent container("x:point");
250   int old_size = SIZE(Container_metadata);
251   // scanning an address to an array of the container precomputes the size of the container
252   reagent r("x:address:array:point:10");
253   compute_container_sizes(r, "");
254   CHECK(contains_key(Container_metadata, container.type));
255   CHECK_EQ(get(Container_metadata, container.type).size, 2);
256   // no non-container types precomputed
257   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
258 }
259 
260 void test_container_sizes_from_repeated_address_and_array_types() {
261   // a container we don't have the size for
262   reagent container("x:point");
263   int old_size = SIZE(Container_metadata);
264   // scanning repeated address and array types modifying the container precomputes the size of the container
265   reagent r("x:address:array:address:array:point:10");
266   compute_container_sizes(r, "");
267   CHECK(contains_key(Container_metadata, container.type));
268   CHECK_EQ(get(Container_metadata, container.type).size, 2);
269   // no non-container types precomputed
270   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
271 }
272 
273 void test_container_sizes_on_unknown_type() {
274   // a container we don't have the size for
275   reagent container("x:point");
276   int old_size = SIZE(Container_metadata);
277   // scanning address to array with a typo
278   reagent r("x:address:array:adress:number");
279   compute_container_sizes(r, "");  // should not crash
280   // no non-container types precomputed
281   CHECK_EQ(SIZE(Container_metadata), old_size);
282 }
283 
284 //:: To access elements of an array, use 'index'
285 
286 :(scenario index)
287 def main [
288   1:array:num:3 <- create-array
289   2:num <- copy 14
290   3:num <- copy 15
291   4:num <- copy 16
292   5:num <- index 1:array:num:3, 0/index  # the index must be a non-negative whole number
293 ]
294 +mem: storing 14 in location 5
295 
296 :(scenario index_compound_element)
297 def main [
298   {1: (array (address number) 3)} <- create-array
299   2:num <- copy 14
300   3:num <- copy 15
301   4:num <- copy 16
302   5:address:num <- index {1: (array (address number) 3)}, 0
303 ]
304 +mem: storing 14 in location 5
305 
306 :(scenario index_direct_offset)
307 def main [
308   1:array:num:3 <- create-array
309   2:num <- copy 14
310   3:num <- copy 15
311   4:num <- copy 16
312   5:num <- copy 0
313   6:num <- index 1:array:num, 5:num
314 ]
315 +mem: storing 14 in location 6
316 
317 :(before "End Primitive Recipe Declarations")
318 INDEX,
319 :(before "End Primitive Recipe Numbers")
320 put(Recipe_ordinal, "index", INDEX);
321 :(before "End Primitive Recipe Checks")
322 case INDEX: {
323   if (SIZE(inst.ingredients) != 2) {
324   ¦ raise << maybe(get(Recipe, r).name) << "'index' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
325   ¦ break;
326   }
327   reagent/*copy*/ base = inst.ingredients.at(0);
328   // Update INDEX base in Check
329   if (!is_mu_array(base)) {
330   ¦ raise << maybe(get(Recipe, r).name) << "'index' on a non-array '" << base.original_string << "'\n" << end();
331   ¦ break;
332   }
333   reagent/*copy*/ index = inst.ingredients.at(1);
334   // Update INDEX index in Check
335   if (!is_mu_number(index)) {
336   ¦ raise << maybe(get(Recipe, r).name) << "second ingredient of 'index' should be a number, but got '" << index.original_string << "'\n" << end();
337   ¦ break;
338   }
339   if (inst.products.empty()) break;
340   reagent/*copy*/ product = inst.products.at(0);
341   // Update INDEX product in Check
342   reagent/*local*/ element;
343   element.type = copy_array_element(base.type);
344   if (!types_coercible(product, element)) {
345   ¦ raise << maybe(get(Recipe, r).name) << "'index' on '" << base.original_string << "' can't be saved in '" << product.original_string << "'; type should be '" << names_to_string_without_quotes(element.type) << "'\n" << end();
346   ¦ break;
347   }
348   break;
349 }
350 :(before "End Primitive Recipe Implementations")
351 case INDEX: {
352   reagent/*copy*/ base = current_instruction().ingredients.at(0);
353   // Update INDEX base in Run
354   int base_address = base.value;
355   trace(9998, "run") << "base address is " << base_address << end();
356   if (base_address == 0) {
357   ¦ raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
358   ¦ break;
359   }
360   reagent/*copy*/ index = current_instruction().ingredients.at(1);
361   // Update INDEX index in Run
362   vector<double> index_val(read_memory(index));
363   if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) {
364   ¦ raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
365   ¦ break;
366   }
367   reagent/*local*/ element;
368   element.type = copy_array_element(base.type);
369   element.set_value(base_address + /*skip length*/1 + index_val.at(0)*size_of(element.type));
370   trace(9998, "run") << "address to copy is " << element.value << end();
371   trace(9998, "run") << "its type is " << to_string(element.type) << end();
372   // Read element
373   products.push_back(read_memory(element));
374   break;
375 }
376 
377 :(code)
378 type_tree* copy_array_element(const type_tree* type) {
379   return new type_tree(*array_element(type));
380 }
381 
382 type_tree* array_element(const type_tree* type) {
383   assert(type->right);
384   if (type->right->atom) {
385   ¦ return type->right;
386   }
387   else if (!type->right->right) {
388   ¦ return type->right->left;
389   }
390   // hack: support array:num:3 without requiring extra parens
391   else if (type->right->right->left && type->right->right->left->atom && is_integer(type->right->right->left->name)) {
392   ¦ assert(!type->right->right->right);
393   ¦ return type->right->left;
394   }
395   return type->right;
396 }
397 
398 int array_length(const reagent& x) {
399   // x should already be canonized.
400   // hack: look for length in type
401   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
402   ¦ ¦ && x.type->right->right->left && x.type->right->right->left->atom && is_integer(x.type->right->right->left->name)) {  // third 'type' is a number
403   ¦ // get size from type
404   ¦ return to_integer(x.type->right->right->left->name);
405   }
406   // this should never happen at transform time
407   return get_or_insert(Memory, x.value);
408 }
409 
410 :(before "End Unit Tests")
411 void test_array_length_compound() {
412   put(Memory, 1, 3);
413   put(Memory, 2, 14);
414   put(Memory, 3, 15);
415   put(Memory, 4, 16);
416   reagent x("1:array:address:num");  // 3 types, but not a static array
417   populate_value(x);
418   CHECK_EQ(array_length(x), 3);
419 }
420 
421 void test_array_length_static() {
422   reagent x("1:array:num:3");
423   CHECK_EQ(array_length(x), 3);
424 }
425 
426 :(scenario index_truncates)
427 def main [
428   1:array:num:3 <- create-array
429   2:num <- copy 14
430   3:num <- copy 15
431   4:num <- copy 16
432   5:num <- index 1:array:num:3, 1.5  # non-whole number
433 ]
434 # fraction is truncated away
435 +mem: storing 15 in location 5
436 
437 :(scenario index_out_of_bounds)
438 % Hide_errors = true;
439 def main [
440   1:array:num:3 <- create-array
441   2:num <- copy 14
442   3:num <- copy 15
443   4:num <- copy 16
444   5:num <- copy 14
445   6:num <- copy 15
446   7:num <- copy 16
447   index 1:array:num:3, 4  # less than size of array in locations, but larger than its length in elements
448 ]
449 +error: main: invalid index 4 in 'index 1:array:num:3, 4'
450 
451 :(scenario index_out_of_bounds_2)
452 % Hide_errors = true;
453 def main [
454   1:array:point:3 <- create-array
455   2:num <- copy 14
456   3:num <- copy 15
457   4:num <- copy 16
458   5:num <- copy 14
459   6:num <- copy 15
460   7:num <- copy 16
461   index 1:array:point, -1
462 ]
463 +error: main: invalid index -1 in 'index 1:array:point, -1'
464 
465 :(scenario index_product_type_mismatch)
466 % Hide_errors = true;
467 def main [
468   1:array:point:3 <- create-array
469   2:num <- copy 14
470   3:num <- copy 15
471   4:num <- copy 16
472   5:num <- copy 14
473   6:num <- copy 15
474   7:num <- copy 16
475   9:num <- index 1:array:point, 0
476 ]
477 +error: main: 'index' on '1:array:point' can't be saved in '9:num'; type should be 'point'
478 
479 //: we might want to call 'index' without saving the results, say in a sandbox
480 
481 :(scenario index_without_product)
482 def main [
483   1:array:num:3 <- create-array
484   2:num <- copy 14
485   3:num <- copy 15
486   4:num <- copy 16
487   index 1:array:num:3, 0
488 ]
489 # just don't die
490 
491 //:: To write to elements of arrays, use 'put'.
492 
493 :(scenario put_index)
494 def main [
495   1:array:num:3 <- create-array
496   2:num <- copy 14
497   3:num <- copy 15
498   4:num <- copy 16
499   1:array:num <- put-index 1:array:num, 1, 34
500 ]
501 +mem: storing 34 in location 3
502 
503 :(before "End Primitive Recipe Declarations")
504 PUT_INDEX,
505 :(before "End Primitive Recipe Numbers")
506 put(Recipe_ordinal, "put-index", PUT_INDEX);
507 :(before "End Primitive Recipe Checks")
508 case PUT_INDEX: {
509   if (SIZE(inst.ingredients) != 3) {
510   ¦ raise << maybe(get(Recipe, r).name) << "'put-index' expects exactly 3 ingredients in '" << to_original_string(inst) << "'\n" << end();
511   ¦ break;
512   }
513   reagent/*copy*/ base = inst.ingredients.at(0);
514   // Update PUT_INDEX base in Check
515   if (!is_mu_array(base)) {
516   ¦ raise << maybe(get(Recipe, r).name) << "'put-index' on a non-array '" << base.original_string << "'\n" << end();
517   ¦ break;
518   }
519   reagent/*copy*/ index = inst.ingredients.at(1);
520   // Update PUT_INDEX index in Check
521   if (!is_mu_number(index)) {
522   ¦ 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();
523   ¦ break;
524   }
525   reagent/*copy*/ value = inst.ingredients.at(2);
526   // Update PUT_INDEX value in Check
527   reagent/*local*/ element;
528   element.type = copy_array_element(base.type);
529   if (!types_coercible(element, value)) {
530   ¦ 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();
531   ¦ break;
532   }
533   if (inst.products.empty()) break;  // no more checks necessary
534   if (inst.products.at(0).name != inst.ingredients.at(0).name) {
535   ¦ 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();
536   ¦ break;
537   }
538   // End PUT_INDEX Product Checks
539   break;
540 }
541 :(before "End Primitive Recipe Implementations")
542 case PUT_INDEX: {
543   reagent/*copy*/ base = current_instruction().ingredients.at(0);
544   // Update PUT_INDEX base in Run
545   int base_address = base.value;
546   if (base_address == 0) {
547   ¦ raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
548   ¦ break;
549   }
550   reagent/*copy*/ index = current_instruction().ingredients.at(1);
551   // Update PUT_INDEX index in Run
552   vector<double> index_val(read_memory(index));
553   if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) {
554   ¦ raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
555   ¦ break;
556   }
557   int address = base_address + /*skip length*/1 + index_val.at(0)*size_of(array_element(base.type));
558   trace(9998, "run") << "address to copy to is " << address << end();
559   // optimization: directly write the element rather than updating 'product'
560   // and writing the entire array
561   write_products = false;
562   vector<double> value = read_memory(current_instruction().ingredients.at(2));
563   // Write Memory in PUT_INDEX in Run
564   for (int i = 0;  i < SIZE(value);  ++i) {
565   ¦ trace(9999, "mem") << "storing " << no_scientific(value.at(i)) << " in location " << address+i << end();
566   ¦ put(Memory, address+i, value.at(i));
567   }
568   break;
569 }
570 
571 :(scenario put_index_out_of_bounds)
572 % Hide_errors = true;
573 def main [
574   1:array:point:3 <- create-array
575   2:num <- copy 14
576   3:num <- copy 15
577   4:num <- copy 16
578   5:num <- copy 14
579   6:num <- copy 15
580   7:num <- copy 16
581   8:point <- merge 34, 35
582   1:array:point <- put-index 1:array:point, 4, 8:point  # '4' is less than size of array in locations, but larger than its length in elements
583 ]
584 +error: main: invalid index 4 in '1:array:point <- put-index 1:array:point, 4, 8:point'
585 
586 :(scenario put_index_out_of_bounds_2)
587 % Hide_errors = true;
588 def main [
589   1:array:point:3 <- create-array
590   2:num <- copy 14
591   3:num <- copy 15
592   4:num <- copy 16
593   5:num <- copy 14
594   6:num <- copy 15
595   7:num <- copy 16
596   8:point <- merge 34, 35
597   1:array:point <- put-index 1:array:point, -1, 8:point
598 ]
599 +error: main: invalid index -1 in '1:array:point <- put-index 1:array:point, -1, 8:point'
600 
601 :(scenario put_index_product_error)
602 % Hide_errors = true;
603 def main [
604   local-scope
605   load-ingredients
606   1:array:num:3 <- create-array
607   4:array:num:3 <- put-index 1:array:num:3, 0, 34
608 ]
609 +error: main: product of 'put-index' must be first ingredient '1:array:num:3', but got '4:array:num:3'
610 
611 //:: compute the length of an array
612 
613 :(scenario array_length)
614 def main [
615   1:array:num:3 <- create-array
616   2:num <- copy 14
617   3:num <- copy 15
618   4:num <- copy 16
619   5:num <- length 1:array:num:3
620 ]
621 +mem: storing 3 in location 5
622 
623 :(before "End Primitive Recipe Declarations")
624 LENGTH,
625 :(before "End Primitive Recipe Numbers")
626 put(Recipe_ordinal, "length", LENGTH);
627 :(before "End Primitive Recipe Checks")
628 case LENGTH: {
629   if (SIZE(inst.ingredients) != 1) {
630   ¦ raise << maybe(get(Recipe, r).name) << "'length' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
631   ¦ break;
632   }
633   reagent/*copy*/ array = inst.ingredients.at(0);
634   // Update LENGTH array in Check
635   if (!is_mu_array(array)) {
636   ¦ raise << "tried to calculate length of non-array '" << array.original_string << "'\n" << end();
637   ¦ break;
638   }
639   break;
640 }
641 :(before "End Primitive Recipe Implementations")
642 case LENGTH: {
643   reagent/*copy*/ array = current_instruction().ingredients.at(0);
644   // Update LENGTH array in Run
645   if (array.value == 0) {
646   ¦ raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
647   ¦ break;
648   }
649   products.resize(1);
650   products.at(0).push_back(get_or_insert(Memory, array.value));
651   break;
652 }
653 
654 //: optimization: none of the instructions in this layer use 'ingredients' so
655 //: stop copying potentially huge arrays into it.
656 :(before "End should_copy_ingredients Special-cases")
657 recipe_ordinal r = current_instruction().operation;
658 if (r == CREATE_ARRAY || r == INDEX || r == PUT_INDEX || r == LENGTH)
659   return false;