From a80db9e4c26bc1e977cbcb1c0d83259404790f17 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 21 May 2018 03:09:38 -0700 Subject: [PATCH] Debug types WIP --- schala-lang/src/lib.rs | 4 +++- schala-lang/src/typechecking.rs | 11 ++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index 389c7ad..e6a5c2d 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -91,10 +91,12 @@ fn symbol_table(handle: &mut Schala, input: parsing::AST, comp: Option<&mut Unfi fn typechecking(handle: &mut Schala, input: parsing::AST, comp: Option<&mut UnfinishedComputation>) -> Result { match handle.type_context.type_check_ast(&input) { Ok(ty) => { - comp.map(|comp| comp.add_artifact(TraceArtifact::new("type_check", format!("{:?}", ty)))); + comp.map(|c| c.add_artifact(TraceArtifact::new("type_table", format!("{}", handle.type_context.debug_types())))); + comp.map(|c| c.add_artifact(TraceArtifact::new("type_check", format!("{:?}", ty)))); Ok(input) }, Err(msg) => { + comp.map(|comp| comp.add_artifact(TraceArtifact::new("type_table", format!("{}", handle.type_context.debug_types())))); comp.map(|comp| comp.add_artifact(TraceArtifact::new("type_check", format!("Type error: {:?}", msg)))); Ok(input) } diff --git a/schala-lang/src/typechecking.rs b/schala-lang/src/typechecking.rs index cff850d..af85756 100644 --- a/schala-lang/src/typechecking.rs +++ b/schala-lang/src/typechecking.rs @@ -2,7 +2,8 @@ use std::rc::Rc; use std::collections::{HashSet, HashMap}; use std::collections::hash_set::Union; use std::iter::Iterator; -//use std::char; +use std::fmt; +use std::fmt::Write; use itertools::Itertools; @@ -151,6 +152,14 @@ impl TypeContext { pub fn new() -> TypeContext { TypeContext { environment: TypeEnvironment::default() } } + + pub fn debug_types(&self) -> String { + let mut output = format!("Type context\n"); + for (sym, ty) in &self.environment.map { + write!(output, "{} -> {:?}\n", sym, ty).unwrap(); + } + output + } pub fn type_check_ast(&mut self, ast: &parsing::AST) -> TypeResult { let ref block = ast.0;