1 // So far instructions can only contain linear lists of properties. Now we add
2 // support for more complex trees of properties in dilated reagents. This will
3 // come in handy later for expressing complex types, like "a dictionary from
4 // (address to array of charaters) to (list of numbers)".
6 // Type trees aren't as general as s-expressions even if they look like them:
7 // the first element of a type tree is always an atom, and it can never be
8 // dotted (right->right->right->...->right is always NULL).
10 // For now you can't use the simpler 'colon-based' representation inside type
11 // trees. Once you start typing parens, keep on typing parens.
13 void test_dilated_reagent_with_nested_brackets() {
16 " {1: number, foo: (bar (baz quux))} <- copy 34\n"
20 "parse: product: {1: \"number\", \"foo\": (\"bar\" (\"baz\" \"quux\"))}\n"
24 :(before
"End Parsing Dilated Reagent Property(value)")
25 value
= parse_string_tree(value
);
26 :(before
"End Parsing Dilated Reagent Type Property(type_names)")
27 type_names
= parse_string_tree(type_names
);
30 string_tree
* parse_string_tree(string_tree
* s
) {
32 if (!starts_with(s
->value
, "(")) return s
;
33 string_tree
* result
= parse_string_tree(s
->value
);
38 string_tree
* parse_string_tree(const string
& s
) {
41 return parse_string_tree(in
);
44 string_tree
* parse_string_tree(istream
& in
) {
45 skip_whitespace_but_not_newline(in
);
46 if (!has_data(in
)) return NULL
;
47 if (in
.peek() == ')') {
51 if (in
.peek() != '(') {
52 string s
= next_word(in
);
54 assert(!has_data(in
));
55 raise
<< "incomplete string tree at end of file (0)\n" << end();
58 string_tree
* result
= new string_tree(s
);
62 string_tree
* result
= NULL
;
63 string_tree
** curr
= &result
;
65 skip_whitespace_but_not_newline(in
);
67 if (in
.peek() == ')') break;
68 *curr
= new string_tree(NULL
, NULL
);
69 if (in
.peek() == '(') {
70 (*curr
)->left
= parse_string_tree(in
);
73 string s
= next_word(in
);
75 assert(!has_data(in
));
76 raise
<< "incomplete string tree at end of file (1)\n" << end();
79 (*curr
)->left
= new string_tree(s
);
81 curr
= &(*curr
)->right
;
84 assert(*curr
== NULL
);
88 void test_dilated_reagent_with_type_tree() {
89 Hide_errors
= true; // 'map' isn't defined yet
92 " {1: (foo (address array character) (bar number))} <- copy 34\n"
100 "parse: product: {1: (\"foo\" (\"address\" \"array\" \"character\") (\"bar\" \"number\"))}\n"
104 void test_dilated_empty_tree() {
107 " {1: number, foo: ()} <- copy 34\n"
110 CHECK_TRACE_CONTENTS(
111 "parse: product: {1: \"number\", \"foo\": ()}\n"
115 void test_dilated_singleton_tree() {
118 " {1: number, foo: (bar)} <- copy 34\n"
121 CHECK_TRACE_CONTENTS(
122 "parse: product: {1: \"number\", \"foo\": (\"bar\")}\n"