Resolver - Refactored declare()

This commit is contained in:
Emmanuel BENOîT 2023-01-07 10:53:05 +01:00
parent 8d65c288a3
commit c971df6f60

View file

@ -70,26 +70,27 @@ 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() { if self.scopes.is_empty() {
let idx = self.scopes.len() - 1; return Ok(());
let scope = &mut self.scopes[idx]; }
if scope.contains_key(&name.lexeme as &str) { let idx = self.scopes.len() - 1;
return Err(SloxError::with_token( let scope = &mut self.scopes[idx];
ErrorKind::Parse, if scope.contains_key(&name.lexeme as &str) {
name, Err(SloxError::with_token(
"already a symbol with this name in this scope".to_owned(), ErrorKind::Parse,
)); name,
} else { "already a symbol with this name in this scope".to_owned(),
scope.insert( ))
name.lexeme.clone(), } else {
SymInfo { scope.insert(
kind, name.lexeme.clone(),
state: SymState::Declared, SymInfo {
}, kind,
); state: SymState::Declared,
} },
);
Ok(())
} }
Ok(())
} }
/// 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