about summary refs log tree commit diff stats
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
parent067c8ed66c9bd4e9b4c936b54df293c665afa9bc (diff)
downloadmu-db69ad7a38d9ff1c2bb5b1a2274514d2bdc44d1b.tar.gz
3210 - new primitive: character-to-code
Thanks Ella Couch; this was long overdue.
-rw-r--r--021check_instruction.cc7
-rw-r--r--022arithmetic.cc35
-rw-r--r--035lookup.cc2
3 files changed, 44 insertions, 0 deletions
diff --git a/021check_instruction.cc b/021check_instruction.cc
index f1390eb3..3554d253 100644
--- a/021check_instruction.cc
+++ b/021check_instruction.cc
@@ -203,6 +203,13 @@ bool is_mu_number(reagent/*copy*/ r) {
   return r.type->value == get(Type_ordinal, "number");
 }
 
+bool is_mu_character(reagent/*copy*/ r) {
+  // End Preprocess is_mu_character(reagent r)
+  if (!r.type) return false;
+  if (is_literal(r)) return false;
+  return r.type->value == get(Type_ordinal, "character");
+}
+
 bool is_mu_scalar(reagent/*copy*/ r) {
   if (!r.type) return false;
   if (is_literal(r))
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>
diff --git a/035lookup.cc b/035lookup.cc
index 6e43f66a..0c9e1543 100644
--- a/035lookup.cc
+++ b/035lookup.cc
@@ -136,6 +136,8 @@ if (!canonize_type(r)) return false;
 if (!canonize_type(r)) return false;
 :(before "End Preprocess is_mu_boolean(reagent r)")
 if (!canonize_type(r)) return false;
+:(before "End Preprocess is_mu_character(reagent r)")
+if (!canonize_type(r)) return false;
 
 :(after "Update product While Type-checking Merge")
 if (!canonize_type(product)) continue;