about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-11-28 23:58:43 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-11-28 23:58:43 -0800
commit67bc24e7e283cba3aebd37f7282b9ee1f146d8c8 (patch)
tree5231c49f3eb66a8d14f289603b9bfced3123993c
parentb08450c208bc14d15e96726dac0d7b6e278f05ac (diff)
downloadmu-67bc24e7e283cba3aebd37f7282b9ee1f146d8c8.tar.gz
2504 - support to-text in 'stash'
-rw-r--r--042name.cc17
-rw-r--r--053rewrite_stash.cc51
-rw-r--r--073list.mu11
-rw-r--r--999spaces.cc1
4 files changed, 72 insertions, 8 deletions
diff --git a/042name.cc b/042name.cc
index 682d89cb..bb9fb08c 100644
--- a/042name.cc
+++ b/042name.cc
@@ -30,31 +30,32 @@ for (long long int i = 0; i < SIZE(recently_added_recipes); ++i) {
 
 :(code)
 void transform_names(const recipe_ordinal r) {
-  trace(9991, "transform") << "--- transform names for recipe " << get(Recipe, r).name << end();
-//?   cerr << "--- transform names for recipe " << get(Recipe, r).name << '\n';
+  recipe& caller = get(Recipe, r);
+  trace(9991, "transform") << "--- transform names for recipe " << caller.name << end();
+//?   cerr << "--- transform names for recipe " << caller.name << '\n';
   bool names_used = false;
   bool numeric_locations_used = false;
   map<string, long long int>& names = Name[r];
   // store the indices 'used' so far in the map
   long long int& curr_idx = names[""];
   ++curr_idx;  // avoid using index 0, benign skip in some other cases
-  for (long long int i = 0; i < SIZE(get(Recipe, r).steps); ++i) {
-    instruction& inst = get(Recipe, r).steps.at(i);
+  for (long long int i = 0; i < SIZE(caller.steps); ++i) {
+    instruction& inst = caller.steps.at(i);
     // End transform_names(inst) Special-cases
     // map names to addresses
     for (long long int in = 0; in < SIZE(inst.ingredients); ++in) {
       if (is_numeric_location(inst.ingredients.at(in))) numeric_locations_used = true;
       if (is_named_location(inst.ingredients.at(in))) names_used = true;
-      if (disqualified(inst.ingredients.at(in), inst, get(Recipe, r).name)) continue;
+      if (disqualified(inst.ingredients.at(in), inst, caller.name)) continue;
       if (!already_transformed(inst.ingredients.at(in), names)) {
-        raise_error << maybe(get(Recipe, r).name) << "use before set: " << inst.ingredients.at(in).name << '\n' << end();
+        raise_error << maybe(caller.name) << "use before set: " << inst.ingredients.at(in).name << '\n' << end();
       }
       inst.ingredients.at(in).set_value(lookup_name(inst.ingredients.at(in), r));
     }
     for (long long int out = 0; out < SIZE(inst.products); ++out) {
       if (is_numeric_location(inst.products.at(out))) numeric_locations_used = true;
       if (is_named_location(inst.products.at(out))) names_used = true;
-      if (disqualified(inst.products.at(out), inst, get(Recipe, r).name)) continue;
+      if (disqualified(inst.products.at(out), inst, caller.name)) continue;
       if (names.find(inst.products.at(out).name) == names.end()) {
         trace(9993, "name") << "assign " << inst.products.at(out).name << " " << curr_idx << end();
         names[inst.products.at(out).name] = curr_idx;
@@ -64,7 +65,7 @@ void transform_names(const recipe_ordinal r) {
     }
   }
   if (names_used && numeric_locations_used)
-    raise_error << maybe(get(Recipe, r).name) << "mixing variable names and numeric addresses\n" << end();
+    raise_error << maybe(caller.name) << "mixing variable names and numeric addresses\n" << end();
 }
 
 bool disqualified(/*mutable*/ reagent& x, const instruction& inst, const string& recipe_name) {
diff --git a/053rewrite_stash.cc b/053rewrite_stash.cc
new file mode 100644
index 00000000..df64baaf
--- /dev/null
+++ b/053rewrite_stash.cc
@@ -0,0 +1,51 @@
+//: when encountering other types, try to convert them to strings using
+//: 'to-text'
+
+:(before "End Instruction Inserting/Deleting Transforms")
+Transform.push_back(rewrite_stashes_to_text);
+
+:(code)
+void rewrite_stashes_to_text(recipe_ordinal r) {
+  recipe& caller = get(Recipe, r);
+  if (contains_named_locations(caller))
+    rewrite_stashes_to_text_named(caller);
+  // in recipes without named locations, 'stash' is still not configurable
+}
+
+bool contains_named_locations(const recipe& caller) {
+  for (long long int i = 0; i < SIZE(caller.steps); ++i) {
+    const instruction& inst = caller.steps.at(i);
+    for (long long int in = 0; in < SIZE(inst.ingredients); ++in)
+      if (is_named_location(inst.ingredients.at(in)))
+        return true;
+    for (long long int out = 0; out < SIZE(inst.products); ++out)
+      if (is_named_location(inst.products.at(out)))
+        return true;
+  }
+  return false;
+}
+
+void rewrite_stashes_to_text_named(recipe& caller) {
+  static long long int stash_instruction_idx = 0;
+  vector<instruction> new_instructions;
+  for (long long int i = 0; i < SIZE(caller.steps); ++i) {
+    instruction& inst = caller.steps.at(i);
+    if (inst.name == "stash") {
+      for (long long int j = 0; j < SIZE(inst.ingredients); ++j) {
+        if (is_literal(inst.ingredients.at(j))) continue;
+        if (is_mu_string(inst.ingredients.at(j))) continue;
+        instruction def;
+        def.name = "to-text";
+        def.ingredients.push_back(inst.ingredients.at(j));
+        ostringstream ingredient_name;
+        ingredient_name << "stash_" << stash_instruction_idx << '_' << j << ":address:array:character";
+        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);
+  }
+  new_instructions.swap(caller.steps);
+}
diff --git a/073list.mu b/073list.mu
index f3b25142..e2879624 100644
--- a/073list.mu
+++ b/073list.mu
@@ -101,3 +101,14 @@ scenario display-list [
     3:array:character <- [6 -> 5 -> 4]
   ]
 ]
+
+scenario stash-on-list-converts-to-text [
+  run [
+    x:address:list:number <- push 4, 0
+    x <- push 5, x
+    stash [foo foo], x
+  ]
+  trace-should-contain [
+    app: foo foo 5 -> 4
+  ]
+]
diff --git a/999spaces.cc b/999spaces.cc
index 1e85f908..2ab83f87 100644
--- a/999spaces.cc
+++ b/999spaces.cc
@@ -38,6 +38,7 @@ assert(Max_callstack_depth == 9989);
 //:     52 insert fragments
 //:      ↳ 52.2 check fragments
 //:   ---
+//:     53 rewrite 'stash' instructions
 //:   end instruction inserting transforms
 //:
 //:   begin instruction modifying transforms