From 85e9c7db386c92062ddb6b2586d9c2300d0474cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 8 Jan 2023 07:58:56 +0100 Subject: [PATCH] Interpreter - Callables now receive the callee value as an argument --- src/interpreter/callable.rs | 2 +- src/interpreter/functions.rs | 7 ++++++- src/interpreter/interpretable.rs | 2 +- src/interpreter/native_fn.rs | 7 ++++++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/interpreter/callable.rs b/src/interpreter/callable.rs index d6f9e59..bb00afc 100644 --- a/src/interpreter/callable.rs +++ b/src/interpreter/callable.rs @@ -11,7 +11,7 @@ pub trait Callable: Debug + ToString { /// Run the callable in the execution environment with the specified /// arguments. - fn call(&self, environment: &mut InterpreterState, arguments: Vec) -> SloxResult; + fn call(&self, callee: &Value, environment: &mut InterpreterState, arguments: Vec) -> SloxResult; } /// A reference to a callable. diff --git a/src/interpreter/functions.rs b/src/interpreter/functions.rs index 826903e..d45c654 100644 --- a/src/interpreter/functions.rs +++ b/src/interpreter/functions.rs @@ -39,7 +39,12 @@ impl Callable for Function { self.params.len() } - fn call(&self, es: &mut InterpreterState, arguments: Vec) -> SloxResult { + fn call( + &self, + _callee: &Value, + es: &mut InterpreterState, + arguments: Vec, + ) -> SloxResult { assert_eq!(arguments.len(), self.arity()); let param_env = InterpreterState { environment: Environment::create_child(&self.env), diff --git a/src/interpreter/interpretable.rs b/src/interpreter/interpretable.rs index 7144a6b..c938077 100644 --- a/src/interpreter/interpretable.rs +++ b/src/interpreter/interpretable.rs @@ -505,7 +505,7 @@ impl ExprNode { ), )) } else { - Ok(callable.call(es, arg_values)?.into()) + Ok(callable.call(&callee, es, arg_values)?.into()) } } else { error(right_paren, "can only call functions and classes") diff --git a/src/interpreter/native_fn.rs b/src/interpreter/native_fn.rs index a0e8719..b868bd1 100644 --- a/src/interpreter/native_fn.rs +++ b/src/interpreter/native_fn.rs @@ -20,7 +20,12 @@ impl Callable for Clock { 0 } - fn call(&self, _environment: &mut InterpreterState, _arguments: Vec) -> SloxResult { + fn call( + &self, + _callee: &Value, + _environment: &mut InterpreterState, + _arguments: Vec, + ) -> SloxResult { let now = SystemTime::now(); let since_epoch = now .duration_since(UNIX_EPOCH)