mu/017parse_tree.cc
Kartik Agaram 4a943d4ed3 5001 - drop the :(scenario) DSL
I've been saying for a while[1][2][3] that adding extra abstractions makes
things harder for newcomers, and adding new notations doubly so. And then
I notice this DSL in my own backyard. Makes me feel like a hypocrite.

[1] https://news.ycombinator.com/item?id=13565743#13570092
[2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning
[3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2

The implementation of the DSL was also highly hacky:

a) It was happening in the tangle/ tool, but was utterly unrelated to tangling
layers.

b) There were several persnickety constraints on the different kinds of
lines and the specific order they were expected in. I kept finding bugs
where the translator would silently do the wrong thing. Or the error messages
sucked, and readers may be stuck looking at the generated code to figure
out what happened. Fixing error messages would require a lot more code,
which is one of my arguments against DSLs in the first place: they may
be easy to implement, but they're hard to design to go with the grain of
the underlying platform. They require lots of iteration. Is that effort
worth prioritizing in this project?

On the other hand, the DSL did make at least some readers' life easier,
the ones who weren't immediately put off by having to learn a strange syntax.
There were fewer quotes to parse, fewer backslash escapes.

Anyway, since there are also people who dislike having to put up with strange
syntaxes, we'll call that consideration a wash and tear this DSL out.

---

This commit was sheer drudgery. Hopefully it won't need to be redone with
a new DSL because I grow sick of backslashes.
2019-03-12 19:14:12 -07:00

125 lines
3.4 KiB
C++

// So far instructions can only contain linear lists of properties. Now we add
// support for more complex trees of properties in dilated reagents. This will
// come in handy later for expressing complex types, like "a dictionary from
// (address to array of charaters) to (list of numbers)".
//
// Type trees aren't as general as s-expressions even if they look like them:
// the first element of a type tree is always an atom, and it can never be
// dotted (right->right->right->...->right is always NULL).
//
// For now you can't use the simpler 'colon-based' representation inside type
// trees. Once you start typing parens, keep on typing parens.
void test_dilated_reagent_with_nested_brackets() {
load(
"def main [\n"
" {1: number, foo: (bar (baz quux))} <- copy 34\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"parse: product: {1: \"number\", \"foo\": (\"bar\" (\"baz\" \"quux\"))}\n"
);
}
:(before "End Parsing Dilated Reagent Property(value)")
value = parse_string_tree(value);
:(before "End Parsing Dilated Reagent Type Property(type_names)")
type_names = parse_string_tree(type_names);
:(code)
string_tree* parse_string_tree(string_tree* s) {
assert(s->atom);
if (!starts_with(s->value, "(")) return s;
string_tree* result = parse_string_tree(s->value);
delete s;
return result;
}
string_tree* parse_string_tree(const string& s) {
istringstream in(s);
in >> std::noskipws;
return parse_string_tree(in);
}
string_tree* parse_string_tree(istream& in) {
skip_whitespace_but_not_newline(in);
if (!has_data(in)) return NULL;
if (in.peek() == ')') {
in.get();
return NULL;
}
if (in.peek() != '(') {
string s = next_word(in);
if (s.empty()) {
assert(!has_data(in));
raise << "incomplete string tree at end of file (0)\n" << end();
return NULL;
}
string_tree* result = new string_tree(s);
return result;
}
in.get(); // skip '('
string_tree* result = NULL;
string_tree** curr = &result;
while (true) {
skip_whitespace_but_not_newline(in);
assert(has_data(in));
if (in.peek() == ')') break;
*curr = new string_tree(NULL, NULL);
if (in.peek() == '(') {
(*curr)->left = parse_string_tree(in);
}
else {
string s = next_word(in);
if (s.empty()) {
assert(!has_data(in));
raise << "incomplete string tree at end of file (1)\n" << end();
return NULL;
}
(*curr)->left = new string_tree(s);
}
curr = &(*curr)->right;
}
in.get(); // skip ')'
assert(*curr == NULL);
return result;
}
void test_dilated_reagent_with_type_tree() {
Hide_errors = true; // 'map' isn't defined yet
load(
"def main [\n"
" {1: (foo (address array character) (bar number))} <- copy 34\n"
"]\n"
"container foo [\n"
"]\n"
"container bar [\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"parse: product: {1: (\"foo\" (\"address\" \"array\" \"character\") (\"bar\" \"number\"))}\n"
);
}
void test_dilated_empty_tree() {
load(
"def main [\n"
" {1: number, foo: ()} <- copy 34\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"parse: product: {1: \"number\", \"foo\": ()}\n"
);
}
void test_dilated_singleton_tree() {
load(
"def main [\n"
" {1: number, foo: (bar)} <- copy 34\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"parse: product: {1: \"number\", \"foo\": (\"bar\")}\n"
);
}