Enum Primary

Source
pub enum Primary {
Show 13 variants Literal(Literal), Reference(Reference), Parentheses(Expression), Postfix(Postfix), Unary(Unary), Multiplicative(Multiplicative), Additive(Additive), Relational(Relational), Equality(Equality), LogicalAnd(LogicalAnd), LogicalOr(LogicalOr), Conditional(Conditional), Closure(Closure),
}
Expand description

Represents a primary expression in the AIMX grammar.

Primary expressions are the fundamental building blocks of expressions in the AIMX language. They include literal values, variable references, and parenthesized expressions. Primary expressions form the base of the expression hierarchy, with other expression types building upon them.

The Primary enum uses AST flattening optimization, where it includes variants for all higher-level expression types. This allows expressions that consist solely of primary components to be evaluated more efficiently without deep recursion through the expression hierarchy.

§Variants

  • Literal - A literal value (Bool, Date, Number, Task, Text, or Empty)
  • Reference - A variable or field reference
  • Parentheses - A parenthesized expression
  • Postfix - Postfix operations (method calls, indexing, function calls)
  • Unary - Unary operations (logical NOT, arithmetic negation, casts)
  • Multiplicative - Multiplicative operations (*, /, %)
  • Additive - Additive operations (+, -)
  • Relational - Relational operations (<, <=, >, >=)
  • Equality - Equality operations (=, !=)
  • LogicalAnd - Logical AND operations (&, &&)
  • LogicalOr - Logical OR operations (|, ||)
  • Conditional - Conditional operations (?, :)
  • Closure - Closure operations (=>)

§AST Flattening

The flattened variants (Postfix through Closure) are populated during parsing when an expression contains higher-level operators but can be represented efficiently within the Primary enum. This optimization reduces AST depth and improves evaluation performance.

§Examples

use aimx::{Primary};
use aimx::expressions::{parse_primary};
use aimx::Literal;

// Parse a literal expression
let (_, primary) = parse_primary("42").unwrap();
assert!(matches!(primary, Primary::Literal(Literal::Number(_))));

// Parse a reference expression
let (_, primary) = parse_primary("variable_name").unwrap();
assert!(matches!(primary, Primary::Reference(_)));

// Parse a parenthesized expression
let (_, primary) = parse_primary("(1 + 2)").unwrap();
assert!(matches!(primary, Primary::Parentheses(_)));

§See Also

Variants§

§

Literal(Literal)

A literal value such as a number, text string, boolean, date, or task

§

Reference(Reference)

A variable or field reference

§

Parentheses(Expression)

A parenthesized expression

§

Postfix(Postfix)

§

Unary(Unary)

§

Multiplicative(Multiplicative)

§

Additive(Additive)

§

Relational(Relational)

§

Equality(Equality)

§

LogicalAnd(LogicalAnd)

§

LogicalOr(LogicalOr)

§

Conditional(Conditional)

§

Closure(Closure)

Implementations§

Source§

impl Primary

Source

pub fn is_literal(&self) -> bool

Check if this primary expression is a literal value.

Returns true if this Primary variant is Literal, false otherwise.

§Examples
use aimx::{Primary};
use aimx::expressions::{parse_primary};
use aimx::Literal;

let (_, literal) = parse_primary("42").unwrap();
assert!(literal.is_literal());

let (_, reference) = parse_primary("variable").unwrap();
assert!(!reference.is_literal());
Source

pub fn is_reference(&self) -> bool

Check if this primary expression is a variable or field reference.

Returns true if this Primary variant is Reference, false otherwise.

§Examples
use aimx::{Primary};
use aimx::expressions::{parse_primary};

let (_, reference) = parse_primary("variable").unwrap();
assert!(reference.is_reference());

let (_, literal) = parse_primary("42").unwrap();
assert!(!literal.is_reference());

Trait Implementations§

Source§

impl Clone for Primary

Source§

fn clone(&self) -> Primary

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 Primary

Source§

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

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

impl Display for Primary

Source§

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

Format this primary expression as a string.

This implementation delegates to the write method using a string-based writer, producing a human-readable representation of the expression.

§Arguments
  • f - The formatter to write the string representation to
§Returns

Returns fmt::Result indicating success or failure of formatting.

Source§

impl ExpressionLike for Primary

Source§

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

Evaluate this primary expression within the given context.

This method recursively evaluates the primary expression, delegating to the appropriate evaluation methods for each variant. The evaluation follows the AST flattening optimization where higher-level expression variants are evaluated directly without recursion through the expression hierarchy.

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

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

§Evaluation Strategy
  • Literal expressions return their literal value
  • Reference expressions look up the variable in the context
  • Parentheses expressions evaluate the inner expression
  • Flattened variants delegate to their respective evaluation methods
§Examples
use aimx::{Primary};
use aimx::expressions::{parse_primary};
use aimx::{ContextLike, ExpressionLike, Literal};

let mut context = aimx::Context::new();
 
// Evaluate a literal
let primary = Primary::Literal(Literal::Number(42.0));
let result = primary.evaluate(&mut context).unwrap();
assert_eq!(result.to_string(), "42");
§See Also
Source§

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

Write this primary expression to a Writer for formatting.

This method recursively writes the primary expression to the provided writer, producing a formatted representation suitable for display or serialization.

§Arguments
  • writer - The writer to write the formatted expression to
§Writing Strategy
  • Literal expressions write their literal representation
  • Reference expressions write the reference path
  • Parentheses expressions write parentheses around the inner expression
  • Flattened variants delegate to their respective write methods
Source§

fn to_sanitized(&self) -> String

Convert this primary expression to a sanitized string representation.

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

§Returns

Returns a String containing the sanitized expression representation.

Source§

fn to_formula(&self) -> String

Convert this primary expression to its original formula representation.

The formula representation attempts to reconstruct the original expression text as it was parsed, suitable for use in formulas or serialization.

§Returns

Returns a String containing the formula representation.

Source§

impl PartialEq for Primary

Source§

fn eq(&self, other: &Primary) -> 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 Primary

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,