2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-01-06 03:40:58 +00:00
|
|
|
use crate::{
|
2024-02-12 23:55:54 +00:00
|
|
|
built_in_functions::Callable,
|
2024-01-31 18:51:48 +00:00
|
|
|
error::{RuntimeError, SyntaxError, ValidationError},
|
2024-02-13 13:10:34 +00:00
|
|
|
AbstractTree, Context, Expression, Format, Function, FunctionExpression, SourcePosition,
|
|
|
|
SyntaxNode, Type, Value,
|
2024-01-06 03:40:58 +00:00
|
|
|
};
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2024-01-30 23:19:05 +00:00
|
|
|
/// A function being invoked and the arguments it is being passed.
|
2023-10-06 17:32:58 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
2023-11-28 22:54:17 +00:00
|
|
|
pub struct FunctionCall {
|
2023-12-29 23:59:15 +00:00
|
|
|
function_expression: FunctionExpression,
|
2023-11-28 22:54:17 +00:00
|
|
|
arguments: Vec<Expression>,
|
2024-02-01 00:07:18 +00:00
|
|
|
syntax_position: SourcePosition,
|
2024-02-12 22:55:45 +00:00
|
|
|
|
|
|
|
#[serde(skip)]
|
|
|
|
context: Context,
|
2023-11-28 22:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionCall {
|
2024-01-30 23:19:05 +00:00
|
|
|
/// Returns a new FunctionCall.
|
2024-01-06 03:40:58 +00:00
|
|
|
pub fn new(
|
|
|
|
function_expression: FunctionExpression,
|
|
|
|
arguments: Vec<Expression>,
|
2024-02-01 00:07:18 +00:00
|
|
|
syntax_position: SourcePosition,
|
2024-02-12 22:55:45 +00:00
|
|
|
context: Context,
|
2024-01-06 03:40:58 +00:00
|
|
|
) -> Self {
|
2023-11-28 22:54:17 +00:00
|
|
|
Self {
|
2023-12-02 07:34:23 +00:00
|
|
|
function_expression,
|
2023-11-28 22:54:17 +00:00
|
|
|
arguments,
|
2024-01-06 03:40:58 +00:00
|
|
|
syntax_position,
|
2024-02-12 22:55:45 +00:00
|
|
|
context,
|
2023-11-28 22:54:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-09 19:54:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 17:32:58 +00:00
|
|
|
impl AbstractTree for FunctionCall {
|
2024-02-11 00:31:47 +00:00
|
|
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
2024-02-01 00:35:27 +00:00
|
|
|
SyntaxError::expect_syntax_node(source, "function_call", node)?;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-12-30 01:14:03 +00:00
|
|
|
let function_node = node.child(0).unwrap();
|
2024-01-10 20:03:52 +00:00
|
|
|
let function_expression = FunctionExpression::from_syntax(function_node, source, context)?;
|
2023-11-28 22:54:17 +00:00
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
let mut arguments = Vec::new();
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-11-15 00:38:19 +00:00
|
|
|
for index in 2..node.child_count() - 1 {
|
2023-11-30 03:54:46 +00:00
|
|
|
let child = node.child(index).unwrap();
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-11-30 03:54:46 +00:00
|
|
|
if child.is_named() {
|
2024-01-10 20:03:52 +00:00
|
|
|
let expression = Expression::from_syntax(child, source, context)?;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-10-11 16:07:30 +00:00
|
|
|
arguments.push(expression);
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
2023-12-12 23:21:16 +00:00
|
|
|
Ok(FunctionCall {
|
2023-12-02 07:34:23 +00:00
|
|
|
function_expression,
|
2023-11-28 22:54:17 +00:00
|
|
|
arguments,
|
2024-01-06 03:40:58 +00:00
|
|
|
syntax_position: node.range().into(),
|
2024-02-12 22:55:45 +00:00
|
|
|
context: Context::new(),
|
2023-12-12 23:21:16 +00:00
|
|
|
})
|
2023-11-28 22:54:17 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 00:31:47 +00:00
|
|
|
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError> {
|
2024-01-31 18:51:48 +00:00
|
|
|
match &self.function_expression {
|
|
|
|
FunctionExpression::Identifier(identifier) => {
|
|
|
|
let identifier_type = identifier.expected_type(context)?;
|
|
|
|
|
|
|
|
if let Type::Function {
|
|
|
|
parameter_types: _,
|
|
|
|
return_type,
|
|
|
|
} = &identifier_type
|
|
|
|
{
|
|
|
|
Ok(*return_type.clone())
|
|
|
|
} else {
|
|
|
|
Ok(identifier_type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FunctionExpression::FunctionCall(function_call) => function_call.expected_type(context),
|
|
|
|
FunctionExpression::Value(value_node) => {
|
|
|
|
let value_type = value_node.expected_type(context)?;
|
|
|
|
|
|
|
|
if let Type::Function { return_type, .. } = value_type {
|
|
|
|
Ok(*return_type)
|
|
|
|
} else {
|
|
|
|
Ok(value_type)
|
|
|
|
}
|
|
|
|
}
|
2024-02-14 23:46:05 +00:00
|
|
|
FunctionExpression::Index(index) => {
|
|
|
|
let index_type = index.expected_type(context)?;
|
|
|
|
|
|
|
|
if let Type::Function { return_type, .. } = index_type {
|
|
|
|
Ok(*return_type)
|
|
|
|
} else {
|
|
|
|
Ok(index_type)
|
|
|
|
}
|
|
|
|
}
|
2024-01-31 18:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 00:31:47 +00:00
|
|
|
fn validate(&self, _source: &str, context: &Context) -> Result<(), ValidationError> {
|
2024-01-04 00:57:06 +00:00
|
|
|
let function_expression_type = self.function_expression.expected_type(context)?;
|
2024-01-06 01:02:29 +00:00
|
|
|
|
2024-02-13 13:10:34 +00:00
|
|
|
let parameter_types = if let Type::Function {
|
|
|
|
parameter_types, ..
|
|
|
|
} = function_expression_type
|
|
|
|
{
|
|
|
|
parameter_types
|
|
|
|
} else {
|
|
|
|
return Err(ValidationError::TypeCheckExpectedFunction {
|
|
|
|
actual: function_expression_type,
|
|
|
|
position: self.syntax_position,
|
|
|
|
});
|
2024-01-04 00:57:06 +00:00
|
|
|
};
|
|
|
|
|
2024-01-25 09:45:25 +00:00
|
|
|
if self.arguments.len() != parameter_types.len() {
|
2024-02-01 00:07:18 +00:00
|
|
|
return Err(ValidationError::ExpectedFunctionArgumentAmount {
|
2024-01-25 09:45:25 +00:00
|
|
|
expected: parameter_types.len(),
|
2024-01-04 00:57:06 +00:00
|
|
|
actual: self.arguments.len(),
|
2024-02-01 00:07:18 +00:00
|
|
|
position: self.syntax_position,
|
|
|
|
});
|
2024-01-10 01:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (index, expression) in self.arguments.iter().enumerate() {
|
2024-02-01 00:07:18 +00:00
|
|
|
if let Some(expected) = parameter_types.get(index) {
|
|
|
|
let actual = expression.expected_type(context)?;
|
|
|
|
|
|
|
|
if !expected.accepts(&actual) {
|
|
|
|
return Err(ValidationError::TypeCheck {
|
|
|
|
expected: expected.clone(),
|
|
|
|
actual,
|
|
|
|
position: self.syntax_position,
|
|
|
|
});
|
|
|
|
}
|
2024-01-10 01:38:40 +00:00
|
|
|
}
|
2024-01-04 00:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-11 00:31:47 +00:00
|
|
|
fn run(&self, source: &str, context: &Context) -> Result<Value, RuntimeError> {
|
2024-01-17 20:12:37 +00:00
|
|
|
let value = match &self.function_expression {
|
2023-12-29 23:59:15 +00:00
|
|
|
FunctionExpression::Identifier(identifier) => {
|
2024-02-15 20:20:29 +00:00
|
|
|
if let Some(value) = context.get_value(identifier)? {
|
|
|
|
self.context.set_value(identifier.clone(), value.clone())?;
|
2024-01-17 20:12:37 +00:00
|
|
|
value.clone()
|
2023-12-02 07:34:23 +00:00
|
|
|
} else {
|
2024-02-15 20:20:29 +00:00
|
|
|
return Err(RuntimeError::VariableIdentifierNotFound(identifier.clone()));
|
2023-12-02 07:34:23 +00:00
|
|
|
}
|
2023-11-18 01:10:07 +00:00
|
|
|
}
|
2023-12-29 23:59:15 +00:00
|
|
|
FunctionExpression::FunctionCall(function_call) => {
|
2024-01-17 20:12:37 +00:00
|
|
|
function_call.run(source, context)?
|
2023-12-29 23:59:15 +00:00
|
|
|
}
|
2024-01-17 20:12:37 +00:00
|
|
|
FunctionExpression::Value(value_node) => value_node.run(source, context)?,
|
|
|
|
FunctionExpression::Index(index) => index.run(source, context)?,
|
2023-11-30 16:05:09 +00:00
|
|
|
};
|
2024-02-12 22:55:45 +00:00
|
|
|
let function = value.as_function()?;
|
|
|
|
|
2024-02-12 23:55:54 +00:00
|
|
|
match function {
|
2024-02-13 13:10:34 +00:00
|
|
|
Function::BuiltIn(built_in_function) => {
|
2024-02-12 23:55:54 +00:00
|
|
|
let mut arguments = Vec::with_capacity(self.arguments.len());
|
2023-12-18 00:06:36 +00:00
|
|
|
|
2024-02-12 23:55:54 +00:00
|
|
|
for expression in &self.arguments {
|
|
|
|
let value = expression.run(source, context)?;
|
|
|
|
|
|
|
|
arguments.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
built_in_function.call(&arguments, source, &self.context)
|
|
|
|
}
|
2024-02-13 13:10:34 +00:00
|
|
|
Function::ContextDefined(function_node) => {
|
2024-02-12 23:55:54 +00:00
|
|
|
let parameter_expression_pairs =
|
|
|
|
function_node.parameters().iter().zip(self.arguments.iter());
|
2023-12-18 00:06:36 +00:00
|
|
|
|
2024-02-12 23:55:54 +00:00
|
|
|
for (identifier, expression) in parameter_expression_pairs {
|
|
|
|
let value = expression.run(source, context)?;
|
|
|
|
|
2024-02-15 20:20:29 +00:00
|
|
|
self.context.set_value(identifier.clone(), value)?;
|
2024-02-12 23:55:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function_node.call(source, &self.context)
|
|
|
|
}
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-06 10:00:36 +00:00
|
|
|
|
2024-01-06 13:11:09 +00:00
|
|
|
impl Format for FunctionCall {
|
|
|
|
fn format(&self, output: &mut String, indent_level: u8) {
|
|
|
|
self.function_expression.format(output, indent_level);
|
|
|
|
output.push('(');
|
2024-01-06 10:00:36 +00:00
|
|
|
|
2024-01-06 13:11:09 +00:00
|
|
|
for expression in &self.arguments {
|
|
|
|
expression.format(output, indent_level);
|
2024-01-06 10:00:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 13:11:09 +00:00
|
|
|
output.push(')');
|
2024-01-06 10:00:36 +00:00
|
|
|
}
|
|
|
|
}
|