https://github.com/akkartik/mu/blob/master/060rewrite_literal_string.cc

     1 //: allow using literal strings anywhere that will accept immutable strings
     2
     3 :(scenario passing_literals_to_recipes)
     4 def main [
     5   1:num/raw <- foo [abc]
     6 ]
     7 def foo x:text -> n:num [
     8   local-scope
     9   load-ingredients
    10   n <- length *x
    11 ]
    12 +mem: storing 3 in location 1
    13
    14 :(before "End Instruction Inserting/Deleting Transforms")
    15 initialize_transform_rewrite_literal_string_to_text();
    16 Transform.push_back(rewrite_literal_string_to_text);  // idempotent
    17
    18 :(before "End Globals")
    19 set<string> recipes_taking_literal_strings;
    20 :(code)
    21 void initialize_transform_rewrite_literal_string_to_text() {
    22   recipes_taking_literal_strings.insert("assert");
    23   recipes_taking_literal_strings.insert("$print");
    24   recipes_taking_literal_strings.insert("$dump-trace");
    25   recipes_taking_literal_strings.insert("$system");
    26   recipes_taking_literal_strings.insert("trace");
    27   recipes_taking_literal_strings.insert("stash");
    28   recipes_taking_literal_strings.insert("new");
    29   recipes_taking_literal_strings.insert("run");
    30   recipes_taking_literal_strings.insert("memory-should-contain");
    31   recipes_taking_literal_strings.insert("trace-should-contain");
    32   recipes_taking_literal_strings.insert("trace-should-not-contain");
    33   recipes_taking_literal_strings.insert("check-trace-count-for-label");
    34   recipes_taking_literal_strings.insert("check-trace-count-for-label-greater-than");
    35   recipes_taking_literal_strings.insert("check-trace-count-for-label-lesser-than");
    36   // End initialize_transform_rewrite_literal_string_to_text()
    37 }
    38
    39 void rewrite_literal_string_to_text(const recipe_ordinal r) {
    40   recipe& caller = get(Recipe, r);
    41   trace(9991, "transform") << "--- rewrite literal strings in recipe " << caller.name << end();
    42   if (contains_numeric_locations(caller)) return;
    43   vector<instruction> new_instructions;
    44   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
    45     instruction& inst = caller.steps.at(i);
    46     if (recipes_taking_literal_strings.find(inst.name) == recipes_taking_literal_strings.end()) {
    47       for (int j = 0;  j < SIZE(inst.ingredients);  ++j) {
    48         if (!is_literal_text(inst.ingredients.at(j))) continue;
    49         instruction def;
    50         ostringstream ingredient_name;
    51         ingredient_name << inst.name << '_' << i << '_' << j << ":text";
    52         def.name = "new";
    53         def.ingredients.push_back(inst.ingredients.at(j));
    54         def.products.push_back(reagent(ingredient_name.str()));
    55         new_instructions.push_back(def);
    56         inst.ingredients.at(j).clear();  // reclaim old memory
    57         inst.ingredients.at(j) = reagent(ingredient_name.str());
    58       }
    59     }
    60     new_instructions.push_back(inst);
    61   }
    62   caller.steps.swap(new_instructions);
    63 }
    64
    65 bool contains_numeric_locations(const recipe& caller) {
    66   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
    67     const instruction& inst = caller.steps.at(i);
    68     for (int in = 0;  in < SIZE(inst.ingredients);  ++in)
    69       if (is_numeric_location(inst.ingredients.at(in)))
    70         return true;
    71     for (int out = 0;  out < SIZE(inst.products);  ++out)
    72       if (is_numeric_location(inst.products.at(out)))
    73         return true;
    74   }
    75   return false;
    76 }