From 1089a33634f75731bee994211f417ad2b8b11e13 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 10 Oct 2017 04:26:40 -0700 Subject: [PATCH] Convert unify to are types b/c Type implements Clone Maybe wanna kill this later for efficiency --- src/schala_lang/type_check.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/schala_lang/type_check.rs b/src/schala_lang/type_check.rs index 14791f2..6de0948 100644 --- a/src/schala_lang/type_check.rs +++ b/src/schala_lang/type_check.rs @@ -28,9 +28,14 @@ pub enum Type { #[derive(Debug, PartialEq, Clone)] pub enum TypeVar { - Univ(String), + Univ(Rc), Exist(u64), } +impl TypeVar { + fn univ(label: &str) -> TypeVar { + TypeVar::Univ(Rc::new(label.to_string())) + } +} #[derive(Debug, PartialEq, Clone)] pub enum TypeConst { @@ -186,11 +191,11 @@ impl TypeContext { match op_type { TConst(FunctionT(box t1, box t2)) => { - let _ = self.unify(&t1, &lhs_type)?; + let _ = self.unify(t1, lhs_type)?; let rhs_type = self.infer(&rhs)?; match t2 { TConst(FunctionT(box t3, box t_ret)) => { - let _ = self.unify(&t3, &rhs_type)?; + let _ = self.unify(t3, rhs_type)?; t_ret }, _ => return Err(format!("Another bad type for operator")) @@ -204,7 +209,7 @@ impl TypeContext { let arg_type = self.infer(arguments.get(0).unwrap())?; // TODO fix later match f_type { TConst(FunctionT(box t1, box ret_type)) => { - let _ = self.unify(&t1, &arg_type)?; + let _ = self.unify(t1, arg_type)?; ret_type }, _ => return Err(format!("Type error")) @@ -222,10 +227,10 @@ impl TypeContext { if opstr == "+" { return Ok( TConst(FunctionT( - Box::new(TConst(StringT)), + Box::new(TVar(TypeVar::univ("a"))), Box::new(TConst(FunctionT( - Box::new(TConst(StringT)), - Box::new(TConst(StringT)) + Box::new(TVar(TypeVar::univ("a"))), + Box::new(TVar(TypeVar::univ("a"))) ))) )) ) @@ -242,7 +247,7 @@ impl TypeContext { ) } - fn unify(&mut self, t1: &Type, t2: &Type) -> TypeCheckResult { + fn unify(&mut self, t1: Type, t2: Type) -> TypeCheckResult { if t1 == t2 { Ok(t1.clone()) } else {