Skip to main content

Crate exprkit

Crate exprkit 

Source
Expand description

A mathematical expression parser and evaluator library.

This library provides runtime parsing and evaluation of mathematical expressions, inspired by the ExprTk C++ library.

§Quick Start

use exprkit::{Expression, SymbolTable};

let mut symbol_table = SymbolTable::<f64>::new();
symbol_table.add_variable("x", 2.0);
symbol_table.add_variable("y", 3.0);

let mut expr = Expression::new();
expr.compile_with_symbol_table("x + y * 2", symbol_table).unwrap();

assert_eq!(expr.value(), 8.0);

§Features

  • Variables and constants: Register named values in the symbol table
  • Arrays/vectors: Work with numeric arrays and vector operations
  • User-defined functions: Add custom functions written in Rust
  • Control flow: if/else, for loops, while loops, switch statements
  • Built-in functions: 100+ mathematical functions (trig, exponential, etc.)

§Working with Variables

use exprkit::{Expression, SymbolTable};

let mut symbol_table = SymbolTable::<f64>::new();
symbol_table.add_variable("x", 1.0);

let mut expr = Expression::new();
expr.compile_with_symbol_table("x := x + 1; x * 2", symbol_table).unwrap();

// After evaluation, x has been modified
assert_eq!(expr.value(), 4.0);

// Access the updated value
let x = expr.symbol_table().unwrap().get_value("x").unwrap().as_number();
assert_eq!(x, 2.0);

§Working with Vectors

use exprkit::{Expression, SymbolTable};

let mut symbol_table = SymbolTable::<f64>::new();
symbol_table.add_vector("v", vec![1.0, 2.0, 3.0, 4.0, 5.0]);

let mut expr = Expression::new();
expr.compile_with_symbol_table("sum(v)", symbol_table).unwrap();

assert_eq!(expr.value(), 15.0);

§User-Defined Functions

use exprkit::{Expression, SymbolTable};

let mut symbol_table = SymbolTable::<f64>::new();
symbol_table.add_function("cube", Box::new(|args: &[f64]| args[0].powi(3)));
symbol_table.add_variable("x", 3.0);

let mut expr = Expression::new();
expr.compile_with_symbol_table("cube(x)", symbol_table).unwrap();

assert_eq!(expr.value(), 27.0);

Re-exports§

pub use expression::Expression;
pub use parser::Parser;
pub use parser::ParserError;
pub use symbol_table::SymbolTable;
pub use value::Value;

Modules§

ast
compositor
evaluator
expression
parser
symbol_table
tape
tokenizer
trace
value