2022-04-23 23:43:06 +02:00
use crate ::lexer ::TokenKind ;
/// Error that occured during parsing or evaluation.
#[ derive(Debug, Clone, PartialEq) ]
pub enum KalkError {
CannotIndexByImaginary ,
CanOnlyIndexX ,
Expected ( String ) ,
ExpectedDx ,
ExpectedIf ,
ExpectedReal ,
IncompatibleTypesForOperation ( String , String , String ) ,
IncompatibleVectorsMatrixes ,
IncorrectAmountOfArguments ( usize , String , usize ) ,
IncorrectAmountOfIndexes ( usize , usize ) ,
ItemOfIndexDoesNotExist ( Vec < usize > ) ,
InconsistentColumnWidths ,
2023-09-15 17:24:38 +02:00
InvalidBase ,
2022-04-23 23:43:06 +02:00
InvalidComprehension ( String ) ,
InvalidNumberLiteral ( String ) ,
InvalidOperator ,
InvalidUnit ,
2023-07-19 02:21:17 +02:00
StackOverflow ,
2022-04-23 23:43:06 +02:00
TimedOut ,
VariableReferencesItself ,
PiecewiseConditionsAreFalse ,
EvaluationError ( String ) ,
2022-06-01 00:51:01 +02:00
UnexpectedToken ( TokenKind , Option < TokenKind > ) ,
2022-04-23 23:43:06 +02:00
UnexpectedType ( String , Vec < String > ) ,
UndefinedFn ( String ) ,
UndefinedVar ( String ) ,
UnableToInvert ( String ) ,
UnableToSolveEquation ,
UnableToOverrideConstant ( String ) ,
UnableToParseExpression ,
UnrecognizedBase ,
Unknown ,
2022-04-24 23:57:46 +02:00
WasStmt ( crate ::ast ::Stmt ) ,
2022-04-23 23:43:06 +02:00
}
impl ToString for KalkError {
fn to_string ( & self ) -> String {
match self {
KalkError ::CannotIndexByImaginary = > String ::from ( " Cannot index by imaginary numbers. " ) ,
KalkError ::CanOnlyIndexX = > String ::from ( " Indexing (getting an item with a specific index) is only possible on vectors and matrices. " ) ,
KalkError ::Expected ( description ) = > format! ( " Expected: {} " , description ) ,
KalkError ::ExpectedDx = > String ::from ( " Expected eg. dx, to specify for which variable the operation is being done to. Example with integration: ∫(0, 1, x dx) or ∫(0, 1, x, dx). You may need to put parenthesis around the expression before dx/dy/du/etc. " ) ,
KalkError ::ExpectedIf = > String ::from ( " Expected 'if', with a condition after it. " ) ,
KalkError ::ExpectedReal = > String ::from ( " Expected a real value but got imaginary. " ) ,
KalkError ::IncompatibleTypesForOperation ( operation , got1 , got2 ) = > format! ( " Incompatible types for operation ' {} ': {} and {} . " , operation , got1 , got2 ) ,
KalkError ::IncompatibleVectorsMatrixes = > String ::from ( " Incompatible vectors/matrixes. " ) ,
KalkError ::IncorrectAmountOfArguments ( expected , func , got ) = > format! (
" Expected {} arguments for function {}, but got {}. " ,
expected , func , got
) ,
KalkError ::IncorrectAmountOfIndexes ( expected , got ) = > format! (
" Expected {} indexes but got {}. " ,
expected , got
) ,
KalkError ::ItemOfIndexDoesNotExist ( indices ) = > format! ( " Item of index ⟦ {} ⟧ does not exist. " , indices . iter ( ) . map ( | x | x . to_string ( ) ) . collect ::< Vec < String > > ( ) . join ( " , " ) ) ,
KalkError ::InconsistentColumnWidths = > String ::from ( " Inconsistent column widths. Matrix columns must be the same size. " ) ,
2023-09-15 17:24:38 +02:00
KalkError ::InvalidBase = > String ::from ( " Invalid base. " ) ,
2022-04-23 23:43:06 +02:00
KalkError ::InvalidComprehension ( x ) = > format! ( " Invalid comprehension: {} " , x ) ,
KalkError ::InvalidNumberLiteral ( x ) = > format! ( " Invalid number literal: ' {} '. " , x ) ,
KalkError ::InvalidOperator = > String ::from ( " Invalid operator. " ) ,
KalkError ::InvalidUnit = > String ::from ( " Invalid unit. " ) ,
2023-07-19 02:21:17 +02:00
KalkError ::StackOverflow = > String ::from ( " Operation recursed too deeply. " ) ,
2022-04-23 23:43:06 +02:00
KalkError ::TimedOut = > String ::from ( " Operation took too long. " ) ,
KalkError ::VariableReferencesItself = > String ::from ( " Variable references itself. " ) ,
KalkError ::PiecewiseConditionsAreFalse = > String ::from ( " All the conditions in the piecewise are false. " ) ,
KalkError ::EvaluationError ( msg ) = > format! ( " Evaluation error: {} " , msg ) ,
KalkError ::UnexpectedToken ( got , expected ) = > {
2022-06-01 00:51:01 +02:00
if let Some ( expected ) = expected {
format! ( " Unexpected token: ' {:?} ', expected ' {:?} '. " , got , expected )
} else {
format! ( " Unexpected token: ' {:?} '. " , got )
}
2022-04-23 23:43:06 +02:00
}
KalkError ::UnexpectedType ( got , expected ) = > {
format! ( " Unexpected type. Got {:?} but expected: {:?} . " , got , expected . join ( " , " ) )
}
KalkError ::UnableToInvert ( msg ) = > format! ( " Unable to invert: {} " , msg ) ,
KalkError ::UndefinedFn ( name ) = > format! ( " Undefined function: ' {} '. " , name ) ,
KalkError ::UndefinedVar ( name ) = > format! ( " Undefined variable: ' {} '. " , name ) ,
KalkError ::UnableToParseExpression = > String ::from ( " Unable to parse expression. " ) ,
KalkError ::UnableToSolveEquation = > String ::from ( " Unable to solve equation. " ) ,
KalkError ::UnableToOverrideConstant ( name ) = > format! ( " Unable to override constant: ' {} '. " , name ) ,
KalkError ::UnrecognizedBase = > String ::from ( " Unrecognized base. " ) ,
2022-04-24 23:57:46 +02:00
KalkError ::Unknown | KalkError ::WasStmt ( _ ) = > String ::from ( " Unknown error. " ) ,
2022-04-23 23:43:06 +02:00
}
}
}