Refactor binder to simplify root scope logic
The previous implementation of the binder had a slightly complex way of handling the root scope. This commit simplifies that logic by directly checking if the current scope is the root scope and the scope stack has only one element. This makes the code more readable and maintainable.
This commit is contained in:
@@ -70,8 +70,7 @@ impl FunctionCompiler {
|
|||||||
identity: Identity,
|
identity: Identity,
|
||||||
globals: &Rc<RefCell<HashMap<Symbol, (GlobalIdx, Identity)>>>,
|
globals: &Rc<RefCell<HashMap<Symbol, (GlobalIdx, Identity)>>>,
|
||||||
) -> Result<Address, String> {
|
) -> Result<Address, String> {
|
||||||
match self.kind {
|
if self.kind == ScopeKind::Root && self.scopes.len() == 1 {
|
||||||
ScopeKind::Root => {
|
|
||||||
let current_scope = self.scopes.last_mut().unwrap();
|
let current_scope = self.scopes.last_mut().unwrap();
|
||||||
if current_scope.locals.contains_key(name) {
|
if current_scope.locals.contains_key(name) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
@@ -101,8 +100,7 @@ impl FunctionCompiler {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
Ok(addr)
|
Ok(addr)
|
||||||
}
|
} else {
|
||||||
ScopeKind::Local => {
|
|
||||||
let current_scope = self.scopes.last_mut().unwrap();
|
let current_scope = self.scopes.last_mut().unwrap();
|
||||||
if current_scope.locals.contains_key(name) {
|
if current_scope.locals.contains_key(name) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
@@ -123,7 +121,6 @@ impl FunctionCompiler {
|
|||||||
Ok(Address::Local(slot))
|
Ok(Address::Local(slot))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn resolve_local(&self, sym: &Symbol) -> Option<LocalInfo> {
|
fn resolve_local(&self, sym: &Symbol) -> Option<LocalInfo> {
|
||||||
for scope in self.scopes.iter().rev() {
|
for scope in self.scopes.iter().rev() {
|
||||||
|
|||||||
Reference in New Issue
Block a user