From 21132a369c37eace245bbbde48554b5798c4102d Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 9 Nov 2018 02:05:59 -0800 Subject: [PATCH] Paramaterize Type --- schala-lang/language/src/typechecking.rs | 64 ++++++++++++------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index 5c5e314..4abbc00 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -6,7 +6,7 @@ use util::ScopeStack; pub type TypeName = Rc; pub struct TypeContext<'a> { - variable_map: ScopeStack<'a, Rc, MonoType> + variable_map: ScopeStack<'a, Rc, Type<()>> } type InferResult = Result; @@ -21,24 +21,24 @@ impl TypeError { } #[derive(Debug, Clone)] -enum MonoType { - Var(Rc), +enum Type { + Var(a), Const(TConst), - Arrow(Box, Box), + Arrow(Box>, Box>), ExistentialVar(u32) } impl TypeIdentifier { - fn to_monotype(&self) -> MonoType { + fn to_monotype(&self) -> Type<()> { match self { TypeIdentifier::Tuple(items) => unimplemented!(), TypeIdentifier::Singleton(TypeSingletonName { name, .. }) => { match &name[..] { - "Nat" => MonoType::Const(TConst::Nat), - "Int" => MonoType::Const(TConst::Int), - "Float" => MonoType::Const(TConst::Float), - "Bool" => MonoType::Const(TConst::Bool), - "String" => MonoType::Const(TConst::StringT), + "Nat" => Type::Const(TConst::Nat), + "Int" => Type::Const(TConst::Int), + "Float" => Type::Const(TConst::Float), + "Bool" => Type::Const(TConst::Bool), + "String" => Type::Const(TConst::StringT), _ => unimplemented!() } } @@ -66,7 +66,7 @@ impl TConst { #[derive(Debug, Clone)] struct PolyType { vars: Vec>, - ty: MonoType + ty: Type<()> } impl<'a> TypeContext<'a> { @@ -85,22 +85,22 @@ impl<'a> TypeContext<'a> { } impl<'a> TypeContext<'a> { - fn infer_ast(&mut self, ast: &AST) -> InferResult { - let mut output = MonoType::Const(TConst::Unit); + fn infer_ast(&mut self, ast: &AST) -> InferResult> { + let mut output = Type::Const(TConst::Unit); for statement in ast.0.iter() { output = self.infer_statement(statement)?; } Ok(output) } - fn infer_statement(&mut self, stmt: &Statement) -> InferResult { + fn infer_statement(&mut self, stmt: &Statement) -> InferResult> { match stmt { Statement::ExpressionStatement(ref expr) => self.infer_expr(expr), Statement::Declaration(ref decl) => self.infer_decl(decl), } } - fn infer_expr(&mut self, expr: &Expression) -> InferResult { + fn infer_expr(&mut self, expr: &Expression) -> InferResult> { match expr { Expression(expr_type, Some(type_anno)) => { let tx = self.infer_expr_type(expr_type)?; @@ -111,17 +111,17 @@ impl<'a> TypeContext<'a> { } } - fn infer_decl(&mut self, expr: &Declaration) -> InferResult { - Ok(MonoType::Const(TConst::user("unimplemented"))) + fn infer_decl(&mut self, expr: &Declaration) -> InferResult> { + Ok(Type::Const(TConst::user("unimplemented"))) } - fn infer_expr_type(&mut self, expr_type: &ExpressionType) -> InferResult { + fn infer_expr_type(&mut self, expr_type: &ExpressionType) -> InferResult> { use self::ExpressionType::*; Ok(match expr_type { - NatLiteral(_) => MonoType::Const(TConst::Nat), - FloatLiteral(_) => MonoType::Const(TConst::Float), - StringLiteral(_) => MonoType::Const(TConst::StringT), - BoolLiteral(_) => MonoType::Const(TConst::Bool), + NatLiteral(_) => Type::Const(TConst::Nat), + FloatLiteral(_) => Type::Const(TConst::Float), + StringLiteral(_) => Type::Const(TConst::StringT), + BoolLiteral(_) => Type::Const(TConst::Bool), Value(name) => { //TODO handle the distinction between 0-arg constructors and variables at some point // need symbol table for that @@ -132,10 +132,10 @@ impl<'a> TypeContext<'a> { }, IfExpression { discriminator, body } => self.infer_if_expr(discriminator, body)?, Call { f, arguments } => { - let tf: MonoType = self.infer_expr(f)?; //has to be an Arrow MonoType + let tf: Type<()> = self.infer_expr(f)?; //has to be an Arrow Type let targ = self.infer_expr(&arguments[0])?; // TODO make this work with functions with more than one arg match tf { - MonoType::Arrow(t1, t2) => { + Type::Arrow(t1, t2) => { self.unify(&t1, &targ)?; *t2.clone() }, @@ -148,13 +148,13 @@ impl<'a> TypeContext<'a> { let arg_type = unimplemented!(); let result_type = unimplemented!(); - MonoType::Arrow(Box::new(arg_type), Box::new(result_type)) + Type::Arrow(Box::new(arg_type), Box::new(result_type)) } - _ => MonoType::Const(TConst::user("unimplemented")) + _ => Type::Const(TConst::user("unimplemented")) }) } - fn infer_if_expr(&mut self, discriminator: &Discriminator, body: &IfExpressionBody) -> InferResult { + fn infer_if_expr(&mut self, discriminator: &Discriminator, body: &IfExpressionBody) -> InferResult> { let test = match discriminator { Discriminator::Simple(expr) => expr, _ => return TypeError::new("Dame desu") @@ -168,20 +168,20 @@ impl<'a> TypeContext<'a> { unimplemented!() } - fn infer_block(&mut self, block: &Block) -> InferResult { - let mut output = MonoType::Const(TConst::Unit); + fn infer_block(&mut self, block: &Block) -> InferResult> { + let mut output = Type::Const(TConst::Unit); for statement in block.iter() { output = self.infer_statement(statement)?; } Ok(output) } - fn unify(&mut self, t1: &MonoType, t2: &MonoType) -> InferResult { + fn unify(&mut self, t1: &Type<()>, t2: &Type<()>) -> InferResult> { unimplemented!() } - fn allocate_existential(&mut self) -> MonoType { - MonoType::ExistentialVar(0) + fn allocate_existential(&mut self) -> Type<()> { + Type::ExistentialVar(0) } }