Expression Language
Expression Language
Section titled “Expression Language”Evaluate dynamic expressions using expr-lang syntax. Compile and execute safe expressions for filtering, validation, and rule evaluation without full Lua execution.
Caching
Section titled “Caching”expr.eval keeps an internal LRU cache of compiled expressions (default capacity 1000). The cache is built into the module and requires no configuration.
Loading
Section titled “Loading”local expr = require("expr")Evaluating Expressions
Section titled “Evaluating Expressions”Evaluate an expression string and return the result. Uses internal LRU cache for compiled expressions:
-- Simple mathlocal result = expr.eval("1 + 2 * 3") -- 7
-- With variableslocal total = expr.eval("price * quantity", { price = 29.99, quantity = 3}) -- 89.97
-- Boolean expressionslocal is_adult = expr.eval("age >= 18", {age = 21}) -- true
-- String operationslocal greeting = expr.eval('name + " is " + status', { name = "Alice", status = "online"}) -- "Alice is online"
-- Ternary operatorlocal label = expr.eval('score > 90 ? "A" : score > 80 ? "B" : "C"', { score = 85}) -- "B"
-- Array operationslocal has_admin = expr.eval('"admin" in roles', { roles = {"user", "admin", "viewer"}}) -- true| Parameter | Type | Description |
|---|---|---|
expression | string | expr-lang syntax expression |
env | table | Variable environment for expression (optional) |
Returns: any, error
Compiling Expressions
Section titled “Compiling Expressions”Compile an expression into a reusable Program object for repeated evaluation:
-- Compile once for repeated uselocal discount_calc, err = expr.compile("price * (1 - discount_rate)")if err then return nil, errend
-- Reuse with different inputslocal price1 = discount_calc:run({price = 100, discount_rate = 0.1}) -- 90local price2 = discount_calc:run({price = 50, discount_rate = 0.2}) -- 40local price3 = discount_calc:run({price = 200, discount_rate = 0.15}) -- 170| Parameter | Type | Description |
|---|---|---|
expression | string | expr-lang syntax expression |
env | table | Type hint environment for compilation (optional) |
Returns: Program, error
Running Compiled Programs
Section titled “Running Compiled Programs”Execute a compiled expression with provided environment:
-- Validation rulelocal validator, _ = expr.compile("len(password) >= 8 and len(password) <= 128")
local valid1 = validator:run({password = "short"}) -- falselocal valid2 = validator:run({password = "securepass123"}) -- true
-- Pricing rulelocal pricer, _ = expr.compile([[ base_price * quantity * (1 - bulk_discount) + shipping]])
local order_total = pricer:run({ base_price = 25.00, quantity = 10, bulk_discount = 0.15, shipping = 12.50}) -- 225.00| Parameter | Type | Description |
|---|---|---|
env | table | Variable environment for expression (optional) |
Returns: any, error
Built-in Functions
Section titled “Built-in Functions”Expr-lang provides many built-in functions:
-- Math functionsexpr.eval("max(1, 5, 3)") -- 5expr.eval("min(10, 2, 8)") -- 2expr.eval("abs(-42)") -- 42expr.eval("ceil(3.2)") -- 4expr.eval("floor(3.8)") -- 3
-- String functionsexpr.eval('len("hello")') -- 5expr.eval('upper("hello")') -- "HELLO"expr.eval('lower("HELLO")') -- "hello"expr.eval('trim(" hi ")') -- "hi"expr.eval('"hello" contains "ell"') -- true
-- Array functionsexpr.eval("len(items)", {items = {1,2,3}}) -- 3expr.eval("sum(values)", {values = {1,2,3,4}}) -- 10Errors
Section titled “Errors”| Condition | Kind | Retryable |
|---|---|---|
| Expression is empty | errors.INVALID | no |
| Expression syntax invalid | errors.INTERNAL | no |
| Expression evaluation fails | errors.INTERNAL | no |
| Result conversion fails | errors.INTERNAL | no |
See Error Handling for working with errors.