From 9540dc70f24b70fa997ae0570b094d4eb4c2cb3d Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 24 Oct 2021 01:00:32 -0700 Subject: [PATCH] Successfully refactor the ScopeResolver tables --- schala-lang/language/src/symbol_table/mod.rs | 2 +- .../language/src/symbol_table/resolver.rs | 109 +++++++++++------- schala-lang/language/src/util.rs | 1 - 3 files changed, 68 insertions(+), 44 deletions(-) diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index 7762789..b958f61 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -248,7 +248,7 @@ impl fmt::Display for SymbolSpec { "RecordConstructor(idx: {})( -> {})", index, type_name ), - Binding => write!(f, "Binding"), + GlobalBinding => write!(f, "GlobalBinding"), } } } diff --git a/schala-lang/language/src/symbol_table/resolver.rs b/schala-lang/language/src/symbol_table/resolver.rs index 71e68d0..66c0b24 100644 --- a/schala-lang/language/src/symbol_table/resolver.rs +++ b/schala-lang/language/src/symbol_table/resolver.rs @@ -9,9 +9,9 @@ type FqsnPrefix = Vec; #[derive(Debug)] enum NameType { //TODO eventually this needs to support closures - Param(u8), //TODO functions limited to 255 params + Param(u8), //TODO handle implications of functions being limited to 255 params LocalVariable, - ImportedDefinition(DefId), + Import(Fqsn), } #[derive(Debug)] @@ -24,18 +24,14 @@ enum ScopeType { pub struct ScopeResolver<'a> { symbol_table: &'a mut super::SymbolTable, - /// Used for import resolution. TODO maybe this can also use the lexical scope table. - name_scope_stack: ScopeStack<'a, Rc, FqsnPrefix>, - lexical_scopes: ScopeStack<'a, Rc, NameType, ScopeType> + lexical_scopes: ScopeStack<'a, Rc, NameType, ScopeType>, } impl<'a> ScopeResolver<'a> { pub fn new(symbol_table: &'a mut SymbolTable) -> Self { - let name_scope_stack = ScopeStack::new(None); let lexical_scopes = ScopeStack::new(None); Self { symbol_table, - name_scope_stack, lexical_scopes, } } @@ -44,6 +40,8 @@ impl<'a> ScopeResolver<'a> { walk_ast(self, ast); } + + /* fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> Fqsn { let QualifiedName { components, .. } = sym_name; let first_component = &components[0]; @@ -66,15 +64,34 @@ impl<'a> ScopeResolver<'a> { } } } + */ /// This method correctly modifies the id_to_symbol table (ItemId) to have the appropriate /// mappings. - fn handle_qualified_name(&mut self, name: &QualifiedName) { - println!("Handling qualified_name in resolver.rs: {:?}", name); - let fqsn = self.lookup_name_in_scope(name); - let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); - if let Some(symbol) = symbol { - self.symbol_table.id_to_symbol.insert(name.id.clone(), symbol.clone()); + fn lookup_name_in_scope(&mut self, name: &QualifiedName) { + let QualifiedName { id, components } = name; + //TODO handle a "partial" qualified name + if components.len() == 1 { + let local_name: Rc = components[0].clone(); + let name_type = self.lexical_scopes.lookup(&local_name); + println!("resolver.rs lookup_name_in_scope: {:?} with name_type {:?}", name, name_type); + match name_type { + Some(NameType::Import(fqsn)) => { + let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); + if let Some(symbol) = symbol { + self.symbol_table.id_to_symbol.insert(id.clone(), symbol.clone()); + } + }, + Some(NameType::Param(n)) => (), + Some(NameType::LocalVariable) => (), + None => (), + } + } else { + let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() }; + let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); + if let Some(symbol) = symbol { + self.symbol_table.id_to_symbol.insert(id.clone(), symbol.clone()); + } } } } @@ -98,28 +115,29 @@ impl<'a> ASTVisitor for ScopeResolver<'a> { .collect(), }; let members = self.symbol_table.symbol_trie.get_children(&prefix); - for member in members.into_iter() { - let Scope::Name(n) = member.scopes.last().unwrap(); - let local_name = n.clone(); - self.name_scope_stack.insert(local_name, member.scopes); + for fqsn in members.into_iter() { + self.lexical_scopes.insert( fqsn.local_name(), NameType::Import(fqsn)); } } ImportedNames::LastOfPath => { - let name = path_components.last().unwrap(); //TODO handle better - let fqsn_prefix = path_components + let fqsn = Fqsn { + scopes: path_components .iter() .map(|c| Scope::Name(c.clone())) - .collect(); - self.name_scope_stack.insert(name.clone(), fqsn_prefix); + .collect() + }; + self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn)); } ImportedNames::List(ref names) => { - let fqsn_prefix: FqsnPrefix = path_components + let fqsn_prefix: Vec = path_components .iter() .map(|c| Scope::Name(c.clone())) .collect(); for name in names.iter() { - self.name_scope_stack - .insert(name.clone(), fqsn_prefix.clone()); + let mut scopes = fqsn_prefix.clone(); + scopes.push(Scope::Name(name.clone())); + let fqsn = Fqsn { scopes }; + self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn)); } } }; @@ -127,23 +145,30 @@ impl<'a> ASTVisitor for ScopeResolver<'a> { } fn declaration(&mut self, declaration: &Declaration) -> Recursion { - if let Declaration::FuncDecl(signature, block) = declaration { - let param_names = signature.params.iter().map(|param| param.name.clone()); - let mut new_scope = self.lexical_scopes.new_scope(Some(ScopeType::Function { name: signature.name.clone() })); + let is_function_scope = matches!(self.lexical_scopes.get_name(), Some(ScopeType::Function { .. })); + match declaration { + Declaration::FuncDecl(signature, block) => { + let param_names = signature.params.iter().map(|param| param.name.clone()); + //TODO I'm 90% sure this is right, until I get to closures + //let mut new_scope = self.lexical_scopes.new_scope(Some(ScopeType::Function { name: signature.name.clone() })); + let mut new_scope = ScopeStack::new(None); - for (n, param) in param_names.enumerate().into_iter() { - new_scope.insert(param, NameType::Param(n as u8)); + for (n, param) in param_names.enumerate().into_iter() { + new_scope.insert(param, NameType::Param(n as u8)); + } + + let mut new_resolver = ScopeResolver { + symbol_table: self.symbol_table, + lexical_scopes: new_scope, + }; + walk_block(&mut new_resolver, block); + Recursion::Stop } - - let mut new_resolver = ScopeResolver { - symbol_table: self.symbol_table, - name_scope_stack: self.name_scope_stack.new_scope(None), - lexical_scopes: new_scope, - }; - walk_block(&mut new_resolver, block); - Recursion::Stop - } else { - Recursion::Continue + Declaration::Binding { name, .. } if is_function_scope => { + self.lexical_scopes.insert(name.clone(), NameType::LocalVariable); + Recursion::Continue + } + _ => Recursion::Continue } } @@ -151,10 +176,10 @@ impl<'a> ASTVisitor for ScopeResolver<'a> { use ExpressionKind::*; match &expression.kind { Value(name) => { - self.handle_qualified_name(name); + self.lookup_name_in_scope(name); }, NamedStruct { name, fields: _ } => { - self.handle_qualified_name(name); + self.lookup_name_in_scope(name); }, _ => (), } @@ -169,7 +194,7 @@ impl<'a> ASTVisitor for ScopeResolver<'a> { TuplePattern(_) => (), Literal(_) | Ignored => (), TupleStruct(name, _) | Record(name, _) | VarOrName(name) => { - self.handle_qualified_name(name); + self.lookup_name_in_scope(name); } }; Recursion::Continue diff --git a/schala-lang/language/src/util.rs b/schala-lang/language/src/util.rs index 6842041..999e20f 100644 --- a/schala-lang/language/src/util.rs +++ b/schala-lang/language/src/util.rs @@ -49,7 +49,6 @@ impl<'a, T, V, N> ScopeStack<'a, T, V, N> where T: Hash + Eq { } } - #[allow(dead_code)] pub fn get_name(&self) -> Option<&N> { self.scope_name.as_ref() }