From 5ac5425fac64746389d0234a4eb14d553ffcd0ce Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 21 Oct 2019 04:17:30 -0700 Subject: [PATCH] Use symbol table handle in resolver --- schala-lang/language/src/eval/test.rs | 3 +-- schala-lang/language/src/schala.rs | 3 +-- schala-lang/language/src/scope_resolution.rs | 23 ++++++++++++-------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/schala-lang/language/src/eval/test.rs b/schala-lang/language/src/eval/test.rs index 9a8306b..a683213 100644 --- a/schala-lang/language/src/eval/test.rs +++ b/schala-lang/language/src/eval/test.rs @@ -14,8 +14,7 @@ fn evaluate_all_outputs(input: &str) -> Vec> { let mut ast = crate::util::quick_ast(input); state.symbol_table_handle.borrow_mut().add_top_level_symbols(&ast).unwrap(); { - let mut t = &mut state.symbol_table_handle.borrow_mut(); - let mut scope_resolver = ScopeResolver::new(&mut t); + let mut scope_resolver = ScopeResolver::new(state.symbol_table_handle.clone()); let _ = scope_resolver.resolve(&mut ast); } diff --git a/schala-lang/language/src/schala.rs b/schala-lang/language/src/schala.rs index 85548d1..16ee7bc 100644 --- a/schala-lang/language/src/schala.rs +++ b/schala-lang/language/src/schala.rs @@ -156,8 +156,7 @@ fn symbol_table(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebu } fn scope_resolution(mut input: ast::AST, handle: &mut Schala, _com: Option<&mut PassDebugArtifact>) -> Result { - let mut symbol_table = handle.symbol_table.borrow_mut(); - let mut resolver = crate::scope_resolution::ScopeResolver::new(&mut symbol_table); + let mut resolver = crate::scope_resolution::ScopeResolver::new(handle.symbol_table.clone()); let () = resolver.resolve(&mut input)?; Ok(input) } diff --git a/schala-lang/language/src/scope_resolution.rs b/schala-lang/language/src/scope_resolution.rs index 69f5294..da3d0dc 100644 --- a/schala-lang/language/src/scope_resolution.rs +++ b/schala-lang/language/src/scope_resolution.rs @@ -1,25 +1,27 @@ use std::rc::Rc; -use crate::symbol_table::{SymbolTable, ScopeSegment, FullyQualifiedSymbolName}; +use crate::schala::SymbolTableHandle; +use crate::symbol_table::{ScopeSegment, FullyQualifiedSymbolName}; use crate::ast::*; use crate::util::ScopeStack; type FQSNPrefix = Vec; pub struct ScopeResolver<'a> { - symbol_table: &'a mut SymbolTable, + symbol_table_handle: SymbolTableHandle, name_scope_stack: ScopeStack<'a, Rc, FQSNPrefix>, } impl<'a> ASTVisitor for ScopeResolver<'a> { fn import(&mut self, import_spec: &ImportSpecifier) { + let ref symbol_table = self.symbol_table_handle.borrow(); let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec; match imported_names { ImportedNames::All => { let prefix = FullyQualifiedSymbolName(path_components.iter().map(|c| ScopeSegment { name: c.clone(), }).collect()); - let members = self.symbol_table.lookup_children_of_fqsn(&prefix); + let members = symbol_table.lookup_children_of_fqsn(&prefix); for member in members.into_iter() { let local_name = member.0.last().unwrap().name.clone(); self.name_scope_stack.insert(local_name.clone(), member.0); @@ -44,15 +46,17 @@ impl<'a> ASTVisitor for ScopeResolver<'a> { } fn qualified_name(&mut self, qualified_name: &QualifiedName) { + let ref mut symbol_table = self.symbol_table_handle.borrow_mut(); let fqsn = self.lookup_name_in_scope(&qualified_name); let ref id = qualified_name.id; - self.symbol_table.map_id_to_fqsn(id, fqsn); + symbol_table.map_id_to_fqsn(id, fqsn); } fn named_struct(&mut self, name: &QualifiedName, _fields: &Vec<(Rc, Expression)>) { + let ref mut symbol_table = self.symbol_table_handle.borrow_mut(); let ref id = name.id; let fqsn = self.lookup_name_in_scope(&name); - self.symbol_table.map_id_to_fqsn(id, fqsn); + symbol_table.map_id_to_fqsn(id, fqsn); } fn pattern(&mut self, pat: &Pattern) { @@ -69,9 +73,9 @@ impl<'a> ASTVisitor for ScopeResolver<'a> { } impl<'a> ScopeResolver<'a> { - pub fn new(symbol_table: &'a mut SymbolTable) -> ScopeResolver { + pub fn new(symbol_table_handle: SymbolTableHandle) -> ScopeResolver<'static> { let name_scope_stack = ScopeStack::new(None); - ScopeResolver { symbol_table, name_scope_stack } + ScopeResolver { symbol_table_handle, name_scope_stack } } pub fn resolve(&mut self, ast: &mut AST) -> Result<(), String> { walk_ast(self, ast); @@ -96,10 +100,11 @@ impl<'a> ScopeResolver<'a> { /// this might be a variable or a pattern. if a variable, set to none fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) { + let ref mut symbol_table = self.symbol_table_handle.borrow_mut(); let ref id = qualified_name.id; let fqsn = self.lookup_name_in_scope(qualified_name); - if self.symbol_table.lookup_by_fqsn(&fqsn).is_some() { - self.symbol_table.map_id_to_fqsn(&id, fqsn); + if symbol_table.lookup_by_fqsn(&fqsn).is_some() { + symbol_table.map_id_to_fqsn(&id, fqsn); } } }