From 38ffd9b01b59a85b33935cf9d388cfa08cee4f28 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 24 Jun 2024 10:26:38 -0400 Subject: [PATCH] Add validations --- dust-lang/src/abstract_tree/function_call.rs | 33 ++++++++++---------- dust-lang/src/error.rs | 14 ++++----- dust-lang/src/lib.rs | 16 ++++++++-- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/dust-lang/src/abstract_tree/function_call.rs b/dust-lang/src/abstract_tree/function_call.rs index 5de24a0..ec9a419 100644 --- a/dust-lang/src/abstract_tree/function_call.rs +++ b/dust-lang/src/abstract_tree/function_call.rs @@ -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) => {} } diff --git a/dust-lang/src/error.rs b/dust-lang/src/error.rs index de04628..3e614b4 100644 --- a/dust-lang/src/error.rs +++ b/dust-lang/src/error.rs @@ -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, }, - WrongTypeArgumentCount { - expected: usize, - actual: usize, + WrongTypeArguments { + parameters: Vec, + arguments: Vec, }, - WrongArguments { - expected: Vec, - actual: Vec, + WrongValueArguments { + parameters: Vec<(Identifier, Type)>, + arguments: Vec, }, VariableNotFound { identifier: Identifier, diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index 9d25111..8001a50 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -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 } => {