Continuing work

This commit is contained in:
greg 2018-02-27 03:01:05 -08:00
parent fa6c2a6f45
commit 9d72a92f0b
2 changed files with 30 additions and 4 deletions

View File

@ -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 { if options.debug_symbol_table {
let text = self.type_context.debug_symbol_table(); let text = self.type_context.debug_symbol_table();
output.add_artifact(TraceArtifact::new("symbol_table", text)); output.add_artifact(TraceArtifact::new("symbol_table", text));

View File

@ -4,7 +4,7 @@ use std::collections::HashMap;
use schala_lang::parsing; use schala_lang::parsing;
pub struct TypeContext { pub struct TypeContext {
bindings: HashMap<Rc<String>, Type> bindings: HashMap<Rc<String>, Type>,
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
@ -53,9 +53,28 @@ impl TypeContext {
} }
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 { pub fn debug_symbol_table(&self) -> String {
format!("Symbols: {:?}", self.bindings) format!("Symbols: {:?}", self.bindings)