From a4ef18b194a24710847be59554e51a1fd618228d Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 16 Mar 2015 22:52:04 -0700 Subject: 940 - c++: some changes to instruction model --- cpp/010vm | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'cpp/010vm') diff --git a/cpp/010vm b/cpp/010vm index a8dd451a..870df1e4 100644 --- a/cpp/010vm +++ b/cpp/010vm @@ -24,7 +24,8 @@ struct recipe { struct instruction { bool is_label; string label; // only if is_label - recipe_number operation; // only if !is_label + string name; // only if !is_label + recipe_number operation; // Recipe_number[name] vector ingredients; // only if !is_label vector products; // only if !is_label instruction(); @@ -37,9 +38,10 @@ struct instruction { // us how to interpret them. They also can contain arbitrary other lists of // properties besides types, but we're getting ahead of ourselves. struct reagent { + vector > > properties; string name; + int value; vector types; - vector > properties; reagent(string s); reagent(type_number t); string to_string(); @@ -74,6 +76,7 @@ int Next_type_number = 1; void setup_types() { Type.clear(); Type_number.clear(); Type_number["literal"] = 0; + Type_number["offset"] = 0; Next_type_number = 1; // Mu Types Initialization. int integer = Type_number["integer"] = Next_type_number++; @@ -138,16 +141,20 @@ void setup_recipes() { // Reagents have the form :::...///... reagent::reagent(string s) { istringstream in(s); - name = slurp_until(in, ':'); - istringstream ts(slurp_until(in, '/')); - string t; - while (!(t = slurp_until(ts, ':')).empty()) - types.push_back(Type_number[t]); // properties while (!in.eof()) { - istringstream prop(slurp_until(in, '/')); - string name = slurp_until(prop, ':'); - properties.push_back(pair(name, property())); + istringstream row(slurp_until(in, '/')); + string name = slurp_until(row, ':'); + vector values; + while (!row.eof()) + values.push_back(slurp_until(row, ':')); + properties.push_back(pair >(name, values)); + } + // structures for the first row of properties + name = properties[0].first; + value = to_int(name); + for (size_t i = 0; i < properties[0].second.size(); ++i) { + types.push_back(Type_number[properties[0].second[i]]); } } reagent::reagent(type_number t) { @@ -155,19 +162,21 @@ void setup_recipes() { } string reagent::to_string() { ostringstream out; - out << "{name: \"" << name << "\", type: "; + out << "{name: \"" << name << "\", value: " << value << ", type: "; for (size_t i = 0; i < types.size(); ++i) { out << types[i]; if (i < types.size()-1) out << "-"; } if (!properties.empty()) { - out << ", property: "; + out << ", properties: ["; for (size_t i = 0; i < properties.size(); ++i) { - out << properties[i].first << ":"; - for (size_t j = 0; j < properties[i].second.values.size(); ++j) { - out << properties[i].second.values[j]; - if (j < properties[i].second.values.size()-1) out << ":"; + out << properties[i].first << ": "; + for (size_t j = 0; j < properties[i].second.size(); ++j) { + out << properties[i].second[j]; + if (j < properties[i].second.size()-1) out << ":"; } + if (i < properties.size()-1) out << ", "; + else out << "]"; } } out << "}"; @@ -192,3 +201,10 @@ void dump_memory() { cout << p->first << ": " << p->second << '\n'; } } + +int to_int(string n) { + char* end = NULL; + int result = strtol(n.c_str(), &end, /*any base*/0); + assert(*end == '\0'); + return result; +} -- cgit 1.4.1-2-gfad0