Use symbol table handle in resolver

This commit is contained in:
greg 2019-10-21 04:17:30 -07:00
parent 944916d6af
commit 5ac5425fac
3 changed files with 16 additions and 13 deletions

View File

@ -14,8 +14,7 @@ fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
let mut ast = crate::util::quick_ast(input); let mut ast = crate::util::quick_ast(input);
state.symbol_table_handle.borrow_mut().add_top_level_symbols(&ast).unwrap(); 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(state.symbol_table_handle.clone());
let mut scope_resolver = ScopeResolver::new(&mut t);
let _ = scope_resolver.resolve(&mut ast); let _ = scope_resolver.resolve(&mut ast);
} }

View File

@ -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<ast::AST, String> { fn scope_resolution(mut input: ast::AST, handle: &mut Schala, _com: Option<&mut PassDebugArtifact>) -> Result<ast::AST, String> {
let mut symbol_table = handle.symbol_table.borrow_mut(); let mut resolver = crate::scope_resolution::ScopeResolver::new(handle.symbol_table.clone());
let mut resolver = crate::scope_resolution::ScopeResolver::new(&mut symbol_table);
let () = resolver.resolve(&mut input)?; let () = resolver.resolve(&mut input)?;
Ok(input) Ok(input)
} }

View File

@ -1,25 +1,27 @@
use std::rc::Rc; 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::ast::*;
use crate::util::ScopeStack; use crate::util::ScopeStack;
type FQSNPrefix = Vec<ScopeSegment>; type FQSNPrefix = Vec<ScopeSegment>;
pub struct ScopeResolver<'a> { pub struct ScopeResolver<'a> {
symbol_table: &'a mut SymbolTable, symbol_table_handle: SymbolTableHandle,
name_scope_stack: ScopeStack<'a, Rc<String>, FQSNPrefix>, name_scope_stack: ScopeStack<'a, Rc<String>, FQSNPrefix>,
} }
impl<'a> ASTVisitor for ScopeResolver<'a> { impl<'a> ASTVisitor for ScopeResolver<'a> {
fn import(&mut self, import_spec: &ImportSpecifier) { 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; let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec;
match imported_names { match imported_names {
ImportedNames::All => { ImportedNames::All => {
let prefix = FullyQualifiedSymbolName(path_components.iter().map(|c| ScopeSegment { let prefix = FullyQualifiedSymbolName(path_components.iter().map(|c| ScopeSegment {
name: c.clone(), name: c.clone(),
}).collect()); }).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() { for member in members.into_iter() {
let local_name = member.0.last().unwrap().name.clone(); let local_name = member.0.last().unwrap().name.clone();
self.name_scope_stack.insert(local_name.clone(), member.0); 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) { 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 fqsn = self.lookup_name_in_scope(&qualified_name);
let ref id = qualified_name.id; 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<String>, Expression)>) { fn named_struct(&mut self, name: &QualifiedName, _fields: &Vec<(Rc<String>, Expression)>) {
let ref mut symbol_table = self.symbol_table_handle.borrow_mut();
let ref id = name.id; let ref id = name.id;
let fqsn = self.lookup_name_in_scope(&name); 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) { fn pattern(&mut self, pat: &Pattern) {
@ -69,9 +73,9 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
} }
impl<'a> 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); 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> { pub fn resolve(&mut self, ast: &mut AST) -> Result<(), String> {
walk_ast(self, ast); 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 /// this might be a variable or a pattern. if a variable, set to none
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) { 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 ref id = qualified_name.id;
let fqsn = self.lookup_name_in_scope(qualified_name); let fqsn = self.lookup_name_in_scope(qualified_name);
if self.symbol_table.lookup_by_fqsn(&fqsn).is_some() { if symbol_table.lookup_by_fqsn(&fqsn).is_some() {
self.symbol_table.map_id_to_fqsn(&id, fqsn); symbol_table.map_id_to_fqsn(&id, fqsn);
} }
} }
} }