formulac 0.8.0

A complex-number and extensible function supported math expression parser for Rust
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! # astnode/parser.rs
//!
//! Converts a slice of [`Lexeme`]s into an [`AstNode`] tree using the
//! Shunting-Yard algorithm.
//!
//! ## Responsibilities
//! - Tokenizing lexemes and classifying operators as unary or binary
//! - Managing the operator stack (precedence, associativity)
//! - Resolving function calls, user-defined functions, and `diff` expressions
//! - Returning a single root [`AstNode`] or a [`ParseError`]
//!
//! Symbolic differentiation invoked by `diff(...)` is delegated to
//! [`AstNode::differentiate`], which is implemented separately.

use std::rc::Rc;
use std::str::FromStr;

use crate::astnode::AstNode;
use crate::constants::Constants;
use crate::core::Real;
use crate::functions::Arity;
use crate::err::ParseError;
use crate::lexer::{
    Lexeme,
    Span,
};
use crate::operators::{
    BinaryOperatorKind,
    OperatorKind,
    UnaryOperatorKind,
};
use crate::token::{
    Token,
    UserFnTable,
};

impl<T: Real> AstNode<T> {
    /// Parses a slice of `Lexeme`s into an AST using a shunting-yard algorithm.
    pub(crate) fn from(
        lexemes:   &[Lexeme],
        args:      &[&str],
        constants: &Constants<T>,
        users:     &UserFnTable<T>,
    ) -> Result<Self, ParseError>
    where
        T: FromStr,
    {
        let (mut output, mut ops)
            = Self::process_tokens(lexemes, args, constants, users)?;
        Self::finalize_parsing(&mut output, &mut ops)
    }

    // ── shunting-yard helpers ────────────────────────────────────────────────

    fn process_tokens(
        lexemes: &[Lexeme],
        args: &[&str],
        constants: &Constants<T>,
        users: &UserFnTable<T>,
    ) -> Result<(Vec<Self>, Vec<Token<T>>), ParseError>
    where
        T: FromStr,
    {
        let mut output: Vec<Self>  = Vec::new();
        let mut ops:    Vec<Token<T>> = Vec::new();

        // Tracks whether the previous token produced a value,
        // used to distinguish unary from binary operators.
        let mut prev_is_value = false;

        for lexeme in lexemes {
            let token = Token::try_from(lexeme, args, constants, users)?;
            match token {
                Token::Number { value, span } => {
                    output.push(Self::Number { value, span });
                    prev_is_value = true;
                }
                Token::Argument { index, span } => {
                    output.push(Self::Argument { index, span });
                    prev_is_value = true;
                }
                Token::Operator { kind, span } => {
                    if prev_is_value {
                        Self::push_binary_op(&mut output, &mut ops, kind, span)?;
                    } else {
                        Self::push_unary_op(&mut ops, kind, span)?;
                    }
                    prev_is_value = false;
                }
                // Functions and diff are pushed onto the op stack;
                // they are resolved when their closing ')' is encountered.
                Token::DiffOperator { .. } | Token::Function { .. } | Token::UserFunction { .. } => {
                    ops.push(token);
                    prev_is_value = false;
                }
                // '(' resets prev_is_value so the next token
                // (e.g. `-` in `(-x)`) is treated as unary.
                Token::LParen { .. } => {
                    ops.push(token);
                    prev_is_value = false;
                }
                Token::RParen { .. } => {
                    Self::flush_until_lparen(&mut output, &mut ops, lexeme)?;
                    prev_is_value = true;
                }
                Token::Comma { .. } => {
                    Self::flush_until_lparen_keep(&mut output, &mut ops, lexeme)?;
                    prev_is_value = false;
                }
                _ => return Err(ParseError::InvalidFormula {
                    reason: format!("unexpected token from '{}'", lexeme.text()),
                    span: lexeme.span(),
                }),
            }
        }

        Ok((output, ops))
    }

    /// Pushes a unary operator onto the op stack.
    fn push_unary_op(ops: &mut Vec<Token<T>>, kind: OperatorKind, span: Span) -> Result<(), ParseError> {
        let kind = UnaryOperatorKind::try_from(kind)
            .map_err(|_| ParseError::InvalidFormula {
                reason: format!("unknown unary operator '{}'", kind),
                span,
            })?;
        ops.push(Token::UnaryOperator { kind, span });
        Ok(())
    }

    /// Pops higher-precedence binary operators from the stack, then pushes the new one.
    fn push_binary_op(
        output: &mut Vec<Self>,
        ops:    &mut Vec<Token<T>>,
        kind:   OperatorKind,
        span:   Span,
    ) -> Result<(), ParseError> {
        let oper = BinaryOperatorKind::try_from(kind)
            .map_err(|_| ParseError::InvalidFormula {
                reason: format!("unknown binary operator '{}'", kind),
                span,
            })?;

        // Shunting-yard precedence rule.
        while let Some(Token::BinaryOperator { kind: top, span }) = ops.last() {
            let should_pop = if oper.is_left_assoc() {
                top.precedence() >= oper.precedence()
            } else {
                top.precedence() > oper.precedence()
            };
            if !should_pop { break; }
            Self::apply_binary(output, *top, *span)?;
            ops.pop();
        }
        ops.push(Token::BinaryOperator { kind: oper, span });
        Ok(())
    }

    /// Drains the entire op stack at end-of-input.
    fn flush_all(
        output: &mut Vec<Self>,
        ops:    &mut Vec<Token<T>>,
    ) -> Result<(), ParseError> {
        while let Some(token) = ops.pop() {
            match token {
                Token::LParen { span } | Token::RParen { span } => {
                    return Err(ParseError::InvalidFormula {
                        reason: "mismatched parentheses".into(),
                        span
                    });
                }
                t => Self::apply_token(output, t)?,
            }
        }
        Ok(())
    }

    /// Pops and applies operators until a `(` is found, then resolves any
    /// pending function/diff call sitting just below the `(`.
    fn flush_until_lparen(
        output: &mut Vec<Self>,
        ops:    &mut Vec<Token<T>>,
        lex:    &Lexeme,
    ) -> Result<(), ParseError> {
        loop {
            match ops.pop() {
                Some(Token::LParen { .. }) => break,
                Some(t) => Self::apply_token(output, t)?,
                None => return Err(ParseError::InvalidFormula {
                    reason: "mismatched ')'".into(),
                    span: lex.span()
                }),
            }
        }
        // Check for a function/diff call sitting just below the '('.
        if let Some(top) = ops.pop() {
            match top {
                Token::Function { kind, span } => Self::apply_fn(output, kind.arity(), span, |args| Self::FunctionCall { kind, args, span })?,
                Token::UserFunction { func, span } => Self::apply_fn(output, func.arity(), span, |args| Self::UserFunctionCall { func, args, span })?,
                Token::DiffOperator { span } => Self::apply_diff(output, span)?,
                other => ops.push(other), // not a call; put it back
            }
        }
        Ok(())
    }

    /// Like `flush_until_lparen`, but leaves the `(` on the stack (used for `,`).
    fn flush_until_lparen_keep(
        output: &mut Vec<Self>,
        ops:    &mut Vec<Token<T>>,
        lex:    &Lexeme,
    ) -> Result<(), ParseError> {
        loop {
            match ops.last() {
                Some(Token::LParen { .. }) => return Ok(()),
                Some(_) => {
                    let t = ops.pop().unwrap();
                    Self::apply_token(output, t)?;
                }
                None => return Err(ParseError::InvalidFormula {
                    reason: "mismatched ','".into(),
                    span: lex.span(),
                }),
            }
        }
    }

    // ── token application ────────────────────────────────────────────────────

    /// Dispatches a single token to the appropriate `apply_*` function.
    fn apply_token(output: &mut Vec<Self>, token: Token<T>) -> Result<(), ParseError> {
        let span = token.span();
        match token {
            Token::UnaryOperator { kind, span }  => Self::apply_unary(output, kind, span),
            Token::BinaryOperator { kind, span } => Self::apply_binary(output, kind, span),
            Token::Function { kind, span } => Self::apply_fn(output, kind.arity(), span, |args| Self::FunctionCall { kind, args, span }),
            Token::UserFunction { func, span } => Self::apply_fn(output, func.arity(), span, |args| Self::UserFunctionCall { func, args, span }),
            Token::DiffOperator { span }  => Self::apply_diff(output, span),
            other => Err(ParseError::InvalidFormula {
                reason: format!("unexpected token in operator stack: {:?}", other),
                span,
            }),
        }
    }

    fn apply_unary(output: &mut Vec<Self>, op: UnaryOperatorKind, span: Span) -> Result<(), ParseError> {
        let expr = output.pop().ok_or(ParseError::InternalError {
            reason: format!("missing operand for unary '{}'", op),
            span
        })?;
        output.push(Self::UnaryOperator { kind: op, expr: Rc::new(expr), span });
        Ok(())
    }

    fn apply_binary(output: &mut Vec<Self>, op: BinaryOperatorKind, span: Span) -> Result<(), ParseError> {
        let right = output.pop().ok_or(ParseError::MissingRightOperator { operator: op.to_string(), span })?;
        let left  = output.pop().ok_or(ParseError::MissingLeftOperator  { operator: op.to_string(), span })?;
        output.push(Self::BinaryOperator { kind: op, left: Rc::new(left), right: Rc::new(right), span });
        Ok(())
    }

    /// Generic function-application helper shared by built-in and user-defined functions.
    ///
    /// Pops `arity` nodes from `output` in order and calls `make_node` to build the AST node.
    fn apply_fn<F>(
        output:    &mut Vec<Self>,
        arity:     usize,
        span: Span,
        make_node: F,
    ) -> Result<(), ParseError>
    where
        F: FnOnce(Vec<Rc<Self>>) -> Self,
    {
        if output.len() < arity {
            return Err(ParseError::MissingArgs { func: format!("<arity {}>", arity), span });
        }
        let start = output.len() - arity;
        let args  = output.drain(start..).map(Rc::new).collect();
        output.push(make_node(args));
        Ok(())
    }

    fn parse_diff_args(output: &mut Vec<Self>, span: Span) -> Result<(AstNode<T>, usize, usize), ParseError>
    {
        let top = output.pop().ok_or(ParseError::InvalidDerivative {
            span,
            reason: "missing argument (expected variable or order)".into(),
        })?;

        let (var_idx, order) = match top {
            // diff(f, x, n) — explicit order
            Self::Number { value: z, .. } => {
                if !z.im.is_zero() || !z.re.clone().fract().is_zero() {
                    return Err(ParseError::InvalidDerivativeOrder { span, order: format!("{:?}", z) });
                }
                let order = z.re.clone().to_i32();
                if order > i8::MAX as i32 {
                    return Err(ParseError::InvalidDerivativeOrder { span, order: format!("{:?}", z) });
                }
                let var = match output.pop() {
                    Some(Self::Argument { index, .. }) => index,
                    Some(other) => return Err(ParseError::InvalidDerivative {
                        span,
                        reason: format!("expected Argument before order, got {:?}", other),
                    }),
                    None => return Err(ParseError::InvalidDerivative {
                        span,
                        reason: "missing variable before order".into(),
                    }),
                };
                (var, order)
            }
            // diff(f, x) — default order 1
            Self::Argument { index, .. } => (index, 1),

            other => return Err(ParseError::InvalidDerivative {
                span,
                reason: format!("expected Argument or Number, got {:?}", other),
            }),
        };

        let expr = output.pop().ok_or(ParseError::InvalidDerivative {
            span,
            reason: "missing expression to differentiate".into(),
        })?;

        Ok((expr, var_idx, order as usize))
    }

    fn apply_diff(output: &mut Vec<Self>, span: Span) -> Result<(), ParseError> {
        let (mut expr, var, order) = Self::parse_diff_args(output, span)?;

        for _ in 0..order {
            expr = expr.differentiate(var)?;
        }
        output.push(expr);
        Ok(())
    }

    fn finalize_parsing(output: &mut Vec<Self>, ops: &mut Vec<Token<T>>) -> Result<Self, ParseError> {
        // Drain the remaining operator stack.
        Self::flush_all(output, ops)?;

        match output.len() {
            1 => Ok(output.pop().unwrap()),
            0 => Err(ParseError::WrongReturn("no AST node produced".into())),
            _ => Err(ParseError::WrongReturn("too many AST nodes remaining".into())),
        }
    }
}

#[cfg(test)]
mod astnode_tests {
    use std::collections::HashMap;
    use num_complex::Complex;

    use super::*;
    use crate::lexer;
    use crate::functions::{
        FunctionKind,
        UserFn,
    };

    type UserFnTable<T> = HashMap<String, UserFn<T>>;

    macro_rules! assert_astnode_eq {
        ($left:expr, $right:expr) => {{
            fn inner<T: Real>(left: &AstNode<T>, right: &AstNode<T>) {
                let epsilon = 1.0e-12;
                match (left, right) {
                    (AstNode::Number { value: lv, span: ls }, AstNode::Number { value: rv, span: rs }) => {
                        assert!((lv.re.clone() - rv.re.clone()).abs() < T::from_f64(epsilon));
                        assert!((lv.im.clone() - rv.im.clone()).abs() < T::from_f64(epsilon));
                        assert_eq!(ls, rs);
                    }
                    (AstNode::Argument { index: li, span: ls }, AstNode::Argument { index: ri, span: rs }) => {
                        assert_eq!(li, ri);
                        assert_eq!(ls, rs);
                    }
                    (AstNode::UnaryOperator { kind: lk, expr: le, span: ls }, AstNode::UnaryOperator { kind: rk, expr: re, span: rs }) => {
                        assert_eq!(lk, rk);
                        inner(le, re);
                        assert_eq!(ls, rs);
                    }
                    (AstNode::BinaryOperator { kind: lk, left: ll, right: lr, span: ls },
                    AstNode::BinaryOperator { kind: rk, left: rl, right: rr, span: rs }) => {
                        assert_eq!(lk, rk);
                        inner(ll, rl);
                        inner(lr, rr);
                        assert_eq!(ls, rs);
                    }
                    (AstNode::FunctionCall { kind: lk, args: la, span: ls },
                    AstNode::FunctionCall { kind: rk, args: ra, span: rs }) => {
                        assert_eq!(lk, rk);
                        assert_eq!(la.len(), ra.len());
                        for (a, b) in la.iter().zip(ra.iter()) {
                            inner(a, b);
                        }
                        assert_eq!(ls, rs);
                    }
                    (l, r) => panic!("AST nodes differ: left = {:?}, right = {:?}", l, r),
                }
            }
            inner(&$left, &$right);
        }};
    }

    #[test]
    fn single_number_astnode() {
        let lexemes = lexer::from("42");
        let ast = AstNode::from(&lexemes, &[], &Constants::new(), &UserFnTable::new()).unwrap();
        assert_astnode_eq!(ast, AstNode::Number { value: Complex::new(42.0, 0.0), span: Span::from(0..2) })
    }

    #[test]
    fn unary_operator_negative_astnode() {
        let lexemes = lexer::from("- 3");
        let ast = AstNode::from(&lexemes, &[], &Constants::new(), &UserFnTable::new()).unwrap();
        assert_astnode_eq!(ast, AstNode::UnaryOperator {
            kind: UnaryOperatorKind::Negative,
            expr: Rc::new(AstNode::Number { value: Complex::new(3.0, 0.0), span: Span::from(2..3) }),
            span: Span::from(0..1),
        });
    }

    #[test]
    fn binary_operator_precedence_astnode() {
        let lexemes = lexer::from("2 + 3 * 4");
        let ast = AstNode::from(&lexemes, &[], &Constants::new(), &UserFnTable::new()).unwrap();
        // expected: (2 + (3 * 4))
        assert_astnode_eq!(ast, AstNode::BinaryOperator {
            kind: BinaryOperatorKind::Add,
            left: Rc::new(AstNode::Number { value: Complex::from(2.0), span: Span::from(0..1) }),
            right: Rc::new(AstNode::BinaryOperator {
                kind: BinaryOperatorKind::Mul,
                left: Rc::new(AstNode::Number { value: Complex::from(3.0), span: Span::from(4..5) }),
                right: Rc::new(AstNode::Number { value: Complex::from(4.0), span: Span::from(8..9) }),
                span: Span::from(6..7)
            }),
            span: Span::from(2..3)
        });
    }

    #[test]
    fn parentheses_override_precedence_astnode() {
        let lexemes = lexer::from("( 2 + 3 ) * 4");
        let ast = AstNode::from(&lexemes, &[], &Constants::new(), &UserFnTable::new()).unwrap();
        // expected: ((2 + 3) * 4)
        match ast {
            AstNode::BinaryOperator { kind, left, right, .. } => {
                assert_eq!(kind, BinaryOperatorKind::Mul);
                assert_eq!(*right, AstNode::Number { value: Complex::new(4.0, 0.0), span: Span::from(12..13) });
                match *left {
                    AstNode::BinaryOperator { kind, .. } => assert_eq!(kind, BinaryOperatorKind::Add),
                    _ => panic!("Expected Add inside parentheses"),
                }
            }
            _ => panic!("Expected Mul node"),
        }
    }

    #[test]
    fn function_single_arg_astnode() {
        let lexemes = lexer::from("sin ( 0 )");
        let ast = AstNode::from(&lexemes, &[], &Constants::new(), &UserFnTable::new()).unwrap();
        assert_astnode_eq!(ast, AstNode::FunctionCall {
            kind: FunctionKind::Sin,
            args: vec![Rc::new(AstNode::Number { value: Complex::from(0.0), span: Span::from(6..7) })],
            span: Span::from(0..3),
        });
    }

    #[test]
    fn function_multiple_args_astnode() {
        let lexemes = lexer::from("pow ( 2 , 3 )");
        let ast = AstNode::from(&lexemes, &[], &Constants::new(), &UserFnTable::new()).unwrap();
        assert_astnode_eq!(ast, AstNode::FunctionCall {
            kind: FunctionKind::Pow,
            args: vec![
                Rc::new(AstNode::Number { value: Complex::from(2.0), span: Span::from(6..7) }),
                Rc::new(AstNode::Number { value: Complex::from(3.0), span: Span::from(10..11) }),
            ],
            span: Span::from(0..3),
        });

        let lexemes = lexer::from("pow ( sin(x) , 3 )");
        let ast = AstNode::from(&lexemes, &["x"], &Constants::new(), &UserFnTable::new()).unwrap();
        assert_astnode_eq!(ast, AstNode::FunctionCall {
            kind: FunctionKind::Pow,
            args: vec![
                Rc::new(AstNode::FunctionCall {
                    kind: FunctionKind::Sin,
                    args: vec![Rc::new(AstNode::Argument { index: 0, span: Span::from(10..11) })],
                    span: Span::from(6..9),
                }),
                Rc::new(AstNode::Number { value: Complex::from(3.0), span: Span::from(15..16) }),
            ],
            span: Span::from(0..3),
        });
    }

    #[test]
    fn imaginary_number_astnode() {
        let lexemes = lexer::from("5i");
        let ast = AstNode::from(&lexemes, &[], &Constants::new(), &UserFnTable::new()).unwrap();
        assert_eq!(ast, AstNode::Number { value: Complex::new(0.0, 5.0), span: Span::from(0..2) });
    }

    #[test]
    fn unknown_token_astnode_error() {
        let lexemes = lexer::from("@");
        let res = AstNode::from(&lexemes, &[], &Constants::<f64>::new(), &UserFnTable::new());
        assert!(res.is_err());
    }
}