More type work2

This commit is contained in:
greg 2018-11-08 02:26:02 -08:00
parent e9b90412ce
commit 9056e9b0e1
1 changed files with 31 additions and 4 deletions

View File

@ -11,13 +11,25 @@ pub struct TypeContext {
type InferResult<T> = Result<T, TypeError>;
#[derive(Debug, Clone)]
struct TypeError { }
struct TypeError { msg: String }
impl TypeError {
fn new<A>(msg: &str) -> InferResult<A> {
Err(TypeError { msg: msg.to_string() })
}
}
#[derive(Debug, Clone)]
enum MonoType {
Var(Rc<String>),
Const(TConst),
Arrow(Rc<String>)
Arrow(Box<MonoType>, Box<MonoType>)
}
impl TypeIdentifier {
fn to_monotype(&self) -> MonoType {
unimplemented!()
}
}
#[derive(Debug, Clone)]
@ -70,7 +82,11 @@ impl TypeContext {
fn infer_expr(&mut self, expr: &Expression) -> InferResult<MonoType> {
match expr {
Expression(expr_type, Some(type_anno)) => unimplemented!(),
Expression(expr_type, Some(type_anno)) => {
let tx = self.infer_expr_type(expr_type)?;
let ty = type_anno.to_monotype();
self.unify(&ty, &tx)
},
Expression(expr_type, None) => self.infer_expr_type(expr_type)
}
}
@ -86,11 +102,22 @@ impl TypeContext {
FloatLiteral(_) => MonoType::Const(TConst::Float),
StringLiteral(_) => MonoType::Const(TConst::StringT),
BoolLiteral(_) => MonoType::Const(TConst::Bool),
Call { f, arguments } => {
let tf: MonoType = self.infer_expr(f)?; //has to be an Arrow MonoType
let targ = self.infer_expr(&arguments[0])?; // TODO make this work with functions with more than one arg
match tf {
MonoType::Arrow(t1, t2) => {
self.unify(&t1, &targ)?;
*t2.clone()
},
_ => return TypeError::new("not a function")
}
},
_ => MonoType::Const(TConst::user("unimplemented"))
})
}
fn unify(t1: MonoType, t2: MonoType) -> InferResult<MonoType> {
fn unify(&mut self, t1: &MonoType, t2: &MonoType) -> InferResult<MonoType> {
unimplemented!()
}
}