about summary refs log tree commit diff stats
path: root/022arithmetic.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-08-17 08:24:19 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-08-17 08:24:19 -0700
commitdb69ad7a38d9ff1c2bb5b1a2274514d2bdc44d1b (patch)
tree22139431bddda0b8b1664ff6509fef87c01197b7 /022arithmetic.cc
parent067c8ed66c9bd4e9b4c936b54df293c665afa9bc (diff)
downloadmu-db69ad7a38d9ff1c2bb5b1a2274514d2bdc44d1b.tar.gz
3210 - new primitive: character-to-code
Thanks Ella Couch; this was long overdue.
Diffstat (limited to '022arithmetic.cc')
-rw-r--r--022arithmetic.cc35
1 files changed, 35 insertions, 0 deletions
diff --git a/022arithmetic.cc b/022arithmetic.cc
index 2d7e1436..5d1583c4 100644
--- a/022arithmetic.cc
+++ b/022arithmetic.cc
@@ -714,5 +714,40 @@ def main [
 ]
 +mem: storing 12 in location 1
 
+:(before "End Primitive Recipe Declarations")
+CHARACTER_TO_CODE,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "character-to-code", CHARACTER_TO_CODE);
+:(before "End Primitive Recipe Checks")
+case CHARACTER_TO_CODE: {
+  if (SIZE(inst.ingredients) != 1) {
+    raise << maybe(get(Recipe, r).name) << "'character-to-code' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end();
+    break;
+  }
+  if (!is_mu_character(inst.ingredients.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "first ingredient of 'character-to-code' should be a character, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
+    break;
+  }
+  if (SIZE(inst.products) != 1) {
+    raise << maybe(get(Recipe, r).name) << "'character-to-code' requires exactly one product, but got '" << inst.original_string << "'\n" << end();
+    break;
+  }
+  if (!is_mu_number(inst.products.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "first product of 'character-to-code' should be a number, but got '" << inst.products.at(0).original_string << "'\n" << end();
+    break;
+  }
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case CHARACTER_TO_CODE: {
+  double result = 0;
+  for (int i = 0; i < SIZE(ingredients); ++i) {
+    result += ingredients.at(i).at(0);
+  }
+  products.resize(1);
+  products.at(0).push_back(result);
+  break;
+}
+
 :(before "End Includes")
 #include <math.h>