pub struct Eval<'str> { /* private fields */ }Expand description
Expression evaluator with support for custom symbols and bytecode compilation.
Eval is the main entry point for evaluating mathematical expressions. It supports
both quick one-off evaluations and reusable evaluators with custom symbol tables.
§Examples
use expr_solver::Eval;
// Quick evaluation
let result = Eval::evaluate("2 + 3 * 4").unwrap();
assert_eq!(result.to_string(), "14");
// Reusable evaluator
let mut eval = Eval::new("sqrt(16) + pi");
let result = eval.run().unwrap();Implementations§
Source§impl<'str> Eval<'str>
impl<'str> Eval<'str>
Sourcepub fn evaluate(expression: &'str str) -> Result<Decimal, String>
pub fn evaluate(expression: &'str str) -> Result<Decimal, String>
Quick evaluation of an expression with the standard library.
This is a convenience method for one-off evaluations.
§Examples
use expr_solver::Eval;
let result = Eval::evaluate("2^8").unwrap();
assert_eq!(result.to_string(), "256");Sourcepub fn evaluate_with_table(
expression: &'str str,
table: SymTable,
) -> Result<Decimal, String>
pub fn evaluate_with_table( expression: &'str str, table: SymTable, ) -> Result<Decimal, String>
Quick evaluation of an expression with a custom symbol table.
§Examples
use expr_solver::{Eval, SymTable};
use rust_decimal_macros::dec;
let mut table = SymTable::stdlib();
table.add_const("x", dec!(42)).unwrap();
let result = Eval::evaluate_with_table("x * 2", table).unwrap();
assert_eq!(result, dec!(84));Sourcepub fn new(string: &'str str) -> Self
pub fn new(string: &'str str) -> Self
Creates a new evaluator with the standard library.
§Examples
use expr_solver::Eval;
let mut eval = Eval::new("sin(pi/2)");
let result = eval.run().unwrap();Sourcepub fn with_table(string: &'str str, table: SymTable) -> Self
pub fn with_table(string: &'str str, table: SymTable) -> Self
Creates a new evaluator with a custom symbol table.
§Examples
use expr_solver::{Eval, SymTable};
use rust_decimal_macros::dec;
let mut table = SymTable::stdlib();
table.add_const("x", dec!(42)).unwrap();
let mut eval = Eval::with_table("x * 2", table);
let result = eval.run().unwrap();
assert_eq!(result, dec!(84));Sourcepub fn new_from_source(source: &'str Source<'str>) -> Self
pub fn new_from_source(source: &'str Source<'str>) -> Self
Creates a new evaluator from a Source reference.
Sourcepub fn from_source_with_table(
source: &'str Source<'str>,
table: SymTable,
) -> Self
pub fn from_source_with_table( source: &'str Source<'str>, table: SymTable, ) -> Self
Creates a new evaluator from a Source reference with a custom symbol table.
Sourcepub fn new_from_file(path: PathBuf) -> Self
pub fn new_from_file(path: PathBuf) -> Self
Creates a new evaluator from a compiled binary file.
The file must have been created using compile_to_file.
Sourcepub fn from_file_with_table(path: PathBuf, table: SymTable) -> Self
pub fn from_file_with_table(path: PathBuf, table: SymTable) -> Self
Creates a new evaluator from a compiled binary file with a custom symbol table.
Sourcepub fn run(&mut self) -> Result<Decimal, String>
pub fn run(&mut self) -> Result<Decimal, String>
Evaluates the expression and returns the result.
§Examples
use expr_solver::Eval;
let mut eval = Eval::new("2 + 3");
assert_eq!(eval.run().unwrap().to_string(), "5");Sourcepub fn compile_to_file(&mut self, path: &PathBuf) -> Result<(), String>
pub fn compile_to_file(&mut self, path: &PathBuf) -> Result<(), String>
Compiles the expression to a binary file.
The compiled bytecode can later be loaded with new_from_file.
§Examples
use expr_solver::Eval;
use std::path::PathBuf;
let mut eval = Eval::new("2 + 3 * 4");
eval.compile_to_file(&PathBuf::from("expr.bin")).unwrap();Sourcepub fn get_assembly(&mut self) -> Result<String, String>
pub fn get_assembly(&mut self) -> Result<String, String>
Returns a human-readable assembly representation of the compiled expression.
§Examples
use expr_solver::Eval;
let mut eval = Eval::new("2 + 3");
let assembly = eval.get_assembly().unwrap();
assert!(assembly.contains("PUSH"));
assert!(assembly.contains("ADD"));