From c50626241e84a9fe98cf92f93eccc9642f69bf88 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 27 Feb 2018 03:01:05 -0800 Subject: [PATCH] Continuing work --- src/schala_lang/mod.rs | 9 ++++++++- src/schala_lang/typechecking.rs | 25 ++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/schala_lang/mod.rs b/src/schala_lang/mod.rs index 6eeb4eb..87161bb 100644 --- a/src/schala_lang/mod.rs +++ b/src/schala_lang/mod.rs @@ -69,7 +69,14 @@ impl ProgrammingLanguageInterface for Schala { } }; - self.type_context.add_top_level_types(&ast); + match self.type_context.add_top_level_types(&ast) { + Ok(()) => (), + Err(msg) => { + output.add_artifact(TraceArtifact::new("type_check", msg)); + //return output + } + }; + if options.debug_symbol_table { let text = self.type_context.debug_symbol_table(); output.add_artifact(TraceArtifact::new("symbol_table", text)); diff --git a/src/schala_lang/typechecking.rs b/src/schala_lang/typechecking.rs index 0551784..c89f61d 100644 --- a/src/schala_lang/typechecking.rs +++ b/src/schala_lang/typechecking.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use schala_lang::parsing; pub struct TypeContext { - bindings: HashMap, Type> + bindings: HashMap, Type>, } #[derive(Debug, PartialEq, Clone)] @@ -53,9 +53,28 @@ impl TypeContext { } impl TypeContext { - pub fn add_top_level_types(&mut self, ast: &parsing::AST) { - + pub fn add_top_level_types(&mut self, ast: &parsing::AST) -> TypeResult<()> { + use self::parsing::TypeName; + use self::parsing::Declaration::*; + use self::Type::*; use self::TConst::*; + for statement in ast.0.iter() { + if let &self::parsing::Statement::Declaration(ref decl) = statement { + match decl { + &FuncSig(ref signature) | &FuncDecl(ref signature, _) => { + //TODO this needs to be a type variable, not Void + let mut ty: Type = signature.type_anno.as_ref().map(|name: &TypeName| name.to_type()).unwrap_or(Ok(Void))?; + for &(_, ref type_name) in signature.params.iter() { + let arg_type = type_name.as_ref().map(|name| name.to_type()).unwrap_or(Ok(Void))?; + ty = Func(bx!(arg_type), bx!(ty)); + } + self.bindings.insert(signature.name.clone(), ty); + }, + _ => () + } + } + } + Ok(()) } pub fn debug_symbol_table(&self) -> String { format!("Symbols: {:?}", self.bindings)