fix other mandelbrot variants
[mu.git] / archive / 1.vm / 032array.cc
blob30adde20258226d27e5a0aa048a9bd75602873e2
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.
8 //: You can create arrays using 'create-array'.
9 void test_create_array() {
10 run(
11 "def main [\n"
12 // create an array occupying locations 1 (for the size) and 2-4 (for the elements)
13 " 1:array:num:3 <- create-array\n"
14 "]\n"
16 CHECK_TRACE_CONTENTS(
17 "run: creating array from 4 locations\n"
21 :(before "End Primitive Recipe Declarations")
22 CREATE_ARRAY,
23 :(before "End Primitive Recipe Numbers")
24 put(Recipe_ordinal, "create-array", CREATE_ARRAY);
25 :(before "End Primitive Recipe Checks")
26 case CREATE_ARRAY: {
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();
29 break;
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();
35 break;
37 if (!product.type->right) {
38 raise << maybe(get(Recipe, r).name) << "create array of what? '" << to_original_string(inst) << "'\n" << end();
39 break;
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();
45 break;
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();
51 break;
53 break;
55 :(before "End Primitive Recipe Implementations")
56 case CREATE_ARRAY: {
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();
69 // initialize array
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;
74 break;
77 :(code)
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() {
85 run(
86 "def main [\n"
87 " 1:array:num:3 <- create-array\n"
88 " 2:num <- copy 14\n"
89 " 3:num <- copy 15\n"
90 " 4:num <- copy 16\n"
91 " 5:array:num <- copy 1:array:num:3\n"
92 "]\n"
94 CHECK_TRACE_CONTENTS(
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() {
103 run(
104 "def main [\n"
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"
110 "]\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();
125 return 1;
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);
132 :(code)
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';
140 assert(false);
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
150 //: advance
152 :(code)
153 void test_container_permits_static_array_element() {
154 run(
155 "container foo [\n"
156 " x:array:num:3\n"
157 "]\n"
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
164 type->value = 0;
167 :(code)
168 void test_container_disallows_dynamic_array_element() {
169 Hide_errors = true;
170 run(
171 "container foo [\n"
172 " x:array:num\n"
173 "]\n"
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();
185 continue;
187 if (type && !type->atom && type->left->atom && type->left->name == "array") {
188 if (!type->right) {
189 raise << "container '" << name << "' doesn't specify type of array elements for '" << info.elements.back().name << "'\n" << end();
190 continue;
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();
194 continue;
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) {
204 return false;
207 :(code)
208 void test_merge_static_array_into_container() {
209 run(
210 "container foo [\n"
211 " x:num\n"
212 " y:array:num:3\n"
213 "]\n"
214 "def main [\n"
215 " 1:array:num:3 <- create-array\n"
216 " 10:foo <- merge 34, 1:array:num:3\n"
217 "]\n"
219 // no errors
222 void test_code_inside_container() {
223 Hide_errors = true;
224 run(
225 "container card [\n"
226 " rank:num <- next-ingredient\n"
227 "]\n"
228 "def foo [\n"
229 " 1:card <- merge 3\n"
230 " 2:num <- get 1:card rank:offset\n"
231 "]\n"
233 // shouldn't die
236 //:: To access elements of an array, use 'index'
238 void test_index() {
239 run(
240 "def main [\n"
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
246 "]\n"
248 CHECK_TRACE_CONTENTS(
249 "mem: storing 14 in location 10\n"
253 void test_index_compound_element() {
254 run(
255 "def main [\n"
256 " {1: (array (address number) 3)} <- create-array\n"
257 // skip alloc id
258 " 3:num <- copy 14\n"
259 // skip alloc id
260 " 5:num <- copy 15\n"
261 // skip alloc id
262 " 7:num <- copy 16\n"
263 " 10:address:num <- index {1: (array (address number) 3)}, 0\n"
264 "]\n"
266 CHECK_TRACE_CONTENTS(
267 // skip alloc id
268 "mem: storing 14 in location 11\n"
272 void test_index_direct_offset() {
273 run(
274 "def main [\n"
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"
281 "]\n"
283 CHECK_TRACE_CONTENTS(
284 "mem: storing 14 in location 20\n"
288 :(before "End Primitive Recipe Declarations")
289 INDEX,
290 :(before "End Primitive Recipe Numbers")
291 put(Recipe_ordinal, "index", INDEX);
292 :(before "End Primitive Recipe Checks")
293 case INDEX: {
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();
296 break;
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();
302 break;
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();
308 break;
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();
316 break;
318 break;
320 :(before "End Primitive Recipe Implementations")
321 case INDEX: {
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();
328 break;
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();
335 break;
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();
341 // Read element
342 products.push_back(read_memory(element));
343 break;
346 :(code)
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) {
352 assert(type->right);
353 if (type->right->atom) {
354 return type->right;
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;
364 return type->right;
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() {
381 put(Memory, 1, 3);
382 put(Memory, 2, 14);
383 put(Memory, 3, 15);
384 put(Memory, 4, 16);
385 reagent x("1:array:address:num"); // 3 types, but not a static array
386 populate_value(x);
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() {
396 run(
397 "def main [\n"
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
403 "]\n"
405 CHECK_TRACE_CONTENTS(
406 // fraction is truncated away
407 "mem: storing 15 in location 10\n"
411 void test_index_out_of_bounds() {
412 Hide_errors = true;
413 run(
414 "def main [\n"
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
417 "]\n"
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() {
425 Hide_errors = true;
426 run(
427 "def main [\n"
428 " 1:array:num:3 <- create-array\n"
429 " index 1:array:num, -1\n"
430 "]\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() {
438 Hide_errors = true;
439 run(
440 "def main [\n"
441 " 1:array:point:3 <- create-array\n"
442 " 10:num <- index 1:array:point, 0\n"
443 "]\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() {
453 run(
454 "def main [\n"
455 " 1:array:num:3 <- create-array\n"
456 " index 1:array:num:3, 0\n"
457 "]\n"
459 // just don't die
462 //:: To write to elements of arrays, use 'put'.
464 void test_put_index() {
465 run(
466 "def main [\n"
467 " 1:array:num:3 <- create-array\n"
468 " 1:array:num <- put-index 1:array:num, 1, 34\n"
469 "]\n"
471 CHECK_TRACE_CONTENTS(
472 "mem: storing 34 in location 3\n"
476 :(before "End Primitive Recipe Declarations")
477 PUT_INDEX,
478 :(before "End Primitive Recipe Numbers")
479 put(Recipe_ordinal, "put-index", PUT_INDEX);
480 :(before "End Primitive Recipe Checks")
481 case PUT_INDEX: {
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();
484 break;
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();
490 break;
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();
496 break;
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();
503 break;
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();
508 break;
510 // End PUT_INDEX Product Checks
511 break;
513 :(before "End Primitive Recipe Implementations")
514 case PUT_INDEX: {
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();
520 break;
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();
527 break;
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));
540 break;
543 :(code)
544 void test_put_index_out_of_bounds() {
545 Hide_errors = true;
546 run(
547 "def main [\n"
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
551 "]\n"
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() {
559 Hide_errors = true;
560 run(
561 "def main [\n"
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"
565 "]\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() {
573 Hide_errors = true;
574 run(
575 "def main [\n"
576 " 1:array:num:3 <- create-array\n"
577 " 4:array:num:3 <- put-index 1:array:num:3, 0, 34\n"
578 "]\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() {
588 run(
589 "def main [\n"
590 " 1:array:num:3 <- create-array\n"
591 " 10:num <- length 1:array:num\n"
592 "]\n"
594 CHECK_TRACE_CONTENTS(
595 "mem: storing 3 in location 10\n"
599 :(before "End Primitive Recipe Declarations")
600 LENGTH,
601 :(before "End Primitive Recipe Numbers")
602 put(Recipe_ordinal, "length", LENGTH);
603 :(before "End Primitive Recipe Checks")
604 case LENGTH: {
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();
607 break;
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();
613 break;
615 break;
617 :(before "End Primitive Recipe Implementations")
618 case LENGTH: {
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();
623 break;
625 products.resize(1);
626 products.at(0).push_back(get_or_insert(Memory, array.value));
627 break;
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)
635 return false;