Expand description
Logical expression parsing and evaluation.
This module handles parsing and evaluating logical expressions using the
& (AND) and | (OR) operators. Both single (&, |) and double (&&, ||)
operators are accepted for compatibility with C-style syntax.
Logical operators have left associativity with AND having higher precedence than OR. This follows the standard mathematical convention where AND binds tighter than OR.
§Operator Precedence
Logical operators fit into the overall AIMX operator precedence hierarchy:
| Operator Group | Operators | Associativity |
|---|---|---|
| Equality | = != | Left to right |
| Logical AND | & && | Left to right |
| Logical OR | ` | |
| Conditional | ? : | Right to left |
Note that logical AND has higher precedence than logical OR, so expressions like
a & b | c & d are evaluated as (a & b) | (c & d).
§Grammar
or := and (S? ('|' | "||") S? and)*
and := equality (S? ('&' | "&&") S? equality)*Where S represents optional whitespace.
§Examples
§Basic Operations
true & false // false
true | false // true
true && false // false (C-style compatibility)
true || false // true (C-style compatibility)§Complex Expressions
x > 0 & y < 10 // Both conditions must be true
x > 0 | y < 10 // Either condition can be true
a & b | c & d // Evaluated as (a & b) | (c & d)
!x & y | z // Evaluated as (!x & y) | z§Type Safety
Logical operators require boolean operands. When evaluating expressions:
- Both operands are checked for boolean type compatibility
- Type mismatch errors provide detailed error messages with source locations
- Evaluation follows standard logical rules with short-circuiting semantics
§Evaluation Behavior
- AND (
&): Returnstrueonly if both operands aretrue. Implements short-circuiting - if the left operand isfalse, the right operand is not evaluated. - OR (
|): Returnstrueif at least one operand istrue. Implements short-circuiting - if the left operand istrue, the right operand is not evaluated. - Error Handling: Type mismatches return descriptive error messages with the formula and types involved
§Related Modules
equality- Higher precedence equality operationsexpression- Top-level expression parsingevaluate- Core evaluation traitsconditional- Lower precedence ternary operations
Enums§
- Logical
And - A logical AND expression node in the abstract syntax tree.
- Logical
Or - A logical OR expression node in the abstract syntax tree.