Module postfix

Source
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)

OperatorDescriptionExample
_Empty placeholder_
()Function callsum(1, 2, 3)
[]Array indexingarray[0]
.Method accessdata.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 parser

The 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

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.