Resolver - Split resolve_local() into specific methods
This commit is contained in:
parent
24d9d6d880
commit
06917ca2d6
1 changed files with 44 additions and 36 deletions
|
@ -106,19 +106,37 @@ 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,
|
let mut i = self.scopes.len();
|
||||||
expr_id: &usize,
|
while i != 0 {
|
||||||
name: &Token,
|
i -= 1;
|
||||||
from_assignment: bool,
|
if let Some(info) = self.scopes[i].get_mut(&name.lexeme as &str) {
|
||||||
) -> ResolverResult {
|
if info.state == SymState::Declared {
|
||||||
|
return Err(SloxError::with_token(
|
||||||
|
ErrorKind::Parse,
|
||||||
|
name,
|
||||||
|
"symbol accessed before definition".to_owned(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
info.state = SymState::Used;
|
||||||
|
self.mark_resolved(expr_id, i);
|
||||||
|
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();
|
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.kind != SymKind::Variable {
|
if info.kind != SymKind::Variable {
|
||||||
return Err(SloxError::with_token(
|
return Err(SloxError::with_token(
|
||||||
ErrorKind::Parse,
|
ErrorKind::Parse,
|
||||||
|
@ -129,30 +147,20 @@ impl ResolverState {
|
||||||
if info.state == SymState::Declared {
|
if info.state == SymState::Declared {
|
||||||
info.state = SymState::Defined;
|
info.state = SymState::Defined;
|
||||||
}
|
}
|
||||||
} else {
|
self.mark_resolved(expr_id, i);
|
||||||
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);
|
|
||||||
}
|
|
||||||
return Ok(());
|
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 } => {
|
||||||
|
|
Loading…
Reference in a new issue