diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-03-18 22:09:31 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-03-18 22:09:31 -0700 |
commit | 9cc16d04958753a62474d13bd823f13a33a6a0a3 (patch) | |
tree | 2fc4b37f8fda0d8ac7662cdf5cf05a942b428f45 | |
parent | b1299d632715669c90dde918f5522c776b853e66 (diff) | |
download | mu-9cc16d04958753a62474d13bd823f13a33a6a0a3.tar.gz |
956
Cleaner way to signal that a reagent's value has been computed.
-rw-r--r-- | cpp/010vm | 5 | ||||
-rw-r--r-- | cpp/012transform | 2 | ||||
-rw-r--r-- | cpp/024brace | 13 |
3 files changed, 11 insertions, 9 deletions
diff --git a/cpp/010vm b/cpp/010vm index 8bb1604b..d8394fdb 100644 --- a/cpp/010vm +++ b/cpp/010vm @@ -41,6 +41,7 @@ struct reagent { vector<pair<string, vector<string> > > properties; string name; int value; + bool initialized; vector<type_number> types; reagent(string s); reagent(type_number t); @@ -139,7 +140,7 @@ void setup_recipes() { void instruction::clear() { is_label=false; label.clear(); operation=IDLE; ingredients.clear(); products.clear(); } // Reagents have the form <name>:<type>:<type>:.../<property>/<property>/... - reagent::reagent(string s) :value(0) { + reagent::reagent(string s) :value(0), initialized(false) { istringstream in(s); // properties while (!in.eof()) { @@ -156,7 +157,7 @@ void setup_recipes() { types.push_back(Type_number[properties[0].second[i]]); } } - reagent::reagent(type_number t) :value(0) { + reagent::reagent(type_number t) :value(0), initialized(false) { types.push_back(t); } string reagent::to_string() { diff --git a/cpp/012transform b/cpp/012transform index 7a591a87..21bcf60f 100644 --- a/cpp/012transform +++ b/cpp/012transform @@ -46,7 +46,7 @@ void parse_int_reagents() { } void populate_value(reagent& r) { - if (r.name.empty()) return; + if (r.initialized) return; char* end = NULL; int result = strtol(r.name.c_str(), &end, /*any base*/0); if (*end != '\0') return; diff --git a/cpp/024brace b/cpp/024brace index d97f228f..aa8c7a4c 100644 --- a/cpp/024brace +++ b/cpp/024brace @@ -67,7 +67,7 @@ void transform_braces(const recipe_number r) { else { reagent ing(0); // literal ing.value = open_braces.top()-index; - ing.name.clear(); + ing.initialized = true; inst.ingredients.push_back(ing); trace("after-brace") << "jump " << ing.value << ":offset"; trace("after-brace") << index << ": " << ing.to_string(); @@ -83,7 +83,7 @@ void transform_braces(const recipe_number r) { else { reagent ing(0); // literal ing.value = matching_brace(open_braces.top(), braces) - index - 1; - ing.name.clear(); + ing.initialized = true; inst.ingredients.push_back(ing); trace("after-brace") << "jump " << ing.value << ":offset"; } @@ -97,7 +97,7 @@ void transform_braces(const recipe_number r) { else { reagent ing(0); // literal ing.value = open_braces.top()-index; - ing.name.clear(); + ing.initialized = true; inst.ingredients.push_back(ing); trace("after-brace") << "jump-if " << inst.ingredients[0].name << ", " << ing.value << ":offset"; } @@ -111,7 +111,7 @@ void transform_braces(const recipe_number r) { else { reagent ing(0); // literal ing.value = matching_brace(open_braces.top(), braces) - index - 1; - ing.name.clear(); + ing.initialized = true; inst.ingredients.push_back(ing); trace("after-brace") << "jump-if " << inst.ingredients[0].name << ", " << ing.value << ":offset"; } @@ -125,7 +125,7 @@ void transform_braces(const recipe_number r) { else { reagent ing(0); // literal ing.value = open_braces.top()-index; - ing.name.clear(); + ing.initialized = true; inst.ingredients.push_back(ing); trace("after-brace") << "jump-unless " << inst.ingredients[0].name << ", " << ing.value << ":offset"; } @@ -139,7 +139,7 @@ void transform_braces(const recipe_number r) { else { reagent ing(0); // literal ing.value = matching_brace(open_braces.top(), braces) - index - 1; - ing.name.clear(); + ing.initialized = true; inst.ingredients.push_back(ing); trace("after-brace") << "jump-unless " << inst.ingredients[0].name << ", " << ing.value << ":offset"; } @@ -359,6 +359,7 @@ recipe factorial [ { 3:boolean <- equal 1:integer 1:literal break-if 3:boolean +# $print 1:integer 2:integer <- multiply 2:integer, 1:integer 1:integer <- subtract 1:integer, 1:literal loop |