1
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);
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 recipes_taking_literal_strings.insert("check-trace-count-for-label-greater-than");
34 recipes_taking_literal_strings.insert("check-trace-count-for-label-lesser-than");
35
36 }
37
38 void rewrite_literal_string_to_text(const recipe_ordinal r) {
39 recipe& caller = get(Recipe, r);
40 trace(9991, "transform") << "--- rewrite literal strings in recipe " << caller.name << end();
41 if (contains_numeric_locations(caller)) return;
42 vector<instruction> new_instructions;
43 for (int i = 0; i < SIZE(caller.steps); ++i) {
44 instruction& inst = caller.steps.at(i);
45 if (recipes_taking_literal_strings.find(inst.name) == recipes_taking_literal_strings.end()) {
46 for (int j = 0; j < SIZE(inst.ingredients); ++j) {
47 if (!is_literal_text(inst.ingredients.at(j))) continue;
48 instruction def;
49 ostringstream ingredient_name;
50 ingredient_name << inst.name << '_' << i << '_' << j << ":text";
51 def.name = "new";
52 def.ingredients.push_back(inst.ingredients.at(j));
53 def.products.push_back(reagent(ingredient_name.str()));
54 new_instructions.push_back(def);
55 inst.ingredients.at(j).clear();
56 inst.ingredients.at(j) = reagent(ingredient_name.str());
57 }
58 }
59 new_instructions.push_back(inst);
60 }
61 caller.steps.swap(new_instructions);
62 }
63
64 bool contains_numeric_locations(const recipe& caller) {
65 for (int i = 0; i < SIZE(caller.steps); ++i) {
66 const instruction& inst = caller.steps.at(i);
67 for (int in = 0; in < SIZE(inst.ingredients); ++in)
68 if (is_numeric_location(inst.ingredients.at(in)))
69 return true;
70 for (int out = 0; out < SIZE(inst.products); ++out)
71 if (is_numeric_location(inst.products.at(out)))
72 return true;
73 }
74 return false;
75 }