diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-07-29 18:40:36 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-07-29 18:40:36 -0700 |
commit | 89b87bc7c493670ecb598784e1073a09f691d43e (patch) | |
tree | b645853bb524a9a57a897a729269786aad8c4ce6 /021arithmetic.cc | |
parent | 9570363aec35e187e2395b1760a4b94e71580ac9 (diff) | |
download | mu-89b87bc7c493670ecb598784e1073a09f691d43e.tar.gz |
1886 - gracefully handle malformed ingredients
For example: x:number <- index y:address:array:number, 3 (forgetting to do a lookup) Thanks Caleb Couch.
Diffstat (limited to '021arithmetic.cc')
-rw-r--r-- | 021arithmetic.cc | 6 |
1 files changed, 3 insertions, 3 deletions
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<long long int>(ingredients.at(0).at(0)) % static_cast<long long int>(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); |