2024-03-09 12:34:34 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::{AbstractTree, Action, Expression, Type};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct FunctionCall {
|
|
|
|
function: Box<Expression>,
|
|
|
|
arguments: Vec<Expression>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionCall {
|
|
|
|
pub fn new(function: Expression, arguments: Vec<Expression>) -> Self {
|
|
|
|
FunctionCall {
|
|
|
|
function: Box::new(function),
|
|
|
|
arguments,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for FunctionCall {
|
|
|
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
2024-03-09 13:10:54 +00:00
|
|
|
if let Type::Function { return_type, .. } = self.function.expected_type(_context)? {
|
|
|
|
Ok(*return_type)
|
|
|
|
} else {
|
|
|
|
Err(ValidationError::ExpectedFunction)
|
|
|
|
}
|
2024-03-09 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
2024-03-09 13:10:54 +00:00
|
|
|
if let Type::Function { .. } = self.function.expected_type(_context)? {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ValidationError::ExpectedFunction)
|
|
|
|
}
|
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-12 01:57:27 +00:00
|
|
|
let value = self.function.run(context)?.as_return_value()?;
|
2024-03-09 13:10:54 +00:00
|
|
|
let function = value.as_function()?;
|
|
|
|
let mut arguments = Vec::with_capacity(self.arguments.len());
|
|
|
|
|
|
|
|
for expression in self.arguments {
|
2024-03-12 01:57:27 +00:00
|
|
|
let value = expression.run(context)?.as_return_value()?;
|
2024-03-09 13:10:54 +00:00
|
|
|
|
|
|
|
arguments.push(value);
|
|
|
|
}
|
|
|
|
|
2024-03-11 18:51:02 +00:00
|
|
|
let function_context = Context::inherit_data_from(context)?;
|
2024-03-09 13:10:54 +00:00
|
|
|
|
2024-03-17 05:26:05 +00:00
|
|
|
function.clone().call(arguments, function_context)
|
2024-03-09 12:34:34 +00:00
|
|
|
}
|
|
|
|
}
|