about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-11-08 22:42:21 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-11-08 22:42:21 -0800
commitd379d683577e66ea927c212f579f7f45853623f3 (patch)
tree51316f1bc7c67c6ef6f2b6727d610b09a87aff07
parentb766f5f8747166b688413d15375880be510d8af6 (diff)
downloadmu-d379d683577e66ea927c212f579f7f45853623f3.tar.gz
2405
-rw-r--r--010vm.cc24
-rw-r--r--059generic_recipe.cc24
2 files changed, 25 insertions, 23 deletions
diff --git a/010vm.cc b/010vm.cc
index cfd12fce..6b7d58ff 100644
--- a/010vm.cc
+++ b/010vm.cc
@@ -99,6 +99,8 @@ struct string_tree {
   string_tree(string v, string_tree* r) :value(v), left(NULL), right(r) {}
   // advanced: tree containing strings
   string_tree(string_tree* l, string_tree* r) :left(l), right(r) {}
+  // print as s-expression
+  string to_string() const;
 };
 
 :(before "End Globals")
@@ -507,6 +509,28 @@ string recipe::to_string() const {
   return out.str();
 }
 
+string string_tree::to_string() const {
+  ostringstream out;
+  dump(this, out);
+  return out.str();
+}
+
+void dump(const string_tree* x, ostream& out) {
+  if (!x->left && !x->right) {
+    out << x->value;
+    return;
+  }
+  out << '(';
+  for (const string_tree* curr = x; curr; curr = curr->right) {
+    if (curr != x) out << ' ';
+    if (curr->left)
+      dump(curr->left, out);
+    else
+      out << curr->value;
+  }
+  out << ')';
+}
+
 void skip_whitespace(istream& in) {
   while (!in.eof() && isspace(in.peek()) && in.peek() != '\n') {
     in.get();
diff --git a/059generic_recipe.cc b/059generic_recipe.cc
index 7d5f64f1..2c91c669 100644
--- a/059generic_recipe.cc
+++ b/059generic_recipe.cc
@@ -259,34 +259,12 @@ void replace_type_ingredients(recipe& new_recipe, const map<string, const string
     if (inst.name == "new" && inst.ingredients.at(0).name.at(0) != '[') {
       string_tree* type_name = parse_string_tree(inst.ingredients.at(0).name);
       replace_type_ingredients(type_name, mappings);
-      inst.ingredients.at(0).name = simple_string(type_name);
+      inst.ingredients.at(0).name = type_name->to_string();
       delete type_name;
     }
   }
 }
 
-string simple_string(string_tree* x) {
-  ostringstream out;
-  simple_string(x, out);
-  return out.str();
-}
-
-void simple_string(string_tree* x, ostream& out) {
-  if (!x->left && !x->right) {
-    out << x->value;
-    return;
-  }
-  out << '(';
-  for (string_tree* curr = x; curr; curr = curr->right) {
-    if (curr != x) out << ' ';
-    if (curr->left)
-      simple_string(curr->left, out);
-    else
-      out << curr->value;
-  }
-  out << ')';
-}
-
 void replace_type_ingredients(reagent& x, const map<string, const string_tree*>& mappings) {
   trace(9993, "transform") << "replacing in ingredient " << x.original_string << end();
   // replace properties