Skip to content

Commit e24a723

Browse files
committed
Refactor checks for symbol in register_name
1 parent b8cb2df commit e24a723

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

compiler/src/symboltable.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -951,10 +951,8 @@ impl SymbolTableBuilder {
951951
let table = self.tables.last_mut().unwrap();
952952
let location = Default::default();
953953

954-
// Some checks:
955-
let containing = table.symbols.contains_key(name);
956-
if containing {
957-
let symbol = table.symbols.get(name).unwrap();
954+
// Some checks for the symbol that present on this scope level:
955+
if let Some(symbol) = table.symbols.get(name) {
958956
// Role already set..
959957
match role {
960958
SymbolUsage::Global => {
@@ -1000,23 +998,21 @@ impl SymbolTableBuilder {
1000998
// Ok?
1001999
}
10021000
}
1003-
}
1004-
1005-
// Some more checks:
1006-
match role {
1007-
SymbolUsage::Nonlocal if scope_depth < 2 => {
1008-
return Err(SymbolTableError {
1009-
error: format!("cannot define nonlocal '{}' at top level.", name),
1010-
location,
1011-
})
1012-
}
1013-
_ => {
1014-
// Ok!
1001+
} else {
1002+
// The symbol does not present on this scope level.
1003+
// Some checks to insert new symbol into symbol table:
1004+
match role {
1005+
SymbolUsage::Nonlocal if scope_depth < 2 => {
1006+
return Err(SymbolTableError {
1007+
error: format!("cannot define nonlocal '{}' at top level.", name),
1008+
location,
1009+
})
1010+
}
1011+
_ => {
1012+
// Ok!
1013+
}
10151014
}
1016-
}
1017-
1018-
// Insert symbol when required:
1019-
if !containing {
1015+
// Insert symbol when required:
10201016
let symbol = Symbol::new(name);
10211017
table.symbols.insert(name.to_owned(), symbol);
10221018
}

0 commit comments

Comments
 (0)