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 referenceParentheses- A parenthesized expressionPostfix- 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
crate::expressions::parse_primary- Function to parse primary expressions from textExpressionLike- Trait for expression evaluationLiteral- Literal value representationReference- Variable reference representation
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
impl Primary
Sourcepub fn is_literal(&self) -> bool
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());Sourcepub fn is_reference(&self) -> bool
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 Display for Primary
impl Display for Primary
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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
impl ExpressionLike for Primary
Source§fn evaluate(&self, context: &mut dyn ContextLike) -> Result<Value>
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
Literalexpressions return their literal valueReferenceexpressions look up the variable in the contextParenthesesexpressions 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
ExpressionLike- The trait being implementedValue- The result type returned by evaluationContextLike- The context trait for evaluation
Source§fn write(&self, writer: &mut Writer)
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
Literalexpressions write their literal representationReferenceexpressions write the reference pathParenthesesexpressions write parentheses around the inner expression- Flattened variants delegate to their respective write methods
Source§fn to_sanitized(&self) -> String
fn to_sanitized(&self) -> String
Source§fn to_formula(&self) -> String
fn to_formula(&self) -> String
impl StructuralPartialEq for Primary
Auto Trait Implementations§
impl Freeze for Primary
impl RefUnwindSafe for Primary
impl Send for Primary
impl Sync for Primary
impl Unpin for Primary
impl UnwindSafe for Primary
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.