From 7642b2355302c3f8ffe09976bf195af7ed9cbefe Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 17 Jan 2024 15:12:37 -0500 Subject: [PATCH] Remove useless function call "name" argument --- src/abstract_tree/function_call.rs | 14 +++++++------- src/abstract_tree/function_node.rs | 15 +-------------- src/built_in_functions/string.rs | 2 +- src/value/function.rs | 10 ++-------- src/value/mod.rs | 2 +- 5 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/abstract_tree/function_call.rs b/src/abstract_tree/function_call.rs index e55778b..2ecd926 100644 --- a/src/abstract_tree/function_call.rs +++ b/src/abstract_tree/function_call.rs @@ -88,13 +88,13 @@ impl AbstractTree for FunctionCall { } fn run(&self, source: &str, context: &Map) -> Result { - let (name, value) = match &self.function_expression { + let value = match &self.function_expression { FunctionExpression::Identifier(identifier) => { let key = identifier.inner(); let variables = context.variables()?; if let Some((value, _)) = variables.get(key) { - (Some(key.clone()), value.clone()) + value.clone() } else { return Err(Error::FunctionIdentifierNotFound( identifier.inner().clone(), @@ -102,11 +102,11 @@ impl AbstractTree for FunctionCall { } } FunctionExpression::FunctionCall(function_call) => { - (None, function_call.run(source, context)?) + function_call.run(source, context)? } - FunctionExpression::Value(value_node) => (None, value_node.run(source, context)?), - FunctionExpression::Index(index) => (None, index.run(source, context)?), - FunctionExpression::Yield(r#yield) => (None, r#yield.run(source, context)?), + FunctionExpression::Value(value_node) => value_node.run(source, context)?, + FunctionExpression::Index(index) => index.run(source, context)?, + FunctionExpression::Yield(r#yield) => r#yield.run(source, context)?, }; let mut arguments = Vec::with_capacity(self.arguments.len()); @@ -117,7 +117,7 @@ impl AbstractTree for FunctionCall { arguments.push(value); } - value.as_function()?.call(name, &arguments, source, context) + value.as_function()?.call(&arguments, source, context) } fn expected_type(&self, context: &Map) -> Result { diff --git a/src/abstract_tree/function_node.rs b/src/abstract_tree/function_node.rs index 3d9e683..ca38adc 100644 --- a/src/abstract_tree/function_node.rs +++ b/src/abstract_tree/function_node.rs @@ -54,13 +54,7 @@ impl FunctionNode { } } - pub fn call( - &self, - name: Option, - arguments: &[Value], - source: &str, - outer_context: &Map, - ) -> Result { + pub fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result { self.context.clone_complex_values_from(outer_context)?; let parameter_argument_pairs = self.parameters.iter().zip(arguments.iter()); @@ -71,13 +65,6 @@ impl FunctionNode { self.context.set(key, value.clone())?; } - if let Some(name) = name { - self.context.set( - name, - Value::Function(Function::ContextDefined(self.clone())), - )?; - } - let return_value = self.body.run(source, &self.context)?; Ok(return_value) diff --git a/src/built_in_functions/string.rs b/src/built_in_functions/string.rs index 495218e..e5baf89 100644 --- a/src/built_in_functions/string.rs +++ b/src/built_in_functions/string.rs @@ -298,7 +298,7 @@ impl StringFunction { string.retain(|char| { predicate - .call(None, &[Value::string(char)], _source, _outer_context) + .call(&[Value::string(char)], _source, _outer_context) .unwrap() .as_boolean() .unwrap() diff --git a/src/value/function.rs b/src/value/function.rs index b1ae934..c95973b 100644 --- a/src/value/function.rs +++ b/src/value/function.rs @@ -11,19 +11,13 @@ pub enum Function { } impl Function { - pub fn call( - &self, - name: Option, - arguments: &[Value], - source: &str, - outer_context: &Map, - ) -> Result { + pub fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result { match self { Function::BuiltIn(built_in_function) => { built_in_function.call(arguments, source, outer_context) } Function::ContextDefined(context_defined_function) => { - context_defined_function.call(name, arguments, source, outer_context) + context_defined_function.call(arguments, source, outer_context) } } } diff --git a/src/value/mod.rs b/src/value/mod.rs index 5026bfa..1079183 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -154,7 +154,7 @@ impl Value { } pub fn is_function(&self) -> bool { - matches!(self, Value::Map(_)) + matches!(self, Value::Function(_)) } /// Borrows the value stored in `self` as `&String`, or returns `Err` if `self` is not a `Value::String`.