Successfully refactor the ScopeResolver tables

This commit is contained in:
Greg Shuflin 2021-10-24 01:00:32 -07:00
parent ba09919aa1
commit 9540dc70f2
3 changed files with 68 additions and 44 deletions

View File

@ -248,7 +248,7 @@ impl fmt::Display for SymbolSpec {
"RecordConstructor(idx: {})(<members> -> {})", "RecordConstructor(idx: {})(<members> -> {})",
index, type_name index, type_name
), ),
Binding => write!(f, "Binding"), GlobalBinding => write!(f, "GlobalBinding"),
} }
} }
} }

View File

@ -9,9 +9,9 @@ type FqsnPrefix = Vec<Scope>;
#[derive(Debug)] #[derive(Debug)]
enum NameType { enum NameType {
//TODO eventually this needs to support closures //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, LocalVariable,
ImportedDefinition(DefId), Import(Fqsn),
} }
#[derive(Debug)] #[derive(Debug)]
@ -24,18 +24,14 @@ enum ScopeType {
pub struct ScopeResolver<'a> { pub struct ScopeResolver<'a> {
symbol_table: &'a mut super::SymbolTable, symbol_table: &'a mut super::SymbolTable,
/// Used for import resolution. TODO maybe this can also use the lexical scope table. lexical_scopes: ScopeStack<'a, Rc<String>, NameType, ScopeType>,
name_scope_stack: ScopeStack<'a, Rc<String>, FqsnPrefix>,
lexical_scopes: ScopeStack<'a, Rc<String>, NameType, ScopeType>
} }
impl<'a> ScopeResolver<'a> { impl<'a> ScopeResolver<'a> {
pub fn new(symbol_table: &'a mut SymbolTable) -> Self { pub fn new(symbol_table: &'a mut SymbolTable) -> Self {
let name_scope_stack = ScopeStack::new(None);
let lexical_scopes = ScopeStack::new(None); let lexical_scopes = ScopeStack::new(None);
Self { Self {
symbol_table, symbol_table,
name_scope_stack,
lexical_scopes, lexical_scopes,
} }
} }
@ -44,6 +40,8 @@ impl<'a> ScopeResolver<'a> {
walk_ast(self, ast); walk_ast(self, ast);
} }
/*
fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> Fqsn { fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> Fqsn {
let QualifiedName { components, .. } = sym_name; let QualifiedName { components, .. } = sym_name;
let first_component = &components[0]; 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 /// This method correctly modifies the id_to_symbol table (ItemId) to have the appropriate
/// mappings. /// mappings.
fn handle_qualified_name(&mut self, name: &QualifiedName) { fn lookup_name_in_scope(&mut self, name: &QualifiedName) {
println!("Handling qualified_name in resolver.rs: {:?}", name); let QualifiedName { id, components } = name;
let fqsn = self.lookup_name_in_scope(name); //TODO handle a "partial" qualified name
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); if components.len() == 1 {
if let Some(symbol) = symbol { let local_name: Rc<String> = components[0].clone();
self.symbol_table.id_to_symbol.insert(name.id.clone(), symbol.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(), .collect(),
}; };
let members = self.symbol_table.symbol_trie.get_children(&prefix); let members = self.symbol_table.symbol_trie.get_children(&prefix);
for member in members.into_iter() { for fqsn in members.into_iter() {
let Scope::Name(n) = member.scopes.last().unwrap(); self.lexical_scopes.insert( fqsn.local_name(), NameType::Import(fqsn));
let local_name = n.clone();
self.name_scope_stack.insert(local_name, member.scopes);
} }
} }
ImportedNames::LastOfPath => { ImportedNames::LastOfPath => {
let name = path_components.last().unwrap(); //TODO handle better let fqsn = Fqsn {
let fqsn_prefix = path_components scopes: path_components
.iter() .iter()
.map(|c| Scope::Name(c.clone())) .map(|c| Scope::Name(c.clone()))
.collect(); .collect()
self.name_scope_stack.insert(name.clone(), fqsn_prefix); };
self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn));
} }
ImportedNames::List(ref names) => { ImportedNames::List(ref names) => {
let fqsn_prefix: FqsnPrefix = path_components let fqsn_prefix: Vec<Scope> = path_components
.iter() .iter()
.map(|c| Scope::Name(c.clone())) .map(|c| Scope::Name(c.clone()))
.collect(); .collect();
for name in names.iter() { for name in names.iter() {
self.name_scope_stack let mut scopes = fqsn_prefix.clone();
.insert(name.clone(), 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 { fn declaration(&mut self, declaration: &Declaration) -> Recursion {
if let Declaration::FuncDecl(signature, block) = declaration { let is_function_scope = matches!(self.lexical_scopes.get_name(), Some(ScopeType::Function { .. }));
let param_names = signature.params.iter().map(|param| param.name.clone()); match declaration {
let mut new_scope = self.lexical_scopes.new_scope(Some(ScopeType::Function { name: signature.name.clone() })); 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() { for (n, param) in param_names.enumerate().into_iter() {
new_scope.insert(param, NameType::Param(n as u8)); 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
} }
Declaration::Binding { name, .. } if is_function_scope => {
let mut new_resolver = ScopeResolver { self.lexical_scopes.insert(name.clone(), NameType::LocalVariable);
symbol_table: self.symbol_table, Recursion::Continue
name_scope_stack: self.name_scope_stack.new_scope(None), }
lexical_scopes: new_scope, _ => Recursion::Continue
};
walk_block(&mut new_resolver, block);
Recursion::Stop
} else {
Recursion::Continue
} }
} }
@ -151,10 +176,10 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
use ExpressionKind::*; use ExpressionKind::*;
match &expression.kind { match &expression.kind {
Value(name) => { Value(name) => {
self.handle_qualified_name(name); self.lookup_name_in_scope(name);
}, },
NamedStruct { name, fields: _ } => { 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(_) => (), TuplePattern(_) => (),
Literal(_) | Ignored => (), Literal(_) | Ignored => (),
TupleStruct(name, _) | Record(name, _) | VarOrName(name) => { TupleStruct(name, _) | Record(name, _) | VarOrName(name) => {
self.handle_qualified_name(name); self.lookup_name_in_scope(name);
} }
}; };
Recursion::Continue Recursion::Continue

View File

@ -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> { pub fn get_name(&self) -> Option<&N> {
self.scope_name.as_ref() self.scope_name.as_ref()
} }