aboutsummaryrefslogtreecommitdiff
path: root/typecheck.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-06 13:36:36 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-06 13:36:36 -0500
commit425466ce6ee05b0604784868d55f959d12fd275f (patch)
treebd2b94422c14d8b963b01862b57be3187722397c /typecheck.c
parent93b8e0ae170c14159e64538a670e149d63e07832 (diff)
Add 'if x := var' conditionals for optional pointers
Diffstat (limited to 'typecheck.c')
-rw-r--r--typecheck.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/typecheck.c b/typecheck.c
index 6528b4b3..ac6ed811 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -603,7 +603,22 @@ type_t *get_type(env_t *env, ast_t *ast)
case If: {
auto if_ = Match(ast, If);
- type_t *true_t = get_type(env, if_->body);
+ type_t *true_t;
+ if (if_->condition->tag == Declare) {
+ auto decl = Match(if_->condition, Declare);
+ env_t *scope = fresh_scope(env);
+ type_t *var_t = get_type(env, decl->value);
+ if (var_t->tag == PointerType) {
+ auto ptr = Match(var_t, PointerType);
+ var_t = Type(PointerType, .pointed=ptr->pointed, .is_optional=false, .is_stack=ptr->is_stack, .is_readonly=ptr->is_readonly);
+ }
+ CORD var = Match(decl->var, Var)->name;
+ set_binding(scope, CORD_to_const_char_star(var), new(binding_t, .type=var_t));
+ true_t = get_type(scope, if_->body);
+ } else {
+ true_t = get_type(env, if_->body);
+ }
+
if (if_->else_body) {
type_t *false_t = get_type(env, if_->else_body);
type_t *t_either = type_or_type(true_t, false_t);