diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2016-02-20 08:36:06 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2016-02-20 08:36:06 -0800 |
commit | 96db34759717072fa600f2c4f4e27b2aa808859c (patch) | |
tree | 8a8e1036c802c4175bd31a8d7764d4cb5c3f1fe0 | |
parent | a284b697db8306e747afc79d90d2365152bd622f (diff) | |
download | mu-96db34759717072fa600f2c4f4e27b2aa808859c.tar.gz |
2674 - tried to follow s-exps more closely
But I realize that this won't actually streamline replace_type_ingredients(), which needs that 'if (curr->left) curr = curr->left' dance for an unrelated reason. So there's no justification for entering into this big refactoring.
-rw-r--r-- | 010vm.cc | 40 | ||||
-rw-r--r-- | 058shape_shifting_container.cc | 3 |
2 files changed, 24 insertions, 19 deletions
diff --git a/010vm.cc b/010vm.cc index f4f3cb11..af4bd1b2 100644 --- a/010vm.cc +++ b/010vm.cc @@ -87,17 +87,16 @@ struct type_tree { }; struct string_tree { - string value; - string_tree* left; - string_tree* right; + bool is_leaf; + string value; // only if is_leaf is true + string_tree* left; // only if is_leaf is false + string_tree* right; // only if is_leaf is false ~string_tree(); string_tree(const string_tree& old); // simple: flat string - explicit string_tree(string v) :value(v), left(NULL), right(NULL) {} - // intermediate: list of strings - string_tree(string v, string_tree* r) :value(v), left(NULL), right(r) {} + explicit string_tree(string v) :is_leaf(true), value(v), left(NULL), right(NULL) {} // advanced: tree containing strings - string_tree(string_tree* l, string_tree* r) :left(l), right(r) {} + string_tree(string_tree* l, string_tree* r) :is_leaf(false), value(""), left(l), right(r) {} }; :(before "End Globals") @@ -237,6 +236,7 @@ reagent::reagent(string s) :original_string(s), value(0), initialized(false), ty row >> std::noskipws; string key = slurp_until(row, ':'); string_tree* value = parse_property_list(row); + cerr << "bb: " << to_string(value) << '\n'; properties.push_back(pair<string, string_tree*>(key, value)); } // structures for the first row of properties: name and list of types @@ -258,9 +258,14 @@ reagent::reagent(string s) :original_string(s), value(0), initialized(false), ty string_tree* parse_property_list(istream& in) { skip_whitespace_but_not_newline(in); if (!has_data(in)) return NULL; - string_tree* result = new string_tree(slurp_until(in, ':')); - result->right = parse_property_list(in); - return result; + string next = slurp_until(in, ':'); + if (!has_data(in)) { + string_tree* result = new string_tree(next); + cerr << "aa: " << to_string(result) << '\n'; + return result; + } + return new string_tree(new string_tree(next), + parse_property_list(in)); } type_tree* new_type_tree(const string_tree* properties) { @@ -471,7 +476,7 @@ string inspect(const string_tree* x) { } void dump_inspect(const string_tree* x, ostream& out) { - if (!x->left && !x->right) { + if (x->is_leaf) { out << x->value; return; } @@ -489,17 +494,14 @@ void dump_inspect(const string_tree* x, ostream& out) { 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(property, out); + cerr << "AAA " << property->is_leaf << " " << property->value << '\n'; + dump(property, out); return out.str(); } void dump(const string_tree* x, ostream& out) { - if (!x->left && !x->right) { - out << x->value; + if (x->is_leaf) { + out << '"' + x->value + '"'; return; } out << '('; @@ -540,7 +542,7 @@ void dump(const type_tree* type, ostream& out) { void dump(type_ordinal type, ostream& out) { if (contains_key(Type, type)) - out << get(Type, type).name; + out << get(Type, type).name << '(' << type << ") "; else out << "?" << type; } diff --git a/058shape_shifting_container.cc b/058shape_shifting_container.cc index f16d4c75..256c3800 100644 --- a/058shape_shifting_container.cc +++ b/058shape_shifting_container.cc @@ -269,6 +269,9 @@ void replace_type_ingredients(type_tree* element_type, string_tree* element_type replacement = curr->left; } else { + // We want foo:_t to be used like foo:number, which expands to {foo: number} + // rather than {foo: (number)} + // We'd also like to use it with multiple types: foo:address:number. replacement = curr; if (!final_type_ingredient(type_ingredient_index, container_info)) splice_right = false; |