From 3bb323667d0f5c42575391d376827d85a7297d33 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 14 Oct 2021 06:55:57 -0700 Subject: [PATCH] Rename SourceMap -> DeclLocations --- schala-lang/language/src/symbol_table/mod.rs | 24 +++++++++---------- .../language/src/symbol_table/tables.rs | 11 +++++---- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index 09a20d9..90d83a9 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -24,7 +24,7 @@ macro_rules! fqsn { } mod tables; -use tables::SourceMap; +use tables::DeclLocations; mod symbol_trie; use symbol_trie::SymbolTrie; mod test; @@ -40,14 +40,14 @@ impl DuplicateNameTrackTable { DuplicateNameTrackTable { table: HashMap::new() } } - fn try_register(&mut self, name: &Rc, id: &ItemId, source_map: &SourceMap) -> Result<(), LineNumber> { + fn try_register(&mut self, name: &Rc, id: &ItemId, decl_locations: &DeclLocations) -> Result<(), LineNumber> { match self.table.entry(name.clone()) { Entry::Occupied(o) => { let line_number = o.get(); Err(*line_number) }, Entry::Vacant(v) => { - let line_number = if let Some(loc) = source_map.lookup(id) { + let line_number = if let Some(loc) = decl_locations.lookup(id) { loc.line_num } else { 0 @@ -94,7 +94,7 @@ impl ScopeSegment { //cf. p. 150 or so of Language Implementation Patterns pub struct SymbolTable { - source_map: SourceMap, + decl_locations: DeclLocations, symbol_path_to_symbol: HashMap, id_to_fqsn: HashMap, symbol_trie: SymbolTrie, @@ -103,7 +103,7 @@ pub struct SymbolTable { impl SymbolTable { pub fn new() -> SymbolTable { SymbolTable { - source_map: SourceMap::new(), + decl_locations: DeclLocations::new(), symbol_path_to_symbol: HashMap::new(), id_to_fqsn: HashMap::new(), symbol_trie: SymbolTrie::new() @@ -200,16 +200,16 @@ impl SymbolTable { for statement in statements.iter() { match statement { Statement { kind: StatementKind::Declaration(decl), id, location, } => { - self.source_map.add_location(id, *location); + self.decl_locations.add_location(id, *location); match decl { FuncSig(ref signature) => { - seen_identifiers.try_register(&signature.name, id, &self.source_map) + seen_identifiers.try_register(&signature.name, id, &self.decl_locations) .map_err(|line| format!("Duplicate function definition: {}. It's already defined at {}", signature.name, line))?; self.add_function_signature(signature, scope_name_stack)? } FuncDecl(ref signature, ref body) => { - seen_identifiers.try_register(&signature.name, id, &self.source_map) + seen_identifiers.try_register(&signature.name, id, &self.decl_locations) .map_err(|line| format!("Duplicate function definition: {}. It's already defined at {}", signature.name, line))?; self.add_function_signature(signature, scope_name_stack)?; scope_name_stack.push(ScopeSegment{ @@ -220,12 +220,12 @@ impl SymbolTable { output? }, TypeDecl { name, body, mutable } => { - seen_identifiers.try_register(&name.name, &id, &self.source_map) + seen_identifiers.try_register(&name.name, &id, &self.decl_locations) .map_err(|line| format!("Duplicate type definition: {}. It's already defined at {}", name.name, line))?; self.add_type_decl(name, body, mutable, scope_name_stack)? }, Binding { name, .. } => { - seen_identifiers.try_register(&name, &id, &self.source_map) + seen_identifiers.try_register(&name, &id, &self.decl_locations) .map_err(|line| format!("Duplicate variable definition: {}. It's already defined at {}", name, line))?; self.add_new_symbol(name, scope_name_stack, SymbolSpec::Binding); } @@ -233,8 +233,8 @@ impl SymbolTable { } }, Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id, location } => { - self.source_map.add_location(id, *location); - seen_modules.try_register(name, id, &self.source_map) + self.decl_locations.add_location(id, *location); + seen_modules.try_register(name, id, &self.decl_locations) .map_err(|line| format!("Duplicate module definition: {}. It's already defined at {}", name, line))?; scope_name_stack.push(ScopeSegment { name: name.clone() }); let output = self.add_symbols_from_scope(contents, scope_name_stack); diff --git a/schala-lang/language/src/symbol_table/tables.rs b/schala-lang/language/src/symbol_table/tables.rs index 7244d8b..fd25cdb 100644 --- a/schala-lang/language/src/symbol_table/tables.rs +++ b/schala-lang/language/src/symbol_table/tables.rs @@ -4,14 +4,15 @@ use crate::ast::ItemId; use crate::tokenizing::Location; -//TODO rename this type to make its purpose clearer -pub struct SourceMap { +/// Maps top-level declarations to Locations in source code, to detect +/// multiply-defined top level items. +pub struct DeclLocations { map: HashMap } -impl SourceMap { - pub fn new() -> SourceMap { - SourceMap { map: HashMap::new() } +impl DeclLocations { + pub fn new() -> Self { + Self { map: HashMap::new() } } pub(crate) fn add_location(&mut self, id: &ItemId, loc: Location) {