Skip to main content

Expression

Struct Expression 

Source
pub struct Expression { /* private fields */ }
Expand description

A symbolic expression in postfix notation

Implementations§

Source§

impl Expression

Source

pub fn new() -> Self

Create an empty expression

Source

pub fn parse(s: &str) -> Option<Self>

Parse a well-formed postfix expression (e.g., “32s1+s*”).

This validates stack discipline while parsing, so malformed or incomplete postfix strings return None instead of constructing an expression that will later panic during formatting.

Source

pub fn symbols(&self) -> &[Symbol]

Get the symbols in this expression

Source

pub fn len(&self) -> usize

Get the expression length

Source

pub fn is_empty(&self) -> bool

Check if expression is empty

Source

pub fn complexity(&self) -> u32

Get the complexity score

Source

pub fn contains_x(&self) -> bool

Check if this expression contains the variable x

Source

pub fn count_symbol(&self, sym: Symbol) -> u32

Count occurrences of a symbol in this expression.

Source

pub fn is_valid(&self) -> bool

Check if this is a valid complete expression (stack depth = 1)

This method is part of the public API for external consumers who may want to validate expressions before processing them.

Source

pub fn push(&mut self, sym: Symbol)

Append a symbol to this expression

Source

pub fn pop(&mut self) -> Option<Symbol>

Remove the last symbol

Source

pub fn push_with_table(&mut self, sym: Symbol, table: &SymbolTable)

Append a symbol using a symbol table for weight lookup

This is the table-driven version that uses per-run configuration instead of global overrides.

Source

pub fn pop_with_table(&mut self, table: &SymbolTable) -> Option<Symbol>

Remove the last symbol using a symbol table for weight lookup

This is the table-driven version that uses per-run configuration instead of global overrides.

Source

pub fn to_postfix(&self) -> String

Get the postfix string representation

Source

pub fn try_to_infix(&self) -> Result<String, EvalError>

Convert to infix notation for display

Uses proper operator precedence and associativity rules:

  • Precedence levels (higher = tighter binding):
    • 100: Atoms (constants, x, function calls)
    • 9: Power (right-associative)
    • 7: Unary operators (negation, reciprocal)
    • 6: Multiplication, division
    • 5: Addition, subtraction
  • Right-associative operators (power) bind right-to-left
  • Left-associative operators bind left-to-right

Convert to infix notation, returning Err(EvalError::StackUnderflow) if the expression is malformed (e.g. a binary operator with no operands).

Prefer this over to_infix when the expression may come from untrusted or user-provided input.

Source

pub fn to_infix(&self) -> String

Source

pub fn to_infix_or_postfix(&self) -> String

Format as infix, falling back to postfix when the expression is malformed.

Prefer this over to_infix at FFI and other untrusted-output boundaries where a panic would be unacceptable.

Source

pub fn to_infix_with_table(&self, table: &SymbolTable) -> String

Convert to infix notation using a symbol table for display names

This is the table-driven version that uses per-run configuration instead of global overrides for symbol display names.

Source

pub fn to_infix_with_format(&self, format: OutputFormat) -> String

Convert to infix notation with specified format

Source

pub fn operator_count(&self) -> usize

Count the number of operators (non-atoms) in the expression

Source

pub fn tree_depth(&self) -> usize

Compute the maximum depth of the expression tree

Source

pub fn to_infix_mathematica(&self) -> String

Source

pub fn to_infix_sympy(&self) -> String

Trait Implementations§

Source§

impl Clone for Expression

Source§

fn clone(&self) -> Expression

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expression

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Expression

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Expression

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for Expression

Source§

impl Hash for Expression

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Expression

Source§

fn eq(&self, other: &Expression) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Expression

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.