diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-02-19 16:54:33 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-02-19 16:56:26 -0800 |
commit | 42b31beba71db2012db3ea47f4ca8cfc18136d14 (patch) | |
tree | e0c8aa54ec492b3aa2c4b90ed3d972113340defd | |
parent | 8d7839b99723553c08b33c30980a001e7a3557b6 (diff) | |
download | mu-42b31beba71db2012db3ea47f4ca8cfc18136d14.tar.gz |
789
-rw-r--r-- | cpp/010vm | 13 | ||||
-rw-r--r-- | cpp/012run | 2 | ||||
-rw-r--r-- | cpp/013arithmetic | 164 |
3 files changed, 95 insertions, 84 deletions
diff --git a/cpp/010vm b/cpp/010vm index 317f6d70..b864179e 100644 --- a/cpp/010vm +++ b/cpp/010vm @@ -96,6 +96,9 @@ struct type_info { type_info() :size(0) {} }; +:(before "End Globals") +const int IDLE = 0; // always the first entry in the recipe book +const int COPY = 1; :(code) // It's all very well to construct recipes out of other recipes, but we need // to know how to do *something* out of the box. For the following @@ -103,15 +106,13 @@ struct type_info { // what to do for them. void setup_recipes() { Recipe.clear(); Recipe_number.clear(); - Recipe_number["idle"] = 0; + Recipe_number["idle"] = IDLE; Next_recipe_number = 1; // Primitive Recipe Numbers. - Recipe_number["copy"] = 1; + Recipe_number["copy"] = COPY; Next_recipe_number++; // End Primitive Recipe Numbers. } -:(before "End Types") -const int idle = 0; // always the first entry in the recipe book :(before "End Setup") setup_recipes(); @@ -119,8 +120,8 @@ const int idle = 0; // always the first entry in the recipe book :(code) // Helpers - instruction::instruction() :is_label(false), operation(idle) {} - void instruction::clear() { is_label=false; label.clear(); operation=idle; ingredients.clear(); products.clear(); } + instruction::instruction() :is_label(false), operation(IDLE) {} + 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) { diff --git a/cpp/012run b/cpp/012run index aa6bceb4..5ba43ee5 100644 --- a/cpp/012run +++ b/cpp/012run @@ -30,7 +30,7 @@ void run(recipe_number r) { trace("run") << "instruction " << n; switch (p->operation) { // Primitive Recipe Implementations. - case 1: { // copy + case COPY: { trace("run") << "ingredient 0 is " << p->ingredients[0].name; vector<int> data = read_memory(p->ingredients[0]); write_memory(p->products[0], data); diff --git a/cpp/013arithmetic b/cpp/013arithmetic index 1c3f9d92..7aca4264 100644 --- a/cpp/013arithmetic +++ b/cpp/013arithmetic @@ -1,21 +1,23 @@ -:(before "End Primitive Recipe Numbers") +:(before "End Globals") // Arithmetic ops. -Recipe_number["add"] = 2; +const int ADD = 2; +:(before "End Primitive Recipe Numbers") +Recipe_number["add"] = ADD; Next_recipe_number++; :(before "End Primitive Recipe Implementations") - case 2: { // add - trace("run") << "ingredient 0 is " << p->ingredients[0].name; - vector<int> arg0 = read_memory(p->ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << p->ingredients[1].name; - vector<int> arg1 = read_memory(p->ingredients[1]); - assert(arg1.size() == 1); - vector<int> result; - result.push_back(arg0[0]+arg1[0]); - trace("run") << "product 0 is " << result[0]; - write_memory(p->products[0], result); - break; - } +case ADD: { + trace("run") << "ingredient 0 is " << p->ingredients[0].name; + vector<int> arg0 = read_memory(p->ingredients[0]); + assert(arg0.size() == 1); + trace("run") << "ingredient 1 is " << p->ingredients[1].name; + vector<int> arg1 = read_memory(p->ingredients[1]); + assert(arg1.size() == 1); + vector<int> result; + result.push_back(arg0[0]+arg1[0]); + trace("run") << "product 0 is " << result[0]; + write_memory(p->products[0], result); + break; +} :(scenario "add_literal") recipe main [ @@ -41,23 +43,25 @@ recipe main [ +run: product 0 is 57 +mem: storing in location 3 +:(before "End Globals") +const int SUBTRACT = 3; :(before "End Primitive Recipe Numbers") -Recipe_number["subtract"] = 3; +Recipe_number["subtract"] = SUBTRACT; Next_recipe_number++; :(before "End Primitive Recipe Implementations") - case 3: { // subtract - trace("run") << "ingredient 0 is " << p->ingredients[0].name; - vector<int> arg0 = read_memory(p->ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << p->ingredients[1].name; - vector<int> arg1 = read_memory(p->ingredients[1]); - assert(arg1.size() == 1); - vector<int> result; - result.push_back(arg0[0]-arg1[0]); - trace("run") << "product 0 is " << result[0]; - write_memory(p->products[0], result); - break; - } +case SUBTRACT: { + trace("run") << "ingredient 0 is " << p->ingredients[0].name; + vector<int> arg0 = read_memory(p->ingredients[0]); + assert(arg0.size() == 1); + trace("run") << "ingredient 1 is " << p->ingredients[1].name; + vector<int> arg1 = read_memory(p->ingredients[1]); + assert(arg1.size() == 1); + vector<int> result; + result.push_back(arg0[0]-arg1[0]); + trace("run") << "product 0 is " << result[0]; + write_memory(p->products[0], result); + break; +} :(scenario "subtract_literal") recipe main [ @@ -83,24 +87,26 @@ recipe main [ +run: product 0 is -11 +mem: storing in location 3 +:(before "End Globals") +const int MULTIPLY = 4; :(before "End Primitive Recipe Numbers") -Recipe_number["multiply"] = 4; +Recipe_number["multiply"] = MULTIPLY; Next_recipe_number++; :(before "End Primitive Recipe Implementations") - case 4: { // multiply - trace("run") << "ingredient 0 is " << p->ingredients[0].name; - vector<int> arg0 = read_memory(p->ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << p->ingredients[1].name; - vector<int> arg1 = read_memory(p->ingredients[1]); - assert(arg1.size() == 1); - trace("run") << "ingredient 1 is " << arg1[0]; - vector<int> result; - result.push_back(arg0[0]*arg1[0]); - trace("run") << "product 0 is " << result[0]; - write_memory(p->products[0], result); - break; - } +case MULTIPLY: { + trace("run") << "ingredient 0 is " << p->ingredients[0].name; + vector<int> arg0 = read_memory(p->ingredients[0]); + assert(arg0.size() == 1); + trace("run") << "ingredient 1 is " << p->ingredients[1].name; + vector<int> arg1 = read_memory(p->ingredients[1]); + assert(arg1.size() == 1); + trace("run") << "ingredient 1 is " << arg1[0]; + vector<int> result; + result.push_back(arg0[0]*arg1[0]); + trace("run") << "product 0 is " << result[0]; + write_memory(p->products[0], result); + break; +} :(scenario "multiply_literal") recipe main [ @@ -126,24 +132,26 @@ recipe main [ +run: product 0 is 24 +mem: storing in location 3 +:(before "End Globals") +const int DIVIDE = 5; :(before "End Primitive Recipe Numbers") -Recipe_number["divide"] = 5; +Recipe_number["divide"] = DIVIDE; Next_recipe_number++; :(before "End Primitive Recipe Implementations") - case 5: { // divide - trace("run") << "ingredient 0 is " << p->ingredients[0].name; - vector<int> arg0 = read_memory(p->ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << p->ingredients[1].name; - vector<int> arg1 = read_memory(p->ingredients[1]); - assert(arg1.size() == 1); - trace("run") << "ingredient 1 is " << arg1[0]; - vector<int> result; - result.push_back(arg0[0]/arg1[0]); - trace("run") << "product 0 is " << result[0]; - write_memory(p->products[0], result); - break; - } +case DIVIDE: { + trace("run") << "ingredient 0 is " << p->ingredients[0].name; + vector<int> arg0 = read_memory(p->ingredients[0]); + assert(arg0.size() == 1); + trace("run") << "ingredient 1 is " << p->ingredients[1].name; + vector<int> arg1 = read_memory(p->ingredients[1]); + assert(arg1.size() == 1); + trace("run") << "ingredient 1 is " << arg1[0]; + vector<int> result; + result.push_back(arg0[0]/arg1[0]); + trace("run") << "product 0 is " << result[0]; + write_memory(p->products[0], result); + break; +} :(scenario "divide_literal") recipe main [ @@ -169,27 +177,29 @@ recipe main [ +run: product 0 is 9 +mem: storing in location 3 +:(before "End Globals") +const int DIVIDE_WITH_REMAINDER = 6; :(before "End Primitive Recipe Numbers") -Recipe_number["divide_with_remainder"] = 6; +Recipe_number["divide_with_remainder"] = DIVIDE_WITH_REMAINDER; Next_recipe_number++; :(before "End Primitive Recipe Implementations") - case 6: { // divide_with_remainder - trace("run") << "ingredient 0 is " << p->ingredients[0].name; - vector<int> arg0 = read_memory(p->ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << p->ingredients[1].name; - vector<int> arg1 = read_memory(p->ingredients[1]); - assert(arg1.size() == 1); - vector<int> result0; - result0.push_back(arg0[0]/arg1[0]); - trace("run") << "product 0 is " << result0[0]; - write_memory(p->products[0], result0); - vector<int> result1; - result1.push_back(arg0[0]%arg1[0]); - trace("run") << "product 1 is " << result1[0]; - write_memory(p->products[1], result1); - break; - } +case DIVIDE_WITH_REMAINDER: { + trace("run") << "ingredient 0 is " << p->ingredients[0].name; + vector<int> arg0 = read_memory(p->ingredients[0]); + assert(arg0.size() == 1); + trace("run") << "ingredient 1 is " << p->ingredients[1].name; + vector<int> arg1 = read_memory(p->ingredients[1]); + assert(arg1.size() == 1); + vector<int> result0; + result0.push_back(arg0[0]/arg1[0]); + trace("run") << "product 0 is " << result0[0]; + write_memory(p->products[0], result0); + vector<int> result1; + result1.push_back(arg0[0]%arg1[0]); + trace("run") << "product 1 is " << result1[0]; + write_memory(p->products[1], result1); + break; +} :(scenario "divide_with_remainder_literal") recipe main [ |