diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index ceb210f..10c40e6 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -141,11 +141,6 @@ pub struct SymbolTable { fq_names: NameTable, //Note that presence of two tables implies that a type and other binding with the same name can co-exist types: NameTable, - /// A map of the Fqsn of an AST definition to a Symbol data structure, which contains - /// some basic information about what that symbol is and (ideally) references to other tables - /// (e.g. typechecking tables) with more information about that symbol. - fqsn_to_symbol: HashMap>, - id_to_symbol: HashMap>, def_to_symbol: HashMap>, } @@ -158,7 +153,6 @@ impl SymbolTable { fq_names: NameTable::new(), types: NameTable::new(), - fqsn_to_symbol: HashMap::new(), id_to_symbol: HashMap::new(), def_to_symbol: HashMap::new(), }; @@ -207,8 +201,7 @@ impl SymbolTable { fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) { let def_id = self.def_id_store.fresh(); let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id }); - self.symbol_trie.insert(&fqsn); - self.fqsn_to_symbol.insert(fqsn, symbol.clone()); + self.symbol_trie.insert(&fqsn, def_id); self.id_to_symbol.insert(*id, symbol.clone()); self.def_to_symbol.insert(def_id, symbol); } @@ -218,8 +211,7 @@ impl SymbolTable { let spec = SymbolSpec::Builtin(builtin); let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id }); - self.symbol_trie.insert(&fqsn); - self.fqsn_to_symbol.insert(fqsn, symbol.clone()); + self.symbol_trie.insert(&fqsn, def_id); self.def_to_symbol.insert(def_id, symbol); } diff --git a/schala-lang/language/src/symbol_table/resolver.rs b/schala-lang/language/src/symbol_table/resolver.rs index 7fea208..29e30a8 100644 --- a/schala-lang/language/src/symbol_table/resolver.rs +++ b/schala-lang/language/src/symbol_table/resolver.rs @@ -47,7 +47,13 @@ impl<'a> ScopeResolver<'a> { let local_name = components.first().unwrap().clone(); let name_type = self.lexical_scopes.lookup(&local_name); let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() }; - let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); + let symbol = self + .symbol_table + .symbol_trie + .lookup(&fqsn) + .as_ref() + .and_then(|def_id| self.symbol_table.lookup_symbol_by_def(def_id)) + .cloned(); //println!("\tFound lexical_scope entry: {:?} and {} symbol: {:?}", name_type, fqsn, symbol); //TODO handle a "partial" qualified name, and also handle it down in the pattern-matching @@ -57,9 +63,16 @@ impl<'a> ScopeResolver<'a> { if components.len() == 1 { match name_type { Some(NameType::Import(fqsn)) => { - let symbol = self.symbol_table.fqsn_to_symbol.get(fqsn); + let symbol = self + .symbol_table + .symbol_trie + .lookup(&fqsn) + .as_ref() + .and_then(|def_id| self.symbol_table.lookup_symbol_by_def(def_id)) + .cloned(); + if let Some(symbol) = symbol { - self.symbol_table.id_to_symbol.insert(*id, symbol.clone()); + self.symbol_table.id_to_symbol.insert(*id, Rc::new(symbol)); } } Some(NameType::Param(n)) => { @@ -77,12 +90,12 @@ impl<'a> ScopeResolver<'a> { } None => if let Some(symbol) = symbol { - self.symbol_table.id_to_symbol.insert(*id, symbol.clone()); + self.symbol_table.id_to_symbol.insert(*id, Rc::new(symbol)); }, } } else { if let Some(symbol) = symbol { - self.symbol_table.id_to_symbol.insert(*id, symbol.clone()); + self.symbol_table.id_to_symbol.insert(*id, Rc::new(symbol)); } } } @@ -254,9 +267,15 @@ impl<'a> ASTVisitor for ScopeResolver<'a> { } else { let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() }; - let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); + let symbol = self + .symbol_table + .symbol_trie + .lookup(&fqsn) + .as_ref() + .and_then(|def_id| self.symbol_table.lookup_symbol_by_def(def_id)) + .cloned(); if let Some(symbol) = symbol { - self.symbol_table.id_to_symbol.insert(*id, symbol.clone()); + self.symbol_table.id_to_symbol.insert(*id, Rc::new(symbol)); } } } diff --git a/schala-lang/language/src/symbol_table/symbol_trie.rs b/schala-lang/language/src/symbol_table/symbol_trie.rs index 2e02c66..9ba2085 100644 --- a/schala-lang/language/src/symbol_table/symbol_trie.rs +++ b/schala-lang/language/src/symbol_table/symbol_trie.rs @@ -5,10 +5,10 @@ use std::{ use radix_trie::{Trie, TrieCommon, TrieKey}; -use super::{Fqsn, Scope}; +use super::{DefId, Fqsn, Scope}; #[derive(Debug)] -pub struct SymbolTrie(Trie); +pub struct SymbolTrie(Trie); impl TrieKey for Fqsn { fn encode_bytes(&self) -> Vec { @@ -28,8 +28,12 @@ impl SymbolTrie { SymbolTrie(Trie::new()) } - pub fn insert(&mut self, fqsn: &Fqsn) { - self.0.insert(fqsn.clone(), ()); + pub fn insert(&mut self, fqsn: &Fqsn, def_id: DefId) { + self.0.insert(fqsn.clone(), def_id); + } + + pub fn lookup(&self, fqsn: &Fqsn) -> Option { + self.0.get(fqsn).cloned() } pub fn get_children(&self, fqsn: &Fqsn) -> Vec { @@ -53,11 +57,12 @@ mod test { #[test] fn test_trie_insertion() { + let id = DefId::default(); let mut trie = SymbolTrie::new(); - trie.insert(&make_fqsn(&["unrelated", "thing"])); - trie.insert(&make_fqsn(&["outer", "inner"])); - trie.insert(&make_fqsn(&["outer", "inner", "still_inner"])); + trie.insert(&make_fqsn(&["unrelated", "thing"]), id); + trie.insert(&make_fqsn(&["outer", "inner"]), id); + trie.insert(&make_fqsn(&["outer", "inner", "still_inner"]), id); let children = trie.get_children(&make_fqsn(&["outer", "inner"])); assert_eq!(children.len(), 1);