about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-02-19 14:50:44 -0800
committerKartik K. Agaram <vc@akkartik.com>2016-02-19 14:50:44 -0800
commit6cc999ae812b835786f08a0b8d958a545c3d8ef0 (patch)
tree6eb40a9e71d4cc24a3a34c56aa471ca0b49c32b6
parent8a3d101e3d617107596c3eed393571eb5cbddbd6 (diff)
downloadmu-6cc999ae812b835786f08a0b8d958a545c3d8ef0.tar.gz
2670 - better names for string conversions
  to_string(): relatively stable fields only; for trace()
  debug_string(): all fields; for debugging
  inspect(): for a form that can be parsed back later
-rw-r--r--010vm.cc20
-rw-r--r--037new.cc2
-rw-r--r--059shape_shifting_recipe.cc12
-rw-r--r--060immutable.cc2
4 files changed, 19 insertions, 17 deletions
diff --git a/010vm.cc b/010vm.cc
index a14bd0d7..711f8fa4 100644
--- a/010vm.cc
+++ b/010vm.cc
@@ -401,6 +401,8 @@ void dump_memory() {
 //: Use to_string() in trace(), and try to avoid relying on unstable codes that
 //: will perturb .traces/ from commit to commit.
 //: Use debug_string() while debugging, and throw everything into it.
+//: Use inspect() only for emitting a canonical format that can be parsed back
+//: into the value.
 
 string to_string(const recipe& r) {
   ostringstream out;
@@ -450,7 +452,7 @@ string to_string(const reagent& r) {
     out << "{";
     for (long long int i = 0; i < SIZE(r.properties); ++i) {
       if (i > 0) out << ", ";
-      out << "\"" << r.properties.at(i).first << "\": " << debug_string(r.properties.at(i).second);
+      out << "\"" << r.properties.at(i).first << "\": " << to_string(r.properties.at(i).second);
     }
     out << "}";
   }
@@ -463,13 +465,13 @@ string debug_string(const reagent& x) {
   return out.str();
 }
 
-string to_string(const string_tree* x) {
+string inspect(const string_tree* x) {
   ostringstream out;
-  dump(x, out);
+  dump_inspect(x, out);
   return out.str();
 }
 
-void dump(const string_tree* x, ostream& out) {
+void dump_inspect(const string_tree* x, ostream& out) {
   if (!x->left && !x->right) {
     out << x->value;
     return;
@@ -478,25 +480,25 @@ void dump(const string_tree* x, ostream& out) {
   for (const string_tree* curr = x; curr; curr = curr->right) {
     if (curr != x) out << ' ';
     if (curr->left)
-      dump(curr->left, out);
+      dump_inspect(curr->left, out);
     else
       out << curr->value;
   }
   out << ')';
 }
 
-string debug_string(const string_tree* property) {
+string to_string(const string_tree* property) {
   if (!property) return "()";
   ostringstream out;
   if (!property->left && !property->right)
     // abbreviate a single-node tree to just its contents
     out << '"' << property->value << '"';
   else
-    dump_debug(property, out);
+    dump(property, out);
   return out.str();
 }
 
-void dump_debug(const string_tree* x, ostream& out) {
+void dump(const string_tree* x, ostream& out) {
   if (!x->left && !x->right) {
     out << x->value;
     return;
@@ -505,7 +507,7 @@ void dump_debug(const string_tree* x, ostream& out) {
   for (const string_tree* curr = x; curr; curr = curr->right) {
     if (curr != x) out << ' ';
     if (curr->left)
-      dump_debug(curr->left, out);
+      dump(curr->left, out);
     else
       out << '"' << curr->value << '"';
   }
diff --git a/037new.cc b/037new.cc
index 05d8a3e7..fdda83fb 100644
--- a/037new.cc
+++ b/037new.cc
@@ -135,7 +135,7 @@ void transform_new_to_allocate(const recipe_ordinal r) {
       // End Post-processing(type_name) When Converting 'new'
       type_tree* type = new_type_tree(type_name);
       inst.ingredients.at(0).set_value(size_of(type));
-      trace(9992, "new") << "size of " << debug_string(type_name) << " is " << inst.ingredients.at(0).value << end();
+      trace(9992, "new") << "size of " << to_string(type_name) << " is " << inst.ingredients.at(0).value << end();
       delete type;
       delete type_name;
     }
diff --git a/059shape_shifting_recipe.cc b/059shape_shifting_recipe.cc
index 5784af16..a9bf9071 100644
--- a/059shape_shifting_recipe.cc
+++ b/059shape_shifting_recipe.cc
@@ -310,10 +310,10 @@ void compute_type_names(recipe& variant) {
 }
 
 void save_or_deduce_type_name(reagent& x, map<string, string_tree*>& type_name, const recipe& variant) {
-  trace(9994, "transform") << "    checking " << to_string(x) << ": " << debug_string(x.properties.at(0).second) << end();
+  trace(9994, "transform") << "    checking " << to_string(x) << ": " << to_string(x.properties.at(0).second) << end();
   if (!x.properties.at(0).second && contains_key(type_name, x.name)) {
     x.properties.at(0).second = new string_tree(*get(type_name, x.name));
-    trace(9994, "transform") << "    deducing type to " << debug_string(x.properties.at(0).second) << end();
+    trace(9994, "transform") << "    deducing type to " << to_string(x.properties.at(0).second) << end();
     return;
   }
   if (!x.properties.at(0).second) {
@@ -323,7 +323,7 @@ void save_or_deduce_type_name(reagent& x, map<string, string_tree*>& type_name,
   if (contains_key(type_name, x.name)) return;
   if (x.properties.at(0).second->value == "offset" || x.properties.at(0).second->value == "variant") return;  // special-case for container-access instructions
   put(type_name, x.name, x.properties.at(0).second);
-  trace(9993, "transform") << "type of " << x.name << " is " << debug_string(x.properties.at(0).second) << end();
+  trace(9993, "transform") << "type of " << x.name << " is " << to_string(x.properties.at(0).second) << end();
 }
 
 void compute_type_ingredient_mappings(const recipe& exemplar, const instruction& inst, map<string, const string_tree*>& mappings, const recipe& caller_recipe, bool* error) {
@@ -369,7 +369,7 @@ void accumulate_type_ingredients(const string_tree* exemplar_type, const string_
       return;
     }
     if (!contains_key(mappings, exemplar_type->value)) {
-      trace(9993, "transform") << "adding mapping from " << exemplar_type->value << " to " << debug_string(refinement_type) << end();
+      trace(9993, "transform") << "adding mapping from " << exemplar_type->value << " to " << to_string(refinement_type) << end();
       put(mappings, exemplar_type->value, new string_tree(*refinement_type));
     }
     else {
@@ -413,7 +413,7 @@ void replace_type_ingredients(recipe& new_recipe, const map<string, const string
     if (inst.name == "new" && inst.ingredients.at(0).properties.at(0).second->value != "literal-string") {
       string_tree* type_name = parse_string_tree(inst.ingredients.at(0).name);
       replace_type_ingredients(type_name, mappings);
-      inst.ingredients.at(0).name = to_string(type_name);
+      inst.ingredients.at(0).name = inspect(type_name);
       delete type_name;
     }
   }
@@ -438,7 +438,7 @@ void replace_type_ingredients(string_tree* type, const map<string, const string_
   if (!type) return;
   if (is_type_ingredient_name(type->value) && contains_key(mappings, type->value)) {
     const string_tree* replacement = get(mappings, type->value);
-    trace(9993, "transform") << type->value << " => " << debug_string(replacement) << end();
+    trace(9993, "transform") << type->value << " => " << to_string(replacement) << end();
     if (replacement->value == "literal")
       type->value = "number";
     else
diff --git a/060immutable.cc b/060immutable.cc
index 496e33c7..67087d28 100644
--- a/060immutable.cc
+++ b/060immutable.cc
@@ -343,6 +343,6 @@ if (has_property(current_ingredient, "contained-in")) {
   if (tmp->left || tmp->right
       || !is_present_in_ingredients(caller, tmp->value)
       || !is_present_in_products(caller, tmp->value))
-    raise_error << maybe(caller.name) << "contained-in can only point to another ingredient+product, but got " << debug_string(property(current_ingredient, "contained-in")) << '\n' << end();
+    raise_error << maybe(caller.name) << "contained-in can only point to another ingredient+product, but got " << to_string(property(current_ingredient, "contained-in")) << '\n' << end();
   continue;
 }