mu/archive/1.vm/024jump.cc

238 lines
7.8 KiB
C++
Raw Permalink Normal View History

2015-04-18 14:50:51 +00:00
//: Jump primitives
2015-04-17 18:22:59 +00:00
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-13 01:56:55 +00:00
void test_jump_can_skip_instructions() {
run(
"def main [\n"
" jump 1:offset\n"
" 1:num <- copy 1\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"run: jump {1: \"offset\"}\n"
);
CHECK_TRACE_DOESNT_CONTAIN("run: {1: \"number\"} <- copy {1: \"literal\"}");
CHECK_TRACE_DOESNT_CONTAIN("mem: storing 1 in location 1");
}
:(before "End Primitive Recipe Declarations")
JUMP,
2015-02-20 01:28:25 +00:00
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "jump", JUMP);
2015-09-30 09:18:05 +00:00
:(before "End Primitive Recipe Checks")
2015-02-20 01:28:25 +00:00
case JUMP: {
2015-09-30 09:18:05 +00:00
if (SIZE(inst.ingredients) != 1) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' should get exactly one ingredient\n" << end();
break;
}
if (!is_literal(inst.ingredients.at(0))) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "first ingredient of '" << to_original_string(inst) << "' should be a label or offset, but '" << inst.ingredients.at(0).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(0).type) << "'\n" << end();
break;
}
2018-05-25 19:25:13 +00:00
if (!inst.products.empty()) {
raise << maybe(get(Recipe, r).name) << "'jump' instructions write no products\n" << end();
break;
}
2015-09-30 09:18:05 +00:00
break;
}
:(before "End Primitive Recipe Implementations")
case JUMP: {
assert(current_instruction().ingredients.at(0).initialized);
current_step_index() += ingredients.at(0).at(0)+1;
trace(Callstack_depth+1, "run") << "jumping to instruction " << current_step_index() << end();
// skip rest of this instruction
write_products = false;
fall_through_to_next_instruction = false;
break;
2015-02-20 01:28:25 +00:00
}
//: special type to designate jump targets
:(before "End Mu Types Initialization")
put(Type_ordinal, "offset", 0);
2015-02-20 01:28:25 +00:00
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-13 01:56:55 +00:00
:(code)
void test_jump_backward() {
run(
"def main [\n"
" jump 1:offset\n" // 0 -+
" jump 3:offset\n" // | +-+ 1
// \/ /\ |
" jump -2:offset\n" // 2 +-->+ |
"]\n" // \/ 3
);
CHECK_TRACE_CONTENTS(
"run: jump {1: \"offset\"}\n"
"run: jump {-2: \"offset\"}\n"
"run: jump {3: \"offset\"}\n"
);
}
2015-02-20 02:25:25 +00:00
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-13 01:56:55 +00:00
void test_jump_takes_no_products() {
Hide_errors = true;
run(
"def main [\n"
" 1:num <- jump 1\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"error: main: 'jump' instructions write no products\n"
);
}
2018-05-25 19:25:13 +00:00
:(before "End Primitive Recipe Declarations")
JUMP_IF,
2015-02-20 02:25:25 +00:00
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "jump-if", JUMP_IF);
2015-09-30 09:18:05 +00:00
:(before "End Primitive Recipe Checks")
2015-02-20 02:25:25 +00:00
case JUMP_IF: {
2015-09-30 09:18:05 +00:00
if (SIZE(inst.ingredients) != 2) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' should get exactly two ingredients\n" << end();
break;
}
if (!is_mu_address(inst.ingredients.at(0)) && !is_mu_scalar(inst.ingredients.at(0))) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires a boolean for its first ingredient, but '" << inst.ingredients.at(0).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(0).type) << "'\n" << end();
break;
}
if (!is_literal(inst.ingredients.at(1))) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires a label or offset for its second ingredient, but '" << inst.ingredients.at(1).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(1).type) << "'\n" << end();
break;
}
2018-05-25 19:25:13 +00:00
if (!inst.products.empty()) {
raise << maybe(get(Recipe, r).name) << "'jump-if' instructions write no products\n" << end();
break;
}
// End JUMP_IF Checks
2015-09-30 09:18:05 +00:00
break;
}
:(before "End Primitive Recipe Implementations")
case JUMP_IF: {
assert(current_instruction().ingredients.at(1).initialized);
if (!scalar_ingredient(ingredients, 0)) {
trace(Callstack_depth+1, "run") << "jump-if fell through" << end();
2015-02-20 02:25:25 +00:00
break;
}
current_step_index() += ingredients.at(1).at(0)+1;
trace(Callstack_depth+1, "run") << "jumping to instruction " << current_step_index() << end();
// skip rest of this instruction
write_products = false;
fall_through_to_next_instruction = false;
break;
2015-02-20 02:25:25 +00:00
}
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-13 01:56:55 +00:00
:(code)
void test_jump_if() {
run(
"def main [\n"
" jump-if 999, 1:offset\n"
" 123:num <- copy 1\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"run: jump-if {999: \"literal\"}, {1: \"offset\"}\n"
"run: jumping to instruction 2\n"
);
CHECK_TRACE_DOESNT_CONTAIN("run: {123: \"number\"} <- copy {1: \"literal\"}");
CHECK_TRACE_DOESNT_CONTAIN("mem: storing 1 in location 123");
}
2015-02-20 02:25:25 +00:00
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-13 01:56:55 +00:00
void test_jump_if_fallthrough() {
run(
"def main [\n"
" jump-if 0, 1:offset\n"
" 123:num <- copy 1\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"run: jump-if {0: \"literal\"}, {1: \"offset\"}\n"
"run: jump-if fell through\n"
"run: {123: \"number\"} <- copy {1: \"literal\"}\n"
"mem: storing 1 in location 123\n"
);
}
2015-02-20 02:25:25 +00:00
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-13 01:56:55 +00:00
void test_jump_if_on_address() {
run(
"def main [\n"
" 10:num/alloc-id, 11:num <- copy 0, 999\n"
" jump-if 10:&:number, 1:offset\n"
" 123:num <- copy 1\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"run: jump-if {10: (\"address\" \"number\")}, {1: \"offset\"}\n"
"run: jumping to instruction 3\n"
);
CHECK_TRACE_DOESNT_CONTAIN("run: {123: \"number\"} <- copy {1: \"literal\"}");
CHECK_TRACE_DOESNT_CONTAIN("mem: storing 1 in location 123");
}
:(before "End Primitive Recipe Declarations")
JUMP_UNLESS,
2015-02-20 02:25:25 +00:00
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "jump-unless", JUMP_UNLESS);
2015-09-30 09:18:05 +00:00
:(before "End Primitive Recipe Checks")
2015-02-20 02:25:25 +00:00
case JUMP_UNLESS: {
2015-09-30 09:18:05 +00:00
if (SIZE(inst.ingredients) != 2) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' should get exactly two ingredients\n" << end();
break;
}
if (!is_mu_address(inst.ingredients.at(0)) && !is_mu_scalar(inst.ingredients.at(0))) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires a boolean for its first ingredient, but '" << inst.ingredients.at(0).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(0).type) << "'\n" << end();
break;
}
if (!is_literal(inst.ingredients.at(1))) {
2017-05-26 23:43:18 +00:00
raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires a label or offset for its second ingredient, but '" << inst.ingredients.at(1).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(1).type) << "'\n" << end();
break;
}
2018-05-25 19:25:13 +00:00
if (!inst.products.empty()) {
raise << maybe(get(Recipe, r).name) << "'jump' instructions write no products\n" << end();
break;
}
// End JUMP_UNLESS Checks
2015-09-30 09:18:05 +00:00
break;
}
:(before "End Primitive Recipe Implementations")
case JUMP_UNLESS: {
assert(current_instruction().ingredients.at(1).initialized);
if (scalar_ingredient(ingredients, 0)) {
trace(Callstack_depth+1, "run") << "jump-unless fell through" << end();
2015-02-20 02:25:25 +00:00
break;
}
current_step_index() += ingredients.at(1).at(0)+1;
trace(Callstack_depth+1, "run") << "jumping to instruction " << current_step_index() << end();
// skip rest of this instruction
write_products = false;
fall_through_to_next_instruction = false;
break;
2015-02-20 02:25:25 +00:00
}
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-13 01:56:55 +00:00
:(code)
void test_jump_unless() {
run(
"def main [\n"
" jump-unless 0, 1:offset\n"
" 123:num <- copy 1\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"run: jump-unless {0: \"literal\"}, {1: \"offset\"}\n"
"run: jumping to instruction 2\n"
);
CHECK_TRACE_DOESNT_CONTAIN("run: {123: \"number\"} <- copy {1: \"literal\"}");
CHECK_TRACE_DOESNT_CONTAIN("mem: storing 1 in location 123");
}
2015-02-20 02:25:25 +00:00
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-13 01:56:55 +00:00
void test_jump_unless_fallthrough() {
run(
"def main [\n"
" jump-unless 999, 1:offset\n"
" 123:num <- copy 1\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"run: jump-unless {999: \"literal\"}, {1: \"offset\"}\n"
"run: jump-unless fell through\n"
"run: {123: \"number\"} <- copy {1: \"literal\"}\n"
"mem: storing 1 in location 123\n"
);
}