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> -> {})",
index, type_name
),
Binding => write!(f, "Binding"),
GlobalBinding => write!(f, "GlobalBinding"),
}
}
}

View File

@ -9,9 +9,9 @@ type FqsnPrefix = Vec<Scope>;
#[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<String>, FqsnPrefix>,
lexical_scopes: ScopeStack<'a, Rc<String>, NameType, ScopeType>
lexical_scopes: ScopeStack<'a, Rc<String>, 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);
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<String> = 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(name.id.clone(), symbol.clone());
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<Scope> = 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,9 +145,13 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
}
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 { .. }));
match declaration {
Declaration::FuncDecl(signature, block) => {
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() }));
//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));
@ -137,24 +159,27 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
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 {
}
Declaration::Binding { name, .. } if is_function_scope => {
self.lexical_scopes.insert(name.clone(), NameType::LocalVariable);
Recursion::Continue
}
_ => Recursion::Continue
}
}
fn expression(&mut self, expression: &Expression) -> Recursion {
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

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> {
self.scope_name.as_ref()
}