From 69b7b9f52882478566f098c1b476348e9f826ed7 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 7 Nov 2018 13:44:28 -0800 Subject: [PATCH] Print out types to REPL --- schala-lang/language/src/lib.rs | 11 +++++++++-- schala-lang/language/src/typechecking.rs | 12 +++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/schala-lang/language/src/lib.rs b/schala-lang/language/src/lib.rs index 1c36b66..e5cdafc 100644 --- a/schala-lang/language/src/lib.rs +++ b/schala-lang/language/src/lib.rs @@ -128,9 +128,16 @@ fn symbol_table(handle: &mut Schala, input: ast::AST, comp: Option<&mut Unfinish } } -fn typechecking(handle: &mut Schala, input: ast::AST, _comp: Option<&mut UnfinishedComputation>) -> Result { +fn typechecking(handle: &mut Schala, input: ast::AST, comp: Option<&mut UnfinishedComputation>) -> Result { - handle.type_context.typecheck(&input).map(|_| input) + handle.type_context.typecheck(&input).map(|ty| { + comp.map(|comp| { + let artifact = TraceArtifact::new("type", ty); + comp.add_artifact(artifact); + }); + + input + }) } fn ast_reducing(handle: &mut Schala, input: ast::AST, comp: Option<&mut UnfinishedComputation>) -> Result { diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index 68c610d..e900ea7 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -13,12 +13,14 @@ type TypeResult = Result; #[derive(Debug, Clone)] struct TypeError { } +#[derive(Debug, Clone)] enum MonoType { Var(Rc), Const(Rc), Arrow(Rc) } +#[derive(Debug, Clone)] struct PolyType { vars: Vec>, ty: MonoType @@ -29,9 +31,9 @@ impl TypeContext { TypeContext { } } - pub fn typecheck(&mut self, ast: &AST) -> Result<(), String> { + pub fn typecheck(&mut self, ast: &AST) -> Result { match self.infer_ast(ast) { - Ok(_) => Ok(()), + Ok(t) => Ok(format!("{:?}", t)), Err(err) => Err(format!("Type error: {:?}", err)) } } @@ -41,7 +43,7 @@ impl TypeContext { fn infer_ast(&mut self, ast: &AST) -> TypeResult { let mut output = MonoType::Const(Rc::new("Unit".to_string())); for statement in ast.0.iter() { - match statement { + output = match statement { Statement::ExpressionStatement(ref expr) => self.infer_expr(expr)?, Statement::Declaration(ref decl) => self.infer_decl(decl)?, }; @@ -57,14 +59,14 @@ impl TypeContext { } fn infer_decl(&mut self, expr: &Declaration) -> TypeResult { - unimplemented!() + Ok(MonoType::Const(Rc::new("Unimplemented".to_string()))) } fn infer_expr_type(&mut self, expr_type: &ExpressionType) -> TypeResult { use self::ExpressionType::*; match expr_type { NatLiteral(_) => Ok(MonoType::Const(Rc::new("Nat".to_string()))), - _ => unimplemented!() + _ => Ok(MonoType::Const(Rc::new("Unimplemented".to_string()))) } } }