From 09e4e9a86cf74f800e25c3eb1d0f7afbbffa2cab Mon Sep 17 00:00:00 2001 From: minoring Date: Sun, 6 Sep 2020 11:40:32 +0900 Subject: [PATCH] Refactor checks for symbol in register_name --- src/symboltable.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/symboltable.rs b/src/symboltable.rs index 4e7fbef..7d35f78 100644 --- a/src/symboltable.rs +++ b/src/symboltable.rs @@ -951,10 +951,8 @@ impl SymbolTableBuilder { let table = self.tables.last_mut().unwrap(); let location = Default::default(); - // Some checks: - let containing = table.symbols.contains_key(name); - if containing { - let symbol = table.symbols.get(name).unwrap(); + // Some checks for the symbol that present on this scope level: + if let Some(symbol) = table.symbols.get(name) { // Role already set.. match role { SymbolUsage::Global => { @@ -1000,23 +998,21 @@ impl SymbolTableBuilder { // Ok? } } - } - - // Some more checks: - match role { - SymbolUsage::Nonlocal if scope_depth < 2 => { - return Err(SymbolTableError { - error: format!("cannot define nonlocal '{}' at top level.", name), - location, - }) + } else { + // The symbol does not present on this scope level. + // Some checks to insert new symbol into symbol table: + match role { + SymbolUsage::Nonlocal if scope_depth < 2 => { + return Err(SymbolTableError { + error: format!("cannot define nonlocal '{}' at top level.", name), + location, + }) + } + _ => { + // Ok! + } } - _ => { - // Ok! - } - } - - // Insert symbol when required: - if !containing { + // Insert symbol when required: let symbol = Symbol::new(name); table.symbols.insert(name.to_owned(), symbol); }