Module logical

Source
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 GroupOperatorsAssociativity
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 (&): Returns true only if both operands are true. Implements short-circuiting - if the left operand is false, the right operand is not evaluated.
  • OR (|): Returns true if at least one operand is true. Implements short-circuiting - if the left operand is true, the right operand is not evaluated.
  • Error Handling: Type mismatches return descriptive error messages with the formula and types involved

Enums§

LogicalAnd
A logical AND expression node in the abstract syntax tree.
LogicalOr
A logical OR expression node in the abstract syntax tree.

Functions§

parse_and
Parses logical AND operations (&, &&) according to left associativity, or falls back to parsing equality expressions.
parse_or
Parse a logical OR expression.