Resolver - Split resolve_local() into specific methods

This commit is contained in:
Emmanuel BENOîT 2023-01-07 11:06:22 +01:00
parent 24d9d6d880
commit 06917ca2d6

View file

@ -106,53 +106,61 @@ impl ResolverState {
} }
} }
/// Try to resolve some access to a symbol. If a local symbol is found /// Resolve a symbol when it is being used. If the symbol is local,
/// matching the specified name, add it to the resolution map. /// the lookup distance will be stored to the resolution map.
fn resolve_local( fn resolve_use(&mut self, expr_id: &usize, name: &Token) -> ResolverResult {
&mut self,
expr_id: &usize,
name: &Token,
from_assignment: bool,
) -> ResolverResult {
let mut i = self.scopes.len(); let mut i = self.scopes.len();
while i != 0 { while i != 0 {
i -= 1; i -= 1;
if let Some(info) = self.scopes[i].get_mut(&name.lexeme as &str) { if let Some(info) = self.scopes[i].get_mut(&name.lexeme as &str) {
if from_assignment { if info.state == SymState::Declared {
if info.kind != SymKind::Variable { return Err(SloxError::with_token(
return Err(SloxError::with_token( ErrorKind::Parse,
ErrorKind::Parse, name,
name, "symbol accessed before definition".to_owned(),
"cannot assign to this symbol".to_owned(), ));
));
}
if info.state == SymState::Declared {
info.state = SymState::Defined;
}
} else {
if info.state == SymState::Declared {
return Err(SloxError::with_token(
ErrorKind::Parse,
name,
"symbol accessed before definition".to_owned(),
));
}
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);
} }
info.state = SymState::Used;
self.mark_resolved(expr_id, i);
return Ok(()); return Ok(());
} }
} }
// XXX not found !
Ok(())
}
/// Resolve a symbol when it is being assigned to. If the symbol is local,
/// the lookup distance will be stored to the resolution map. Trying to
/// assign to something that isn't a variable will cause an error.
fn resolve_assignment(&mut self, expr_id: &usize, name: &Token) -> ResolverResult {
let mut i = self.scopes.len();
while i != 0 {
i -= 1;
if let Some(info) = self.scopes[i].get_mut(&name.lexeme as &str) {
if info.kind != SymKind::Variable {
return Err(SloxError::with_token(
ErrorKind::Parse,
name,
"cannot assign to this symbol".to_owned(),
));
}
if info.state == SymState::Declared {
info.state = SymState::Defined;
}
self.mark_resolved(expr_id, i);
return Ok(());
}
}
// XXX not found !
Ok(()) Ok(())
} }
/// Add an entry to the resolution map for an AST node. /// Add an entry to the resolution map for an AST node.
fn mark_resolved(&mut self, expr_id: &usize, depth: usize) { fn mark_resolved(&mut self, expr_id: &usize, depth: usize) {
self.resolved.insert(*expr_id, depth); // Only mark symbols as locals if we're not at the top-level scope.
if depth != 0 {
self.resolved.insert(*expr_id, self.scopes.len() - 1 - depth);
}
} }
} }
@ -266,11 +274,11 @@ impl VarResolver for ast::StmtNode {
impl VarResolver for ast::ExprNode { impl VarResolver for ast::ExprNode {
fn resolve(&self, rs: &mut ResolverState) -> ResolverResult { fn resolve(&self, rs: &mut ResolverState) -> ResolverResult {
match self { match self {
ast::ExprNode::Variable { name, id } => rs.resolve_local(id, name, false), ast::ExprNode::Variable { name, id } => rs.resolve_use(id, name),
ast::ExprNode::Assignment { name, value, id } => { ast::ExprNode::Assignment { name, value, id } => {
value.resolve(rs)?; value.resolve(rs)?;
rs.resolve_local(id, name, true) rs.resolve_assignment(id, name)
} }
ast::ExprNode::Lambda { params, body } => { ast::ExprNode::Lambda { params, body } => {