aimx/expressions/
mod.rs

1//! Expression parsing and evaluation modules for the AIMX language.
2//!
3//! This module contains the core expression parsing infrastructure for the AIMX
4//! (Agentic Inference Markup Expressions) language. It provides a hierarchical
5//! parser that follows the operator precedence rules defined in the AIMX grammar.
6//!
7//! # Expression Grammar Hierarchy
8//!
9//! The expression modules are organized according to operator precedence, with
10//! higher precedence operators parsed first. The parsing hierarchy follows this
11//! order (highest to lowest precedence):
12//!
13//! 1. **Primary** - Literals, references, parentheses (`parse_primary`)
14//! 2. **Postfix** - Method calls, indexing, function calls (`.`, `()`, `[]`, `$`)
15//! 3. **Unary** - Prefix operators (`!`, `+`, `-`, casts)
16//! 4. **Multiplicative** - Multiplication, division, modulo (`*`, `/`, `%`)
17//! 5. **Additive** - Addition, subtraction (`+`, `-`)
18//! 6. **Relational** - Comparison operators (`<`, `<=`, `>`, `>=`)
19//! 7. **Equality** - Equality and inequality (`=`, `!=`)
20//! 8. **Logical** - Logical AND and OR (`&`, `&&`, `|`, `||`)
21//! 9. **Conditional** - Ternary operator (`?`, `:`)
22//! 10. **Closure** - Anonymous functions (`=>`)
23//! 11. **Procedure** - Sequence expressions (`;`)
24//!
25//! Each module provides both parsing functions and AST node types that implement
26//! the [`ExpressionLike`](crate::ExpressionLike) trait for evaluation.
27//!
28//! # AST Flattening Optimization
29//!
30//! The expression modules use an AST flattening optimization where each expression
31//! type includes variants for all lower-precedence expression types. This allows
32//! for efficient evaluation without deep recursion and simplifies the implementation
33//! of the [`ExpressionLike`](crate::ExpressionLike) trait.
34//!
35//! # Public API
36//!
37//! The public API of this module exposes parsing functions and AST node types for
38//! each expression level. These can be used for advanced parsing scenarios or when
39//! working with specific expression types:
40//!
41//! ```rust
42//! use aimx::expressions::{parse_additive, Additive};
43//!
44//! // Parse an additive expression
45//! let result = parse_additive("2 + 3");
46//! assert!(result.is_ok());
47//! ```
48//!
49//! # Related Modules
50//!
51//! - [`expression`](crate::expression) - Top-level expression parsing and evaluation
52//! - [`evaluate`](crate::evaluate) - Core evaluation traits
53//! - [`literal`](crate::literal) - Literal value parsing
54//! - [`value`](crate::value) - Runtime value representation
55
56// Core expression parsing and evaluation modules
57pub mod additive;
58pub mod closure;
59pub mod conditional;
60pub mod equality;
61pub mod logical;
62pub mod multiplicative;
63pub mod postfix;
64pub mod primary;
65pub mod procedure;
66pub mod reference;
67pub mod relational;
68pub mod unary;
69
70// Re-export key types for public API
71
72/// Additive expression parsing and evaluation (`+`, `-` operators)
73pub use additive::{Additive, parse_additive};
74
75/// Closure expression parsing and evaluation (`=>` operator)
76pub use closure::{Closure, parse_closure, parse_closure_conditional};
77
78/// Conditional expression parsing and evaluation (`?`, `:` ternary operator)
79pub use conditional::{Conditional, parse_conditional};
80
81/// Equality expression parsing and evaluation (`=`, `!=` operators)
82pub use equality::{Equality, parse_equality};
83
84/// Logical expression parsing and evaluation (`&`, `&&`, `|`, `||` operators)
85pub use logical::{LogicalAnd, parse_and, LogicalOr, parse_or};
86
87/// Multiplicative expression parsing and evaluation (`*`, `/`, `%` operators)
88pub use multiplicative::{Multiplicative, parse_multiplicative};
89
90/// Postfix expression parsing and evaluation (`.`, `()`, `[]`, `$` operators)
91pub use postfix::{Postfix, parse_postfix, parse_accessor};
92
93/// Primary expression parsing and evaluation (literals, references, parentheses)
94pub use primary::{Primary, parse_parentheses, parse_primary};
95
96/// Procedure expression parsing and evaluation (`;` operator within closures)
97pub use procedure::{Procedure, parse_procedure};
98
99/// Reference expression parsing and evaluation (variable and field references)
100pub use reference::{Reference, parse_reference, parse_identifier};
101
102/// Relational expression parsing and evaluation (`<`, `<=`, `>`, `>=` operators)
103pub use relational::{Relational, parse_relational};
104
105/// Unary expression parsing and evaluation (`!`, `+`, `-`, casts)
106pub use unary::{Unary, parse_unary};