Expand description
Additive expression parsing and evaluation for the AIMX language.
This module handles parsing and evaluating additive expressions using the + (addition)
and - (subtraction) operators. Additive operators have left associativity and
higher precedence than relational operators but lower precedence than multiplicative
operators (*, /, %).
Additive expressions are a fundamental part of the AIMX grammar hierarchy and provide the building blocks for arithmetic expressions, string operations, and boolean logic. The module implements the core parsing logic and evaluation behavior for these operations.
§Grammar
Additive expressions follow the grammar:
additive := multiplicative (S? ('+' | '-') S? multiplicative)*Where S represents optional whitespace. The parser builds an abstract syntax tree
(AST) with left associativity, ensuring operations are grouped from left to right.
§Operator Precedence
Additive operators have the following precedence characteristics:
- Higher precedence than: relational (
<,<=,>,>=), equality (=,!=), logical (&,|), conditional (?,:), closure (=>), procedure (;) - Lower precedence than: multiplicative (
*,/,%), unary (!,+,-), postfix (.,(),[]), primary (literals, references, parentheses)
§Examples
2 + 3 + 4 // Parsed as (2 + 3) + 4 = 9 (left associative)
10 - 5 - 2 // Parsed as (10 - 5) - 2 = 3 (left associative)
2 * 3 + 4 // Parsed as (2 * 3) + 4 = 10 (multiplicative precedence)
2 + 3 * 4 // Parsed as 2 + (3 * 4) = 14 (multiplicative precedence)
"Hello" + "World" // Parsed as string concatenation = "HelloWorld"
"abc" - "b" // Parsed as string removal = "ac"
true + false // Parsed as boolean XOR = true
true - false // Parsed as boolean XNOR = false§Type Behavior
Additive operations support operations between compatible types with specific behaviors:
§Addition (+)
- Numbers: Mathematical addition (
2 + 3 = 5) - Text: String concatenation (
"Hello" + "World" = "HelloWorld") - Booleans: XOR operation (
true + false = true,true + true = false) - Other types: Attempted conversion to compatible types where possible
§Subtraction (-)
- Numbers: Mathematical subtraction (
5 - 2 = 3) - Text: String removal (
"HelloWorld" - "World" = "Hello") - Booleans: XNOR operation (
true - false = false,true - true = true) - Other types: Attempted conversion to compatible types where possible
§Boolean Operations Note
The boolean behavior for additive operators is not standard in most programming languages:
+performs XOR operation on booleans (true + false = true,true + true = false)-performs XNOR operation on booleans (true - false = false,true - true = true)
Users should be aware that this is a special feature of the AIMX language and might be unintuitive for developers familiar with other languages.
§Error Handling
During evaluation, additive operations:
- Return errors for incompatible type combinations
- Provide detailed error messages with type information
§Examples
use aimx::expressions::additive::{parse_additive, Additive};
// Parse a simple addition
let result = parse_additive("123 + 456");
assert!(result.is_ok());
assert!(matches!(result.unwrap().1, Additive::Add(_, _)));
// Parse chained operations (left associative)
let result = parse_additive("10 - 5 - 2");
assert!(result.is_ok());
// Parsed as (10 - 5) - 2 = 3§Related Modules
multiplicative- Higher precedence multiplicative operationsrelational- Lower precedence relational operationsexpression- Top-level expression parsingprimary- Flattened primary expression optimization
Enums§
- Additive
- An additive expression node in the abstract syntax tree.
Functions§
- parse_
additive - Parse an additive expression.