Eval

Struct Eval 

Source
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>

Source

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");
Source

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));
Source

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();
Source

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));
Source

pub fn new_from_source(source: &'str Source<'str>) -> Self

Creates a new evaluator from a Source reference.

Source

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.

Source

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.

Source

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.

Source

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");
Source

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();
Source

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"));

Trait Implementations§

Source§

impl<'str> Debug for Eval<'str>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'str> Freeze for Eval<'str>

§

impl<'str> RefUnwindSafe for Eval<'str>

§

impl<'str> Send for Eval<'str>

§

impl<'str> Sync for Eval<'str>

§

impl<'str> Unpin for Eval<'str>

§

impl<'str> UnwindSafe for Eval<'str>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.