Skip to main content

exprkit/
lib.rs

1//! A mathematical expression parser and evaluator library.
2//!
3//! This library provides runtime parsing and evaluation of mathematical expressions,
4//! inspired by the [ExprTk C++ library](https://www.partow.net/programming/exprtk/).
5//!
6//! # Quick Start
7//!
8//! ```
9//! use exprkit::{Expression, SymbolTable};
10//!
11//! let mut symbol_table = SymbolTable::<f64>::new();
12//! symbol_table.add_variable("x", 2.0);
13//! symbol_table.add_variable("y", 3.0);
14//!
15//! let mut expr = Expression::new();
16//! expr.compile_with_symbol_table("x + y * 2", symbol_table).unwrap();
17//!
18//! assert_eq!(expr.value(), 8.0);
19//! ```
20//!
21//! # Features
22//!
23//! - **Variables and constants**: Register named values in the symbol table
24//! - **Arrays/vectors**: Work with numeric arrays and vector operations
25//! - **User-defined functions**: Add custom functions written in Rust
26//! - **Control flow**: if/else, for loops, while loops, switch statements
27//! - **Built-in functions**: 100+ mathematical functions (trig, exponential, etc.)
28//!
29//! # Working with Variables
30//!
31//! ```
32//! use exprkit::{Expression, SymbolTable};
33//!
34//! let mut symbol_table = SymbolTable::<f64>::new();
35//! symbol_table.add_variable("x", 1.0);
36//!
37//! let mut expr = Expression::new();
38//! expr.compile_with_symbol_table("x := x + 1; x * 2", symbol_table).unwrap();
39//!
40//! // After evaluation, x has been modified
41//! assert_eq!(expr.value(), 4.0);
42//!
43//! // Access the updated value
44//! let x = expr.symbol_table().unwrap().get_value("x").unwrap().as_number();
45//! assert_eq!(x, 2.0);
46//! ```
47//!
48//! # Working with Vectors
49//!
50//! ```
51//! use exprkit::{Expression, SymbolTable};
52//!
53//! let mut symbol_table = SymbolTable::<f64>::new();
54//! symbol_table.add_vector("v", vec![1.0, 2.0, 3.0, 4.0, 5.0]);
55//!
56//! let mut expr = Expression::new();
57//! expr.compile_with_symbol_table("sum(v)", symbol_table).unwrap();
58//!
59//! assert_eq!(expr.value(), 15.0);
60//! ```
61//!
62//! # User-Defined Functions
63//!
64//! ```
65//! use exprkit::{Expression, SymbolTable};
66//!
67//! let mut symbol_table = SymbolTable::<f64>::new();
68//! symbol_table.add_function("cube", Box::new(|args: &[f64]| args[0].powi(3)));
69//! symbol_table.add_variable("x", 3.0);
70//!
71//! let mut expr = Expression::new();
72//! expr.compile_with_symbol_table("cube(x)", symbol_table).unwrap();
73//!
74//! assert_eq!(expr.value(), 27.0);
75//! ```
76
77pub mod ast;
78pub mod compositor;
79pub mod evaluator;
80pub mod expression;
81pub mod parser;
82pub mod symbol_table;
83pub mod tape;
84pub mod tokenizer;
85pub mod trace;
86pub mod value;
87
88pub use expression::Expression;
89pub use parser::{Parser, ParserError};
90pub use symbol_table::SymbolTable;
91pub use value::Value;