Refactor checks for symbol in register_name

This commit is contained in:
minoring 2020-09-06 11:40:32 +09:00
parent 678b8e779b
commit 09e4e9a86c

View file

@ -951,10 +951,8 @@ impl SymbolTableBuilder {
let table = self.tables.last_mut().unwrap(); let table = self.tables.last_mut().unwrap();
let location = Default::default(); let location = Default::default();
// Some checks: // Some checks for the symbol that present on this scope level:
let containing = table.symbols.contains_key(name); if let Some(symbol) = table.symbols.get(name) {
if containing {
let symbol = table.symbols.get(name).unwrap();
// Role already set.. // Role already set..
match role { match role {
SymbolUsage::Global => { SymbolUsage::Global => {
@ -1000,23 +998,21 @@ impl SymbolTableBuilder {
// Ok? // Ok?
} }
} }
} } else {
// The symbol does not present on this scope level.
// Some more checks: // Some checks to insert new symbol into symbol table:
match role { match role {
SymbolUsage::Nonlocal if scope_depth < 2 => { SymbolUsage::Nonlocal if scope_depth < 2 => {
return Err(SymbolTableError { return Err(SymbolTableError {
error: format!("cannot define nonlocal '{}' at top level.", name), error: format!("cannot define nonlocal '{}' at top level.", name),
location, location,
}) })
}
_ => {
// Ok!
}
} }
_ => { // Insert symbol when required:
// Ok!
}
}
// Insert symbol when required:
if !containing {
let symbol = Symbol::new(name); let symbol = Symbol::new(name);
table.symbols.insert(name.to_owned(), symbol); table.symbols.insert(name.to_owned(), symbol);
} }