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("$print");
23   recipes_taking_literal_strings.insert("$dump-trace");
24   recipes_taking_literal_strings.insert("$system");
25   recipes_taking_literal_strings.insert("trace");
26   recipes_taking_literal_strings.insert("stash");
27   recipes_taking_literal_strings.insert("new");
28   recipes_taking_literal_strings.insert("run");
29   recipes_taking_literal_strings.insert("memory-should-contain");
30   recipes_taking_literal_strings.insert("trace-should-contain");
31   recipes_taking_literal_strings.insert("trace-should-not-contain");
32   recipes_taking_literal_strings.insert("check-trace-count-for-label");
33   // End initialize_transform_rewrite_literal_string_to_text()
34 }
35 
36 void rewrite_literal_string_to_text(const recipe_ordinal r) {
37   recipe& caller = get(Recipe, r);
38   trace(9991, "transform") << "--- rewrite literal strings in recipe " << caller.name << end();
39   if (contains_numeric_locations(caller)) return;
40   vector<instruction> new_instructions;
41   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
42   ¦ instruction& inst = caller.steps.at(i);
43   ¦ if (recipes_taking_literal_strings.find(inst.name) == recipes_taking_literal_strings.end()) {
44   ¦ ¦ for (int j = 0;  j < SIZE(inst.ingredients);  ++j) {
45   ¦ ¦ ¦ if (!is_literal_text(inst.ingredients.at(j))) continue;
46   ¦ ¦ ¦ instruction def;
47   ¦ ¦ ¦ ostringstream ingredient_name;
48   ¦ ¦ ¦ ingredient_name << inst.name << '_' << i << '_' << j << ":text";
49   ¦ ¦ ¦ def.name = "new";
50   ¦ ¦ ¦ def.ingredients.push_back(inst.ingredients.at(j));
51   ¦ ¦ ¦ def.products.push_back(reagent(ingredient_name.str()));
52   ¦ ¦ ¦ new_instructions.push_back(def);
53   ¦ ¦ ¦ inst.ingredients.at(j).clear();  // reclaim old memory
54   ¦ ¦ ¦ inst.ingredients.at(j) = reagent(ingredient_name.str());
55   ¦ ¦ }
56   ¦ }
57   ¦ new_instructions.push_back(inst);
58   }
59   caller.steps.swap(new_instructions);
60 }
61 
62 bool contains_numeric_locations(const recipe& caller) {
63   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
64   ¦ const instruction& inst = caller.steps.at(i);
65   ¦ for (int in = 0;  in < SIZE(inst.ingredients);  ++in)
66   ¦ ¦ if (is_numeric_location(inst.ingredients.at(in)))
67   ¦ ¦ ¦ return true;
68   ¦ for (int out = 0;  out < SIZE(inst.products);  ++out)
69   ¦ ¦ if (is_numeric_location(inst.products.at(out)))
70   ¦ ¦ ¦ return true;
71   }
72   return false;
73 }