Resolver - Errors on undefined symbols
This commit is contained in:
parent
06917ca2d6
commit
4bce85e14b
1 changed files with 20 additions and 15 deletions
|
@ -114,19 +114,14 @@ impl ResolverState {
|
||||||
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 info.state == SymState::Declared {
|
if info.state == SymState::Declared {
|
||||||
return Err(SloxError::with_token(
|
return self.error(name, "symbol accessed before definition");
|
||||||
ErrorKind::Parse,
|
|
||||||
name,
|
|
||||||
"symbol accessed before definition".to_owned(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
info.state = SymState::Used;
|
info.state = SymState::Used;
|
||||||
self.mark_resolved(expr_id, i);
|
self.mark_resolved(expr_id, i);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// XXX not found !
|
self.symbol_not_found(name)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve a symbol when it is being assigned to. If the symbol is local,
|
/// Resolve a symbol when it is being assigned to. If the symbol is local,
|
||||||
|
@ -138,11 +133,7 @@ impl ResolverState {
|
||||||
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 info.kind != SymKind::Variable {
|
if info.kind != SymKind::Variable {
|
||||||
return Err(SloxError::with_token(
|
return self.error(name, "cannot assign to this symbol");
|
||||||
ErrorKind::Parse,
|
|
||||||
name,
|
|
||||||
"cannot assign to this symbol".to_owned(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
if info.state == SymState::Declared {
|
if info.state == SymState::Declared {
|
||||||
info.state = SymState::Defined;
|
info.state = SymState::Defined;
|
||||||
|
@ -151,17 +142,31 @@ impl ResolverState {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// XXX not found !
|
self.symbol_not_found(name)
|
||||||
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) {
|
||||||
// Only mark symbols as locals if we're not at the top-level scope.
|
// Only mark symbols as locals if we're not at the top-level scope.
|
||||||
if depth != 0 {
|
if depth != 0 {
|
||||||
self.resolved.insert(*expr_id, self.scopes.len() - 1 - depth);
|
self.resolved
|
||||||
|
.insert(*expr_id, self.scopes.len() - 1 - depth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return an error corresponding to an undeclared symbol.
|
||||||
|
fn symbol_not_found(&mut self, name: &Token) -> ResolverResult {
|
||||||
|
self.error(name, "undeclared symbol")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return an error.
|
||||||
|
fn error(&mut self, name: &Token, message: &str) -> ResolverResult {
|
||||||
|
Err(SloxError::with_token(
|
||||||
|
ErrorKind::Parse,
|
||||||
|
name,
|
||||||
|
message.to_owned(),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Process a function declaration.
|
/// Process a function declaration.
|
||||||
|
|
Loading…
Reference in a new issue