Resolver - Handle the global namespace as well
This commit is contained in:
parent
c971df6f60
commit
24d9d6d880
1 changed files with 15 additions and 12 deletions
|
@ -14,7 +14,9 @@ pub type ResolvedVariables = HashMap<usize, usize>;
|
||||||
/// Resolve all variables in a program's AST.
|
/// Resolve all variables in a program's AST.
|
||||||
pub fn resolve_variables(program: &ast::ProgramNode) -> SloxResult<ResolvedVariables> {
|
pub fn resolve_variables(program: &ast::ProgramNode) -> SloxResult<ResolvedVariables> {
|
||||||
let mut state = ResolverState::default();
|
let mut state = ResolverState::default();
|
||||||
program.resolve(&mut state).map(|_| state.resolved)
|
state
|
||||||
|
.with_scope(|rs| program.resolve(rs))
|
||||||
|
.map(|_| state.resolved)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResolverResult = SloxResult<()>;
|
type ResolverResult = SloxResult<()>;
|
||||||
|
@ -70,9 +72,7 @@ impl ResolverState {
|
||||||
/// Try to declare a symbol. If the scope already contains a declaration
|
/// Try to declare a symbol. If the scope already contains a declaration
|
||||||
/// for the same name, return an error.
|
/// for the same name, return an error.
|
||||||
fn declare(&mut self, name: &Token, kind: SymKind) -> ResolverResult {
|
fn declare(&mut self, name: &Token, kind: SymKind) -> ResolverResult {
|
||||||
if self.scopes.is_empty() {
|
assert!(!self.scopes.is_empty());
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
let idx = self.scopes.len() - 1;
|
let idx = self.scopes.len() - 1;
|
||||||
let scope = &mut self.scopes[idx];
|
let scope = &mut self.scopes[idx];
|
||||||
if scope.contains_key(&name.lexeme as &str) {
|
if scope.contains_key(&name.lexeme as &str) {
|
||||||
|
@ -96,7 +96,7 @@ impl ResolverState {
|
||||||
/// Mark a symbol as defined. If the symbol has already been defined or
|
/// Mark a symbol as defined. If the symbol has already been defined or
|
||||||
/// used, its state isn't affected.
|
/// used, its state isn't affected.
|
||||||
fn define(&mut self, name: &Token) {
|
fn define(&mut self, name: &Token) {
|
||||||
if !self.scopes.is_empty() {
|
assert!(!self.scopes.is_empty());
|
||||||
let idx = self.scopes.len() - 1;
|
let idx = self.scopes.len() - 1;
|
||||||
let top = &mut self.scopes[idx];
|
let top = &mut self.scopes[idx];
|
||||||
if let Some(info) = top.get_mut(&name.lexeme as &str) {
|
if let Some(info) = top.get_mut(&name.lexeme as &str) {
|
||||||
|
@ -105,7 +105,6 @@ impl ResolverState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Try to resolve some access to a symbol. If a local symbol is found
|
/// Try to resolve some access to a symbol. If a local symbol is found
|
||||||
/// matching the specified name, add it to the resolution map.
|
/// matching the specified name, add it to the resolution map.
|
||||||
|
@ -140,7 +139,11 @@ impl ResolverState {
|
||||||
}
|
}
|
||||||
info.state = SymState::Used;
|
info.state = SymState::Used;
|
||||||
}
|
}
|
||||||
|
// Only mark symbols as locals if we're not at the top-level
|
||||||
|
// scope.
|
||||||
|
if i != 0 {
|
||||||
self.mark_resolved(expr_id, self.scopes.len() - 1 - i);
|
self.mark_resolved(expr_id, self.scopes.len() - 1 - i);
|
||||||
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue