Transform Reference

Expression Macro

Infix arithmetic formulas compiled to a verb tree at parse time, with explicit variable bindings.

VerbSyntaxDescriptionTry It
expr%expr "formula" [@bindings]Evaluate an infix arithmetic formula (parse-time macro) Try

Reference

Parse-time macro: the formula compiles to a tree of existing verbs (add, multiply, pow, ...), so it is exactly as deterministic as those verbs. Operators: + - * / % ^ and unary -. Functions (deterministic only): abs, floor, ceil, trunc, round, sqrt, pow, min, max. Precedence high to low: parentheses and calls, ^ (right-associative), unary minus, * / %, then + -. Bindings are explicit: a variable used without a bindings object is a compile error.

Examples

Expression macro examples
; Literal formula - no variables, no bindings
answer = %expr "2^3^2"                       ; -> 512 (^ is right-associative)
neg = %expr "-2^2"                           ; -> -4 (unary minus is looser than ^)

; Variables resolve under an explicit bindings object
{v}
a = #2
b = #3
c = #4
x = #5

quadratic = %expr "a*x^2 + b*x + c" @.v      ; a -> @.v.a ... -> 69
hyp = %expr "sqrt(x^2 + y^2)" @.v            ; with x=3, y=4 -> 5