Skip to content

Commit 45eefc8

Browse files
committed
Add checker for conditionals
1 parent 5c0a68a commit 45eefc8

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/inference.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,30 @@ fn check_expr(expr: &QccCell<Expr>) -> Result<Type> {
124124
check_expr(expr);
125125
}
126126

127-
// TODO:
128-
Ok(Type::Bottom)
127+
let last_truth = truth_block.last();
128+
let last_false = false_block.last();
129+
130+
if last_false.is_none() && last_truth.is_none() {
131+
return Ok(Type::Bottom);
132+
} else if last_false.is_none() ^ last_truth.is_none() {
133+
let last_expr;
134+
if last_false.is_none() {
135+
last_expr = last_truth;
136+
} else {
137+
last_expr = last_false;
138+
}
139+
140+
return Ok(last_expr.unwrap().as_ref().borrow().get_type());
141+
} else {
142+
let truth_block_type = last_truth.unwrap().as_ref().borrow().get_type();
143+
let false_block_type = last_false.unwrap().as_ref().borrow().get_type();
144+
145+
if truth_block_type != false_block_type {
146+
return Err(QccErrorKind::TypeMismatch)?;
147+
}
148+
149+
return Ok(truth_block_type);
150+
}
129151
}
130152
Expr::Literal(ref lit) => match *lit.as_ref().borrow() {
131153
LiteralAST::Lit_Digit(ref digit) => Ok(Type::F64),
@@ -631,7 +653,7 @@ fn infer_from_table(
631653
return info;
632654
}
633655
}
634-
// TODO: How to infer type for entire if block?
656+
635657
None
636658
}
637659
Expr::Literal(ref mut l) => {

0 commit comments

Comments
 (0)