about summary refs log tree commit diff stats
path: root/010vm.cc
diff options
context:
space:
mode:
Diffstat (limited to '010vm.cc')
-rw-r--r--010vm.cc24
1 files changed, 24 insertions, 0 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();