onedrop_eval/
lib.rs

1//! # onedrop-eval
2//!
3//! Expression evaluator for Milkdrop per-frame and per-pixel equations.
4//!
5//! This crate provides functionality to evaluate mathematical expressions
6//! used in Milkdrop presets, with support for all Milkdrop variables and functions.
7
8pub mod bytecode;
9pub mod carry;
10pub mod compiled_block;
11pub mod context;
12pub mod error;
13pub mod evaluator;
14pub mod math_functions;
15
16pub use bytecode::{CompileError as BytecodeCompileError, CompiledBytecode};
17pub use carry::{PerPointParallelism, analyse_per_point};
18pub use compiled_block::CompiledBlock;
19pub use context::{MilkContext, Q_CHANNEL_NAMES_32};
20pub use error::{EvalError, Result};
21pub use evaluator::{MilkEvaluator, ShapeInstance, WavePoint};
22pub use math_functions::{list_math_functions, register_math_functions};
23
24// Re-export the evalexpr `Node` type so downstream crates (notably
25// `onedrop-engine::warp_eval::WarpExecutor`) can hold compiled expressions
26// without depending on `evalexpr` directly.
27pub use evalexpr::Node;
28
29/// Evaluate a simple expression with default context.
30///
31/// # Examples
32///
33/// ```
34/// use onedrop_eval::eval_simple;
35///
36/// let result = eval_simple("2 + 2").unwrap();
37/// assert_eq!(result, 4.0);
38/// ```
39pub fn eval_simple(expression: &str) -> Result<f64> {
40    let mut evaluator = MilkEvaluator::new();
41    evaluator.eval(expression)
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_eval_simple() {
50        let result = eval_simple("10 * 5").unwrap();
51        assert_eq!(result, 50.0);
52    }
53
54    #[test]
55    fn test_eval_with_math() {
56        let result = eval_simple("sin(0) + cos(0)").unwrap();
57        assert!((result - 1.0).abs() < 1e-10);
58    }
59}