about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-02-19 17:12:55 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-02-19 17:12:55 -0800
commitc7c822b27aac56de1e44555c9c2366ac6bbf13f2 (patch)
treecf921bd1677aa8121f6185678bc78a7e7a74e4e1
parentd7e112371c97b8d9e7b4ea4451071f8914b9b57f (diff)
downloadmu-c7c822b27aac56de1e44555c9c2366ac6bbf13f2.tar.gz
793
-rw-r--r--cpp/012run16
-rw-r--r--cpp/013arithmetic52
-rw-r--r--cpp/014boolean26
3 files changed, 46 insertions, 48 deletions
diff --git a/cpp/012run b/cpp/012run
index 5ba43ee5..acc423a4 100644
--- a/cpp/012run
+++ b/cpp/012run
@@ -24,21 +24,19 @@ void run(string form) {
 
 void run(recipe_number r) {
   vector<instruction>& instructions(Recipe[r].steps);
-  int n = 0;
-  vector<instruction>::iterator p;
-  for (n = 0, p = instructions.begin(); p != instructions.end(); ++p, ++n) {
-    trace("run") << "instruction " << n;
-    switch (p->operation) {
+  for (size_t pc = 0; pc < instructions.size(); ++pc) {
+    trace("run") << "instruction " << pc;
+    switch (instructions[pc].operation) {
     // Primitive Recipe Implementations.
     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);
+      trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+      vector<int> data = read_memory(instructions[pc].ingredients[0]);
+      write_memory(instructions[pc].products[0], data);
       break;
     }
     // End Primitive Recipe Implementations.
     default:
-      raise << "undefined operation " << p->operation;
+      raise << "undefined operation " << instructions[pc].operation;
     }
   }
 }
diff --git a/cpp/013arithmetic b/cpp/013arithmetic
index 47d14b2c..1f12b474 100644
--- a/cpp/013arithmetic
+++ b/cpp/013arithmetic
@@ -6,16 +6,16 @@ Recipe_number["add"] = ADD;
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case ADD: {
-  trace("run") << "ingredient 0 is " << p->ingredients[0].name;
-  vector<int> arg0 = read_memory(p->ingredients[0]);
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
   assert(arg0.size() == 1);
-  trace("run") << "ingredient 1 is " << p->ingredients[1].name;
-  vector<int> arg1 = read_memory(p->ingredients[1]);
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  vector<int> arg1 = read_memory(instructions[pc].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);
+  write_memory(instructions[pc].products[0], result);
   break;
 }
 
@@ -50,16 +50,16 @@ Recipe_number["subtract"] = SUBTRACT;
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case SUBTRACT: {
-  trace("run") << "ingredient 0 is " << p->ingredients[0].name;
-  vector<int> arg0 = read_memory(p->ingredients[0]);
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
   assert(arg0.size() == 1);
-  trace("run") << "ingredient 1 is " << p->ingredients[1].name;
-  vector<int> arg1 = read_memory(p->ingredients[1]);
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  vector<int> arg1 = read_memory(instructions[pc].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);
+  write_memory(instructions[pc].products[0], result);
   break;
 }
 
@@ -94,17 +94,17 @@ Recipe_number["multiply"] = MULTIPLY;
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case MULTIPLY: {
-  trace("run") << "ingredient 0 is " << p->ingredients[0].name;
-  vector<int> arg0 = read_memory(p->ingredients[0]);
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
   assert(arg0.size() == 1);
-  trace("run") << "ingredient 1 is " << p->ingredients[1].name;
-  vector<int> arg1 = read_memory(p->ingredients[1]);
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  vector<int> arg1 = read_memory(instructions[pc].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);
+  write_memory(instructions[pc].products[0], result);
   break;
 }
 
@@ -139,17 +139,17 @@ Recipe_number["divide"] = DIVIDE;
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case DIVIDE: {
-  trace("run") << "ingredient 0 is " << p->ingredients[0].name;
-  vector<int> arg0 = read_memory(p->ingredients[0]);
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
   assert(arg0.size() == 1);
-  trace("run") << "ingredient 1 is " << p->ingredients[1].name;
-  vector<int> arg1 = read_memory(p->ingredients[1]);
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  vector<int> arg1 = read_memory(instructions[pc].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);
+  write_memory(instructions[pc].products[0], result);
   break;
 }
 
@@ -184,20 +184,20 @@ Recipe_number["divide_with_remainder"] = DIVIDE_WITH_REMAINDER;
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case DIVIDE_WITH_REMAINDER: {
-  trace("run") << "ingredient 0 is " << p->ingredients[0].name;
-  vector<int> arg0 = read_memory(p->ingredients[0]);
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
   assert(arg0.size() == 1);
-  trace("run") << "ingredient 1 is " << p->ingredients[1].name;
-  vector<int> arg1 = read_memory(p->ingredients[1]);
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  vector<int> arg1 = read_memory(instructions[pc].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);
+  write_memory(instructions[pc].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);
+  write_memory(instructions[pc].products[1], result1);
   break;
 }
 
diff --git a/cpp/014boolean b/cpp/014boolean
index 87ea6e2f..7da98db4 100644
--- a/cpp/014boolean
+++ b/cpp/014boolean
@@ -6,16 +6,16 @@ Recipe_number["and"] = AND;
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case AND: {
-  trace("run") << "ingredient 0 is " << p->ingredients[0].name;
-  vector<int> arg0 = read_memory(p->ingredients[0]);
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
   assert(arg0.size() == 1);
-  trace("run") << "ingredient 1 is " << p->ingredients[1].name;
-  vector<int> arg1 = read_memory(p->ingredients[1]);
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  vector<int> arg1 = read_memory(instructions[pc].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);
+  write_memory(instructions[pc].products[0], result);
   break;
 }
 
@@ -40,16 +40,16 @@ Recipe_number["or"] = OR;
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case OR: {
-  trace("run") << "ingredient 0 is " << p->ingredients[0].name;
-  vector<int> arg0 = read_memory(p->ingredients[0]);
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
   assert(arg0.size() == 1);
-  trace("run") << "ingredient 1 is " << p->ingredients[1].name;
-  vector<int> arg1 = read_memory(p->ingredients[1]);
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  vector<int> arg1 = read_memory(instructions[pc].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);
+  write_memory(instructions[pc].products[0], result);
   break;
 }
 
@@ -74,13 +74,13 @@ Recipe_number["not"] = NOT;
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case NOT: {
-  trace("run") << "ingredient 0 is " << p->ingredients[0].name;
-  vector<int> arg0 = read_memory(p->ingredients[0]);
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  vector<int> arg0 = read_memory(instructions[pc].ingredients[0]);
   assert(arg0.size() == 1);
   vector<int> result;
   result.push_back(!arg0[0]);
   trace("run") << "product 0 is " << result[0];
-  write_memory(p->products[0], result);
+  write_memory(instructions[pc].products[0], result);
   break;
 }