$ moon run cmd/main -- "1 + 2 * 3"
7
$ moon run cmd/main -- "'hello' + ' ' + 'world'"
"hello world"
$ moon run cmd/main -- "6+x*2>10 ? 'big' : 'small'" '{"x": 3}'
"big"
$ moon run cmd/main -- "age * (3 - 1)" '{"age": 36}'
72
$ moon run cmd/main -- 'assoc[.first == "Lana"].last' \
'{"assoc": [{"first": "Lana", "last": "Kane"}, {"first": "Cyril", "last": "Figgis"}]}'
"Kane"
$ moon run cmd/main -- "user.name + ' scored ' + (score * 10)" \
'{"user": {"name": "alice"}, "score": 8.5}'
"alice scored 85"
$ moon run cmd/main -- "items[.price <= 2].name" \
'{"items": [{"name": "apple", "price": 1.5}, {"name": "pear", "price": 3}]}'
"apple"$ moon run cmd/main -- "[1,2,3]|first"
1
$ moon run cmd/main -- "5|dbl|dbl"
20import {
"dimon-83/mbel/ast",
"dimon-83/mbel/evaluator",
"dimon-83/mbel/jexl",
}
let inst = @jexl.new_jexl()
// Evaluate against a context
let ctx = @ast.ObjectVal([
("user", @ast.ObjectVal([("age", @ast.NumVal(36.0))])),
])
let v = try {
@jexl.Jexl::eval(inst, "user.age > 18 ? 'adult' : 'minor'", ctx)
} catch {
@jexl.JexlErr(msg) => abort(msg)
} // StrVal("adult")
// Register transforms / functions / operators
@jexl.Jexl::add_transform(
inst,
"dbl",
fn(args : Array[@ast.Value]) -> @ast.Value {
@ast.NumVal(@evaluator.to_number(args[0]) * 2.0)
},
)
let doubled = try {
@jexl.Jexl::eval(inst, "21|dbl", @ast.ObjectVal([]))
} catch {
@jexl.JexlErr(msg) => abort(msg)
} // NumVal(42.0)
// expr-style resource budgets: nodes at parse time, depth + steps at
// eval time (0 disables a limit)
@jexl.Jexl::set_limits(inst, 10000, 10000, 1000000)moon test # 111 unit/parity tests
moon run cmd/main -- "2+2" # expression runner
./tools/run_diff.sh tools/corpus.txt # differential check vs Jexl
./tools/run_diff.sh tools/corpus_ctx.txt # JSON-context corpusInstall
Download zip