Expand description
Postfix expression parsing and evaluation for the AIMX language.
This module provides the parsing and evaluation logic for postfix expressions, which are operations that occur after their operands. Postfix expressions have left-to-right associativity and include function calls, array indexing, method calls, and inference calls.
Postfix expressions are the second-highest precedence level in the AIMX grammar
hierarchy, coming immediately after primary expressions. They form a critical
part of the operator precedence resolution system, enabling complex chained
operations like data.filter().map() or matrix[0][1].
§Postfix Operators (Left-to-Right Associativity)
| Operator | Description | Example |
|---|---|---|
_ | Empty placeholder | _ |
() | Function call | sum(1, 2, 3) |
[] | Array indexing | array[0] |
. | Method access | data.filter() |
$ | Inference call | $std.extract(document, prompt) |
§Grammar Rules
The postfix grammar is more complex than a simple rule due to special handling for inference calls and the empty placeholder:
// Simplified grammar - actual implementation includes special cases
postfix = function_call | primary (("." method_call) | indexing)*
function_call = identifier "(" argument_list ")" // Special handling for "_"
method_call = identifier "(" argument_list ")"
indexing = "[" expression "]"
inference_call = "$" reference "(" argument_list ")" // Special handling in parserThe parser has special logic for:
- The empty placeholder
_which is treated as a special function name - Inference calls that start with
$and are detected during primary parsing
§Examples
use aimx::expressions::postfix::{parse_postfix, Postfix};
use aimx::Context;
// Parse empty placeholder
let (_, postfix) = parse_postfix("_").unwrap();
assert!(matches!(postfix, Postfix::Empty));
// Parse function call
let (_, postfix) = parse_postfix("sum(1, 2, 3)").unwrap();
assert!(matches!(postfix, Postfix::Function(_, _)));
// Parse array indexing
let (_, postfix) = parse_postfix("array[0]").unwrap();
assert!(matches!(postfix, Postfix::Index(_, _)));
// Parse method chaining
let (_, postfix) = parse_postfix("data.filter().map()").unwrap();
assert!(matches!(postfix, Postfix::Method(_, _, _)));
// Parse nested indexing
let (_, postfix) = parse_postfix("matrix[0][1]").unwrap();
assert!(matches!(postfix, Postfix::Index(_, _)));§See Also
crate::expression- Top-level expression parsing and evaluationcrate::expressions::primary- Primary expressions (literals, references, parentheses)crate::expressions::unary- Unary expressions (next precedence level)crate::Context- Evaluation context for function and method calls
Enums§
- Postfix
- Represents a postfix expression in the AIMX grammar.
Functions§
- parse_
accessor - Parse an accessor (dot notation) with optional whitespace.
- parse_
postfix - Parse a postfix expression from a string.