AstExpr

Enum AstExpr 

Source
#[repr(C, align(8))]
pub enum AstExpr<'arena> { Constant(Real), Variable(&'arena str), Function { name: &'arena str, args: &'arena [AstExpr<'arena>], }, Array { name: &'arena str, index: &'arena AstExpr<'arena>, }, Attribute { base: &'arena str, attr: &'arena str, }, LogicalOp { op: LogicalOperator, left: &'arena AstExpr<'arena>, right: &'arena AstExpr<'arena>, }, Conditional { condition: &'arena AstExpr<'arena>, true_branch: &'arena AstExpr<'arena>, false_branch: &'arena AstExpr<'arena>, }, }
Expand description

Abstract Syntax Tree (AST) node representing an expression.

The AST is the core data structure used for representing parsed expressions. Each variant of this enum represents a different type of expression node, forming a tree structure that can be evaluated to produce a result.

This type uses arena allocation for all strings and recursive structures, eliminating all dynamic allocations during evaluation.

Using repr(C) with explicit discriminant type and alignment to avoid ARM alignment issues

Variants§

§

Constant(Real)

A literal numerical value.

Examples: 3.14, 42, -1.5

§

Variable(&'arena str)

A named variable reference.

Examples: x, temperature, result

§

Function

A function call with a name and list of argument expressions.

Examples: sin(x), max(a, b), sqrt(x*x + y*y)

Fields

§name: &'arena str

The name of the function being called

§args: &'arena [AstExpr<'arena>]

The arguments passed to the function

§

Array

An array element access.

Examples: array[0], values[i+1]

Fields

§name: &'arena str

The name of the array

§index: &'arena AstExpr<'arena>

The expression for the index

§

Attribute

An attribute access on an object.

Examples: point.x, settings.value

Fields

§base: &'arena str

The base object name

§attr: &'arena str

The attribute name

§

LogicalOp

A logical operation with short-circuit evaluation.

Represents logical AND (&&) and OR (||) operations with short-circuit behavior. Unlike function-based operators, these operators have special evaluation semantics where the right operand may not be evaluated based on the value of the left operand.

§Examples

  • a && b: Evaluates a, then evaluates b only if a is non-zero (true)
  • c || d: Evaluates c, then evaluates d only if c is zero (false)
  • x > 0 && y < 10: Checks if both conditions are true, with short-circuit
  • flag || calculate_value(): Skips calculation if flag is true

§Boolean Logic

The engine represents boolean values as floating-point numbers:

  • 0.0 is considered false
  • Any non-zero value is considered true, typically 1.0 is used

§Operator Precedence

&& has higher precedence than ||, consistent with most programming languages:

  • a || b && c is interpreted as a || (b && c)
  • Use parentheses to override default precedence: (a || b) && c

Fields

§op: LogicalOperator

The logical operator (AND or OR)

§left: &'arena AstExpr<'arena>

The left operand (always evaluated)

§right: &'arena AstExpr<'arena>

The right operand (conditionally evaluated based on left value)

§

Conditional

A ternary conditional operation (condition ? true_expr : false_expr).

Represents a conditional expression with three parts: a condition to evaluate, an expression to return if the condition is true, and an expression to return if the condition is false. This uses short-circuit evaluation, meaning only the relevant branch is evaluated.

§Examples

  • x > 0 ? 1 : -1: Returns 1 if x is positive, -1 otherwise
  • flag ? value1 : value2: Chooses between two values based on flag
  • a > b ? a : b: Returns the maximum of a and b

§Boolean Logic

Like logical operations, the ternary operator uses floating-point values for boolean logic:

  • 0.0 is considered false
  • Any non-zero value is considered true

§Short-Circuit Evaluation

Only one branch is evaluated based on the condition result:

  • If condition is non-zero (true), only the true_branch is evaluated
  • If condition is zero (false), only the false_branch is evaluated

§Operator Precedence

The ternary operator has low precedence:

  • a + b ? c : d * e is interpreted as (a + b) ? c : (d * e)
  • Use parentheses for clarity when nesting operations

Fields

§condition: &'arena AstExpr<'arena>

The condition expression to evaluate

§true_branch: &'arena AstExpr<'arena>

Expression to evaluate if condition is true (non-zero)

§false_branch: &'arena AstExpr<'arena>

Expression to evaluate if condition is false (zero)

Implementations§

Source§

impl<'arena> AstExpr<'arena>

Source

pub fn pow(&self, exp: Real) -> Real

Helper method that raises a constant expression to a power.

This is primarily used in testing to evaluate power operations on constants. For non-constant expressions, it returns 0.0 as a default value.

§Parameters
  • exp - The exponent to raise the constant to
§Returns

The constant raised to the given power, or 0.0 for non-constant expressions

Trait Implementations§

Source§

impl<'arena> Debug for AstExpr<'arena>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'arena> Freeze for AstExpr<'arena>

§

impl<'arena> RefUnwindSafe for AstExpr<'arena>

§

impl<'arena> Send for AstExpr<'arena>

§

impl<'arena> Sync for AstExpr<'arena>

§

impl<'arena> Unpin for AstExpr<'arena>

§

impl<'arena> UnwindSafe for AstExpr<'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.