2024-03-09 12:34:34 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
2024-03-17 20:59:52 +00:00
|
|
|
value::ValueInner,
|
2024-03-09 12:34:34 +00:00
|
|
|
};
|
|
|
|
|
2024-03-17 11:31:45 +00:00
|
|
|
use super::{AbstractTree, Action, Expression, Type, WithPosition};
|
2024-03-09 12:34:34 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct FunctionCall {
|
2024-03-17 11:31:45 +00:00
|
|
|
function: Box<WithPosition<Expression>>,
|
|
|
|
arguments: Vec<WithPosition<Expression>>,
|
2024-03-09 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionCall {
|
2024-03-17 11:31:45 +00:00
|
|
|
pub fn new(
|
|
|
|
function: WithPosition<Expression>,
|
|
|
|
arguments: Vec<WithPosition<Expression>>,
|
|
|
|
) -> Self {
|
2024-03-09 12:34:34 +00:00
|
|
|
FunctionCall {
|
|
|
|
function: Box::new(function),
|
|
|
|
arguments,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for FunctionCall {
|
|
|
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
2024-03-17 20:59:52 +00:00
|
|
|
let function_node_type = self.function.node.expected_type(_context)?;
|
|
|
|
|
|
|
|
if let Type::Function { return_type, .. } = function_node_type {
|
2024-03-09 13:10:54 +00:00
|
|
|
Ok(*return_type)
|
|
|
|
} else {
|
2024-03-17 20:59:52 +00:00
|
|
|
Err(ValidationError::ExpectedFunction {
|
|
|
|
actual: function_node_type,
|
|
|
|
position: self.function.position,
|
|
|
|
})
|
2024-03-09 13:10:54 +00:00
|
|
|
}
|
2024-03-09 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
2024-03-17 20:59:52 +00:00
|
|
|
let function_node_type = self.function.node.expected_type(_context)?;
|
|
|
|
|
|
|
|
if let Type::Function { .. } = function_node_type {
|
2024-03-09 13:10:54 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-03-17 20:59:52 +00:00
|
|
|
Err(ValidationError::ExpectedFunction {
|
|
|
|
actual: function_node_type,
|
|
|
|
position: self.function.position,
|
|
|
|
})
|
2024-03-09 13:10:54 +00:00
|
|
|
}
|
2024-03-09 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 18:48:53 +00:00
|
|
|
fn run(self, context: &Context) -> Result<Action, RuntimeError> {
|
2024-03-17 10:26:12 +00:00
|
|
|
let value = self.function.node.run(context)?.as_return_value()?;
|
2024-03-17 20:59:52 +00:00
|
|
|
let function = if let ValueInner::Function(function) = value.inner().as_ref() {
|
|
|
|
function
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
|
|
|
ValidationError::ExpectedFunction {
|
|
|
|
actual: value.r#type(),
|
|
|
|
position: self.function.position,
|
|
|
|
},
|
|
|
|
));
|
|
|
|
};
|
2024-03-09 13:10:54 +00:00
|
|
|
let mut arguments = Vec::with_capacity(self.arguments.len());
|
|
|
|
|
|
|
|
for expression in self.arguments {
|
2024-03-17 10:26:12 +00:00
|
|
|
let value = expression.node.run(context)?.as_return_value()?;
|
2024-03-09 13:10:54 +00:00
|
|
|
|
|
|
|
arguments.push(value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 17:36:31 +00:00
|
|
|
let function_context = Context::new();
|
2024-03-09 13:10:54 +00:00
|
|
|
|
2024-03-17 17:36:31 +00:00
|
|
|
function_context.inherit_data_from(&context)?;
|
2024-03-17 05:26:05 +00:00
|
|
|
function.clone().call(arguments, function_context)
|
2024-03-09 12:34:34 +00:00
|
|
|
}
|
|
|
|
}
|