1 //: Arrays contain a variable number of elements of the same type. Their value
2 //: starts with the length of the array.
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.
8 //: You can create arrays using 'create-array'.
9 void test_create_array() {
12 // create an array occupying locations 1 (for the size) and 2-4 (for the elements)
13 " 1:array:num:3 <- create-array\n"
17 "run: creating array from 4 locations\n"
21 :(before
"End Primitive Recipe Declarations")
23 :(before
"End Primitive Recipe Numbers")
24 put(Recipe_ordinal
, "create-array", CREATE_ARRAY
);
25 :(before
"End Primitive Recipe Checks")
27 if (inst
.products
.empty()) {
28 raise
<< maybe(get(Recipe
, r
).name
) << "'create-array' needs one product and no ingredients but got '" << to_original_string(inst
) << '\n' << end();
31 reagent
/*copy*/ product
= inst
.products
.at(0);
32 // Update CREATE_ARRAY product in Check
33 if (!is_mu_array(product
)) {
34 raise
<< maybe(get(Recipe
, r
).name
) << "'create-array' cannot create non-array '" << product
.original_string
<< "'\n" << end();
37 if (!product
.type
->right
) {
38 raise
<< maybe(get(Recipe
, r
).name
) << "create array of what? '" << to_original_string(inst
) << "'\n" << end();
41 // 'create-array' will need to check properties rather than types
42 type_tree
* array_length_from_type
= product
.type
->right
->right
;
43 if (!array_length_from_type
) {
44 raise
<< maybe(get(Recipe
, r
).name
) << "create array of what size? '" << to_original_string(inst
) << "'\n" << end();
47 if (!product
.type
->right
->right
->atom
)
48 array_length_from_type
= array_length_from_type
->left
;
49 if (!is_integer(array_length_from_type
->name
)) {
50 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();
55 :(before
"End Primitive Recipe Implementations")
57 reagent
/*copy*/ product
= current_instruction().products
.at(0);
58 // Update CREATE_ARRAY product in Run
59 int base_address
= product
.value
;
60 type_tree
* array_length_from_type
= product
.type
->right
->right
;
61 if (!product
.type
->right
->right
->atom
)
62 array_length_from_type
= array_length_from_type
->left
;
63 int array_length
= to_integer(array_length_from_type
->name
);
64 // initialize array length, so that size_of will work
65 trace(Callstack_depth
+1, "mem") << "storing " << array_length
<< " in location " << base_address
<< end();
66 put(Memory
, base_address
, array_length
); // in array elements
67 int size
= size_of(product
); // in locations
68 trace(Callstack_depth
+1, "run") << "creating array from " << size
<< " locations" << end();
70 for (int i
= 1; i
<= size_of(product
); ++i
)
71 put(Memory
, base_address
+i
, 0);
72 // no need to update product
73 write_products
= false;
78 // Arrays can be copied around with a single instruction just like numbers,
79 // no matter how large they are.
80 // You don't need to pass the size around, since each array variable stores its
81 // size in memory at run-time. We'll call a variable with an explicit size a
82 // 'static' array, and one without a 'dynamic' array since it can contain
83 // arrays of many different sizes.
84 void test_copy_array() {
87 " 1:array:num:3 <- create-array\n"
91 " 5:array:num <- copy 1:array:num:3\n"
95 "mem: storing 3 in location 5\n"
96 "mem: storing 14 in location 6\n"
97 "mem: storing 15 in location 7\n"
98 "mem: storing 16 in location 8\n"
102 void test_stash_array() {
105 " 1:array:num:3 <- create-array\n"
106 " 2:num <- copy 14\n"
107 " 3:num <- copy 15\n"
108 " 4:num <- copy 16\n"
109 " stash [foo:], 1:array:num:3\n"
112 CHECK_TRACE_CONTENTS(
113 "app: foo: 3 14 15 16\n"
117 :(before
"End types_coercible Special-cases")
118 if (is_mu_array(from
) && is_mu_array(to
))
119 return types_strictly_match(array_element(from
.type
), array_element(to
.type
));
121 :(before
"End size_of(reagent r) Special-cases")
122 if (!r
.type
->atom
&& r
.type
->left
->atom
&& r
.type
->left
->value
== Array_type_ordinal
) {
123 if (!r
.type
->right
) {
124 raise
<< maybe(current_recipe_name()) << "'" << r
.original_string
<< "' is an array of what?\n" << end();
127 return /*space for length*/1 + array_length(r
)*size_of(array_element(r
.type
));
130 :(before
"End size_of(type) Non-atom Special-cases")
131 if (type
->left
->value
== Array_type_ordinal
) return static_array_length(type
);
133 int static_array_length(const type_tree
* type
) {
134 if (!type
->atom
&& type
->right
&& !type
->right
->atom
&& type
->right
->right
&& !type
->right
->right
->atom
&& !type
->right
->right
->right
// exactly 3 types
135 && type
->right
->right
->left
&& type
->right
->right
->left
->atom
&& is_integer(type
->right
->right
->left
->name
)) { // third 'type' is a number
136 // get size from type
137 return to_integer(type
->right
->right
->left
->name
);
139 cerr
<< to_string(type
) << '\n';
143 //: disable the size mismatch check for arrays since the destination array
144 //: need not be initialized
145 :(before
"End size_mismatch(x) Special-cases")
146 if (x
.type
&& !x
.type
->atom
&& x
.type
->left
->value
== Array_type_ordinal
) return false;
148 //:: arrays inside containers
149 //: arrays are disallowed inside containers unless their length is fixed in
153 void test_container_permits_static_array_element() {
159 CHECK_TRACE_COUNT("error", 0);
162 :(before
"End insert_container Special-cases")
163 else if (is_integer(type
->name
)) { // sometimes types will contain non-type tags, like numbers for the size of an array
168 void test_container_disallows_dynamic_array_element() {
175 CHECK_TRACE_CONTENTS(
176 "error: container 'foo' cannot determine size of element 'x'\n"
180 :(before
"End Load Container Element Definition")
182 const type_tree
* type
= info
.elements
.back().type
;
183 if (type
&& type
->atom
&& type
->name
== "array") {
184 raise
<< "container '" << name
<< "' doesn't specify type of array elements for '" << info
.elements
.back().name
<< "'\n" << end();
187 if (type
&& !type
->atom
&& type
->left
->atom
&& type
->left
->name
== "array") {
189 raise
<< "container '" << name
<< "' doesn't specify type of array elements for '" << info
.elements
.back().name
<< "'\n" << end();
192 if (!type
->right
->right
|| !is_integer(type
->right
->right
->left
->name
)) { // array has no length
193 raise
<< "container '" << name
<< "' cannot determine size of element '" << info
.elements
.back().name
<< "'\n" << end();
199 //: disable the size mismatch check for 'merge' instructions since containers
200 //: can contain arrays, and since we already do plenty of checking for them
201 :(before
"End size_mismatch(x) Special-cases")
202 if (current_call().running_step_index
< SIZE(get(Recipe
, current_call().running_recipe
).steps
)
203 && current_instruction().operation
== MERGE
) {
208 void test_merge_static_array_into_container() {
215 " 1:array:num:3 <- create-array\n"
216 " 10:foo <- merge 34, 1:array:num:3\n"
222 void test_code_inside_container() {
226 " rank:num <- next-ingredient\n"
229 " 1:card <- merge 3\n"
230 " 2:num <- get 1:card rank:offset\n"
236 //:: To access elements of an array, use 'index'
241 " 1:array:num:3 <- create-array\n"
242 " 2:num <- copy 14\n"
243 " 3:num <- copy 15\n"
244 " 4:num <- copy 16\n"
245 " 10:num <- index 1:array:num:3, 0/index\n" // the index must be a non-negative whole number
248 CHECK_TRACE_CONTENTS(
249 "mem: storing 14 in location 10\n"
253 void test_index_compound_element() {
256 " {1: (array (address number) 3)} <- create-array\n"
258 " 3:num <- copy 14\n"
260 " 5:num <- copy 15\n"
262 " 7:num <- copy 16\n"
263 " 10:address:num <- index {1: (array (address number) 3)}, 0\n"
266 CHECK_TRACE_CONTENTS(
268 "mem: storing 14 in location 11\n"
272 void test_index_direct_offset() {
275 " 1:array:num:3 <- create-array\n"
276 " 2:num <- copy 14\n"
277 " 3:num <- copy 15\n"
278 " 4:num <- copy 16\n"
279 " 10:num <- copy 0\n"
280 " 20:num <- index 1:array:num, 10:num\n"
283 CHECK_TRACE_CONTENTS(
284 "mem: storing 14 in location 20\n"
288 :(before
"End Primitive Recipe Declarations")
290 :(before
"End Primitive Recipe Numbers")
291 put(Recipe_ordinal
, "index", INDEX
);
292 :(before
"End Primitive Recipe Checks")
294 if (SIZE(inst
.ingredients
) != 2) {
295 raise
<< maybe(get(Recipe
, r
).name
) << "'index' expects exactly 2 ingredients in '" << to_original_string(inst
) << "'\n" << end();
298 reagent
/*copy*/ base
= inst
.ingredients
.at(0);
299 // Update INDEX base in Check
300 if (!is_mu_array(base
)) {
301 raise
<< maybe(get(Recipe
, r
).name
) << "'index' on a non-array '" << base
.original_string
<< "'\n" << end();
304 reagent
/*copy*/ index
= inst
.ingredients
.at(1);
305 // Update INDEX index in Check
306 if (!is_mu_number(index
)) {
307 raise
<< maybe(get(Recipe
, r
).name
) << "second ingredient of 'index' should be a number, but got '" << index
.original_string
<< "'\n" << end();
310 if (inst
.products
.empty()) break;
311 reagent
/*copy*/ product
= inst
.products
.at(0);
312 // Update INDEX product in Check
313 reagent
/*local*/ element(copy_array_element(base
.type
));
314 if (!types_coercible(product
, element
)) {
315 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();
320 :(before
"End Primitive Recipe Implementations")
322 reagent
/*copy*/ base
= current_instruction().ingredients
.at(0);
323 // Update INDEX base in Run
324 int base_address
= base
.value
;
325 trace(Callstack_depth
+1, "run") << "base address is " << base_address
<< end();
326 if (base_address
== 0) {
327 raise
<< maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
330 reagent
/*copy*/ index
= current_instruction().ingredients
.at(1);
331 // Update INDEX index in Run
332 vector
<double> index_val(read_memory(index
));
333 if (index_val
.at(0) < 0 || index_val
.at(0) >= get_or_insert(Memory
, base_address
)) {
334 raise
<< maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val
.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
337 reagent
/*local*/ element(copy_array_element(base
.type
));
338 element
.set_value(base_address
+ /*skip length*/1 + index_val
.at(0)*size_of(element
.type
));
339 trace(Callstack_depth
+1, "run") << "address to copy is " << element
.value
<< end();
340 trace(Callstack_depth
+1, "run") << "its type is " << to_string(element
.type
) << end();
342 products
.push_back(read_memory(element
));
347 type_tree
* copy_array_element(const type_tree
* type
) {
348 return new type_tree(*array_element(type
));
351 type_tree
* array_element(const type_tree
* type
) {
353 if (type
->right
->atom
) {
356 else if (!type
->right
->right
) {
357 return type
->right
->left
;
359 // hack: support array:num:3 without requiring extra parens
360 else if (type
->right
->right
->left
&& type
->right
->right
->left
->atom
&& is_integer(type
->right
->right
->left
->name
)) {
361 assert(!type
->right
->right
->right
);
362 return type
->right
->left
;
367 int array_length(const reagent
& x
) {
368 // x should already be canonized.
369 // hack: look for length in type
370 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
371 && x
.type
->right
->right
->left
&& x
.type
->right
->right
->left
->atom
&& is_integer(x
.type
->right
->right
->left
->name
)) { // third 'type' is a number
372 // get size from type
373 return to_integer(x
.type
->right
->right
->left
->name
);
375 // this should never happen at transform time
376 return get_or_insert(Memory
, x
.value
);
379 :(before
"End Unit Tests")
380 void test_array_length_compound() {
385 reagent
x("1:array:address:num"); // 3 types, but not a static array
387 CHECK_EQ(array_length(x
), 3);
390 void test_array_length_static() {
391 reagent
x("1:array:num:3");
392 CHECK_EQ(array_length(x
), 3);
395 void test_index_truncates() {
398 " 1:array:num:3 <- create-array\n"
399 " 2:num <- copy 14\n"
400 " 3:num <- copy 15\n"
401 " 4:num <- copy 16\n"
402 " 10:num <- index 1:array:num:3, 1.5\n" // non-whole number
405 CHECK_TRACE_CONTENTS(
406 // fraction is truncated away
407 "mem: storing 15 in location 10\n"
411 void test_index_out_of_bounds() {
415 " 1:array:point:3 <- create-array\n"
416 " index 1:array:point:3, 4\n" // less than size of array in locations, but larger than its length in elements
419 CHECK_TRACE_CONTENTS(
420 "error: main: invalid index 4 in 'index 1:array:point:3, 4'\n"
424 void test_index_out_of_bounds_2() {
428 " 1:array:num:3 <- create-array\n"
429 " index 1:array:num, -1\n"
432 CHECK_TRACE_CONTENTS(
433 "error: main: invalid index -1 in 'index 1:array:num, -1'\n"
437 void test_index_product_type_mismatch() {
441 " 1:array:point:3 <- create-array\n"
442 " 10:num <- index 1:array:point, 0\n"
445 CHECK_TRACE_CONTENTS(
446 "error: main: 'index' on '1:array:point' can't be saved in '10:num'; type should be 'point'\n"
450 //: we might want to call 'index' without saving the results, say in a sandbox
452 void test_index_without_product() {
455 " 1:array:num:3 <- create-array\n"
456 " index 1:array:num:3, 0\n"
462 //:: To write to elements of arrays, use 'put'.
464 void test_put_index() {
467 " 1:array:num:3 <- create-array\n"
468 " 1:array:num <- put-index 1:array:num, 1, 34\n"
471 CHECK_TRACE_CONTENTS(
472 "mem: storing 34 in location 3\n"
476 :(before
"End Primitive Recipe Declarations")
478 :(before
"End Primitive Recipe Numbers")
479 put(Recipe_ordinal
, "put-index", PUT_INDEX
);
480 :(before
"End Primitive Recipe Checks")
482 if (SIZE(inst
.ingredients
) != 3) {
483 raise
<< maybe(get(Recipe
, r
).name
) << "'put-index' expects exactly 3 ingredients in '" << to_original_string(inst
) << "'\n" << end();
486 reagent
/*copy*/ base
= inst
.ingredients
.at(0);
487 // Update PUT_INDEX base in Check
488 if (!is_mu_array(base
)) {
489 raise
<< maybe(get(Recipe
, r
).name
) << "'put-index' on a non-array '" << base
.original_string
<< "'\n" << end();
492 reagent
/*copy*/ index
= inst
.ingredients
.at(1);
493 // Update PUT_INDEX index in Check
494 if (!is_mu_number(index
)) {
495 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();
498 reagent
/*copy*/ value
= inst
.ingredients
.at(2);
499 // Update PUT_INDEX value in Check
500 reagent
/*local*/ element(copy_array_element(base
.type
));
501 if (!types_coercible(element
, value
)) {
502 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();
505 if (inst
.products
.empty()) break; // no more checks necessary
506 if (inst
.products
.at(0).name
!= inst
.ingredients
.at(0).name
) {
507 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();
510 // End PUT_INDEX Product Checks
513 :(before
"End Primitive Recipe Implementations")
515 reagent
/*copy*/ base
= current_instruction().ingredients
.at(0);
516 // Update PUT_INDEX base in Run
517 int base_address
= base
.value
;
518 if (base_address
== 0) {
519 raise
<< maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
522 reagent
/*copy*/ index
= current_instruction().ingredients
.at(1);
523 // Update PUT_INDEX index in Run
524 vector
<double> index_val(read_memory(index
));
525 if (index_val
.at(0) < 0 || index_val
.at(0) >= get_or_insert(Memory
, base_address
)) {
526 raise
<< maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val
.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
529 int address
= base_address
+ /*skip length*/1 + index_val
.at(0)*size_of(array_element(base
.type
));
530 trace(Callstack_depth
+1, "run") << "address to copy to is " << address
<< end();
531 // optimization: directly write the element rather than updating 'product'
532 // and writing the entire array
533 write_products
= false;
534 vector
<double> value
= read_memory(current_instruction().ingredients
.at(2));
535 // Write Memory in PUT_INDEX in Run
536 for (int i
= 0; i
< SIZE(value
); ++i
) {
537 trace(Callstack_depth
+1, "mem") << "storing " << no_scientific(value
.at(i
)) << " in location " << address
+i
<< end();
538 put(Memory
, address
+i
, value
.at(i
));
544 void test_put_index_out_of_bounds() {
548 " 1:array:point:3 <- create-array\n"
549 " 8:point <- merge 34, 35\n"
550 " 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
553 CHECK_TRACE_CONTENTS(
554 "error: main: invalid index 4 in '1:array:point <- put-index 1:array:point, 4, 8:point'\n"
558 void test_put_index_out_of_bounds_2() {
562 " 1:array:point:3 <- create-array\n"
563 " 10:point <- merge 34, 35\n"
564 " 1:array:point <- put-index 1:array:point, -1, 10:point\n"
567 CHECK_TRACE_CONTENTS(
568 "error: main: invalid index -1 in '1:array:point <- put-index 1:array:point, -1, 10:point'\n"
572 void test_put_index_product_error() {
576 " 1:array:num:3 <- create-array\n"
577 " 4:array:num:3 <- put-index 1:array:num:3, 0, 34\n"
580 CHECK_TRACE_CONTENTS(
581 "error: main: product of 'put-index' must be first ingredient '1:array:num:3', but got '4:array:num:3'\n"
585 //:: compute the length of an array
587 void test_array_length() {
590 " 1:array:num:3 <- create-array\n"
591 " 10:num <- length 1:array:num\n"
594 CHECK_TRACE_CONTENTS(
595 "mem: storing 3 in location 10\n"
599 :(before
"End Primitive Recipe Declarations")
601 :(before
"End Primitive Recipe Numbers")
602 put(Recipe_ordinal
, "length", LENGTH
);
603 :(before
"End Primitive Recipe Checks")
605 if (SIZE(inst
.ingredients
) != 1) {
606 raise
<< maybe(get(Recipe
, r
).name
) << "'length' expects exactly 2 ingredients in '" << to_original_string(inst
) << "'\n" << end();
609 reagent
/*copy*/ array
= inst
.ingredients
.at(0);
610 // Update LENGTH array in Check
611 if (!is_mu_array(array
)) {
612 raise
<< "tried to calculate length of non-array '" << array
.original_string
<< "'\n" << end();
617 :(before
"End Primitive Recipe Implementations")
619 reagent
/*copy*/ array
= current_instruction().ingredients
.at(0);
620 // Update LENGTH array in Run
621 if (array
.value
== 0) {
622 raise
<< maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
626 products
.at(0).push_back(get_or_insert(Memory
, array
.value
));
630 //: optimization: none of the instructions in this layer use 'ingredients' so
631 //: stop copying potentially huge arrays into it.
632 :(before
"End should_copy_ingredients Special-cases")
633 recipe_ordinal r
= current_instruction().operation
;
634 if (r
== CREATE_ARRAY
|| r
== INDEX
|| r
== PUT_INDEX
|| r
== LENGTH
)