From ed8359bcd7598261d7c92cb70e78bb26e967afba Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 9 Oct 2017 02:38:33 -0700 Subject: [PATCH] Store constant state, func/binding as value on symbol table, instead of key --- src/schala_lang/type_check.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/schala_lang/type_check.rs b/src/schala_lang/type_check.rs index 99ef182..bf7de29 100644 --- a/src/schala_lang/type_check.rs +++ b/src/schala_lang/type_check.rs @@ -6,12 +6,16 @@ use schala_lang::parsing::{AST, Statement, Declaration, Signature, Expression, E #[derive(Debug, PartialEq, Eq, Hash)] struct PathSpecifier { name: Rc, - kind: &'static str, - constant: bool, +} + +#[derive(Debug, PartialEq, Clone)] +struct TypeContextEntry { + type_var: TypeVariable, + constant: bool } pub struct TypeContext { - symbol_table: HashMap, + symbol_table: HashMap, existential_type_label_count: u64 } @@ -37,40 +41,33 @@ impl TypeContext { Binding {ref name, ref constant, ref expr} => { let spec = PathSpecifier { name: name.clone(), - kind: "binding", - constant: *constant }; - let binding_contents = expr.1.as_ref() + let type_var = expr.1.as_ref() .map(|ty| self.from_anno(ty)) .unwrap_or_else(|| { self.get_existential_type() }); - self.symbol_table.insert(spec, binding_contents); + let entry = TypeContextEntry { type_var, constant: *constant }; + self.symbol_table.insert(spec, entry); }, FuncDecl(ref signature, _) => { let spec = PathSpecifier { name: signature.name.clone(), - kind: "binding", - constant: true }; - let func_type = self.from_signature(signature); - self.symbol_table.insert(spec, func_type); + let type_var = self.from_signature(signature); + let entry = TypeContextEntry { type_var, constant: true }; + self.symbol_table.insert(spec, entry); }, } } } } } - fn lookup(&mut self, binding: &Rc) -> Option { - use self::TypeVariable::*; - use self::UVar::*; - + fn lookup(&mut self, binding: &Rc) -> Option { let key = PathSpecifier { name: binding.clone(), - kind: "binding", - constant: true }; - self.symbol_table.get(&key).map(|x| x.clone()) + self.symbol_table.get(&key).map(|entry| entry.clone()) } pub fn debug_symbol_table(&self) -> String { format!("Symbol table:\n {:?}", self.symbol_table) @@ -175,7 +172,7 @@ impl TypeContext { }, (&IntLiteral(_), _) => Univ(UVar::Integer), (&BoolLiteral(_), _) => Univ(UVar::Boolean), - (&Variable(ref name), _) => self.lookup(name) + (&Variable(ref name), _) => self.lookup(name).map(|entry| entry.type_var) .ok_or(format!("Couldn't find {}", name))?, (&Call { ref f, ref arguments }, _) => { let f_type = self.infer(&*f)?;