1 //:: Container definitions can contain 'type ingredients'
3 //: pre-requisite: extend our notion of containers to not necessarily be
5 :(after
"Update GET base_type in Check")
6 base_type
= get_base_type(base_type
);
7 :(after
"Update GET base_type in Run")
8 base_type
= get_base_type(base_type
);
9 :(after
"Update PUT base_type in Check")
10 base_type
= get_base_type(base_type
);
11 :(after
"Update PUT base_type in Run")
12 base_type
= get_base_type(base_type
);
13 :(after
"Update MAYBE_CONVERT base_type in Check")
14 base_type
= get_base_type(base_type
);
15 :(after
"Update base_type in element_type")
16 base_type
= get_base_type(base_type
);
17 :(after
"Update base_type in skip_addresses")
18 base_type
= get_base_type(base_type
);
19 :(replace
{} "const type_tree* get_base_type(const type_tree* t)")
20 const type_tree
* get_base_type(const type_tree
* t
) {
21 const type_tree
* result
= t
->atom
? t
: t
->left
;
23 raise
<< "invalid type " << to_string(t
) << '\n' << end();
28 void test_ill_formed_container() {
32 " {1: ((foo) num)} <- copy 0\n"
38 //: update size_of to handle non-atom container types
40 void test_size_of_shape_shifting_container() {
42 "container foo:_t [\n"
47 " 1:foo:num <- merge 12, 13\n"
48 " 3:foo:point <- merge 14, 15, 16\n"
52 "mem: storing 12 in location 1\n"
53 "mem: storing 13 in location 2\n"
54 "mem: storing 14 in location 3\n"
55 "mem: storing 15 in location 4\n"
56 "mem: storing 16 in location 5\n"
59 void test_size_of_shape_shifting_container_2() {
61 // multiple type ingredients
62 "container foo:_a:_b [\n"
67 " 1:foo:num:bool <- merge 34, true\n"
70 CHECK_TRACE_COUNT("error", 0);
72 void test_size_of_shape_shifting_container_3() {
74 "container foo:_a:_b [\n"
79 " 1:text <- new [abc]\n"
80 // compound types for type ingredients
81 " {3: (foo number (address array character))} <- merge 34/x, 1:text/y\n"
84 CHECK_TRACE_COUNT("error", 0);
87 void test_size_of_shape_shifting_container_4() {
89 "container foo:_a:_b [\n"
93 "container bar:_a:_b [\n"
95 " {data: (foo _a (address _b))}\n"
98 " 1:text <- new [abc]\n"
99 " 3:bar:num:@:char <- merge 34/x, 1:text/y\n"
102 CHECK_TRACE_COUNT("error", 0);
105 void test_shape_shifting_container_extend() {
107 "container foo:_a [\n"
110 "container foo:_a [\n"
114 CHECK_TRACE_COUNT("error", 0);
117 void test_shape_shifting_container_extend_error() {
120 "container foo:_a [\n"
123 "container foo:_b [\n"
127 CHECK_TRACE_CONTENTS(
128 "error: headers of container 'foo' must use identical type ingredients\n"
132 void test_type_ingredient_must_start_with_underscore() {
135 "container foo:t [\n"
139 CHECK_TRACE_CONTENTS(
140 "error: foo: type ingredient 't' must begin with an underscore\n"
144 :(before
"End Globals")
145 // We'll use large type ordinals to mean "the following type of the variable".
146 // For example, if we have a generic type called foo:_elem, the type
147 // ingredient _elem in foo's type_info will have value START_TYPE_INGREDIENTS,
148 // and we'll handle it by looking in the current reagent for the next type
149 // that appears after foo.
150 extern const int START_TYPE_INGREDIENTS
= 2000;
151 :(before
"End Commandline Parsing") // after loading .mu files
152 assert(Next_type_ordinal
< START_TYPE_INGREDIENTS
);
154 :(before
"End type_info Fields")
155 map
<string
, type_ordinal
> type_ingredient_names
;
157 //: Suppress unknown type checks in shape-shifting containers.
159 :(before
"Check Container Field Types(info)")
160 if (!info
.type_ingredient_names
.empty()) continue;
162 :(before
"End container Name Refinements")
163 if (name
.find(':') != string::npos
) {
164 trace(101, "parse") << "container has type ingredients; parsing" << end();
165 if (!read_type_ingredients(name
, command
)) {
166 // error; skip rest of the container definition and continue
167 slurp_balanced_bracket(in
);
173 bool read_type_ingredients(string
& name
, const string
& command
) {
174 string save_name
= name
;
175 istringstream
in(save_name
);
176 name
= slurp_until(in
, ':');
177 map
<string
, type_ordinal
> type_ingredient_names
;
178 if (!slurp_type_ingredients(in
, type_ingredient_names
, name
)) {
181 if (contains_key(Type_ordinal
, name
)
182 && contains_key(Type
, get(Type_ordinal
, name
))) {
183 const type_info
& previous_info
= get(Type
, get(Type_ordinal
, name
));
184 // we've already seen this container; make sure type ingredients match
185 if (!type_ingredients_match(type_ingredient_names
, previous_info
.type_ingredient_names
)) {
186 raise
<< "headers of " << command
<< " '" << name
<< "' must use identical type ingredients\n" << end();
191 // we haven't seen this container before
192 if (!contains_key(Type_ordinal
, name
) || get(Type_ordinal
, name
) == 0)
193 put(Type_ordinal
, name
, Next_type_ordinal
++);
194 type_info
& info
= get_or_insert(Type
, get(Type_ordinal
, name
));
195 info
.type_ingredient_names
.swap(type_ingredient_names
);
199 bool slurp_type_ingredients(istream
& in
, map
<string
, type_ordinal
>& out
, const string
& container_name
) {
200 int next_type_ordinal
= START_TYPE_INGREDIENTS
;
201 while (has_data(in
)) {
202 string curr
= slurp_until(in
, ':');
204 raise
<< container_name
<< ": empty type ingredients not permitted\n" << end();
207 if (!starts_with(curr
, "_")) {
208 raise
<< container_name
<< ": type ingredient '" << curr
<< "' must begin with an underscore\n" << end();
211 if (out
.find(curr
) != out
.end()) {
212 raise
<< container_name
<< ": can't repeat type ingredient name'" << curr
<< "' in a single container definition\n" << end();
215 put(out
, curr
, next_type_ordinal
++);
220 bool type_ingredients_match(const map
<string
, type_ordinal
>& a
, const map
<string
, type_ordinal
>& b
) {
221 if (SIZE(a
) != SIZE(b
)) return false;
222 for (map
<string
, type_ordinal
>::const_iterator p
= a
.begin(); p
!= a
.end(); ++p
) {
223 if (!contains_key(b
, p
->first
)) return false;
224 if (p
->second
!= get(b
, p
->first
)) return false;
229 :(before
"End insert_container Special-cases")
230 // check for use of type ingredients
231 else if (is_type_ingredient_name(type
->name
)) {
232 type
->value
= get(info
.type_ingredient_names
, type
->name
);
235 bool is_type_ingredient_name(const string
& type
) {
236 return starts_with(type
, "_");
239 :(before
"End Container Type Checks")
240 if (type
->value
>= START_TYPE_INGREDIENTS
241 && (type
->value
- START_TYPE_INGREDIENTS
) < SIZE(get(Type
, type
->value
).type_ingredient_names
))
245 void test_size_of_shape_shifting_exclusive_container() {
247 "exclusive-container foo:_t [\n"
252 " 1:foo:num <- merge 0/x, 34\n"
253 " 3:foo:point <- merge 0/x, 15, 16\n"
254 " 6:foo:point <- merge 1/y, 23\n"
257 CHECK_TRACE_CONTENTS(
258 "run: {1: (\"foo\" \"number\")} <- merge {0: \"literal\", \"x\": ()}, {34: \"literal\"}\n"
259 "mem: storing 0 in location 1\n"
260 "mem: storing 34 in location 2\n"
261 "run: {3: (\"foo\" \"point\")} <- merge {0: \"literal\", \"x\": ()}, {15: \"literal\"}, {16: \"literal\"}\n"
262 "mem: storing 0 in location 3\n"
263 "mem: storing 15 in location 4\n"
264 "mem: storing 16 in location 5\n"
265 "run: {6: (\"foo\" \"point\")} <- merge {1: \"literal\", \"y\": ()}, {23: \"literal\"}\n"
266 "mem: storing 1 in location 6\n"
267 "mem: storing 23 in location 7\n"
271 CHECK_EQ(trace_count_prefix("mem", "storing"), 7);
274 :(before
"End variant_type Special-cases")
275 if (contains_type_ingredient(element
))
276 replace_type_ingredients(element
.type
, type
->right
, info
, " while computing variant type of exclusive-container");
279 void test_get_on_shape_shifting_container() {
281 "container foo:_t [\n"
286 " 1:foo:point <- merge 14, 15, 16\n"
287 " 4:num <- get 1:foo:point, y:offset\n"
290 CHECK_TRACE_CONTENTS(
291 "mem: storing 16 in location 4\n"
295 void test_get_on_shape_shifting_container_2() {
297 "container foo:_t [\n"
302 " 1:foo:point <- merge 14, 15, 16\n"
303 " 4:point <- get 1:foo:point, x:offset\n"
306 CHECK_TRACE_CONTENTS(
307 "mem: storing 14 in location 4\n"
308 "mem: storing 15 in location 5\n"
312 void test_get_on_shape_shifting_container_3() {
314 "container foo:_t [\n"
319 " 1:num/alloc-id, 2:num <- copy 0, 34\n"
320 " 3:foo:&:point <- merge 1:&:point, 48\n"
321 " 6:&:point <- get 1:foo:&:point, x:offset\n"
324 CHECK_TRACE_CONTENTS(
325 "mem: storing 0 in location 6\n"
326 "mem: storing 34 in location 7\n"
330 void test_get_on_shape_shifting_container_inside_container() {
332 "container foo:_t [\n"
341 " 1:bar <- merge 14, 15, 16, 17\n"
342 " 5:num <- get 1:bar, 1:offset\n"
345 CHECK_TRACE_CONTENTS(
346 "mem: storing 17 in location 5\n"
350 void test_get_on_complex_shape_shifting_container() {
352 "container foo:_a:_b [\n"
357 " 1:text <- new [abc]\n"
358 " {3: (foo number (address array character))} <- merge 34/x, 1:text/y\n"
359 " 6:text <- get {3: (foo number (address array character))}, y:offset\n"
360 " 8:bool <- equal 1:text, 6:text\n"
363 CHECK_TRACE_CONTENTS(
364 "mem: storing 1 in location 8\n"
368 :(before
"End element_type Special-cases")
369 replace_type_ingredients(element
, type
, info
, " while computing element type of container");
371 :(before
"End size_of(type) Non-atom Special-cases")
372 assert(type
->left
->atom
);
373 if (!contains_key(Type
, type
->left
->value
)) {
374 raise
<< "no such type " << type
->left
->value
<< '\n' << end();
377 type_info t
= get(Type
, type
->left
->value
);
378 if (t
.kind
== CONTAINER
) {
379 // size of a container is the sum of the sizes of its elements
381 for (int i
= 0; i
< SIZE(t
.elements
); ++i
) {
382 // todo: strengthen assertion to disallow mutual type recursion
383 if (get_base_type(t
.elements
.at(i
).type
)->value
== get_base_type(type
)->value
) {
384 raise
<< "container " << t
.name
<< " can't include itself as a member\n" << end();
387 result
+= size_of(element_type(type
, i
));
391 if (t
.kind
== EXCLUSIVE_CONTAINER
) {
392 // size of an exclusive container is the size of its largest variant
393 // (So like containers, it can't contain arrays.)
395 for (int i
= 0; i
< SIZE(t
.elements
); ++i
) {
397 tmp
.type
= new type_tree(*type
);
398 int size
= size_of(variant_type(tmp
, i
));
399 if (size
> result
) result
= size
;
401 // ...+1 for its tag.
406 void test_complex_shape_shifting_exclusive_container() {
408 "exclusive-container foo:_a [\n"
413 " 1:text <- new [abc]\n"
414 " 3:foo:point <- merge 0/variant, 34/xx, 35/xy\n"
415 " 10:point, 20:bool <- maybe-convert 3:foo:point, 0/variant\n"
418 CHECK_TRACE_CONTENTS(
419 "mem: storing 1 in location 20\n"
420 "mem: storing 35 in location 11\n"
424 bool contains_type_ingredient(const reagent
& x
) {
425 return contains_type_ingredient(x
.type
);
428 bool contains_type_ingredient(const type_tree
* type
) {
429 if (!type
) return false;
430 if (type
->atom
) return type
->value
>= START_TYPE_INGREDIENTS
;
431 return contains_type_ingredient(type
->left
) || contains_type_ingredient(type
->right
);
434 void replace_type_ingredients(reagent
& element
, const type_tree
* caller_type
, const type_info
& info
, const string
& location_for_error_messages
) {
435 if (contains_type_ingredient(element
)) {
436 if (!caller_type
->right
)
437 raise
<< "illegal type " << names_to_string(caller_type
) << " seems to be missing a type ingredient or three" << location_for_error_messages
<< '\n' << end();
438 replace_type_ingredients(element
.type
, caller_type
->right
, info
, location_for_error_messages
);
442 // replace all type_ingredients in element_type with corresponding elements of callsite_type
443 void replace_type_ingredients(type_tree
* element_type
, const type_tree
* callsite_type
, const type_info
& container_info
, const string
& location_for_error_messages
) {
444 if (!callsite_type
) return; // error but it's already been raised above
445 if (!element_type
) return;
446 if (!element_type
->atom
) {
447 if (element_type
->right
== NULL
&& is_type_ingredient(element_type
->left
)) {
448 int type_ingredient_index
= to_type_ingredient_index(element_type
->left
);
449 if (corresponding(callsite_type
, type_ingredient_index
, is_final_type_ingredient(type_ingredient_index
, container_info
))->right
) {
450 // replacing type ingredient at end of list, and replacement is a non-degenerate compound type -- (a b) but not (a)
451 replace_type_ingredient_at(type_ingredient_index
, element_type
, callsite_type
, container_info
, location_for_error_messages
);
455 replace_type_ingredients(element_type
->left
, callsite_type
, container_info
, location_for_error_messages
);
456 replace_type_ingredients(element_type
->right
, callsite_type
, container_info
, location_for_error_messages
);
459 if (is_type_ingredient(element_type
))
460 replace_type_ingredient_at(to_type_ingredient_index(element_type
), element_type
, callsite_type
, container_info
, location_for_error_messages
);
463 const type_tree
* corresponding(const type_tree
* type
, int index
, bool final
) {
464 for (const type_tree
* curr
= type
; curr
; curr
= curr
->right
, --index
) {
465 assert_for_now(!curr
->atom
);
467 return final
? curr
: curr
->left
;
469 assert_for_now(false);
472 bool is_type_ingredient(const type_tree
* type
) {
473 return type
->atom
&& type
->value
>= START_TYPE_INGREDIENTS
;
476 int to_type_ingredient_index(const type_tree
* type
) {
478 return type
->value
-START_TYPE_INGREDIENTS
;
481 void replace_type_ingredient_at(const int type_ingredient_index
, type_tree
* element_type
, const type_tree
* callsite_type
, const type_info
& container_info
, const string
& location_for_error_messages
) {
482 if (!has_nth_type(callsite_type
, type_ingredient_index
)) {
483 raise
<< "illegal type " << names_to_string(callsite_type
) << " seems to be missing a type ingredient or three" << location_for_error_messages
<< '\n' << end();
486 *element_type
= *nth_type_ingredient(callsite_type
, type_ingredient_index
, container_info
);
489 const type_tree
* nth_type_ingredient(const type_tree
* callsite_type
, int type_ingredient_index
, const type_info
& container_info
) {
490 bool final
= is_final_type_ingredient(type_ingredient_index
, container_info
);
491 const type_tree
* curr
= callsite_type
;
492 for (int i
= 0; i
< type_ingredient_index
; ++i
) {
495 //? cerr << "type ingredient " << i << " is " << to_string(curr->left) << '\n';
499 if (curr
->atom
) return curr
;
500 if (!final
) return curr
->left
;
501 if (!curr
->right
) return curr
->left
;
505 bool is_final_type_ingredient(int type_ingredient_index
, const type_info
& container_info
) {
506 for (map
<string
, type_ordinal
>::const_iterator p
= container_info
.type_ingredient_names
.begin();
507 p
!= container_info
.type_ingredient_names
.end();
509 if (p
->second
> START_TYPE_INGREDIENTS
+type_ingredient_index
) return false;
514 :(before
"End Unit Tests")
515 void test_replace_type_ingredients_entire() {
516 run("container foo:_elem [\n"
520 reagent
callsite("x:foo:point");
521 reagent element
= element_type(callsite
.type
, 0);
522 CHECK_EQ(to_string(element
), "{x: \"point\"}");
525 void test_replace_type_ingredients_tail() {
526 run("container foo:_elem [\n"
529 "container bar:_elem [\n"
532 reagent
callsite("x:bar:point");
533 reagent element
= element_type(callsite
.type
, 0);
534 CHECK_EQ(to_string(element
), "{x: (\"foo\" \"point\")}");
537 void test_replace_type_ingredients_head_tail_multiple() {
538 run("container foo:_elem [\n"
541 "container bar:_elem [\n"
544 reagent
callsite("x:bar:address:array:character");
545 reagent element
= element_type(callsite
.type
, 0);
546 CHECK_EQ(to_string(element
), "{x: (\"foo\" \"address\" \"array\" \"character\")}");
549 void test_replace_type_ingredients_head_middle() {
550 run("container foo:_elem [\n"
553 "container bar:_elem [\n"
556 reagent
callsite("x:bar:address");
557 reagent element
= element_type(callsite
.type
, 0);
558 CHECK_EQ(to_string(element
), "{x: (\"foo\" \"address\" \"number\")}");
561 void test_replace_last_type_ingredient_with_multiple() {
562 run("container foo:_a:_b [\n"
566 reagent
callsite("{f: (foo number (address array character))}");
567 reagent element1
= element_type(callsite
.type
, 0);
568 CHECK_EQ(to_string(element1
), "{x: \"number\"}");
569 reagent element2
= element_type(callsite
.type
, 1);
570 CHECK_EQ(to_string(element2
), "{y: (\"address\" \"array\" \"character\")}");
573 void test_replace_last_type_ingredient_inside_compound() {
574 run("container foo:_a:_b [\n"
575 " {x: (bar _a (address _b))}\n"
577 reagent
callsite("f:foo:number:array:character");
578 reagent element
= element_type(callsite
.type
, 0);
579 CHECK_EQ(names_to_string_without_quotes(element
.type
), "(bar number (address array character))");
582 void test_replace_middle_type_ingredient_with_multiple() {
583 run("container foo:_a:_b:_c [\n"
588 reagent
callsite("{f: (foo number (address array character) boolean)}");
589 reagent element1
= element_type(callsite
.type
, 0);
590 CHECK_EQ(to_string(element1
), "{x: \"number\"}");
591 reagent element2
= element_type(callsite
.type
, 1);
592 CHECK_EQ(to_string(element2
), "{y: (\"address\" \"array\" \"character\")}");
593 reagent element3
= element_type(callsite
.type
, 2);
594 CHECK_EQ(to_string(element3
), "{z: \"boolean\"}");
597 void test_replace_middle_type_ingredient_with_multiple2() {
598 run("container foo:_key:_value [\n"
602 reagent
callsite("{f: (foo (address array character) number)}");
603 reagent element
= element_type(callsite
.type
, 0);
604 CHECK_EQ(to_string(element
), "{key: (\"address\" \"array\" \"character\")}");
607 void test_replace_middle_type_ingredient_with_multiple3() {
608 run("container foo_table:_key:_value [\n"
609 " data:&:@:foo_table_row:_key:_value\n"
612 "container foo_table_row:_key:_value [\n"
616 reagent
callsite("{f: (foo_table (address array character) number)}");
617 reagent element
= element_type(callsite
.type
, 0);
618 CHECK_EQ(to_string(element
), "{data: (\"address\" \"array\" \"foo_table_row\" (\"address\" \"array\" \"character\") \"number\")}");
622 bool has_nth_type(const type_tree
* base
, int n
) {
624 if (!base
) return false;
625 if (n
== 0) return true;
626 return has_nth_type(base
->right
, n
-1);
629 void test_get_on_shape_shifting_container_error() {
632 "container foo:_t [\n"
637 " 1:foo:point <- merge 14, 15, 16\n"
638 " 10:num <- get 1:foo, 1:offset\n"
641 CHECK_TRACE_CONTENTS(
642 "error: illegal type \"foo\" seems to be missing a type ingredient or three while computing element type of container\n"
644 // todo: improve error message
647 void test_typos_in_container_definitions() {
650 "container foo:_t [\n"
651 " x:adress:_t # typo\n"
655 " x:address:foo:num <- new {(foo num): type}\n"
661 void test_typos_in_recipes() {
666 " x:adress:array:number <- copy null # typo\n"
672 //:: 'merge' on shape-shifting containers
674 void test_merge_check_shape_shifting_container_containing_exclusive_container() {
676 "container foo:_elem [\n"
680 "exclusive-container bar [\n"
685 " 1:foo:bar <- merge 23, 1/y, 34\n"
688 CHECK_TRACE_CONTENTS(
689 "mem: storing 23 in location 1\n"
690 "mem: storing 1 in location 2\n"
691 "mem: storing 34 in location 3\n"
693 CHECK_TRACE_COUNT("error", 0);
696 void test_merge_check_shape_shifting_container_containing_exclusive_container_2() {
699 "container foo:_elem [\n"
703 "exclusive-container bar [\n"
708 " 1:foo:bar <- merge 23, 1/y, 34, 35\n"
711 CHECK_TRACE_CONTENTS(
712 "error: main: too many ingredients in '1:foo:bar <- merge 23, 1/y, 34, 35'\n"
716 void test_merge_check_shape_shifting_exclusive_container_containing_container() {
718 "exclusive-container foo:_elem [\n"
727 " 1:foo:bar <- merge 1/y, 23, 34\n"
730 CHECK_TRACE_CONTENTS(
731 "mem: storing 1 in location 1\n"
732 "mem: storing 23 in location 2\n"
733 "mem: storing 34 in location 3\n"
735 CHECK_TRACE_COUNT("error", 0);
738 void test_merge_check_shape_shifting_exclusive_container_containing_container_2() {
740 "exclusive-container foo:_elem [\n"
749 " 1:foo:bar <- merge 0/x, 23\n"
752 CHECK_TRACE_COUNT("error", 0);
755 void test_merge_check_shape_shifting_exclusive_container_containing_container_3() {
758 "exclusive-container foo:_elem [\n"
767 " 1:foo:bar <- merge 1/y, 23\n"
770 CHECK_TRACE_CONTENTS(
771 "error: main: too few ingredients in '1:foo:bar <- merge 1/y, 23'\n"