Crate expr_solver

Crate expr_solver 

Source
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::{SymTable, Program};
use rust_decimal_macros::dec;

// Compile expression to bytecode
let program = Program::new_from_source("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.
SymbolMetadata
Metadata about a symbol required by compiled bytecode.
Vm
Stack-based virtual machine for executing bytecode programs.

Enums§

BinOp
Binary operators: arithmetic and comparison.
ExprKind
Expression kind representing different types of expressions.
LinkError
Errors that can occur during linking.
ParseError
Errors that can occur during parsing.
ProgramError
Errors that can occur during program operations.
ProgramOrigin
Origin of a compiled program.
Symbol
A symbol representing either a constant or function.
SymbolError
Errors that can occur during symbol table operations.
SymbolKind
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.