1
0

Remove useless function call "name" argument

This commit is contained in:
Jeff 2024-01-17 15:12:37 -05:00
parent f04adfc661
commit 7642b23553
5 changed files with 12 additions and 31 deletions

View File

@ -88,13 +88,13 @@ impl AbstractTree for FunctionCall {
} }
fn run(&self, source: &str, context: &Map) -> Result<Value> { fn run(&self, source: &str, context: &Map) -> Result<Value> {
let (name, value) = match &self.function_expression { let value = match &self.function_expression {
FunctionExpression::Identifier(identifier) => { FunctionExpression::Identifier(identifier) => {
let key = identifier.inner(); let key = identifier.inner();
let variables = context.variables()?; let variables = context.variables()?;
if let Some((value, _)) = variables.get(key) { if let Some((value, _)) = variables.get(key) {
(Some(key.clone()), value.clone()) value.clone()
} else { } else {
return Err(Error::FunctionIdentifierNotFound( return Err(Error::FunctionIdentifierNotFound(
identifier.inner().clone(), identifier.inner().clone(),
@ -102,11 +102,11 @@ impl AbstractTree for FunctionCall {
} }
} }
FunctionExpression::FunctionCall(function_call) => { 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::Value(value_node) => value_node.run(source, context)?,
FunctionExpression::Index(index) => (None, index.run(source, context)?), FunctionExpression::Index(index) => index.run(source, context)?,
FunctionExpression::Yield(r#yield) => (None, r#yield.run(source, context)?), FunctionExpression::Yield(r#yield) => r#yield.run(source, context)?,
}; };
let mut arguments = Vec::with_capacity(self.arguments.len()); let mut arguments = Vec::with_capacity(self.arguments.len());
@ -117,7 +117,7 @@ impl AbstractTree for FunctionCall {
arguments.push(value); 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<Type> { fn expected_type(&self, context: &Map) -> Result<Type> {

View File

@ -54,13 +54,7 @@ impl FunctionNode {
} }
} }
pub fn call( pub fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result<Value> {
&self,
name: Option<String>,
arguments: &[Value],
source: &str,
outer_context: &Map,
) -> Result<Value> {
self.context.clone_complex_values_from(outer_context)?; self.context.clone_complex_values_from(outer_context)?;
let parameter_argument_pairs = self.parameters.iter().zip(arguments.iter()); let parameter_argument_pairs = self.parameters.iter().zip(arguments.iter());
@ -71,13 +65,6 @@ impl FunctionNode {
self.context.set(key, value.clone())?; 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)?; let return_value = self.body.run(source, &self.context)?;
Ok(return_value) Ok(return_value)

View File

@ -298,7 +298,7 @@ impl StringFunction {
string.retain(|char| { string.retain(|char| {
predicate predicate
.call(None, &[Value::string(char)], _source, _outer_context) .call(&[Value::string(char)], _source, _outer_context)
.unwrap() .unwrap()
.as_boolean() .as_boolean()
.unwrap() .unwrap()

View File

@ -11,19 +11,13 @@ pub enum Function {
} }
impl Function { impl Function {
pub fn call( pub fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result<Value> {
&self,
name: Option<String>,
arguments: &[Value],
source: &str,
outer_context: &Map,
) -> Result<Value> {
match self { match self {
Function::BuiltIn(built_in_function) => { Function::BuiltIn(built_in_function) => {
built_in_function.call(arguments, source, outer_context) built_in_function.call(arguments, source, outer_context)
} }
Function::ContextDefined(context_defined_function) => { Function::ContextDefined(context_defined_function) => {
context_defined_function.call(name, arguments, source, outer_context) context_defined_function.call(arguments, source, outer_context)
} }
} }
} }

View File

@ -154,7 +154,7 @@ impl Value {
} }
pub fn is_function(&self) -> bool { 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`. /// Borrows the value stored in `self` as `&String`, or returns `Err` if `self` is not a `Value::String`.