Interpreter - Fixed closures

* Environment for function execution is now based on their
    declaration.
  * I had missed that part while skimming over the book.
This commit is contained in:
Emmanuel BENOîT 2023-01-07 08:59:13 +01:00
parent ec70ded29e
commit 363cdab86f
2 changed files with 12 additions and 12 deletions
src/interpreter

View file

@ -38,7 +38,7 @@ impl<'a> InterpreterState<'a> {
}
/// Create a child state.
fn create_child<'b>(parent: &InterpreterState<'b>) -> Self
pub(super) fn create_child<'b>(parent: &InterpreterState<'b>) -> Self
where
'b: 'a,
{
@ -209,7 +209,7 @@ impl ast::StmtNode {
params: &[Token],
body: &[ast::StmtNode],
) -> InterpreterResult {
let fun = Function::new(Some(name), params, body);
let fun = Function::new(Some(name), params, body, es.environment.clone());
es.environment
.borrow_mut()
.define(name, Some(Value::Callable(fun)))?;
@ -336,7 +336,7 @@ impl Interpretable for ast::ExprNode {
arguments,
} => self.on_call(es, callee, right_paren, arguments),
ast::ExprNode::Lambda { params, body } => {
Ok(Value::Callable(Function::new(None, params, body)).into())
Ok(Value::Callable(Function::new(None, params, body, es.environment.clone())).into())
}
}
}