This project is a renewed version of expression parser/evaluator used in jsonslice. It is still work in progress so use it with caution.
git clone https://github.com/bhmj/xpression.git
cd xpression
make build
./build/xpression "1+2"
Expression examples:
1+2
2**1**2
3 + 4 * 2 / (1-5) ** 2 ** 3
5 + -5
1/(3 & 5)
'a' > 'b'
'abc' =~ /a.c/i
!((false))
// simple expression evaluation (error handling skipped)
tokens, _ := xpression.Parse([]byte(`5 - 3 * (6-12)`))
result, _ := xpression.Evaluate(tokens, nil)
switch result.Type {
case xpression.NumberOperand:
fmt.Println(result.Number)
default:
fmt.Println("unexpected result")
}
// external data in expression (aka variables)
foobar := 123
varFunc := func(name []byte, result *xpression.Operator) error {
mapper := map[string]*int{
`foobar`: &foobar,
}
xpression.SetNumber(float64(*mapper[string(name)]))
return nil
}
tokens, _ = xpression.Parse([]byte(`27 / foobar`))
result, _ = xpression.Evaluate(tokens, varFunc)
fmt.Println(result.Number)
Operators | |
---|---|
Arithmetic | + - * / ** % |
Bitwise | | & ^ ~ << >> |
Logical | && || ! |
Comparison | == != === !== >= > <= < |
Regexp | =~ !=~ !~ |
Parentheses | ( ) |
Data types | |
---|---|
String constants | 'string' or "string" |
Numeric | 64-bit integers or floats in decimal or hexadecimal form: 123 or 0.123 or 1.2e34 or 0x12a or 0x12A |
Boolean | true or false . Comparison results in boolean value. |
Regexp | /expression/ with modifiers:i (case-insensitive), m (multiline), s (single-line), U (ungreedy) |
Other | null |
Tests cover the majority of cases described in ECMAScript Language definition (specifically ECMAScript Language: Expressions reference and Testing and Comparison Operations).
Evaluate (2) + (2) == (4)
$ go test -bench=. -benchmem -benchtime=4s
goos: darwin
goarch: amd64
pkg: github.com/bhmj/xpression
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_ModifiedNumericLiteral_WithParsing-16 2622770 1811 ns/op 1272 B/op 26 allocs/op
Benchmark_ModifiedNumericLiteral_WithoutParsing-16 77455698 57.55 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/bhmj/xpression 11.548s
The same expression evaluated with github.com/Knetic/govaluate :
$ go test -bench='LiteralModifiers' -benchmem -benchtime=4s
goos: darwin
goarch: amd64
pkg: github.com/Knetic/govaluate
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
BenchmarkEvaluationLiteralModifiers_WithParsing-16 1000000 4019 ns/op 2208 B/op 43 allocs/op
BenchmarkEvaluationLiteralModifiers-16 30173640 147.2 ns/op 8 B/op 1 allocs/op
PASS
ok github.com/Knetic/govaluate 9.810s
0.9.1 (2022-01-02) -- Variable bounds refined.
0.9.0 (2021-11-19) -- Memory allocation reduced. Speed optimization.
0.8.0 (2021-11-11) -- hex numbers support. Production ready.
0.7.x (2021-11-11) -- WIP
0.7.0 (2021-11-10) -- project renamed to xpression
0.6.0 (2021-11-05) -- a remainder operator %
added. Benchmarks added. Some optimization done.
0.5.0 (2021-11-04) -- Tests added. Multiple bugs fixed.
0.4.0 (2021-11-02) -- Expression evaluation.
0.3.0 (2021-11-01) -- MVP.
- arithmetic operators:
+ - * / **
%
- bitwise operators:
| & ^ ~
- logical operators:
|| && !
- comparison operators:
> < >= <= == === != !==
- full support of parentheses
- regular expressions for strings
- unary minus supported
- expression evaluation
- parser test coverage
- evaluator test coverage
- add external reference type (node reference in jsonslice)
- optimize memory allocations
- Unicode support!
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :)
Michael Gurov aka BHMJ