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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crateFunctions;
use crateOperation;
use ;
/// Represents a parser derived from the grammar specified in "grammar.pest".
///
/// This parser is used to parse mathematical expressions based on the rules defined
/// in the external grammar file. The parsing rules are determined by the provided grammar.
;
/// A type alias for the set of rules defined in the grammar.
///
/// This type makes it easier to refer to the specific rules within the grammar, enhancing code clarity.
pub type EquationRule = Rule;
/// Constructs and returns a `PrattParser` tailored for parsing arithmetic expressions.
///
/// The parser is configured to recognize addition, subtraction, multiplication, and division
/// as left-associative operations based on the rules specified in "grammar.pest".
///
/// # Returns
///
/// Returns a `PrattParser<Rule>` instance with pre-configured operations based on the provided grammar.
/// Evaluates the primary component of an arithmetic expression.
///
/// This function checks the rule associated with a primary component of the arithmetic expression
/// and evaluates it accordingly. For instance, if the rule represents an integer, it converts the
/// string representation to an actual integer. If the rule represents another nested expression, it
/// recursively evaluates the nested expression.
///
/// # Arguments
///
/// * `primary` - A `Pair<Rule>` representing a primary component of the arithmetic expression.
///
/// # Returns
///
/// Returns the integer result of evaluating the primary component.
///
/// # Panics
///
/// This function will panic if an unrecognized rule is encountered, indicating an unexpected token in the expression.
/// Parses and evaluates a trigonometric function call based on the given rule pairs.
///
/// This function processes pairs representing a function call where the first pair
/// is expected to be the function name (e.g., "sin", "cos", "tan") and the subsequent
/// pairs represent the argument(s) to the function. The function argument is expected
/// to be an angle in degrees, and the function returns the integer result of the
/// trigonometric calculation.
///
/// # Arguments
///
/// * `pairs` - A mutable iterator of `Pairs<Rule>` where the first pair represents
/// the function name and the subsequent pairs represent the function's argument(s).
///
/// # Returns
///
/// Returns the result of the trigonometric function call as an `f64`.
/// The return value is truncated from a floating point representation.
///
/// # Panics
///
/// The function will panic if:
///
/// * The first pair does not represent a recognized function name.
/// * There are unexpected or missing pairs for the function call.
/// Evaluates an infix arithmetic operation given the left-hand side value (`lhs`),
/// the operation itself (`op`), and the right-hand side value (`rhs`).
///
/// This function matches against known operations (`add`, `subtract`, `multiply`, `divide`)
/// and applies the corresponding arithmetic operation. If an unrecognized operation is encountered,
/// the function will panic with an "unreachable" message.
///
/// # Arguments
///
/// * `lhs` - The left-hand side operand of the arithmetic expression.
/// * `op` - The infix arithmetic operation to be applied.
/// * `rhs` - The right-hand side operand of the arithmetic expression.
///
/// # Returns
///
/// Returns the result of the arithmetic operation as an `f64`.
///
/// # Panics
///
/// This function will panic if the provided `op` does not match a recognized arithmetic operation.
/// Evaluates a prefix operation on an arithmetic value.
///
/// This function processes recognized unary prefix operations on the provided right-hand side value (`rhs`).
/// Currently, it handles unary negation (`unary_minus`). If an unrecognized operation is encountered,
/// the function will panic.
///
/// # Arguments
///
/// * `op` - A `Pair<Rule>` representing the prefix operation to be applied.
/// * `rhs` - The right-hand side operand of the arithmetic expression. In the context of prefix operations,
/// it's the value the operation is applied to.
///
/// # Returns
///
/// Returns the result of the prefix operation as an `f64`.
///
/// # Panics
///
/// This function will panic if the provided `op` does not match a recognized prefix operation.
/// Recursively evaluates an expression provided as a `Pair` of `Rule`.
///
/// This function handles different arithmetic operations and their precedence according to the provided grammar.
///
/// # Arguments
///
/// * `pair` - A `Pair<Rule>` representing part or all of an arithmetic expression.
///
/// # Returns
///
/// * `f64` - The result of evaluating the expression.