about summary refs log tree commit diff stats
path: root/021check_instruction.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-10-25 21:42:18 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-10-25 21:42:18 -0700
commitc6034af30882b6a0a38bcf1fe546ed3dfd3bed04 (patch)
treed63634d44163ad21ddbbabf9a9386adf697a7aa2 /021check_instruction.cc
parentf5dfb7f7374c7e78575d0c205db391814be1b434 (diff)
downloadmu-c6034af30882b6a0a38bcf1fe546ed3dfd3bed04.tar.gz
2277 - reagents now have a tree of types
Diffstat (limited to '021check_instruction.cc')
-rw-r--r--021check_instruction.cc28
1 files changed, 18 insertions, 10 deletions
diff --git a/021check_instruction.cc b/021check_instruction.cc
index fcd861da..e51424f2 100644
--- a/021check_instruction.cc
+++ b/021check_instruction.cc
@@ -80,11 +80,16 @@ bool types_match(reagent lhs, reagent rhs) {
   // allow writing 0 to any address
   if (rhs.name == "0" && is_mu_address(lhs)) return true;
   if (is_literal(rhs)) return !is_mu_array(lhs) && !is_mu_address(lhs) && size_of(rhs) == size_of(lhs);
-  // more refined types can always be copied to less refined ones
-  if (SIZE(lhs.types) > SIZE(rhs.types)) return false;
-  if (SIZE(lhs.types) == SIZE(rhs.types)) return lhs.types == rhs.types;
-  rhs.types.resize(SIZE(lhs.types));
-  return lhs.types == rhs.types;
+  if (!lhs.type) return !rhs.type;
+  return types_match(lhs.type, rhs.type);
+}
+
+// two types match if the second begins like the first
+// (trees perform the same check recursively on each subtree)
+bool types_match(type_tree* lhs, type_tree* rhs) {
+  if (!lhs) return true;
+  if (lhs->value != rhs->value) return false;
+  return types_match(lhs->left, rhs->left) && types_match(lhs->right, rhs->right);
 }
 
 bool is_raw(const reagent& r) {
@@ -92,25 +97,28 @@ bool is_raw(const reagent& r) {
 }
 
 bool is_mu_array(reagent r) {
+  if (!r.type) return false;
   if (is_literal(r)) return false;
-  return !r.types.empty() && r.types.at(0) == Type_ordinal["array"];
+  return r.type->value == Type_ordinal["array"];
 }
 
 bool is_mu_address(reagent r) {
+  if (!r.type) return false;
   if (is_literal(r)) return false;
-  return !r.types.empty() && r.types.at(0) == Type_ordinal["address"];
+  return r.type->value == Type_ordinal["address"];
 }
 
 bool is_mu_number(reagent r) {
+  if (!r.type) return false;
   if (is_literal(r))
     return r.properties.at(0).second.at(0) == "literal-number"
         || r.properties.at(0).second.at(0) == "literal";
-  if (r.types.empty()) return false;
-  if (r.types.at(0) == Type_ordinal["character"]) return true;  // permit arithmetic on unicode code points
-  return r.types.at(0) == Type_ordinal["number"];
+  if (r.type->value == Type_ordinal["character"]) return true;  // permit arithmetic on unicode code points
+  return r.type->value == Type_ordinal["number"];
 }
 
 bool is_mu_scalar(reagent r) {
+  if (!r.type) return false;
   if (is_literal(r))
     return r.properties.at(0).second.empty() || r.properties.at(0).second.at(0) != "literal-string";
   if (is_mu_array(r)) return false;