From 89b87bc7c493670ecb598784e1073a09f691d43e Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 29 Jul 2015 18:40:36 -0700 Subject: 1886 - gracefully handle malformed ingredients For example: x:number <- index y:address:array:number, 3 (forgetting to do a lookup) Thanks Caleb Couch. --- 021arithmetic.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to '021arithmetic.cc') diff --git a/021arithmetic.cc b/021arithmetic.cc index 1485e194..2a21c2cf 100644 --- a/021arithmetic.cc +++ b/021arithmetic.cc @@ -43,6 +43,7 @@ SUBTRACT, Recipe_ordinal["subtract"] = SUBTRACT; :(before "End Primitive Recipe Implementations") case SUBTRACT: { + products.resize(1); if (ingredients.empty()) { raise << current_recipe_name() << ": 'subtract' has no ingredients\n" << end(); break; @@ -53,7 +54,6 @@ case SUBTRACT: { assert(scalar(ingredients.at(i))); result -= ingredients.at(i).at(0); } - products.resize(1); products.at(0).push_back(result); break; } @@ -120,6 +120,7 @@ DIVIDE, Recipe_ordinal["divide"] = DIVIDE; :(before "End Primitive Recipe Implementations") case DIVIDE: { + products.resize(1); if (ingredients.empty()) { raise << current_recipe_name() << ": 'divide' has no ingredients\n" << end(); break; @@ -130,7 +131,6 @@ case DIVIDE: { assert(scalar(ingredients.at(i))); result /= ingredients.at(i).at(0); } - products.resize(1); products.at(0).push_back(result); break; } @@ -163,13 +163,13 @@ DIVIDE_WITH_REMAINDER, Recipe_ordinal["divide-with-remainder"] = DIVIDE_WITH_REMAINDER; :(before "End Primitive Recipe Implementations") case DIVIDE_WITH_REMAINDER: { + products.resize(2); if (SIZE(ingredients) != 2) { raise << current_recipe_name() << ": 'divide-with-remainder' requires exactly two ingredients, but got " << current_instruction().to_string() << '\n' << end(); break; } long long int quotient = ingredients.at(0).at(0) / ingredients.at(1).at(0); long long int remainder = static_cast(ingredients.at(0).at(0)) % static_cast(ingredients.at(1).at(0)); - products.resize(2); // very large integers will lose precision products.at(0).push_back(quotient); products.at(1).push_back(remainder); -- cgit 1.4.1-2-gfad0