about summary refs log tree commit diff stats
path: root/011load.cc
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-03-12 18:56:55 -0700
committerKartik Agaram <vc@akkartik.com>2019-03-12 19:14:12 -0700
commit4a943d4ed313eff001504c2b5c472266e86a38af (patch)
treea5757233a8c81b303a808f251180c7344071ed51 /011load.cc
parent43711b0e9f18e0225ce14687fb6ea0902aa6fc61 (diff)
downloadmu-4a943d4ed313eff001504c2b5c472266e86a38af.tar.gz
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.
Diffstat (limited to '011load.cc')
-rw-r--r--011load.cc379
1 files changed, 227 insertions, 152 deletions
diff --git a/011load.cc b/011load.cc
index 575509ea..4bb06c36 100644
--- a/011load.cc
+++ b/011load.cc
@@ -3,16 +3,19 @@
 //: The process of running Mu code:
 //:   load -> transform -> run
 
-:(scenarios load)  // use 'load' instead of 'run' in all scenarios in this layer
-:(scenario first_recipe)
-def main [
-  1:number <- copy 23
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {1: "number"}
+void test_first_recipe() {
+  load(
+      "def main [\n"
+      "  1:number <- copy 23\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+  );
+}
 
-:(code)
 vector<recipe_ordinal> load(string form) {
   istringstream in(form);
   in >> std::noskipws;
@@ -237,123 +240,183 @@ void skip_comment(istream& in) {
   }
 }
 
-:(scenario recipe_instead_of_def)
-recipe main [
-  1:number <- copy 23
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {1: "number"}
-
-:(scenario parse_comment_outside_recipe)
-# this comment will be dropped by the tangler, so we need a dummy recipe to stop that
-def f1 [
-]
-# this comment will go through to 'load'
-def main [
-  1:number <- copy 23
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {1: "number"}
-
-:(scenario parse_comment_amongst_instruction)
-def main [
-  # comment
-  1:number <- copy 23
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {1: "number"}
-
-:(scenario parse_comment_amongst_instruction_2)
-def main [
-  # comment
-  1:number <- copy 23
-  # comment
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {1: "number"}
-
-:(scenario parse_comment_amongst_instruction_3)
-def main [
-  1:number <- copy 23
-  # comment
-  2:number <- copy 23
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {1: "number"}
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {2: "number"}
-
-:(scenario parse_comment_after_instruction)
-def main [
-  1:number <- copy 23  # comment
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {1: "number"}
-
-:(scenario parse_label)
-def main [
-  +foo
-]
-+parse: label: +foo
-
-:(scenario parse_dollar_as_recipe_name)
-def main [
-  $foo
-]
-+parse: instruction: $foo
-
-:(scenario parse_multiple_properties)
-def main [
-  1:number <- copy 23/foo:bar:baz
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal", "foo": ("bar" "baz")}
-+parse:   product: {1: "number"}
-
-:(scenario parse_multiple_products)
-def main [
-  1:number, 2:number <- copy 23
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   product: {1: "number"}
-+parse:   product: {2: "number"}
-
-:(scenario parse_multiple_ingredients)
-def main [
-  1:number, 2:number <- copy 23, 4:number
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   ingredient: {4: "number"}
-+parse:   product: {1: "number"}
-+parse:   product: {2: "number"}
-
-:(scenario parse_multiple_types)
-def main [
-  1:number, 2:address:number <- copy 23, 4:number
-]
-+parse: instruction: copy
-+parse:   ingredient: {23: "literal"}
-+parse:   ingredient: {4: "number"}
-+parse:   product: {1: "number"}
-+parse:   product: {2: ("address" "number")}
-
-:(scenario parse_properties)
-def main [
-  1:address:number/lookup <- copy 23
-]
-+parse:   product: {1: ("address" "number"), "lookup": ()}
-
-//: this test we can't represent with a scenario
-:(code)
+void test_recipe_instead_of_def() {
+  load(
+      "recipe main [\n"
+      "  1:number <- copy 23\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+  );
+}
+
+void test_parse_comment_outside_recipe() {
+  load(
+      "# comment\n"
+      "def main [\n"
+      "  1:number <- copy 23\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+  );
+}
+
+void test_parse_comment_amongst_instruction() {
+  load(
+      "def main [\n"
+      "  # comment\n"
+      "  1:number <- copy 23\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+  );
+}
+
+void test_parse_comment_amongst_instruction_2() {
+  load(
+      "def main [\n"
+      "  # comment\n"
+      "  1:number <- copy 23\n"
+      "  # comment\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+  );
+}
+
+void test_parse_comment_amongst_instruction_3() {
+  load(
+      "def main [\n"
+      "  1:number <- copy 23\n"
+      "  # comment\n"
+      "  2:number <- copy 23\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {2: \"number\"}\n"
+  );
+}
+
+void test_parse_comment_after_instruction() {
+  load(
+      "def main [\n"
+      "  1:number <- copy 23  # comment\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+  );
+}
+
+void test_parse_label() {
+  load(
+      "def main [\n"
+      "  +foo\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: label: +foo\n"
+  );
+}
+
+void test_parse_dollar_as_recipe_name() {
+  load(
+      "def main [\n"
+      "  $foo\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: $foo\n"
+  );
+}
+
+void test_parse_multiple_properties() {
+  load(
+      "def main [\n"
+      "  1:number <- copy 23/foo:bar:baz\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\", \"foo\": (\"bar\" \"baz\")}\n"
+      "parse:   product: {1: \"number\"}\n"
+  );
+}
+
+void test_parse_multiple_products() {
+  load(
+      "def main [\n"
+      "  1:number, 2:number <- copy 23\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+      "parse:   product: {2: \"number\"}\n"
+  );
+}
+
+void test_parse_multiple_ingredients() {
+  load(
+      "def main [\n"
+      "  1:number, 2:number <- copy 23, 4:number\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   ingredient: {4: \"number\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+      "parse:   product: {2: \"number\"}\n"
+  );
+}
+
+void test_parse_multiple_types() {
+  load(
+      "def main [\n"
+      "  1:number, 2:address:number <- copy 23, 4:number\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse: instruction: copy\n"
+      "parse:   ingredient: {23: \"literal\"}\n"
+      "parse:   ingredient: {4: \"number\"}\n"
+      "parse:   product: {1: \"number\"}\n"
+      "parse:   product: {2: (\"address\" \"number\")}\n"
+  );
+}
+
+void test_parse_properties() {
+  load(
+      "def main [\n"
+      "  1:address:number/lookup <- copy 23\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "parse:   product: {1: (\"address\" \"number\"), \"lookup\": ()}\n"
+  );
+}
+
 void test_parse_comment_terminated_by_eof() {
   load("recipe main [\n"
        "  a:number <- copy 34\n"
@@ -362,12 +425,17 @@ void test_parse_comment_terminated_by_eof() {
   cerr << ".";  // termination = success
 }
 
-:(scenario warn_on_missing_space_before_bracket)
-% Hide_errors = true;
-def main[
-  1:number <- copy 23
-]
-+error: insert a space before '[' in 'main['
+void test_warn_on_missing_space_before_bracket() {
+  Hide_errors = true;
+  load(
+      "def main[\n"
+      "  1:number <- copy 23\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "error: insert a space before '[' in 'main['\n"
+  );
+}
 
 //: Warn if a recipe gets redefined, because large codebases can accidentally
 //: step on their own toes. But there'll be many occasions later where
@@ -382,27 +450,34 @@ bool should_check_for_redefine(const string& recipe_name) {
   return true;
 }
 
-:(scenario forbid_redefining_recipes)
-% Hide_errors = true;
-def main [
-  1:number <- copy 23
-]
-def main [
-  1:number <- copy 24
-]
-+error: redefining recipe main
-
-:(scenario permit_forcibly_redefining_recipes)
-def main [
-  1:number <- copy 23
-]
-def! main [
-  1:number <- copy 24
-]
--error: redefining recipe main
-$error: 0
+void test_forbid_redefining_recipes() {
+  Hide_errors = true;
+  load(
+      "def main [\n"
+      "  1:number <- copy 23\n"
+      "]\n"
+      "def main [\n"
+      "  1:number <- copy 24\n"
+      "]\n"
+  );
+  CHECK_TRACE_CONTENTS(
+      "error: redefining recipe main\n"
+  );
+}
+
+void test_permit_forcibly_redefining_recipes() {
+  load(
+      "def main [\n"
+      "  1:number <- copy 23\n"
+      "]\n"
+      "def! main [\n"
+      "  1:number <- copy 24\n"
+      "]\n"
+  );
+  CHECK_TRACE_DOESNT_CONTAIN("error: redefining recipe main");
+  CHECK_TRACE_COUNT("error", 0);
+}
 
-:(code)
 // for debugging
 void show_rest_of_stream(istream& in) {
   cerr << '^';