Add validations

This commit is contained in:
Jeff 2024-06-24 10:26:38 -04:00
parent a5c5075e6b
commit 38ffd9b01b
3 changed files with 37 additions and 26 deletions

View File

@ -1,3 +1,4 @@
use chumsky::container::Container;
use serde::{Deserialize, Serialize};
use crate::{
@ -76,11 +77,11 @@ impl AbstractNode for FunctionCall {
} = function_node_type
{
match (type_parameters, &self.type_arguments) {
(Some(type_parameters), Some(type_arguments)) => {
if type_parameters.len() != type_arguments.len() {
return Err(ValidationError::WrongTypeArgumentCount {
actual: type_parameters.len(),
expected: type_arguments.len(),
(Some(parameters), Some(type_arguments)) => {
if parameters.len() != type_arguments.len() {
return Err(ValidationError::WrongTypeArguments {
arguments: type_arguments.clone(),
parameters: parameters.clone(),
});
}
}
@ -90,23 +91,23 @@ impl AbstractNode for FunctionCall {
match (value_parameters, &self.value_arguments) {
(Some(parameters), Some(arguments)) => {
if parameters.len() != arguments.len() {
return Err(ValidationError::WrongTypeArgumentCount {
actual: parameters.len(),
expected: arguments.len(),
return Err(ValidationError::WrongValueArguments {
parameters,
arguments: arguments.clone(),
});
}
}
(Some(parameters), None) => {
return Err(ValidationError::WrongTypeArgumentCount {
expected: parameters.len(),
actual: 0,
})
return Err(ValidationError::WrongValueArguments {
parameters,
arguments: Vec::with_capacity(0),
});
}
(None, Some(arguments)) => {
return Err(ValidationError::WrongTypeArgumentCount {
expected: 0,
actual: arguments.len(),
})
return Err(ValidationError::WrongValueArguments {
parameters: Vec::with_capacity(0),
arguments: arguments.clone(),
});
}
(None, None) => {}
}

View File

@ -3,7 +3,7 @@ use std::{io, sync::PoisonError as StdPoisonError};
use chumsky::{prelude::Rich, span::Span};
use crate::{
abstract_tree::{r#type::Type, SourcePosition, TypeConstructor},
abstract_tree::{r#type::Type, Expression, SourcePosition, TypeConstructor},
identifier::Identifier,
lexer::Token,
};
@ -153,13 +153,13 @@ pub enum ValidationError {
/// The position of the item that gave the "expected" type.
expected_position: Option<SourcePosition>,
},
WrongTypeArgumentCount {
expected: usize,
actual: usize,
WrongTypeArguments {
parameters: Vec<Identifier>,
arguments: Vec<TypeConstructor>,
},
WrongArguments {
expected: Vec<TypeConstructor>,
actual: Vec<TypeConstructor>,
WrongValueArguments {
parameters: Vec<(Identifier, Type)>,
arguments: Vec<Expression>,
},
VariableNotFound {
identifier: Identifier,

View File

@ -451,10 +451,20 @@ impl InterpreterError {
),
),
),
ValidationError::WrongArguments { .. } => todo!(),
ValidationError::WrongTypeArgumentCount { expected, actual } => {
ValidationError::WrongTypeArguments {
parameters,
arguments,
} => {
builder = builder.with_message(format!(
"Expected {expected} arguments but got {actual}."
"Expected {parameters:?} arguments but got {arguments:?}."
));
}
ValidationError::WrongValueArguments {
parameters,
arguments,
} => {
builder = builder.with_message(format!(
"Expected {parameters:?} arguments but got {arguments:?}."
));
}
ValidationError::ExpectedIntegerFloatOrString { actual, position } => {