cl-interpret: Upvars 2.0

- Only captures locals
This commit is contained in:
John 2025-01-28 06:13:38 -06:00
parent 0eef6b910c
commit fd361f2bea
3 changed files with 15 additions and 3 deletions

View File

@ -134,6 +134,18 @@ impl Environment {
}
Err(Error::NotDefined(id))
}
pub(crate) fn get_local(&self, id: Sym) -> IResult<ConValue> {
for (frame, _) in self.frames.iter().rev() {
match frame.get(&id) {
Some(Some(var)) => return Ok(var.clone()),
Some(None) => return Err(Error::NotInitialized(id)),
_ => (),
}
}
Err(Error::NotInitialized(id))
}
/// Inserts a new [ConValue] into this [Environment]
pub fn insert(&mut self, id: Sym, value: Option<ConValue>) {
if let Some((frame, _)) = self.frames.last_mut() {

View File

@ -34,7 +34,7 @@ impl Function {
pub fn upvars(&self) -> Ref<Upvars> {
self.upvars.borrow()
}
pub fn lift_upvars(&self, env: &mut Environment) {
pub fn lift_upvars(&self, env: &Environment) {
let upvars = collect_upvars(&self.decl, env);
if let Ok(mut self_upvars) = self.upvars.try_borrow_mut() {
*self_upvars = upvars;

View File

@ -28,7 +28,7 @@ impl<'env> CollectUpvars<'env> {
if blacklist.contains(name) || upvars.contains_key(name) {
return;
}
if let Ok(upvar) = env.get(*name) {
if let Ok(upvar) = env.get_local(*name) {
upvars.insert(*name, Some(upvar));
}
}
@ -61,7 +61,7 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
self.visit_expr(init)
}
// a bound name can never be an upvar
self.blacklist.insert(*name);
self.bind_name(name);
}
fn visit_function(&mut self, f: &'a cl_ast::Function) {