Documentation
¶
Overview ¶
Package expr is Kiln's tiny expression evaluator. It exists so the world IR can describe predicates, computed values, and conditions declaratively — without requiring an embedded Go interpreter.
The grammar is a deliberately small subset:
expr = or
or = and ("||" and)*
and = comp ("&&" comp)*
comp = add (("==" | "!=" | "<" | ">" | "<=" | ">=") add)?
add = mul (("+" | "-") mul)*
mul = unary (("*" | "/" | "%") unary)*
unary = ("!" | "-") unary | postfix
postfix = primary ("." ident | "[" expr "]" | "(" args? ")")*
primary = number | string | bool | null | ident | "(" expr ")" | "[" args? "]"
Values are JSON-shaped: int64, float64, string, bool, nil, []any, map[string]any. Built-in functions live in Env (see DefaultEnv); the caller may register more.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrArity = errors.New("expr: wrong number of arguments") ErrType = errors.New("expr: wrong argument type") )
Sentinel errors built-in functions return for arity / type mistakes.
Functions ¶
Types ¶
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
Expression is a compiled, ready-to-evaluate expression tree.
func Compile ¶
func Compile(src string) (*Expression, error)
Compile parses src into an Expression. The expression can then be Eval'd repeatedly with different scopes.
func (*Expression) Eval ¶
func (e *Expression) Eval(scope Scope, env *Env) (any, error)
Eval evaluates the compiled expression with the given scope and env. A nil env means DefaultEnv. A nil scope means an empty scope.
func (*Expression) Source ¶
func (e *Expression) Source() string
Source returns the original source string the expression was compiled from.