Work on better Scope abstraction.
This commit is contained in:
parent
7b76771843
commit
57abc03145
@ -39,8 +39,8 @@ impl<'a> SymbolCollectionContext<'a> {
|
|||||||
pub fn find_symbol_in_scope_for(&self, name: &str, node_id: NodeId) -> Option<&Symbol> {
|
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_id = self.nodes_to_scopes[&node_id];
|
||||||
let scope = &self.scopes[scope_id];
|
let scope = &self.scopes[scope_id];
|
||||||
if let Some(symbol_id) = scope.symbols().get(name) {
|
if let Some(symbol_id) = scope.get_symbol_id(name) {
|
||||||
Some(&self.symbols[*symbol_id])
|
Some(&self.symbols[symbol_id])
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ impl<'a> SymbolCollectionContext<'a> {
|
|||||||
let symbol_id = self.symbols.len() - 1;
|
let symbol_id = self.symbols.len() - 1;
|
||||||
let scope_id = self.nodes_to_scopes[&node_id];
|
let scope_id = self.nodes_to_scopes[&node_id];
|
||||||
let scope = &mut self.scopes[scope_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> {
|
pub fn symbols_mut(&mut self) -> &mut Vec<Symbol> {
|
||||||
|
|||||||
@ -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
|
// 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_id = ctx.nodes_to_scopes[&function.node_id()];
|
||||||
let scope = &ctx.scopes[scope_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);
|
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
|
// 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_id = ctx.nodes_to_scopes[&extern_function.node_id()];
|
||||||
let scope = &ctx.scopes[scope_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
|
ctx.nodes_to_symbols
|
||||||
.insert(extern_function.node_id(), symbol_id);
|
.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
|
// point this parameter's node_id to the symbol_id for the parameter symbol
|
||||||
let scope_id = ctx.nodes_to_scopes[¶meter.node_id()];
|
let scope_id = ctx.nodes_to_scopes[¶meter.node_id()];
|
||||||
let scope = &ctx.scopes[scope_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);
|
ctx.nodes_to_symbols.insert(parameter.node_id(), symbol_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,9 +216,7 @@ fn resolve_names_let_statement(
|
|||||||
ctx.symbols_mut().push(symbol);
|
ctx.symbols_mut().push(symbol);
|
||||||
let symbol_id = ctx.symbols().len() - 1;
|
let symbol_id = ctx.symbols().len() - 1;
|
||||||
let scope = &mut ctx.scopes_mut()[scope_id];
|
let scope = &mut ctx.scopes_mut()[scope_id];
|
||||||
scope
|
scope.insert_symbol(let_statement.declared_name_owned(), symbol_id);
|
||||||
.symbols_mut()
|
|
||||||
.insert(let_statement.declared_name_owned(), symbol_id);
|
|
||||||
// associate the let statement with the symbol id
|
// associate the let statement with the symbol id
|
||||||
ctx.nodes_to_symbols
|
ctx.nodes_to_symbols
|
||||||
.insert(let_statement.node_id(), symbol_id);
|
.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 maybe_scope = Some(&ctx.scopes()[scope_id]);
|
||||||
let mut found_symbol_id: Option<SymbolId> = None;
|
let mut found_symbol_id: Option<SymbolId> = None;
|
||||||
while let Some(scope) = maybe_scope {
|
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 {
|
match maybe_symbol_id {
|
||||||
None => {
|
None => {
|
||||||
maybe_scope = match scope.parent_id() {
|
maybe_scope = match scope.parent_id() {
|
||||||
|
|||||||
@ -21,11 +21,11 @@ impl Scope {
|
|||||||
self.parent_id
|
self.parent_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn symbols(&self) -> &HashMap<Rc<str>, SymbolId> {
|
pub fn insert_symbol(&mut self, declared_name: Rc<str>, symbol_id: SymbolId) {
|
||||||
&self.symbols
|
self.symbols.insert(declared_name, symbol_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn symbols_mut(&mut self) -> &mut HashMap<Rc<str>, SymbolId> {
|
pub fn get_symbol_id(&self, declared_name: &str) -> Option<SymbolId> {
|
||||||
&mut self.symbols
|
self.symbols.get(declared_name).cloned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user