From aff421cd99cb5e2d860746f04c604602c0945e3c Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 13 May 2018 15:42:10 -0700 Subject: [PATCH] Working with symbol table Note that symbol table is a different object now than the previous binding table that was used for type-checking. That binding table is not currently debugged and should be debugged in a separate debug output with typechecking proper. --- schala-lang/src/typechecking.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/schala-lang/src/typechecking.rs b/schala-lang/src/typechecking.rs index 4a867f1..a466ae5 100644 --- a/schala-lang/src/typechecking.rs +++ b/schala-lang/src/typechecking.rs @@ -25,6 +25,7 @@ impl SymbolTable { } } +#[derive(Debug)] struct Symbol { name: Rc, ty: Type @@ -126,13 +127,13 @@ impl TypeContext { impl TypeContext { pub fn add_top_level_types(&mut self, ast: &parsing::AST) -> TypeResult<()> { - use self::parsing::TypeName; + use self::parsing::{Statement, TypeName, Variant, TypeSingletonName, TypeBody}; use self::parsing::Declaration::*; use self::Type::*; for statement in ast.0.iter() { - if let &self::parsing::Statement::Declaration(ref decl) = statement { + if let Statement::Declaration(decl) = statement { match decl { - &FuncSig(ref signature) | &FuncDecl(ref signature, _) => { + FuncSig(signature) | FuncDecl(signature, _) => { let mut uvar_gen = UVarGenerator::new(); let mut ty: Type = signature.type_anno.as_ref().map(|name: &TypeName| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?; for &(_, ref type_name) in signature.params.iter().rev() { @@ -140,6 +141,20 @@ impl TypeContext { ty = Func(bx!(arg_type), bx!(ty)); } self.bindings.insert(signature.name.clone(), ty); + + //self.symbol_table.values.insert(); + }, + TypeDecl(TypeSingletonName { name, ..}, TypeBody(variants)) => { + for var in variants { + match var { + Variant::UnitStruct(variant_name) => { + //TODO will have to make this a function to this type eventually + let ty = Type::Const(TConst::Custom(format!("{}", name))); + self.symbol_table.values.insert(variant_name.clone(), Symbol { name: variant_name.clone(), ty }); + }, + e => return Err(format!("{:?} not supported in typing yet", e)), + } + } }, _ => () } @@ -148,9 +163,9 @@ impl TypeContext { Ok(()) } pub fn debug_symbol_table(&self) -> String { - let mut output = format!("Symbols\n"); - for (sym, ty) in &self.bindings { - write!(output, "{} : {}\n", sym, ty).unwrap(); + let mut output = format!("Symbol table\n"); + for (sym, ty) in &self.symbol_table.values { + write!(output, "{} -> {:?}\n", sym, ty).unwrap(); } output }