Expand description
A mathematical expression evaluator library with bytecode compilation.
This library provides a complete compiler pipeline for mathematical expressions, from parsing to bytecode execution on a stack-based virtual machine.
§Features
- Type-safe compilation - Uses Rust’s type system to enforce correct pipeline order
- 128-bit decimal precision - No floating-point errors using
rust_decimal - Rich error messages - Parse errors with syntax highlighting
- Bytecode compilation - Compile once, execute many times
- Custom symbols - Add your own constants and functions
- Serialization - Save/load compiled programs to/from disk
§Quick Start
use expr_solver::eval;
// Simple evaluation
let result = eval("2 + 3 * 4").unwrap();
assert_eq!(result.to_string(), "14");§Custom Symbols
use expr_solver::{eval_with_table, SymTable};
use rust_decimal_macros::dec;
let mut table = SymTable::stdlib();
table.add_const("x", dec!(10)).unwrap();
let result = eval_with_table("x * 2", table).unwrap();
assert_eq!(result, dec!(20));§Advanced: Type-State Pattern
The Program type uses the type-state pattern to enforce correct usage:
use expr_solver::{load, SymTable};
use rust_decimal_macros::dec;
// Compile expression to bytecode
let program = load("x + y").unwrap();
// Link with symbol table (validated at link time)
let mut table = SymTable::new();
table.add_const("x", dec!(10)).unwrap();
table.add_const("y", dec!(5)).unwrap();
let linked = program.link(table).unwrap();
// Execute
let result = linked.execute().unwrap();
assert_eq!(result, dec!(15));§Supported Operators
- Arithmetic:
+,-,*,/,^(power),!(factorial), unary- - Comparison:
==,!=,<,<=,>,>=(return 1 or 0) - Grouping:
()
§Built-in Functions
See SymTable::stdlib() for the complete list of built-in functions and constants.
Structs§
- Compiled
- Compiled state - bytecode ready for linking.
- Expr
- Expression node in the AST with source location.
- Linked
- Linked state - ready to execute.
- Parser
- Recursive descent parser for mathematical expressions.
- Program
- Type-state program using Rust’s type system to enforce correct usage.
- SymTable
- Symbol table containing constants and functions.
- Symbol
Metadata - Metadata about a symbol required by compiled bytecode.
- Vm
- Stack-based virtual machine for executing bytecode programs.
Enums§
- BinOp
- Binary operators: arithmetic and comparison.
- Expr
Kind - Expression kind representing different types of expressions.
- Link
Error - Errors that can occur during linking.
- Parse
Error - Errors that can occur during parsing.
- Program
Error - Errors that can occur during program operations.
- Program
Origin - Origin of a compiled program.
- Symbol
- A symbol representing either a constant or function.
- Symbol
Error - Errors that can occur during symbol table operations.
- Symbol
Kind - The kind of symbol (constant or function) with its requirements.
- UnOp
- Unary operators: negation and factorial.
- VmError
- Virtual machine runtime errors.
Functions§
- eval
- Evaluates an expression string with the standard library.
- eval_
file - Evaluates an expression from a binary file with the standard library.
- eval_
file_ with_ table - Evaluates an expression from a binary file with a custom symbol table.
- eval_
with_ table - Evaluates an expression string with a custom symbol table.
- load
- Loads and compiles an expression, returning a compiled program.
- load_
with_ table - Loads, compiles, and links an expression, returning a ready-to-execute program.