Basic if-statement checking

This commit is contained in:
greg 2019-02-19 23:00:41 -08:00
parent 6012bd1087
commit f0ed63ccf3
1 changed files with 25 additions and 2 deletions

View File

@ -260,9 +260,32 @@ impl<'a> TypeContext<'a> {
}
fn if_expr(&mut self, discriminator: &Discriminator, body: &IfExpressionBody) -> InferResult<Type> {
//only handle simple case for now
use self::Discriminator::*; use self::IfExpressionBody::*;
match (discriminator, body) {
(Simple(expr), SimpleConditional(then_clause, else_clause)) => self.handle_simple_if(expr, then_clause, else_clause),
_ => TypeError::new(&format!("Complex conditionals not supported"))
}
}
Ok(ty!(Unit))
fn handle_simple_if(&mut self, expr: &Expression, then_clause: &Block, else_clause: &Option<Block>) -> InferResult<Type> {
let t1 = self.expr(expr)?;
let t2 = self.block(then_clause)?;
let t3 = match else_clause {
Some(block) => self.block(block)?,
None => ty!(Unit)
};
let _ = self.unify(ty!(Bool), t1)?;
self.unify(t2, t3)
}
fn block(&mut self, block: &Block) -> InferResult<Type> {
let mut output = ty!(Unit);
for s in block.iter() {
let statement = s.node();
output = self.statement(statement)?;
}
Ok(output)
}
fn handle_value(&mut self, val: &Rc<String>) -> InferResult<Type> {