mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 04:12:42 +00:00
fixed closures
This commit is contained in:
parent
d040d92052
commit
671f7d5306
6 changed files with 106 additions and 72 deletions
|
|
@ -182,12 +182,19 @@ impl Eval for Expr {
|
|||
param_names,
|
||||
closure_vars,
|
||||
body,
|
||||
} => Ok(Value::function(LoxFunction::new(
|
||||
name.clone(),
|
||||
env.collect_closure(closure_vars),
|
||||
param_names.clone(),
|
||||
body.as_ref().clone(),
|
||||
))),
|
||||
} => {
|
||||
let name = name.clone();
|
||||
let closure = env.collect_closure(closure_vars);
|
||||
let param_names = param_names.clone();
|
||||
let body = body.as_ref().clone();
|
||||
|
||||
Ok(Value::function(LoxFunction::new(
|
||||
name,
|
||||
closure,
|
||||
param_names,
|
||||
body,
|
||||
)))
|
||||
}
|
||||
Expr::Class {
|
||||
superclass,
|
||||
name,
|
||||
|
|
@ -315,7 +322,7 @@ pub fn call_fun(fun: Rc<LoxFunction>, env: &mut Environment) -> EvalResult<Value
|
|||
|
||||
env.enter_scope()?;
|
||||
|
||||
env.define(fun.name(), Value::Function(fun.clone()));
|
||||
env.define(fun.name(), Value::Function(Rc::clone(&fun)));
|
||||
|
||||
env.insert_closure(fun.closure().clone());
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue