Module expression

Source
Expand description

Top-level expression parsing and evaluation for the AIMX language.

This module defines the Expression enum that represents the root of the Abstract Syntax Tree (AST) for parsed AIMX expressions. It provides parsing functions that handle the complete AIMX grammar hierarchy, including arrays, argument lists, and the full operator precedence chain.

The module bridges the gap between raw text input and the executable expression tree that can be evaluated within a Context. It orchestrates the parsing of complex expressions by delegating to specialized sub-parsers for different operator precedence levels.

§Expression Structure

The Expression enum represents the top-level AST node that can contain:

  • Empty expressions (no operation)
  • Single conditional expressions (the most common case)
  • Arrays of expressions separated by commas
  • Argument lists for function calls
  • Flattened primary expressions (for performance optimization)

§Usage

The primary way to parse expressions is through the public aimx_parse function:

use aimx::{aimx_parse, ExpressionLike, Context};

// Parse and evaluate a simple expression
let expression = aimx_parse("2 + 3 * 4");
let mut context = Context::new();
let result = expression.evaluate(&mut context).unwrap();
assert_eq!(result.to_string(), "14");

// Parse and evaluate array expressions
let expression = aimx_parse("(1, 2, 3, 4, 5)");
// Arrays are evaluated as Value::Array

For more advanced usage, the parsing functions in this module can be used directly:

use aimx::expression::{parse_expression, parse_argument_list};

// Direct parsing (returns nom::IResult)
let result = parse_expression("2 + 3 * 4");
assert!(result.is_ok());

// Parse argument lists for function calls
let result = parse_argument_list("1, 2, 3");
assert!(result.is_ok());

§See Also

Enums§

Expression
The root of the Abstract Syntax Tree (AST) for parsed AIMX expressions.

Functions§

parse_argument_list
Parse a comma-separated argument list for function calls.
parse_expression
Parse a complete AIMX expression string into an abstract syntax tree.