expr

Macro expr 

Source
expr!() { /* proc-macro */ }
Expand description

The expr! procedural macro for boolean expressions

Provides clean syntax for building boolean expressions from existing BoolExpr values with proper operator precedence.

§Supported Syntax

  • a - Variable or any BoolExpr identifier in scope
  • "a" - String literal (creates BoolExpr::variable("a") automatically)
  • 0 - False constant (creates BoolExpr::constant(false))
  • 1 - True constant (creates BoolExpr::constant(true))
  • !a or ~a - NOT operation (both syntaxes supported, like the parser)
  • a * b or a & b - AND operation (both * and & supported)
  • a + b or a | b - OR operation (both + and | supported)
  • (a + b) * c - Parentheses for grouping

§Operator Precedence

From highest to lowest:

  1. ( ) (Parentheses - force evaluation order)
  2. ! / ~ (NOT)
  3. * / & (AND)
  4. + / | (OR)

§Examples

use espresso_logic::{BoolExpr, expr};

// Option 1: Use string literals (variables created automatically)
let xor = expr!("a" * !"b" + !"a" * "b");
let complex = expr!(("a" + "b") * "c");

// Option 2: Use existing BoolExpr variables
let a = BoolExpr::variable("a");
let b = BoolExpr::variable("b");
let c = BoolExpr::variable("c");
let and_expr = expr!(a * b);
let or_expr = expr!(a + b);
let not_expr = expr!(!a);

// Option 3: Use constants
let with_const = expr!("a" * 1 + "b" * 0);  // a AND true OR b AND false
let always_true = expr!(1);
let always_false = expr!(0);

// Option 4: Mix all styles
let expr1 = expr!(a * b);
let combined = expr!(expr1 + "c" * 1);

// Complex nested expressions
let xor = expr!(a * !b + !a * b);
let complex = expr!((a + b) * c);

// Can compose sub-expressions
let sub_expr1 = expr!(a * b);
let sub_expr2 = expr!(c + !a);
let combined = expr!(sub_expr1 + sub_expr2);