1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ParseError {
#[error("Got unexpected token: '{token}'")]
UnexpectedToken {
token: String,
},
#[error("Parentheses didn't match")]
MismatchedParentheses,
#[error("Expected something that wasn't found: {expected}")]
Expected {
expected: Expected,
},
}
#[derive(Debug, Error)]
pub enum MathError {
#[error("Variable '{name}' is not defined")]
UndefinedVariable {
name: String,
},
#[error("Function '{name}' is not defined")]
UndefinedFunction {
name: String,
},
#[error("A function was passed incorrect arguments")]
IncorrectArguments,
#[error("Attempted to divide by zero")]
DivideByZero,
#[error("A NaN value was attempted to be used as an operand")]
NaN,
#[error("Tried to compare a value that can't be compared (eg NaN, Infinity, etc.)")]
CmpError,
#[error("The operation '{op}' is not supported for the type {num_type}")]
Unimplemented {
op: String,
num_type: String,
},
#[error("An unknown error occurred during evaluation")]
Other,
}
#[derive(Debug, Error)]
pub enum EvalError {
#[error("Failed to parse the expression: {error}")]
ParseError {
error: ParseError,
},
#[error("Failed to evaluate the expression: {error}")]
MathError {
error: MathError,
},
}
impl From<ParseError> for EvalError {
fn from(t: ParseError) -> EvalError {
EvalError::ParseError { error: t }
}
}
impl From<MathError> for EvalError {
fn from(t: MathError) -> EvalError {
EvalError::MathError { error: t }
}
}
#[derive(Debug, Error)]
pub enum Expected {
#[error("Expected another operator")]
Operator,
#[error("Expected another expression")]
Expression,
#[error("Expected a parenthesis")]
Paren,
#[error("Expected a function")]
Function,
}