APACHE-2.0 License
Learning Rust by playing with the Common Expression Language.
cargo run -- --expression="-(3.0 + 5.0) * 2.0e0 - -1.0"
Which outputs something like:
AST:
Arithmetic(Arithmetic(Unary(Neg, Arithmetic(Atom(Float(3.0)), Add, Atom(Float(5.0)))), Multiply, Atom(Float(2.0))), Subtract, Unary(Neg, Atom(Float(1.0))))
Evaluating program
NumericCelType(Float(-15.0))
Note while the parser is close to complete I'm not intending to implement a full interpreter with all built-in functions.
The interpreter has basic support for strings, numbers, bools, lists, maps, variables, and binary and unary operators.
cargo run -- --expression="size('hello world') > 5u"
Outputs:
AST:
Relation(Member(Ident("size"), FunctionCall([Atom(String("hello world"))])), GreaterThan, Atom(UInt(5)))
Evaluating program
Bool(true)
To add context from a JSON file use --input
with either a filename or -
to read from
stdin:
echo "{\"foo\":\"bar\"}" | cargo run -- --expression="foo" --input -
Outputs:
Parsed context data: Object {"foo": String("bar")}
AST:
Ident("foo")
Evaluating program
String("bar")
A Python package rustycel
using PyO3 in python_bindings
offers a simple evaluate
function from Python:
>>> import rustycel
>>> rustycel.evaluate("1u + 4u")
5
Note only a few primitive types are mapped back to Python native types.
Test Data
https://github.com/google/cel-spec/blob/master/tests/simple/testdata/basic.textproto