fix other mandelbrot variants
[mu.git] / archive / 1.vm / 017parse_tree.cc
blob02170f6df864fcba897ce6ed30ded3cc5a891e97
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)".
5 //
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).
9 //
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() {
14 load(
15 "def main [\n"
16 " {1: number, foo: (bar (baz quux))} <- copy 34\n"
17 "]\n"
19 CHECK_TRACE_CONTENTS(
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);
29 :(code)
30 string_tree* parse_string_tree(string_tree* s) {
31 assert(s->atom);
32 if (!starts_with(s->value, "(")) return s;
33 string_tree* result = parse_string_tree(s->value);
34 delete s;
35 return result;
38 string_tree* parse_string_tree(const string& s) {
39 istringstream in(s);
40 in >> std::noskipws;
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() == ')') {
48 in.get();
49 return NULL;
51 if (in.peek() != '(') {
52 string s = next_word(in);
53 if (s.empty()) {
54 assert(!has_data(in));
55 raise << "incomplete string tree at end of file (0)\n" << end();
56 return NULL;
58 string_tree* result = new string_tree(s);
59 return result;
61 in.get(); // skip '('
62 string_tree* result = NULL;
63 string_tree** curr = &result;
64 while (true) {
65 skip_whitespace_but_not_newline(in);
66 assert(has_data(in));
67 if (in.peek() == ')') break;
68 *curr = new string_tree(NULL, NULL);
69 if (in.peek() == '(') {
70 (*curr)->left = parse_string_tree(in);
72 else {
73 string s = next_word(in);
74 if (s.empty()) {
75 assert(!has_data(in));
76 raise << "incomplete string tree at end of file (1)\n" << end();
77 return NULL;
79 (*curr)->left = new string_tree(s);
81 curr = &(*curr)->right;
83 in.get(); // skip ')'
84 assert(*curr == NULL);
85 return result;
88 void test_dilated_reagent_with_type_tree() {
89 Hide_errors = true; // 'map' isn't defined yet
90 load(
91 "def main [\n"
92 " {1: (foo (address array character) (bar number))} <- copy 34\n"
93 "]\n"
94 "container foo [\n"
95 "]\n"
96 "container bar [\n"
97 "]\n"
99 CHECK_TRACE_CONTENTS(
100 "parse: product: {1: (\"foo\" (\"address\" \"array\" \"character\") (\"bar\" \"number\"))}\n"
104 void test_dilated_empty_tree() {
105 load(
106 "def main [\n"
107 " {1: number, foo: ()} <- copy 34\n"
108 "]\n"
110 CHECK_TRACE_CONTENTS(
111 "parse: product: {1: \"number\", \"foo\": ()}\n"
115 void test_dilated_singleton_tree() {
116 load(
117 "def main [\n"
118 " {1: number, foo: (bar)} <- copy 34\n"
119 "]\n"
121 CHECK_TRACE_CONTENTS(
122 "parse: product: {1: \"number\", \"foo\": (\"bar\")}\n"