From 0487a30e7078861ed7de42bdb21b5c71fb9b54a1 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 7 May 2015 15:06:53 -0700 Subject: 1298 - better ingredient/product handling All primitives now always write to all their products. If a product is not used that's fine, but if an instruction seems to expect too many products mu will complain. In the process, many primitives can operate on more than two ingredients where it seems intuitive. You can add or divide more than two numbers together, copy or negate multiple corresponding locations, etc. There's one remaining bit of ugliness. Some instructions like get/get-address, index/index-address, wait-for-location, these can unnecessarily load values from memory when they don't need to. Useful vim commands: %s/ingredients\[\([^\]]*\)\]/ingredients.at(\1)/gc %s/products\[\([^\]]*\)\]/products.at(\1)/gc .,$s/\[\(.\)]/.at(\1)/gc --- 024compare.cc | 186 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 126 insertions(+), 60 deletions(-) (limited to '024compare.cc') diff --git a/024compare.cc b/024compare.cc index 91cea1e6..92d87e5c 100644 --- a/024compare.cc +++ b/024compare.cc @@ -6,14 +6,16 @@ EQUAL, Recipe_number["equal"] = EQUAL; :(before "End Primitive Recipe Implementations") case EQUAL: { - trace("run") << "ingredient 0 is " << current_instruction().ingredients[0].name; - vector arg0 = read_memory(current_instruction().ingredients[0]); - trace("run") << "ingredient 1 is " << current_instruction().ingredients[1].name; - vector arg1 = read_memory(current_instruction().ingredients[1]); - vector result; - result.push_back(equal(arg0.begin(), arg0.end(), arg1.begin())); - trace("run") << "product 0 is " << result[0]; - write_memory(current_instruction().products[0], result); + vector& exemplar = ingredients.at(0); + bool result = true; + for (index_t i = 1; i < ingredients.size(); ++i) { + if (!equal(ingredients.at(i).begin(), ingredients.at(i).end(), exemplar.begin())) { + result = false; + break; + } + } + products.resize(1); + products.at(0).push_back(result); break; } @@ -28,7 +30,7 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 33 -+run: product 0 is 0 ++run: product 0 is 3 +mem: storing 0 in location 3 :(scenario equal2) @@ -42,25 +44,38 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 34 -+run: product 0 is 1 ++run: product 0 is 3 +mem: storing 1 in location 3 +:(scenario equal_multiple) +recipe main [ + 1:integer <- equal 34:literal, 34:literal, 34:literal +] ++mem: storing 1 in location 1 + +:(scenario equal_multiple2) +recipe main [ + 1:integer <- equal 34:literal, 34:literal, 35:literal +] ++mem: storing 0 in location 1 + :(before "End Primitive Recipe Declarations") GREATER_THAN, :(before "End Primitive Recipe Numbers") Recipe_number["greater-than"] = GREATER_THAN; :(before "End Primitive Recipe Implementations") case GREATER_THAN: { - trace("run") << "ingredient 0 is " << current_instruction().ingredients[0].name; - vector arg0 = read_memory(current_instruction().ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << current_instruction().ingredients[1].name; - vector arg1 = read_memory(current_instruction().ingredients[1]); - assert(arg1.size() == 1); - vector result; - result.push_back(arg0[0] > arg1[0]); - trace("run") << "product 0 is " << result[0]; - write_memory(current_instruction().products[0], result); + bool result = true; + for (index_t i = 0; i < ingredients.size(); ++i) { + assert(ingredients.at(i).size() == 1); // scalar + } + for (index_t i = /**/1; i < ingredients.size(); ++i) { + if (ingredients.at(i-1).at(0) <= ingredients.at(i).at(0)) { + result = false; + } + } + products.resize(1); + products.at(0).push_back(result); break; } @@ -75,7 +90,7 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 33 -+run: product 0 is 1 ++run: product 0 is 3 +mem: storing 1 in location 3 :(scenario greater_than2) @@ -89,25 +104,38 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 34 -+run: product 0 is 0 ++run: product 0 is 3 +mem: storing 0 in location 3 +:(scenario greater_than_multiple) +recipe main [ + 1:integer <- greater-than 36:literal, 35:literal, 34:literal +] ++mem: storing 1 in location 1 + +:(scenario greater_than_multiple2) +recipe main [ + 1:integer <- greater-than 36:literal, 35:literal, 35:literal +] ++mem: storing 0 in location 1 + :(before "End Primitive Recipe Declarations") LESSER_THAN, :(before "End Primitive Recipe Numbers") Recipe_number["lesser-than"] = LESSER_THAN; :(before "End Primitive Recipe Implementations") case LESSER_THAN: { - trace("run") << "ingredient 0 is " << current_instruction().ingredients[0].name; - vector arg0 = read_memory(current_instruction().ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << current_instruction().ingredients[1].name; - vector arg1 = read_memory(current_instruction().ingredients[1]); - assert(arg1.size() == 1); - vector result; - result.push_back(arg0[0] < arg1[0]); - trace("run") << "product 0 is " << result[0]; - write_memory(current_instruction().products[0], result); + bool result = true; + for (index_t i = 0; i < ingredients.size(); ++i) { + assert(ingredients.at(i).size() == 1); // scalar + } + for (index_t i = /**/1; i < ingredients.size(); ++i) { + if (ingredients.at(i-1).at(0) >= ingredients.at(i).at(0)) { + result = false; + } + } + products.resize(1); + products.at(0).push_back(result); break; } @@ -122,7 +150,7 @@ recipe main [ +mem: location 1 is 32 +run: ingredient 1 is 2 +mem: location 2 is 33 -+run: product 0 is 1 ++run: product 0 is 3 +mem: storing 1 in location 3 :(scenario lesser_than2) @@ -136,25 +164,38 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 33 -+run: product 0 is 0 ++run: product 0 is 3 +mem: storing 0 in location 3 +:(scenario lesser_than_multiple) +recipe main [ + 1:integer <- lesser-than 34:literal, 35:literal, 36:literal +] ++mem: storing 1 in location 1 + +:(scenario lesser_than_multiple2) +recipe main [ + 1:integer <- lesser-than 34:literal, 35:literal, 35:literal +] ++mem: storing 0 in location 1 + :(before "End Primitive Recipe Declarations") GREATER_OR_EQUAL, :(before "End Primitive Recipe Numbers") Recipe_number["greater-or-equal"] = GREATER_OR_EQUAL; :(before "End Primitive Recipe Implementations") case GREATER_OR_EQUAL: { - trace("run") << "ingredient 0 is " << current_instruction().ingredients[0].name; - vector arg0 = read_memory(current_instruction().ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << current_instruction().ingredients[1].name; - vector arg1 = read_memory(current_instruction().ingredients[1]); - assert(arg1.size() == 1); - vector result; - result.push_back(arg0[0] >= arg1[0]); - trace("run") << "product 0 is " << result[0]; - write_memory(current_instruction().products[0], result); + bool result = true; + for (index_t i = 0; i < ingredients.size(); ++i) { + assert(ingredients.at(i).size() == 1); // scalar + } + for (index_t i = /**/1; i < ingredients.size(); ++i) { + if (ingredients.at(i-1).at(0) < ingredients.at(i).at(0)) { + result = false; + } + } + products.resize(1); + products.at(0).push_back(result); break; } @@ -169,7 +210,7 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 33 -+run: product 0 is 1 ++run: product 0 is 3 +mem: storing 1 in location 3 :(scenario greater_or_equal2) @@ -183,7 +224,7 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 34 -+run: product 0 is 1 ++run: product 0 is 3 +mem: storing 1 in location 3 :(scenario greater_or_equal3) @@ -197,25 +238,38 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 35 -+run: product 0 is 0 ++run: product 0 is 3 +mem: storing 0 in location 3 +:(scenario greater_or_equal_multiple) +recipe main [ + 1:integer <- greater-or-equal 36:literal, 35:literal, 35:literal +] ++mem: storing 1 in location 1 + +:(scenario greater_or_equal_multiple2) +recipe main [ + 1:integer <- greater-or-equal 36:literal, 35:literal, 36:literal +] ++mem: storing 0 in location 1 + :(before "End Primitive Recipe Declarations") LESSER_OR_EQUAL, :(before "End Primitive Recipe Numbers") Recipe_number["lesser-or-equal"] = LESSER_OR_EQUAL; :(before "End Primitive Recipe Implementations") case LESSER_OR_EQUAL: { - trace("run") << "ingredient 0 is " << current_instruction().ingredients[0].name; - vector arg0 = read_memory(current_instruction().ingredients[0]); - assert(arg0.size() == 1); - trace("run") << "ingredient 1 is " << current_instruction().ingredients[1].name; - vector arg1 = read_memory(current_instruction().ingredients[1]); - assert(arg1.size() == 1); - vector result; - result.push_back(arg0[0] <= arg1[0]); - trace("run") << "product 0 is " << result[0]; - write_memory(current_instruction().products[0], result); + bool result = true; + for (index_t i = 0; i < ingredients.size(); ++i) { + assert(ingredients.at(i).size() == 1); // scalar + } + for (index_t i = /**/1; i < ingredients.size(); ++i) { + if (ingredients.at(i-1).at(0) > ingredients.at(i).at(0)) { + result = false; + } + } + products.resize(1); + products.at(0).push_back(result); break; } @@ -230,7 +284,7 @@ recipe main [ +mem: location 1 is 32 +run: ingredient 1 is 2 +mem: location 2 is 33 -+run: product 0 is 1 ++run: product 0 is 3 +mem: storing 1 in location 3 :(scenario lesser_or_equal2) @@ -244,7 +298,7 @@ recipe main [ +mem: location 1 is 33 +run: ingredient 1 is 2 +mem: location 2 is 33 -+run: product 0 is 1 ++run: product 0 is 3 +mem: storing 1 in location 3 :(scenario lesser_or_equal3) @@ -258,5 +312,17 @@ recipe main [ +mem: location 1 is 34 +run: ingredient 1 is 2 +mem: location 2 is 33 -+run: product 0 is 0 ++run: product 0 is 3 +mem: storing 0 in location 3 + +:(scenario lesser_or_equal_multiple) +recipe main [ + 1:integer <- lesser-or-equal 34:literal, 35:literal, 35:literal +] ++mem: storing 1 in location 1 + +:(scenario lesser_or_equal_multiple2) +recipe main [ + 1:integer <- lesser-or-equal 34:literal, 35:literal, 34:literal +] ++mem: storing 0 in location 1 -- cgit 1.4.1-2-gfad0