This commit is contained in:
numzero 2025-04-19 12:05:17 +03:00
parent 6c1e6d6bfa
commit b8477148b6

View File

@ -68,6 +68,18 @@ impl Scope {
.collect(), .collect(),
} }
} }
fn lookup(&self, name: &ast::Ident) -> Option<Ident> {
self.scope.get(name).copied()
}
fn add_upvalue(&mut self, name: &ast::Ident, up_ident: Ident) -> Ident {
let index = self.upvalues.len();
self.upvalues.push((name.clone(), up_ident));
let ident = Ident::Upvalue(index);
self.scope.insert(name.clone(), ident);
ident
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -78,16 +90,11 @@ struct BuildContext<'a> {
impl BuildContext<'_> { impl BuildContext<'_> {
fn request(&self, name: &ast::Ident) -> Option<Ident> { fn request(&self, name: &ast::Ident) -> Option<Ident> {
let mut scope = self.scope.borrow_mut(); if let Some(ident) = self.scope.borrow().lookup(name) {
if let Some(ident) = scope.scope.get(name) { return Some(ident);
return Some(*ident);
} }
if let Some(ident) = self.parent.as_ref()?.request(name) { if let Some(ident) = self.parent.as_ref()?.request(name) {
let index = scope.upvalues.len(); return Some(self.scope.borrow_mut().add_upvalue(name, ident));
scope.upvalues.push((name.clone(), ident));
let ident = Ident::Upvalue(index);
scope.scope.insert(name.clone(), ident);
return Some(ident);
} }
None None
} }