1
2
3
4
5
6
7
8
9 :(scenario create_array)
10 def main [
11
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 '" << inst.original_string << '\n' << end();
24 ¦ break;
25 }
26 reagent product = inst.products.at(0);
27
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? '" << inst.original_string << "'\n" << end();
34 ¦ break;
35 }
36
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? '" << inst.original_string << "'\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 product = current_instruction().products.at(0);
53
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
60 trace(9999, "mem") << "storing " << array_length << " in location " << base_address << end();
61 put(Memory, base_address, array_length);
62 int size = size_of(product);
63 trace(9998, "run") << "creating array of size " << size << end();
64
65 for (int i = 1; i <= size_of(product); ++i) {
66 ¦ put(Memory, base_address+i, 0);
67 }
68
69 goto finish_instruction;
70 }
71
72 :(scenario copy_array)
73
74
75
76
77
78
79 def main [
80 1:array:num:3 <- create-array
81 2:num <- copy 14
82 3:num <- copy 15
83 4:num <- copy 16
84 5:array:num <- copy 1:array:num:3
85 ]
86 +mem: storing 3 in location 5
87 +mem: storing 14 in location 6
88 +mem: storing 15 in location 7
89 +mem: storing 16 in location 8
90
91 :(scenario stash_array)
92 def main [
93 1:array:num:3 <- create-array
94 2:num <- copy 14
95 3:num <- copy 15
96 4:num <- copy 16
97 stash [foo:], 1:array:num:3
98 ]
99 +app: foo: 3 14 15 16
100
101 :(before "End types_coercible Special-cases")
102 if (is_mu_array(from) && is_mu_array(to))
103 return types_strictly_match(array_element(from.type), array_element(to.type));
104
105 :(before "End size_of(reagent r) Special-cases")
106 if (!r.type->atom && r.type->left->atom && r.type->left->value == get(Type_ordinal, "array")) {
107 if (!r.type->right) {
108 ¦ raise << maybe(current_recipe_name()) << "'" << r.original_string << "' is an array of what?\n" << end();
109 ¦ return 1;
110 }
111 return 1 + array_length(r)*size_of(array_element(r.type));
112 }
113
114 :(before "End size_of(type) Non-atom Special-cases")
115 if (type->left->value == get(Type_ordinal, "array")) return static_array_length(type);
116 :(code)
117 int static_array_length(const type_tree* type) {
118 if (!type->atom && type->right && !type->right->atom && type->right->right && !type->right->right->atom && !type->right->right->right
119 ¦ ¦ && type->right->right->left && type->right->right->left->atom && is_integer(type->right->right->left->name)) {
120 ¦
121 ¦ return to_integer(type->right->right->left->name);
122 }
123 cerr << to_string(type) << '\n';
124 assert(false);
125 }
126
127
128
129 :(before "End size_mismatch(x) Special-cases")
130 if (x.type && !x.type->atom && x.type->left->value == get(Type_ordinal, "array")) return false;
131
132
133
134
135
136 :(scenario container_permits_static_array_element)
137 container foo [
138 x:array:num:3
139 ]
140 $error: 0
141
142 :(before "End insert_container Special-cases")
143 else if (is_integer(type->name)) {
144 type->value = 0;
145 }
146
147 :(scenario container_disallows_dynamic_array_element)
148 % Hide_errors = true;
149 container foo [
150 x:array:num
151 ]
152 +error: container 'foo' cannot determine size of element 'x'
153
154 :(before "End Load Container Element Definition")
155 {
156 const type_tree* type = info.elements.back().type;
157 if (type && type->atom && type->name == "array") {
158 ¦ raise << "container '" << name << "' doesn't specify type of array elements for '" << info.elements.back().name << "'\n" << end();
159 ¦ continue;
160 }
161 if (type && !type->atom && type->left->atom && type->left->name == "array") {
162 ¦ if (!type->right) {
163 ¦ ¦ raise << "container '" << name << "' doesn't specify type of array elements for '" << info.elements.back().name << "'\n" << end();
164 ¦ ¦ continue;
165 ¦ }
166 ¦ if (!type->right->right || !is_integer(type->right->right->left->name)) {
167 ¦ ¦ raise << "container '" << name << "' cannot determine size of element '" << info.elements.back().name << "'\n" << end();
168 ¦ ¦ continue;
169 ¦ }
170 }
171 }
172
173
174
175 :(before "End size_mismatch(x) Special-cases")
176 if (current_call().running_step_index < SIZE(get(Recipe, current_call().running_recipe).steps)
177 ¦ && current_instruction().operation == MERGE) {
178 return false;
179 }
180
181 :(scenario merge_static_array_into_container)
182 container foo [
183 x:num
184 y:array:num:3
185 ]
186 def main [
187 1:array:num:3 <- create-array
188 10:foo <- merge 34, 1:array:num:3
189 ]
190
191
192 :(scenario code_inside_container)
193 % Hide_errors = true;
194 container card [
195 rank:num <- next-ingredient
196 ]
197 def foo [
198 1:card <- merge 3
199 2:num <- get 1:card rank:offset
200 ]
201
202
203
204
205
206 :(before "End compute_container_sizes Non-atom Special-cases")
207 else if (type->left->name == "array")
208 compute_container_sizes(array_element(type), pending_metadata, location_for_error_messages);
209
210 :(before "End Unit Tests")
211 void test_container_sizes_from_array() {
212
213 reagent container("x:point");
214 CHECK(!contains_key(Container_metadata, container.type));
215
216 reagent r("x:array:point");
217 compute_container_sizes(r, "");
218 CHECK(contains_key(Container_metadata, container.type));
219 CHECK_EQ(get(Container_metadata, container.type).size, 2);
220 }
221
222 void test_container_sizes_from_address_to_array() {
223
224 reagent container("x:point");
225 CHECK(!contains_key(Container_metadata, container.type));
226
227 reagent r("x:address:array:point");
228 compute_container_sizes(r, "");
229 CHECK(contains_key(Container_metadata, container.type));
230 CHECK_EQ(get(Container_metadata, container.type).size, 2);
231 }
232
233 void test_container_sizes_from_static_array() {
234
235 reagent container("x:point");
236 int old_size = SIZE(Container_metadata);
237
238 reagent r("x:array:point:10");
239 compute_container_sizes(r, "");
240 CHECK(contains_key(Container_metadata, container.type));
241 CHECK_EQ(get(Container_metadata, container.type).size, 2);
242
243 CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
244 }
245
246 void test_container_sizes_from_address_to_static_array() {
247
248 reagent container("x:point");
249 int old_size = SIZE(Container_metadata);
250
251 reagent r("x:address:array:point:10");
252 compute_container_sizes(r, "");
253 CHECK(contains_key(Container_metadata, container.type));
254 CHECK_EQ(get(Container_metadata, container.type).size, 2);
255
256 CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
257 }
258
259 void test_container_sizes_from_repeated_address_and_array_types() {
260
261 reagent container("x:point");
262 int old_size = SIZE(Container_metadata);
263
264 reagent r("x:address:array:address:array:point:10");
265 compute_container_sizes(r, "");
266 CHECK(contains_key(Container_metadata, container.type));
267 CHECK_EQ(get(Container_metadata, container.type).size, 2);
268
269 CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
270 }
271
272
273
274 :(scenario index)
275 def main [
276 1:array:num:3 <- create-array
277 2:num <- copy 14
278 3:num <- copy 15
279 4:num <- copy 16
280 5:num <- index 1:array:num:3, 0/index
281 ]
282 +mem: storing 14 in location 5
283
284 :(scenario index_compound_element)
285 def main [
286 {1: (array (address number) 3)} <- create-array
287 2:num <- copy 14
288 3:num <- copy 15
289 4:num <- copy 16
290 5:address:num <- index {1: (array (address number) 3)}, 0
291 ]
292 +mem: storing 14 in location 5
293
294 :(scenario index_direct_offset)
295 def main [
296 1:array:num:3 <- create-array
297 2:num <- copy 14
298 3:num <- copy 15
299 4:num <- copy 16
300 5:num <- copy 0
301 6:num <- index 1:array:num, 5:num
302 ]
303 +mem: storing 14 in location 6
304
305 :(before "End Primitive Recipe Declarations")
306 INDEX,
307 :(before "End Primitive Recipe Numbers")
308 put(Recipe_ordinal, "index", INDEX);
309 :(before "End Primitive Recipe Checks")
310 case INDEX: {
311 if (SIZE(inst.ingredients) != 2) {
312 ¦ raise << maybe(get(Recipe, r).name) << "'index' expects exactly 2 ingredients in '" << inst.original_string << "'\n" << end();
313 ¦ break;
314 }
315 reagent base = inst.ingredients.at(0);
316
317 if (!is_mu_array(base)) {
318 ¦ raise << maybe(get(Recipe, r).name) << "'index' on a non-array '" << base.original_string << "'\n" << end();
319 ¦ break;
320 }
321 reagent index = inst.ingredients.at(1);
322
323 if (!is_mu_number(index)) {
324 ¦ raise << maybe(get(Recipe, r).name) << "second ingredient of 'index' should be a number, but got '" << index.original_string << "'\n" << end();
325 ¦ break;
326 }
327 if (inst.products.empty()) break;
328 reagent product = inst.products.at(0);
329
330 reagent element;
331 element.type = copy_array_element(base.type);
332 if (!types_coercible(product, element)) {
333 ¦ 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();
334 ¦ break;
335 }
336 break;
337 }
338 :(before "End Primitive Recipe Implementations")
339 case INDEX: {
340 reagent base = current_instruction().ingredients.at(0);
341
342 int base_address = base.value;
343 trace(9998, "run") << "base address is " << base_address << end();
344 if (base_address == 0) {
345 ¦ raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
346 ¦ break;
347 }
348 reagent index = current_instruction().ingredients.at(1);
349
350 vector<double> index_val(read_memory(index));
351 if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) {
352 ¦ raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
353 ¦ break;
354 }
355 reagent element;
356 element.type = copy_array_element(base.type);
357 element.set_value(base_address + 1 + index_val.at(0)*size_of(element.type));
358 trace(9998, "run") << "address to copy is " << element.value << end();
359 trace(9998, "run") << "its type is " << to_string(element.type) << end();
360
361 products.push_back(read_memory(element));
362 break;
363 }
364
365 :(code)
366 type_tree* copy_array_element(const type_tree* type) {
367 return new type_tree(*array_element(type));
368 }
369
370 type_tree* array_element(const type_tree* type) {
371 assert(type->right);
372 if (type->right->atom) {
373 ¦ return type->right;
374 }
375 else if (!type->right->right) {
376 ¦ return type->right->left;
377 }
378
379 else if (type->right->right->left && type->right->right->left->atom && is_integer(type->right->right->left->name)) {
380 ¦ assert(!type->right->right->right);
381 ¦ return type->right->left;
382 }
383 return type->right;
384 }
385
386 int array_length(const reagent& x) {
387
388
389 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
390 ¦ ¦ && x.type->right->right->left && x.type->right->right->left->atom && is_integer(x.type->right->right->left->name)) {
391 ¦
392 ¦ return to_integer(x.type->right->right->left->name);
393 }
394
395 return get_or_insert(Memory, x.value);
396 }
397
398 :(before "End Unit Tests")
399 void test_array_length_compound() {
400 put(Memory, 1, 3);
401 put(Memory, 2, 14);
402 put(Memory, 3, 15);
403 put(Memory, 4, 16);
404 reagent x("1:array:address:num");
405 populate_value(x);
406 CHECK_EQ(array_length(x), 3);
407 }
408
409 void test_array_length_static() {
410 reagent x("1:array:num:3");
411 CHECK_EQ(array_length(x), 3);
412 }
413
414 :(scenario index_truncates)
415 def main [
416 1:array:num:3 <- create-array
417 2:num <- copy 14
418 3:num <- copy 15
419 4:num <- copy 16
420 5:num <- index 1:array:num:3, 1.5
421 ]
422
423 +mem: storing 15 in location 5
424
425 :(scenario index_out_of_bounds)
426 % Hide_errors = true;
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 <- copy 14
433 6:num <- copy 15
434 7:num <- copy 16
435 index 1:array:num:3, 4
436 ]
437 +error: main: invalid index 4 in 'index 1:array:num:3, 4'
438
439 :(scenario index_out_of_bounds_2)
440 % Hide_errors = true;
441 def main [
442 1:array:point:3 <- create-array
443 2:num <- copy 14
444 3:num <- copy 15
445 4:num <- copy 16
446 5:num <- copy 14
447 6:num <- copy 15
448 7:num <- copy 16
449 index 1:array:point, -1
450 ]
451 +error: main: invalid index -1 in 'index 1:array:point, -1'
452
453 :(scenario index_product_type_mismatch)
454 % Hide_errors = true;
455 def main [
456 1:array:point:3 <- create-array
457 2:num <- copy 14
458 3:num <- copy 15
459 4:num <- copy 16
460 5:num <- copy 14
461 6:num <- copy 15
462 7:num <- copy 16
463 9:num <- index 1:array:point, 0
464 ]
465 +error: main: 'index' on '1:array:point' can't be saved in '9:num'; type should be 'point'
466
467
468
469 :(scenario index_without_product)
470 def main [
471 1:array:num:3 <- create-array
472 2:num <- copy 14
473 3:num <- copy 15
474 4:num <- copy 16
475 index 1:array:num:3, 0
476 ]
477
478
479
480
481 :(scenario put_index)
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 1:array:num <- put-index 1:array:num, 1, 34
488 ]
489 +mem: storing 34 in location 3
490
491 :(before "End Primitive Recipe Declarations")
492 PUT_INDEX,
493 :(before "End Primitive Recipe Numbers")
494 put(Recipe_ordinal, "put-index", PUT_INDEX);
495 :(before "End Primitive Recipe Checks")
496 case PUT_INDEX: {
497 if (SIZE(inst.ingredients) != 3) {
498 ¦ raise << maybe(get(Recipe, r).name) << "'put-index' expects exactly 3 ingredients in '" << inst.original_string << "'\n" << end();
499 ¦ break;
500 }
501 reagent base = inst.ingredients.at(0);
502
503 if (!is_mu_array(base)) {
504 ¦ raise << maybe(get(Recipe, r).name) << "'put-index' on a non-array '" << base.original_string << "'\n" << end();
505 ¦ break;
506 }
507 reagent index = inst.ingredients.at(1);
508
509 if (!is_mu_number(index)) {
510 ¦ 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();
511 ¦ break;
512 }
513 reagent value = inst.ingredients.at(2);
514
515 reagent element;
516 element.type = copy_array_element(base.type);
517 if (!types_coercible(element, value)) {
518 ¦ 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();
519 ¦ break;
520 }
521 if (inst.products.empty()) break;
522 if (inst.products.at(0).name != inst.ingredients.at(0).name) {
523 ¦ 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();
524 ¦ break;
525 }
526
527 break;
528 }
529 :(before "End Primitive Recipe Implementations")
530 case PUT_INDEX: {
531 reagent base = current_instruction().ingredients.at(0);
532
533 int base_address = base.value;
534 if (base_address == 0) {
535 ¦ raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
536 ¦ break;
537 }
538 reagent index = current_instruction().ingredients.at(1);
539
540 vector<double> index_val(read_memory(index));
541 if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) {
542 ¦ raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
543 ¦ break;
544 }
545 int address = base_address + 1 + index_val.at(0)*size_of(array_element(base.type));
546 trace(9998, "run") << "address to copy to is " << address << end();
547
548
549 vector<double> value = read_memory(current_instruction().ingredients.at(2));
550
551 for (int i = 0; i < SIZE(value); ++i) {
552 ¦ trace(9999, "mem") << "storing " << no_scientific(value.at(i)) << " in location " << address+i << end();
553 ¦ put(Memory, address+i, value.at(i));
554 }
555 goto finish_instruction;
556 }
557
558 :(scenario put_index_out_of_bounds)
559 % Hide_errors = true;
560 def main [
561 1:array:point:3 <- create-array
562 2:num <- copy 14
563 3:num <- copy 15
564 4:num <- copy 16
565 5:num <- copy 14
566 6:num <- copy 15
567 7:num <- copy 16
568 8:point <- merge 34, 35
569 1:array:point <- put-index 1:array:point, 4, 8:point
570 ]
571 +error: main: invalid index 4 in '1:array:point <- put-index 1:array:point, 4, 8:point'
572
573 :(scenario put_index_out_of_bounds_2)
574 % Hide_errors = true;
575 def main [
576 1:array:point:3 <- create-array
577 2:num <- copy 14
578 3:num <- copy 15
579 4:num <- copy 16
580 5:num <- copy 14
581 6:num <- copy 15
582 7:num <- copy 16
583 8:point <- merge 34, 35
584 1:array:point <- put-index 1:array:point, -1, 8:point
585 ]
586 +error: main: invalid index -1 in '1:array:point <- put-index 1:array:point, -1, 8:point'
587
588 :(scenario put_index_product_error)
589 % Hide_errors = true;
590 def main [
591 local-scope
592 load-ingredients
593 1:array:num:3 <- create-array
594 4:array:num:3 <- put-index 1:array:num:3, 0, 34
595 ]
596 +error: main: product of 'put-index' must be first ingredient '1:array:num:3', but got '4:array:num:3'
597
598
599
600 :(scenario array_length)
601 def main [
602 1:array:num:3 <- create-array
603 2:num <- copy 14
604 3:num <- copy 15
605 4:num <- copy 16
606 5:num <- length 1:array:num:3
607 ]
608 +mem: storing 3 in location 5
609
610 :(before "End Primitive Recipe Declarations")
611 LENGTH,
612 :(before "End Primitive Recipe Numbers")
613 put(Recipe_ordinal, "length", LENGTH);
614 :(before "End Primitive Recipe Checks")
615 case LENGTH: {
616 if (SIZE(inst.ingredients) != 1) {
617 ¦ raise << maybe(get(Recipe, r).name) << "'length' expects exactly 2 ingredients in '" << inst.original_string << "'\n" << end();
618 ¦ break;
619 }
620 reagent array = inst.ingredients.at(0);
621
622 if (!is_mu_array(array)) {
623 ¦ raise << "tried to calculate length of non-array '" << array.original_string << "'\n" << end();
624 ¦ break;
625 }
626 break;
627 }
628 :(before "End Primitive Recipe Implementations")
629 case LENGTH: {
630 reagent array = current_instruction().ingredients.at(0);
631
632 if (array.value == 0) {
633 ¦ raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
634 ¦ break;
635 }
636 products.resize(1);
637 products.at(0).push_back(get_or_insert(Memory, array.value));
638 break;
639 }
640
641
642
643 :(before "End should_copy_ingredients Special-cases")
644 recipe_ordinal r = current_instruction().operation;
645 if (r == CREATE_ARRAY || r == INDEX || r == PUT_INDEX || r == LENGTH)
646 return false;