mu/060rewrite_literal_string.cc

82 lines
3.2 KiB
C++
Raw Normal View History

//: allow using literal strings anywhere that will accept immutable strings
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_passing_literals_to_recipes() {
run(
"def main [\n"
" 1:num/raw <- foo [abc]\n"
"]\n"
"def foo x:text -> n:num [\n"
" local-scope\n"
" load-ingredients\n"
" n <- length *x\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"mem: storing 3 in location 1\n"
);
}
:(before "End Instruction Inserting/Deleting Transforms")
initialize_transform_rewrite_literal_string_to_text();
Transform.push_back(rewrite_literal_string_to_text); // idempotent
:(before "End Globals")
set<string> recipes_taking_literal_strings;
:(code)
void initialize_transform_rewrite_literal_string_to_text() {
recipes_taking_literal_strings.insert("assert");
recipes_taking_literal_strings.insert("$print");
recipes_taking_literal_strings.insert("$dump-trace");
recipes_taking_literal_strings.insert("$system");
recipes_taking_literal_strings.insert("trace");
recipes_taking_literal_strings.insert("stash");
recipes_taking_literal_strings.insert("new");
recipes_taking_literal_strings.insert("run");
recipes_taking_literal_strings.insert("memory-should-contain");
recipes_taking_literal_strings.insert("trace-should-contain");
recipes_taking_literal_strings.insert("trace-should-not-contain");
recipes_taking_literal_strings.insert("check-trace-count-for-label");
recipes_taking_literal_strings.insert("check-trace-count-for-label-greater-than");
recipes_taking_literal_strings.insert("check-trace-count-for-label-lesser-than");
2016-08-14 04:18:53 +00:00
// End initialize_transform_rewrite_literal_string_to_text()
}
2016-10-22 23:10:23 +00:00
void rewrite_literal_string_to_text(const recipe_ordinal r) {
recipe& caller = get(Recipe, r);
trace(101, "transform") << "--- rewrite literal strings in recipe " << caller.name << end();
if (contains_numeric_locations(caller)) return;
vector<instruction> new_instructions;
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(caller.steps); ++i) {
instruction& inst = caller.steps.at(i);
if (recipes_taking_literal_strings.find(inst.name) == recipes_taking_literal_strings.end()) {
2016-10-20 05:10:35 +00:00
for (int j = 0; j < SIZE(inst.ingredients); ++j) {
2016-09-17 06:52:15 +00:00
if (!is_literal_text(inst.ingredients.at(j))) continue;
instruction def;
ostringstream ingredient_name;
2016-09-17 07:10:28 +00:00
ingredient_name << inst.name << '_' << i << '_' << j << ":text";
def.name = "new";
def.ingredients.push_back(inst.ingredients.at(j));
def.products.push_back(reagent(ingredient_name.str()));
new_instructions.push_back(def);
inst.ingredients.at(j).clear(); // reclaim old memory
inst.ingredients.at(j) = reagent(ingredient_name.str());
}
}
new_instructions.push_back(inst);
}
caller.steps.swap(new_instructions);
}
2016-03-14 08:00:48 +00:00
bool contains_numeric_locations(const recipe& caller) {
2016-10-20 05:10:35 +00:00
for (int i = 0; i < SIZE(caller.steps); ++i) {
2016-03-14 08:00:48 +00:00
const instruction& inst = caller.steps.at(i);
2016-10-20 05:10:35 +00:00
for (int in = 0; in < SIZE(inst.ingredients); ++in)
2016-03-14 08:00:48 +00:00
if (is_numeric_location(inst.ingredients.at(in)))
return true;
2016-10-20 05:10:35 +00:00
for (int out = 0; out < SIZE(inst.products); ++out)
2016-03-14 08:00:48 +00:00
if (is_numeric_location(inst.products.at(out)))
return true;
}
return false;
}