cl-interpret: Upvars 2.0
- Only captures locals
This commit is contained in:
parent
0eef6b910c
commit
fd361f2bea
@ -134,6 +134,18 @@ impl Environment {
|
|||||||
}
|
}
|
||||||
Err(Error::NotDefined(id))
|
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]
|
/// Inserts a new [ConValue] into this [Environment]
|
||||||
pub fn insert(&mut self, id: Sym, value: Option<ConValue>) {
|
pub fn insert(&mut self, id: Sym, value: Option<ConValue>) {
|
||||||
if let Some((frame, _)) = self.frames.last_mut() {
|
if let Some((frame, _)) = self.frames.last_mut() {
|
||||||
|
@ -34,7 +34,7 @@ impl Function {
|
|||||||
pub fn upvars(&self) -> Ref<Upvars> {
|
pub fn upvars(&self) -> Ref<Upvars> {
|
||||||
self.upvars.borrow()
|
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);
|
let upvars = collect_upvars(&self.decl, env);
|
||||||
if let Ok(mut self_upvars) = self.upvars.try_borrow_mut() {
|
if let Ok(mut self_upvars) = self.upvars.try_borrow_mut() {
|
||||||
*self_upvars = upvars;
|
*self_upvars = upvars;
|
||||||
|
@ -28,7 +28,7 @@ impl<'env> CollectUpvars<'env> {
|
|||||||
if blacklist.contains(name) || upvars.contains_key(name) {
|
if blacklist.contains(name) || upvars.contains_key(name) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let Ok(upvar) = env.get(*name) {
|
if let Ok(upvar) = env.get_local(*name) {
|
||||||
upvars.insert(*name, Some(upvar));
|
upvars.insert(*name, Some(upvar));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,7 +61,7 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
|
|||||||
self.visit_expr(init)
|
self.visit_expr(init)
|
||||||
}
|
}
|
||||||
// a bound name can never be an upvar
|
// 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) {
|
fn visit_function(&mut self, f: &'a cl_ast::Function) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user