Work on better Scope abstraction.

This commit is contained in:
Jesse Brault 2026-07-12 16:42:14 -05:00
parent 7b76771843
commit 57abc03145
3 changed files with 27 additions and 14 deletions

View File

@ -39,8 +39,8 @@ impl<'a> SymbolCollectionContext<'a> {
pub fn find_symbol_in_scope_for(&self, name: &str, node_id: NodeId) -> Option<&Symbol> {
let scope_id = self.nodes_to_scopes[&node_id];
let scope = &self.scopes[scope_id];
if let Some(symbol_id) = scope.symbols().get(name) {
Some(&self.symbols[*symbol_id])
if let Some(symbol_id) = scope.get_symbol_id(name) {
Some(&self.symbols[symbol_id])
} else {
None
}
@ -52,7 +52,7 @@ impl<'a> SymbolCollectionContext<'a> {
let symbol_id = self.symbols.len() - 1;
let scope_id = self.nodes_to_scopes[&node_id];
let scope = &mut self.scopes[scope_id];
scope.symbols_mut().insert(declared_name, symbol_id);
scope.insert_symbol(declared_name, symbol_id);
}
pub fn symbols_mut(&mut self) -> &mut Vec<Symbol> {

View File

@ -130,7 +130,12 @@ fn resolve_names_function(function: &Function, ctx: &mut NameResolutionContext)
// point this function's node_id to the symbol_id for the function symbol
let scope_id = ctx.nodes_to_scopes[&function.node_id()];
let scope = &ctx.scopes[scope_id];
let symbol_id = scope.symbols()[function.declared_name()];
let symbol_id = scope
.get_symbol_id(function.declared_name())
.expect(&format!(
"function's symbol_id could not be found for {}",
function.declared_name()
));
ctx.nodes_to_symbols.insert(function.node_id(), symbol_id);
}
@ -145,7 +150,12 @@ fn resolve_names_extern_function(
// point this extern function's node_id to the symbol_id for this function symbol
let scope_id = ctx.nodes_to_scopes[&extern_function.node_id()];
let scope = &ctx.scopes[scope_id];
let symbol_id = scope.symbols()[extern_function.declared_name()];
let symbol_id = scope
.get_symbol_id(extern_function.declared_name())
.expect(&format!(
"extern function's symbol_id could not be found for {}",
extern_function.declared_name()
));
ctx.nodes_to_symbols
.insert(extern_function.node_id(), symbol_id);
}
@ -154,7 +164,12 @@ fn resolve_names_parameter(parameter: &Parameter, ctx: &mut NameResolutionContex
// point this parameter's node_id to the symbol_id for the parameter symbol
let scope_id = ctx.nodes_to_scopes[&parameter.node_id()];
let scope = &ctx.scopes[scope_id];
let symbol_id = scope.symbols()[parameter.declared_name()];
let symbol_id = scope
.get_symbol_id(parameter.declared_name())
.expect(&format!(
"parameter's symbol_id could not be found for {}",
parameter.declared_name()
));
ctx.nodes_to_symbols.insert(parameter.node_id(), symbol_id);
}
@ -201,9 +216,7 @@ fn resolve_names_let_statement(
ctx.symbols_mut().push(symbol);
let symbol_id = ctx.symbols().len() - 1;
let scope = &mut ctx.scopes_mut()[scope_id];
scope
.symbols_mut()
.insert(let_statement.declared_name_owned(), symbol_id);
scope.insert_symbol(let_statement.declared_name_owned(), symbol_id);
// associate the let statement with the symbol id
ctx.nodes_to_symbols
.insert(let_statement.node_id(), symbol_id);
@ -301,7 +314,7 @@ fn resolve_names_identifier(
let mut maybe_scope = Some(&ctx.scopes()[scope_id]);
let mut found_symbol_id: Option<SymbolId> = None;
while let Some(scope) = maybe_scope {
let maybe_symbol_id = scope.symbols().get(identifier.name()).cloned(); // cloned because ctx cannot be borrowed both immutably and mutably
let maybe_symbol_id = scope.get_symbol_id(identifier.name()); // cloned because ctx cannot be borrowed both immutably and mutably
match maybe_symbol_id {
None => {
maybe_scope = match scope.parent_id() {

View File

@ -21,11 +21,11 @@ impl Scope {
self.parent_id
}
pub fn symbols(&self) -> &HashMap<Rc<str>, SymbolId> {
&self.symbols
pub fn insert_symbol(&mut self, declared_name: Rc<str>, symbol_id: SymbolId) {
self.symbols.insert(declared_name, symbol_id);
}
pub fn symbols_mut(&mut self) -> &mut HashMap<Rc<str>, SymbolId> {
&mut self.symbols
pub fn get_symbol_id(&self, declared_name: &str) -> Option<SymbolId> {
self.symbols.get(declared_name).cloned()
}
}