Some partial work on refactoring type infer fn

This commit is contained in:
greg 2017-10-12 20:14:33 -07:00
parent 936c168cef
commit 9b4a23c4f2
1 changed files with 14 additions and 8 deletions

View File

@ -316,20 +316,26 @@ impl TypeContext {
use self::Type::*; use self::Type::*;
use self::TypeConst::*; use self::TypeConst::*;
Ok(match (&expr.0, &expr.1) { match (&expr.0, &expr.1) {
(ex, &Some(ref anno)) => { (exprtype, &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 let tx = self.infer_no_anno(exprtype)?;
let ty = self.from_anno(anno); let ty = self.from_anno(anno);
self.unify(tx, ty)? self.unify(tx, ty)
}, },
(&IntLiteral(_), _) => TConst(Integer), (exprtype, &None) => self.infer_no_anno(exprtype),
(&BoolLiteral(_), _) => TConst(Boolean), }
(&Value(ref name), _) => { }
fn infer_no_anno(&mut self, ex: &ExpressionType) -> TypeCheckResult {
Ok(match ex {
&IntLiteral(_) => TConst(Integer),
&BoolLiteral(_) => TConst(Boolean),
&Value(ref name) => {
self.lookup(name) self.lookup(name)
.map(|entry| entry.ty) .map(|entry| entry.ty)
.ok_or(format!("Couldn't find {}", name))? .ok_or(format!("Couldn't find {}", name))?
}, },
(&Call { ref f, ref arguments }, _) => { &Call { ref f, ref arguments } => {
let tf = self.infer(f)?; let tf = self.infer(f)?;
let targ = self.infer(arguments.get(0).unwrap())?; let targ = self.infer(arguments.get(0).unwrap())?;
match tf { match tf {