diff options
author | Kartik Agaram <vc@akkartik.com> | 2019-03-12 18:56:55 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2019-03-12 19:14:12 -0700 |
commit | 4a943d4ed313eff001504c2b5c472266e86a38af (patch) | |
tree | a5757233a8c81b303a808f251180c7344071ed51 /040brace.cc | |
parent | 43711b0e9f18e0225ce14687fb6ea0902aa6fc61 (diff) | |
download | mu-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 '040brace.cc')
-rw-r--r-- | 040brace.cc | 550 |
1 files changed, 321 insertions, 229 deletions
diff --git a/040brace.cc b/040brace.cc index 8eead4ab..89ff7b01 100644 --- a/040brace.cc +++ b/040brace.cc @@ -19,17 +19,21 @@ //: benefits of the control-flow primitives we're used to in other languages, //: like 'if', 'while', 'for', etc. -:(scenarios transform) -:(scenario brace_conversion) -def main [ - { - break - 1:num <- copy 0 - } -] -+transform: --- transform braces for recipe main -+transform: jump 1:offset -+transform: copy ... +void test_brace_conversion() { + transform( + "def main [\n" + " {\n" + " break\n" + " 1:num <- copy 0\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: jump 1:offset\n" + "transform: copy ...\n" + ); +} :(before "End Instruction Modifying Transforms") Transform.push_back(transform_braces); // idempotent @@ -143,244 +147,332 @@ int matching_brace(int index, const list<pair<bool, int> >& braces, recipe_ordin return SIZE(get(Recipe, r).steps); // exit current routine } -:(scenario loop) -def main [ - 1:num <- copy 0 - 2:num <- copy 0 - { - 3:num <- copy 0 - loop - } -] -+transform: --- transform braces for recipe main -+transform: copy ... -+transform: copy ... -+transform: copy ... -+transform: jump -2:offset +void test_loop() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " 2:num <- copy 0\n" + " {\n" + " 3:num <- copy 0\n" + " loop\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: copy ...\n" + "transform: copy ...\n" + "transform: copy ...\n" + "transform: jump -2:offset\n" + ); +} -:(scenario break_empty_block) -def main [ - 1:num <- copy 0 - { - break - } -] -+transform: --- transform braces for recipe main -+transform: copy ... -+transform: jump 0:offset +void test_break_empty_block() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " {\n" + " break\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: copy ...\n" + "transform: jump 0:offset\n" + ); +} -:(scenario break_cascading) -def main [ - 1:num <- copy 0 - { - break - } - { - break - } -] -+transform: --- transform braces for recipe main -+transform: copy ... -+transform: jump 0:offset -+transform: jump 0:offset +void test_break_cascading() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " {\n" + " break\n" + " }\n" + " {\n" + " break\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: copy ...\n" + "transform: jump 0:offset\n" + "transform: jump 0:offset\n" + ); +} -:(scenario break_cascading_2) -def main [ - 1:num <- copy 0 - 2:num <- copy 0 - { - break - 3:num <- copy 0 - } - { - break - } -] -+transform: --- transform braces for recipe main -+transform: copy ... -+transform: copy ... -+transform: jump 1:offset -+transform: copy ... -+transform: jump 0:offset +void test_break_cascading_2() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " 2:num <- copy 0\n" + " {\n" + " break\n" + " 3:num <- copy 0\n" + " }\n" + " {\n" + " break\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: copy ...\n" + "transform: copy ...\n" + "transform: jump 1:offset\n" + "transform: copy ...\n" + "transform: jump 0:offset\n" + ); +} -:(scenario break_if) -def main [ - 1:num <- copy 0 - 2:num <- copy 0 - { - break-if 2:num - 3:num <- copy 0 - } - { - break - } -] -+transform: --- transform braces for recipe main -+transform: copy ... -+transform: copy ... -+transform: jump-if 2, 1:offset -+transform: copy ... -+transform: jump 0:offset +void test_break_if() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " 2:num <- copy 0\n" + " {\n" + " break-if 2:num\n" + " 3:num <- copy 0\n" + " }\n" + " {\n" + " break\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: copy ...\n" + "transform: copy ...\n" + "transform: jump-if 2, 1:offset\n" + "transform: copy ...\n" + "transform: jump 0:offset\n" + ); +} -:(scenario break_nested) -def main [ - 1:num <- copy 0 - { - 2:num <- copy 0 - break - { - 3:num <- copy 0 - } - 4:num <- copy 0 - } -] -+transform: jump 4:offset +void test_break_nested() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " {\n" + " 2:num <- copy 0\n" + " break\n" + " {\n" + " 3:num <- copy 0\n" + " }\n" + " 4:num <- copy 0\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: jump 4:offset\n" + ); +} -:(scenario break_nested_degenerate) -def main [ - 1:num <- copy 0 - { - 2:num <- copy 0 - break - { - } - 4:num <- copy 0 - } -] -+transform: jump 3:offset +void test_break_nested_degenerate() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " {\n" + " 2:num <- copy 0\n" + " break\n" + " {\n" + " }\n" + " 4:num <- copy 0\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: jump 3:offset\n" + ); +} -:(scenario break_nested_degenerate_2) -def main [ - 1:num <- copy 0 - { - 2:num <- copy 0 - break - { - } - } -] -+transform: jump 2:offset +void test_break_nested_degenerate_2() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " {\n" + " 2:num <- copy 0\n" + " break\n" + " {\n" + " }\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: jump 2:offset\n" + ); +} -:(scenario break_label) -% Hide_errors = true; -def main [ - 1:num <- copy 0 - { - break +foo:offset - } -] -+transform: jump +foo:offset +void test_break_label() { + Hide_errors = true; + transform( + "def main [\n" + " 1:num <- copy 0\n" + " {\n" + " break +foo:offset\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: jump +foo:offset\n" + ); +} -:(scenario break_unless) -def main [ - 1:num <- copy 0 - 2:num <- copy 0 - { - break-unless 2:num - 3:num <- copy 0 - } -] -+transform: --- transform braces for recipe main -+transform: copy ... -+transform: copy ... -+transform: jump-unless 2, 1:offset -+transform: copy ... +void test_break_unless() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " 2:num <- copy 0\n" + " {\n" + " break-unless 2:num\n" + " 3:num <- copy 0\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: copy ...\n" + "transform: copy ...\n" + "transform: jump-unless 2, 1:offset\n" + "transform: copy ...\n" + ); +} -:(scenario loop_unless) -def main [ - 1:num <- copy 0 - 2:num <- copy 0 - { - loop-unless 2:num - 3:num <- copy 0 - } -] -+transform: --- transform braces for recipe main -+transform: copy ... -+transform: copy ... -+transform: jump-unless 2, -1:offset -+transform: copy ... +void test_loop_unless() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " 2:num <- copy 0\n" + " {\n" + " loop-unless 2:num\n" + " 3:num <- copy 0\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: copy ...\n" + "transform: copy ...\n" + "transform: jump-unless 2, -1:offset\n" + "transform: copy ...\n" + ); +} -:(scenario loop_nested) -def main [ - 1:num <- copy 0 - { - 2:num <- copy 0 - { - 3:num <- copy 0 - } - loop-if 4:bool - 5:num <- copy 0 - } -] -+transform: --- transform braces for recipe main -+transform: jump-if 4, -5:offset +void test_loop_nested() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " {\n" + " 2:num <- copy 0\n" + " {\n" + " 3:num <- copy 0\n" + " }\n" + " loop-if 4:bool\n" + " 5:num <- copy 0\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: jump-if 4, -5:offset\n" + ); +} -:(scenario loop_label) -def main [ - 1:num <- copy 0 - +foo - 2:num <- copy 0 -] -+transform: --- transform braces for recipe main -+transform: copy ... -+transform: copy ... +void test_loop_label() { + transform( + "def main [\n" + " 1:num <- copy 0\n" + " +foo\n" + " 2:num <- copy 0\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "transform: --- transform braces for recipe main\n" + "transform: copy ...\n" + "transform: copy ...\n" + ); +} //: test how things actually run -:(scenarios run) -:(scenario brace_conversion_and_run) -def test-factorial [ - 1:num <- copy 5 - 2:num <- copy 1 - { - 3:bool <- equal 1:num, 1 - break-if 3:bool -# $print 1:num - 2:num <- multiply 2:num, 1:num - 1:num <- subtract 1:num, 1 - loop - } - 4:num <- copy 2:num # trigger a read -] -+mem: location 2 is 120 +void test_brace_conversion_and_run() { + run( + "def test-factorial [\n" + " 1:num <- copy 5\n" + " 2:num <- copy 1\n" + " {\n" + " 3:bool <- equal 1:num, 1\n" + " break-if 3:bool\n" + " 2:num <- multiply 2:num, 1:num\n" + " 1:num <- subtract 1:num, 1\n" + " loop\n" + " }\n" + " 4:num <- copy 2:num\n" // trigger a read + "]\n" + ); + CHECK_TRACE_CONTENTS( + "mem: location 2 is 120\n" + ); +} -:(scenario break_outside_braces_fails) -% Hide_errors = true; -def main [ - break -] -+error: main: 'break' needs a '{' before +void test_break_outside_braces_fails() { + Hide_errors = true; + run( + "def main [\n" + " break\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "error: main: 'break' needs a '{' before\n" + ); +} -:(scenario break_conditional_without_ingredient_fails) -% Hide_errors = true; -def main [ - { - break-if - } -] -+error: main: 'break-if' expects 1 or 2 ingredients, but got none +void test_break_conditional_without_ingredient_fails() { + Hide_errors = true; + run( + "def main [\n" + " {\n" + " break-if\n" + " }\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "error: main: 'break-if' expects 1 or 2 ingredients, but got none\n" + ); +} //: Using break we can now implement conditional returns. -:(scenario return_if) -def main [ - 1:num <- test1 -] -def test1 [ - return-if 0, 34 - return 35 -] -+mem: storing 35 in location 1 +void test_return_if() { + run( + "def main [\n" + " 1:num <- test1\n" + "]\n" + "def test1 [\n" + " return-if 0, 34\n" + " return 35\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "mem: storing 35 in location 1\n" + ); +} -:(scenario return_if_2) -def main [ - 1:num <- test1 -] -def test1 [ - return-if 1, 34 - return 35 -] -+mem: storing 34 in location 1 +void test_return_if_2() { + run( + "def main [\n" + " 1:num <- test1\n" + "]\n" + "def test1 [\n" + " return-if 1, 34\n" + " return 35\n" + "]\n" + ); + CHECK_TRACE_CONTENTS( + "mem: storing 34 in location 1\n" + ); +} :(before "End Rewrite Instruction(curr, recipe result)") // rewrite 'return-if a, b, c, ...' to |