1
0

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

View File

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

View File

@ -451,10 +451,20 @@ impl InterpreterError {
), ),
), ),
), ),
ValidationError::WrongArguments { .. } => todo!(), ValidationError::WrongTypeArguments {
ValidationError::WrongTypeArgumentCount { expected, actual } => { parameters,
arguments,
} => {
builder = builder.with_message(format!( 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 } => { ValidationError::ExpectedIntegerFloatOrString { actual, position } => {