hypervox_expr 0.2.0

High-performance mathematical expression parser, AST optimizer (constant folding, CSE), and closure compiler with multi-dimensional invariant hoisting
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
//! Math expression parser, compiler, and CSE engine.
//!
//! Parses string expressions (e.g. `"sin(x)*cos(y)"`) into an AST ([`Node`]),
//! applies constant folding ([`Node::pre_eval`]),
//! common-subexpression elimination ([`Node::cse`]),
//! and compiles to closures ([`Node::compile`] / [`Node::compile_multi`])
//! for fast repeated evaluation over N-dimensional grids.
//! Variable names are resolved through the [`VarMap`] trait.

use std::{
    fmt::{Debug, Display},
    str::FromStr,
};

mod errors;
mod index_set;
mod node;
mod parse;

pub use errors::{ArithIndexSetTryFromError, Error, LexerErrorKind, ParseErrorKind};
pub use index_set::{ArithIndexSet, ArithRangeFrom, ArithRangeIter, IndexSet, IndexSetIter};
pub use node::{CompiledExpr, CompiledExprMulti, InvariantGroup, Node};
pub use parse::{parse, parse_with_ext, validate, validate_with_ext};

/// Trait for resolving variable names in expressions.
/// Implementors define variable aliases and the indexed-variable naming scheme.
pub trait VarMap {
    /// Number of dimensions.
    fn ndim(&self) -> usize;

    /// Resolve a variable alias (e.g. "x", "y", "z") to a dimension index.
    fn resolve_alias(&self, name: &str) -> Option<usize>;

    /// Primary variable prefix used for indexed variables (e.g. "x0", "x1", ..).
    fn primary_prefix(&self) -> &str;
}

macro_rules! define_f0 {
    ($($variant:ident => $str:literal = $body:expr),* $(,)?) => {
        /// Constants (PI, E).
        #[derive(Debug, Clone, Copy)]
        pub enum F0 {
            $($variant,)*
        }
        impl F0 {
            /// Evaluate the constant as an f64.
            ///
            /// # Examples
            /// ```
            /// # use hypervox_expr::F0;
            /// let pi = F0::PI.to_num();
            /// assert!((pi - std::f64::consts::PI).abs() < 1e-15);
            /// ```
            pub const fn to_num(self) -> f64 {
                match self {
                    $(Self::$variant => $body,)*
                }
            }
            /// All constant names for display.
            pub const NAMES: &'static [&'static str] = &[$($str,)*];
        }
        impl FromStr for F0 {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $($str => Ok(Self::$variant),)*
                    _ => Err(format!("unknown const '{s}'")),
                }
            }
        }
    };
}

macro_rules! define_f1 {
    ($($variant:ident => $str:literal = $body:expr),* $(,)?) => {
        /// Single-argument math functions.
        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
        pub enum F1 {
            $($variant,)*
        }
        impl F1 {
            /// Resolve to a function pointer.
            ///
            /// # Examples
            /// ```
            /// # use hypervox_expr::F1;
            /// let f = F1::Sin.to_fn();
            /// assert!((f(1.0) - 1.0_f64.sin()).abs() < 1e-15);
            /// ```
            #[inline]
            pub const fn to_fn(self) -> fn(f64) -> f64 {
                match self {
                    $(Self::$variant => $body,)*
                }
            }
            /// All function names for display.
            pub const NAMES: &'static [&'static str] = &[$($str,)*];
        }
        impl FromStr for F1 {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $($str => Ok(Self::$variant),)*
                    _ => Err(format!("unknown function '{s}'")),
                }
            }
        }
    };
}

macro_rules! define_f2 {
    ($($variant:ident => $str:literal = $body:expr),* $(,)?) => {
        /// Two-argument math functions (atan2, pow).
        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
        pub enum F2 {
            $($variant,)*
        }
        impl F2 {
            /// Resolve to a function pointer.
            ///
            /// # Examples
            /// ```
            /// # use hypervox_expr::F2;
            /// let atan2 = F2::Atan2.to_fn();
            /// assert_eq!(atan2(0.0, 1.0), 0.0);
            /// ```
            #[inline]
            pub const fn to_fn(self) -> fn(f64, f64) -> f64 {
                match self {
                    $(Self::$variant => $body,)*
                }
            }
            /// All function names for display.
            pub const NAMES: &'static [&'static str] = &[$($str,)*];
        }
        impl FromStr for F2 {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $($str => Ok(Self::$variant),)*
                    _ => Err(format!("unknown function '{s}'")),
                }
            }
        }
    };
}

define_f0! {
    PI => "PI" = std::f64::consts::PI,
    E => "E" = std::f64::consts::E,
}

define_f1! {
    Sin => "sin" = f64::sin,
    Cos => "cos" = f64::cos,
    Tan => "tan" = f64::tan,
    Asin => "asin" = f64::asin,
    Acos => "acos" = f64::acos,
    Atan => "atan" = f64::atan,
    Sinh => "sinh" = f64::sinh,
    Cosh => "cosh" = f64::cosh,
    Tanh => "tanh" = f64::tanh,
    Sqrt => "sqrt" = f64::sqrt,
    Cbrt => "cbrt" = f64::cbrt,
    Exp => "exp" = f64::exp,
    Ln => "ln" = f64::ln,
    Log10 => "log10" = f64::log10,
    Log2 => "log2" = f64::log2,
    Floor => "floor" = f64::floor,
    Ceil => "ceil" = f64::ceil,
    Round => "round" = f64::round,
    Trunc => "trunc" = f64::trunc,
    Abs => "abs" = f64::abs,
}

define_f2! {
    Atan2 => "atan2" = f64::atan2,
    Pow => "pow" = |a, b| {
        if a == 0.0_f64 && b == 0.0_f64 {
            1.0_f64
        } else {
            let exp_int = b as i32;
            if f64::from(exp_int) == b {
                a.powi(exp_int)
            } else {
                a.powf(b)
            }
        }
    },
}

/// Return a comma-separated list of available constant names.
///
/// # Examples
/// ```
/// # use hypervox_expr::f0_list;
/// let list = f0_list();
/// assert!(list.contains("PI"));
/// ```
#[must_use]
pub fn f0_list() -> String {
    F0::NAMES.join(", ")
}

/// Return a comma-separated list of available single-argument function names.
///
/// # Examples
/// ```
/// # use hypervox_expr::f1_list;
/// let list = f1_list();
/// assert!(list.contains("sin"));
/// ```
#[must_use]
pub fn f1_list() -> String {
    F1::NAMES.join(", ")
}

/// Return a comma-separated list of available two-argument function names.
///
/// # Examples
/// ```
/// # use hypervox_expr::f2_list;
/// let list = f2_list();
/// assert!(list.contains("atan2"));
/// ```
#[must_use]
pub fn f2_list() -> String {
    F2::NAMES.join(", ")
}

/// Trait for custom constants.
pub trait ExtF0: Debug + Clone + PartialEq {
    /// Evaluate the constant as an f64.
    fn to_num(&self) -> f64;

    /// All constant names for display.
    fn names(&self) -> &[&str];

    /// List all constant names.
    ///
    /// The default implementation returns the elements of [`Self::names`] separated by commas.
    fn list(&self) -> String {
        self.names().join(", ")
    }
}

/// Trait for custom single-argument functions.
pub trait ExtF1: Debug + Clone + PartialEq {
    /// Resolve to a function pointer.
    fn to_fn(&self) -> fn(f64) -> f64;

    /// All function names for display.
    fn names(&self) -> &[&str];

    /// List all function names.
    ///
    /// The default implementation returns the elements of [`Self::names`] separated by commas.
    fn list(&self) -> String {
        self.names().join(", ")
    }
}

/// Trait for custom two-argument functions.
pub trait ExtF2: Debug + Clone + PartialEq {
    /// Resolve to a function pointer.
    fn to_fn(&self) -> fn(f64, f64) -> f64;

    /// All function names for display.
    fn names(&self) -> &[&str];

    /// List all function names.
    ///
    /// The default implementation returns the elements of [`Self::names`] separated by commas.
    fn list(&self) -> String {
        self.names().join(", ")
    }
}

/// Define a custom-constants enum implementing [`ExtF0`] and [`FromStr`].
///
/// # Examples
/// ```
/// use hypervox_expr::ExtF0;
/// hypervox_expr::define_ext_f0!(MyF0, MyConst => "my_const" = 42.0);
/// assert_eq!(MyF0::MyConst.to_num(), 42.0);
/// ```
#[macro_export]
macro_rules! define_ext_f0 {
    ($enum_name:ident) => {
        define_ext_f0!($enum_name,);
    };
    ($enum_name:ident, $($variant:ident => $str:literal = $body:expr),* $(,)?) => {
        /// Constants.
        #[expect(clippy::derive_partial_eq_without_eq)]
        #[derive(Debug, Clone, Copy, PartialEq)]
        pub enum $enum_name {
            $($variant,)*
        }
        impl hypervox_expr::ExtF0 for $enum_name {
            /// Evaluate the constant as an f64.
            fn to_num(&self) -> f64 {
                match *self {
                    $(Self::$variant => $body,)*
                }
            }
            /// All constant names for display.
            fn names(&self) -> &[&str] {
                &[$($str,)*]
            }
        }
        impl std::str::FromStr for $enum_name {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $($str => Ok(Self::$variant),)*
                    _ => Err(format!("unknown const '{s}'")),
                }
            }
        }
    };
}

/// Define custom single-argument functions implementing [`ExtF1`] and [`FromStr`].
///
/// # Examples
/// ```
/// use hypervox_expr::ExtF1;
/// hypervox_expr::define_ext_f1!(MyF1, Cube => "cube" = |x| x * x * x);
/// assert_eq!(MyF1::Cube.to_fn()(3.0), 27.0);
/// ```
#[macro_export]
macro_rules! define_ext_f1 {
    ($enum_name:ident) => {
        define_ext_f1!($enum_name,);
    };
    ($enum_name:ident, $($variant:ident => $str:literal = $body:expr),* $(,)?) => {
        /// Single-argument math functions.
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum $enum_name {
            $($variant,)*
        }
        impl hypervox_expr::ExtF1 for $enum_name {
            /// Resolve to a function pointer.
            #[inline]
            fn to_fn(&self) -> fn(f64) -> f64 {
                match *self {
                    $(Self::$variant => $body,)*
                }
            }
            /// All function names for display.
            fn names(&self) -> &[&str] {
                &[$($str,)*]
            }
        }
        impl std::str::FromStr for $enum_name {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $($str => Ok(Self::$variant),)*
                    _ => Err(format!("unknown function '{s}'")),
                }
            }
        }
    };
}

/// Define custom two-argument functions implementing [`ExtF2`] and [`FromStr`].
///
/// # Examples
/// ```
/// use hypervox_expr::ExtF2;
/// hypervox_expr::define_ext_f2!(MyF2, Hypot => "hypot" = |x, y| x.hypot(y));
/// assert_eq!(MyF2::Hypot.to_fn()(3.0, 4.0), 5.0);
/// ```
#[macro_export]
macro_rules! define_ext_f2 {
    ($enum_name:ident) => {
        define_ext_f2!($enum_name,);
    };
    ($enum_name:ident, $($variant:ident => $str:literal = $body:expr),* $(,)?) => {
        /// Two-argument math functions (atan2, pow).
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub enum $enum_name {
            $($variant,)*
        }
        impl hypervox_expr::ExtF2 for $enum_name {
            /// Resolve to a function pointer.
            #[inline]
            fn to_fn(&self) -> fn(f64, f64) -> f64 {
                match *self {
                    $(Self::$variant => $body,)*
                }
            }
            /// All function names for display.
            fn names(&self) -> &[&str] {
                &[$($str,)*]
            }
        }
        impl std::str::FromStr for $enum_name {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $($str => Ok(Self::$variant),)*
                    _ => Err(format!("unknown function '{s}'")),
                }
            }
        }
    };
}

/// Zero-variant placeholder type for when no external functions/constants are used.
///
/// Acts as the default type parameter for [`Node`], [`parse`], and [`validate`]
/// so that the regular API requires no generics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NoExtF {}

impl ExtF0 for NoExtF {
    fn to_num(&self) -> f64 {
        unreachable!("NoExtF should never be called")
    }
    fn names(&self) -> &[&str] {
        unreachable!("NoExtF should never be called")
    }
}

impl ExtF1 for NoExtF {
    fn to_fn(&self) -> fn(f64) -> f64 {
        unreachable!("NoExtF should never be called")
    }
    fn names(&self) -> &[&str] {
        unreachable!("NoExtF should never be called")
    }
}

impl ExtF2 for NoExtF {
    fn to_fn(&self) -> fn(f64, f64) -> f64 {
        unreachable!("NoExtF should never be called")
    }
    fn names(&self) -> &[&str] {
        unreachable!("NoExtF should never be called")
    }
}

impl FromStr for NoExtF {
    type Err = String;
    fn from_str(_s: &str) -> Result<Self, Self::Err> {
        Err(String::from("no external constants or functions defined"))
    }
}

/// Token produced by the lexer and consumed by the Pratt parser.
#[derive(Debug, Clone, PartialEq)]
pub enum LexerToken {
    Num(f64),
    Ident(String),
    Plus,
    Minus,
    Star,
    Slash,
    Caret,
    Percent,
    Pipe,
    LParen,
    RParen,
    Comma,
    Eof,
}

impl Display for LexerToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Num(v) => write!(f, "{v}"),
            Self::Ident(s) => write!(f, "'{s}'"),
            Self::Plus => write!(f, "'+'"),
            Self::Minus => write!(f, "'-'"),
            Self::Star => write!(f, "'*'"),
            Self::Slash => write!(f, "'/'"),
            Self::Caret => write!(f, "'^'"),
            Self::Percent => write!(f, "'%'"),
            Self::Pipe => write!(f, "'|'"),
            Self::LParen => write!(f, "'('"),
            Self::RParen => write!(f, "')'"),
            Self::Comma => write!(f, "','"),
            Self::Eof => write!(f, "end of expression"),
        }
    }
}