Expression

Struct Expression 

Source
pub struct Expression<'arena> { /* private fields */ }
Expand description

Arena-aware batch builder for zero-allocation expression evaluation

This structure is similar to BatchBuilder but uses an arena for all AST allocations, eliminating dynamic memory allocation during evaluation.

Implementations§

Source§

impl<'arena> Expression<'arena>

Source

pub fn new(arena: &'arena Bump) -> Self

Create a new empty batch builder with arena

Source

pub fn add_expression(&mut self, expr: &str) -> Result<usize, ExprError>

Add an expression to be evaluated

The expression is parsed immediately into the arena. Returns the index of the added expression.

Source

pub fn add_parameter( &mut self, name: &str, initial_value: Real, ) -> Result<usize, ExprError>

Add a parameter with an initial value

Returns an error if a parameter with the same name already exists. Returns the index of the added parameter.

Source

pub fn set_param(&mut self, idx: usize, value: Real) -> Result<(), ExprError>

Update a parameter value by index (fastest method)

Source

pub fn set_param_by_name( &mut self, name: &str, value: Real, ) -> Result<(), ExprError>

Update a parameter value by name (convenient but slower)

Source

pub fn eval(&mut self, base_ctx: &Rc<EvalContext>) -> Result<(), ExprError>

Evaluate all expressions with current parameter values

Source

pub fn get_result(&self, expr_idx: usize) -> Option<Real>

Get the result of a specific expression by index

Source

pub fn get_all_results(&self) -> &[Real]

Get all results as a slice

Source

pub fn param_count(&self) -> usize

Get the number of parameters

Source

pub fn expression_count(&self) -> usize

Get the number of expressions

Source

pub fn register_expression_function( &mut self, name: &str, params: &[&str], body: &str, ) -> Result<(), ExprError>

Register a local expression function for this batch

Expression functions are mathematical expressions that can call other functions. They are specific to this batch and take precedence over context functions.

§Arguments
  • name - Function name
  • params - Parameter names
  • body - Expression string defining the function
Source

pub fn unregister_expression_function( &mut self, name: &str, ) -> Result<bool, ExprError>

Remove a local expression function from this batch

§Arguments
  • name - Function name to remove
§Returns
  • Ok(true) if the function was removed
  • Ok(false) if the function didn’t exist
  • Err if the name is invalid
Source

pub fn arena_allocated_bytes(&self) -> usize

Get the current number of bytes allocated in the arena

Source

pub fn clear(&mut self)

Clear all expressions, parameters, results, and local functions from this batch

This allows the batch to be reused without recreating it. The arena memory used by previous expressions remains allocated but unused until the arena is reset. The evaluation engine is retained for reuse.

§Example
use bumpalo::Bump;
use exp_rs::expression::Expression;

let arena = Bump::new();
let mut batch = Expression::new(&arena);
batch.add_expression("x + 1").unwrap();
batch.add_parameter("x", 5.0).unwrap();

// Clear and reuse
batch.clear();
assert_eq!(batch.expression_count(), 0);
assert_eq!(batch.param_count(), 0);
Source

pub fn eval_simple(expr: &str, arena: &'arena Bump) -> Result<Real, ExprError>

Evaluate a single expression without parameters

This is the simplest way to evaluate an expression that doesn’t need variables.

§Example
use bumpalo::Bump;
use exp_rs::expression::Expression;

let arena = Bump::new();
let result = Expression::eval_simple("2 + 3 * 4", &arena).unwrap();
assert_eq!(result, 14.0);
Source

pub fn eval_with_context( expr: &str, ctx: &Rc<EvalContext>, arena: &'arena Bump, ) -> Result<Real, ExprError>

Evaluate a single expression with context

Use this when you have a context with pre-defined variables, constants, or functions.

§Example
use bumpalo::Bump;
use exp_rs::{expression::Expression, EvalContext};
use std::rc::Rc;

let arena = Bump::new();
let mut ctx = EvalContext::new();
ctx.set_parameter("x", 5.0);

let result = Expression::eval_with_context("x * 2", &Rc::new(ctx), &arena).unwrap();
assert_eq!(result, 10.0);
Source

pub fn eval_with_params( expr: &str, params: &[(&str, Real)], ctx: &Rc<EvalContext>, arena: &'arena Bump, ) -> Result<Real, ExprError>

Evaluate a single expression with parameters

This is convenient when you want to provide parameters inline without creating a context.

§Example
use bumpalo::Bump;
use exp_rs::{expression::Expression, EvalContext};
use std::rc::Rc;

let arena = Bump::new();
let params = [("x", 3.0), ("y", 4.0)];
let ctx = Rc::new(EvalContext::new());

let result = Expression::eval_with_params("x^2 + y^2", &params, &ctx, &arena).unwrap();
assert_eq!(result, 25.0); // 3^2 + 4^2 = 25
Source

pub fn set(&mut self, name: &str, value: Real) -> Result<(), ExprError>

Convenience setter using string slices

This is an alias for set_param_by_name with a shorter name for convenience.

§Example
use bumpalo::Bump;
use exp_rs::expression::Expression;

let arena = Bump::new();
let mut builder = Expression::new(&arena);
builder.add_parameter("x", 0.0).unwrap();
builder.set("x", 5.0).unwrap();

Trait Implementations§

Source§

impl<'arena> Drop for Expression<'arena>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'arena> Freeze for Expression<'arena>

§

impl<'arena> !RefUnwindSafe for Expression<'arena>

§

impl<'arena> !Send for Expression<'arena>

§

impl<'arena> !Sync for Expression<'arena>

§

impl<'arena> Unpin for Expression<'arena>

§

impl<'arena> !UnwindSafe for Expression<'arena>

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.