aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-11-24 16:49:26 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-11-24 16:49:26 -0500
commita38161179b7cfc925b4d16bd042f2b1754dca7fd (patch)
treed51564fdfb514d2f1cea6e89d8f57cec85a92338
parent52de4258e9e973f453909176b20bebead2f86dd6 (diff)
Cleaner None-handling
-rw-r--r--compile.c16
-rw-r--r--typecheck.c2
2 files changed, 16 insertions, 2 deletions
diff --git a/compile.c b/compile.c
index 817b322e..6221ee57 100644
--- a/compile.c
+++ b/compile.c
@@ -2064,10 +2064,22 @@ CORD compile(env_t *env, ast_t *ast)
}
}
- bool lhs_is_optional_num = (lhs_t->tag == OptionalType && Match(lhs_t, OptionalType)->type->tag == NumType);
+ type_t *non_optional_lhs = lhs_t;
+ if (lhs_t->tag == OptionalType) non_optional_lhs = Match(lhs_t, OptionalType)->type;
+ type_t *non_optional_rhs = rhs_t;
+ if (rhs_t->tag == OptionalType) non_optional_rhs = Match(rhs_t, OptionalType)->type;
+
+ if (!non_optional_lhs && !non_optional_rhs)
+ code_err(ast, "Both of these values do not specify a type");
+ else if (!non_optional_lhs)
+ non_optional_lhs = non_optional_rhs;
+ else if (!non_optional_rhs)
+ non_optional_rhs = non_optional_lhs;
+
+ bool lhs_is_optional_num = (lhs_t->tag == OptionalType && non_optional_lhs->tag == NumType);
if (lhs_is_optional_num)
lhs_t = Match(lhs_t, OptionalType)->type;
- bool rhs_is_optional_num = (rhs_t->tag == OptionalType && Match(rhs_t, OptionalType)->type->tag == NumType);
+ bool rhs_is_optional_num = (rhs_t->tag == OptionalType && non_optional_rhs->tag == NumType);
if (rhs_is_optional_num)
rhs_t = Match(rhs_t, OptionalType)->type;
diff --git a/typecheck.c b/typecheck.c
index 8bb5dc48..a37357d2 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -998,6 +998,8 @@ type_t *get_type(env_t *env, ast_t *ast)
if (type_eq(rhs_ptr->pointed, lhs_ptr->pointed))
return Type(PointerType, .pointed=lhs_ptr->pointed);
}
+ } else if (rhs_t->tag == OptionalType) {
+ return type_or_type(lhs_t, rhs_t);
}
code_err(ast, "I can't figure out the type of this `or` expression between a %T and a %T", lhs_t, rhs_t);
}