Module multiplicative

Source
Expand description

Multiplicative expression parsing and evaluation.

This module handles parsing and evaluating multiplicative expressions using the * (multiplication), / (division), and % (modulus) operators. Multiplicative operators have left associativity and higher precedence than additive operators (+, -).

Multiplicative expressions are a fundamental part of the AIMX grammar hierarchy and provide the building blocks for arithmetic expressions. The module implements the core parsing logic and evaluation behavior for these operations, including type promotion and error handling.

§Grammar

Multiplicative expressions are parsed according to the following grammar:

multiplicative := unary (S? ('*' | '/' | '%') S? unary)*

Where S represents optional whitespace. The parser builds an AST with left associativity, ensuring operations are grouped from left to right.

§Operator Precedence

Multiplicative operators have the following precedence characteristics:

  • Higher precedence than: additive (+, -), relational (<, <=, >, >=), equality (=, !=), logical (&, |), conditional (?, :), closure (=>), procedure (;)
  • Lower precedence than: unary (!, +, -, casts), postfix (., (), [], $), primary (literals, references, parentheses)

§Examples

2 * 3                    // 6
10 / 2                   // 5
17 % 5                   // 2
2 * 3 / 4 * 5            // 7.5 (left associative: ((2 * 3) / 4) * 5)
2 + 3 * 4                // 14 (multiplicative precedence: 2 + (3 * 4))
(2 + 3) * 4              // 20 (parentheses override precedence: (2 + 3) * 4)

§Type Behavior

Multiplicative operations require numeric operands and support type promotion:

  • Multiplication (*): 2 * 36
  • Division (/): 10 / 25
  • Modulus (%): 17 % 52
  • Type Promotion: When operands have different types, the right operand is promoted to match the left operand’s type using AIMX’s type promotion system

§Error Handling

During evaluation, multiplicative operations:

  • Require numeric operands (return error for non-numeric types)
  • Check for division by zero (return error for / 0 or % 0)
  • Use type promotion via evaluate_and_promote
  • Provide detailed error messages with type information

§AST Structure

The Multiplicative enum represents the AST structure, which supports both operations and flattened Primary expressions for optimization.

§Examples

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

// Parse and evaluate a simple multiplication
let expression = aimx_parse("123 * 456");
let mut context = Context::new();
let result = expression.evaluate(&mut context).unwrap();
assert_eq!(result.to_string(), "56088");

// Parse chained operations (left associative)
let expression = aimx_parse("100 * 50 / 25 % 5");
// Parsed as ((100 * 50) / 25) % 5 = 0
let result = expression.evaluate(&mut context).unwrap();
assert_eq!(result.to_string(), "0");
  • unary - Higher precedence unary operations
  • additive - Lower precedence additive operations
  • expression - Top-level expression parsing
  • evaluate - Core evaluation traits

Enums§

Multiplicative
A multiplicative expression node in the abstract syntax tree.

Functions§

parse_multiplicative
Parse a multiplicative expression.