Enum Expression

Source
pub enum Expression {
    Empty,
    Errata(Errata),
    Single(Conditional),
    Array(Vec<Box<Conditional>>),
    ArgumentList(Vec<Box<Closure>>),
    Primary(Box<Primary>),
}
Expand description

The root of the Abstract Syntax Tree (AST) for parsed AIMX expressions.

This enum represents all possible types of expressions that can be parsed and evaluated in the AIMX language. It encompasses the complete grammar hierarchy from the top-level expression types down to individual literals and references through the flattened Primary variant.

The Expression enum serves as the entry point for expression evaluation and provides methods for checking expression properties and invoking evaluation.

§Variants

  • Empty - An empty expression representing no operation
  • Errata - An expression containing parsing or evaluation errors
  • Single - A single conditional expression (ternary operator)
  • Array - An array of conditional expressions separated by commas
  • ArgumentList - A list of closure expressions for function arguments
  • Primary - A flattened primary expression for optimization

§AST Flattening

The Primary variant represents an optimization where expressions that consist solely of primary expressions (literals, references, parentheses) are flattened to reduce AST depth and improve evaluation performance. This flattening occurs during parsing when an expression doesn’t contain any higher-level operators.

§Examples

Parsing expressions with the public API:

use aimx::{aimx_parse, ExpressionLike, Context};

// Parse a simple literal (gets flattened to Primary)
let expression = aimx_parse("42");
assert!(matches!(expression, aimx::Expression::Primary(_)));

// Parse conditional expressions (not flattened)
let expression = aimx_parse("true ? 1 : 0");
assert!(matches!(expression, aimx::Expression::Single(_)));

// Parse another conditional expression
let expression = aimx_parse("5 > 3 ? \"yes\" : \"no\"");
assert!(matches!(expression, aimx::Expression::Single(_)));

§See Also

Variants§

§

Empty

§

Errata(Errata)

§

Single(Conditional)

§

Array(Vec<Box<Conditional>>)

§

ArgumentList(Vec<Box<Closure>>)

§

Primary(Box<Primary>)

Primary flattened AST optimization

Implementations§

Source§

impl Expression

Source

pub fn is_empty(&self) -> bool

Check if this expression is empty.

Returns true if this expression represents an empty expression (no operation), false otherwise.

§Examples
use aimx::expression::{parse_expression, Expression};

let (_, empty_expr) = parse_expression("").unwrap();
assert!(empty_expr.is_empty());

let (_, non_empty_expr) = parse_expression("42").unwrap();
assert!(!non_empty_expr.is_empty());
Source

pub fn has_error(&self) -> bool

Check if this expression contains an error.

Returns true if this expression represents an error condition (parsing failure or evaluation error), false otherwise.

§Examples
use aimx::expression::{parse_expression, Expression};
use aimx::{aimx_parse, ExpressionLike};

// Valid expressions do not have errors
let expression = aimx_parse("42");
assert!(!expression.has_error());

// Parsing errors are wrapped in Errata variants
let expression = aimx_parse("2 + * 3"); // Invalid syntax
assert!(expression.has_error());
Source

pub fn invoke(&self, context: &mut dyn ContextLike) -> Value

Evaluate this expression and return a Value, handling errors gracefully. Invoke is the evaluation entry point for expression formulas, whereas Expression::evaluate is used within the expression to evaluate nested expression instances.

This method provides a simplified evaluation interface that catches evaluation errors and converts them into Errata values. Unlike evaluate, this method never returns a Result error - all errors are captured in the returned Value.

§Arguments
  • context - The evaluation context providing variable values and function implementations
§Returns

Returns a Value representing the result of evaluation. If evaluation succeeds, returns the computed value. If evaluation fails, returns an Errata value containing error information.

§Examples
use aimx::expression::{parse_expression, Expression};
use aimx::{Context, ExpressionLike};

// Parse an expression
let (_, expression) = parse_expression("2 + 2").unwrap();
let mut context = Context::new();
let result = expression.invoke(&mut context);
assert_eq!(result.to_string(), "4");
§See Also

Trait Implementations§

Source§

impl Clone for Expression

Source§

fn clone(&self) -> Expression

Returns a duplicate of the value. Read more
1.0.0 · 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 Display for Expression

Source§

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

Format this expression as a string.

This implementation delegates to the write method using a string-based writer.

Source§

impl ExpressionLike for Expression

Source§

fn evaluate(&self, context: &mut dyn ContextLike) -> Result<Value>

Evaluate this expression within the given context. Evaluate is called within an expression formula to evaluate nested expressions.

This method recursively evaluates the expression tree, delegating to the appropriate evaluation methods for each expression variant.

Expression::invoke is the evaluation entry point for expression formulas.

§Arguments
  • context - The evaluation context providing variable values and function implementations
§Returns

Returns a Result containing the evaluated Value if successful, or an error if evaluation fails.

§Evaluation Strategy
Source§

fn write(&self, writer: &mut Writer)

Write this expression to a Writer for formatting.

This method recursively writes the expression tree to the provided writer, producing a formatted representation of the expression.

§Arguments
  • writer - The writer to write the formatted expression to
Source§

fn to_sanitized(&self) -> String

Convert this expression to a sanitized string representation.

Sanitized expressions are suitable for display purposes and may omit certain implementation details or sensitive information.

Source§

fn to_formula(&self) -> String

Convert this expression to its original formula representation.

The formula representation attempts to reconstruct the original expression text as it was parsed.

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 · 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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. 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
§

impl<T> ToStringFallible for T
where T: Display,

§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,