From 0202aab181c728d13f04ecddb19786d1c070640a Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 12 Oct 2017 20:14:33 -0700 Subject: [PATCH] Some partial work on refactoring type infer fn --- src/schala_lang/type_check.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/schala_lang/type_check.rs b/src/schala_lang/type_check.rs index a35c28f..f17b372 100644 --- a/src/schala_lang/type_check.rs +++ b/src/schala_lang/type_check.rs @@ -316,20 +316,26 @@ impl TypeContext { use self::Type::*; use self::TypeConst::*; - Ok(match (&expr.0, &expr.1) { - (ex, &Some(ref anno)) => { - let tx = self.infer(&Expression(ex.clone(), None))?; //TODO rewrite this to call into a function that takes just an ExprType, to avoid this cloning + match (&expr.0, &expr.1) { + (exprtype, &Some(ref anno)) => { + let tx = self.infer_no_anno(exprtype)?; let ty = self.from_anno(anno); - self.unify(tx, ty)? + self.unify(tx, ty) }, - (&IntLiteral(_), _) => TConst(Integer), - (&BoolLiteral(_), _) => TConst(Boolean), - (&Value(ref name), _) => { + (exprtype, &None) => self.infer_no_anno(exprtype), + } + } + + fn infer_no_anno(&mut self, ex: &ExpressionType) -> TypeCheckResult { + Ok(match ex { + &IntLiteral(_) => TConst(Integer), + &BoolLiteral(_) => TConst(Boolean), + &Value(ref name) => { self.lookup(name) .map(|entry| entry.ty) .ok_or(format!("Couldn't find {}", name))? }, - (&Call { ref f, ref arguments }, _) => { + &Call { ref f, ref arguments } => { let tf = self.infer(f)?; let targ = self.infer(arguments.get(0).unwrap())?; match tf {