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.
This commit is contained in:
greg 2018-05-13 15:42:10 -07:00
parent 493d76da0b
commit aff421cd99

View File

@ -25,6 +25,7 @@ impl SymbolTable {
} }
} }
#[derive(Debug)]
struct Symbol { struct Symbol {
name: Rc<String>, name: Rc<String>,
ty: Type ty: Type
@ -126,13 +127,13 @@ impl TypeContext {
impl TypeContext { impl TypeContext {
pub fn add_top_level_types(&mut self, ast: &parsing::AST) -> TypeResult<()> { 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::parsing::Declaration::*;
use self::Type::*; use self::Type::*;
for statement in ast.0.iter() { for statement in ast.0.iter() {
if let &self::parsing::Statement::Declaration(ref decl) = statement { if let Statement::Declaration(decl) = statement {
match decl { match decl {
&FuncSig(ref signature) | &FuncDecl(ref signature, _) => { FuncSig(signature) | FuncDecl(signature, _) => {
let mut uvar_gen = UVarGenerator::new(); 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())} )?; 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() { for &(_, ref type_name) in signature.params.iter().rev() {
@ -140,6 +141,20 @@ impl TypeContext {
ty = Func(bx!(arg_type), bx!(ty)); ty = Func(bx!(arg_type), bx!(ty));
} }
self.bindings.insert(signature.name.clone(), 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(()) Ok(())
} }
pub fn debug_symbol_table(&self) -> String { pub fn debug_symbol_table(&self) -> String {
let mut output = format!("Symbols\n"); let mut output = format!("Symbol table\n");
for (sym, ty) in &self.bindings { for (sym, ty) in &self.symbol_table.values {
write!(output, "{} : {}\n", sym, ty).unwrap(); write!(output, "{} -> {:?}\n", sym, ty).unwrap();
} }
output output
} }