github.com/qw4990/expr-evaler

EE is used to evaluate a bool expr, and get its result(true/false);


Keywords
evaluator, express, yacc
Install
go get github.com/qw4990/expr-evaler

Documentation

Expr Evaler(EE)

EE is used to evaluate a bool expr, and return its result(true/false);

In EE's exprs, you can use:

  • predefined variables, but all variables' type must be float64;
  • numbers(float64), '+', '-', '*', '/', '(', ')' to do calculations;
  • '>', '<', '==', '>=', '<=', 'and', 'or' to do logical operations;

API & Examples

API

// Eval evaluate this expr with this varTable;
Eval(expr string, varTable VarTable) (result bool, err error)

// VarTable variable table, all variables' type must be float64
type VarTable map[string]float64

The syntax of EE is very simple, so you can see some examples below to catch it;

And completed syntax of EE is defined in ee.y;

Simple Expr

Eval("2 * 2*2*2 > 17", nil)

Eval("2+3*5 < (2+3)*5", nil)

Eval("3*3*3 == 27", nil)

Eval("333*3 > 0 and 123 * 0 == 0 and 2 >= 1", nil)

Eval("-23 * -10 == 230", nil)

Eval("-2*-2*-2*-2*-2 == -32", nil)

With Variables

Eval("a > b", VarTable{
	"a": 2.2222,
	"b": 034,
})

Eval("a + 34 > b", VarTable{
    "a": 0.2222,
    "b": 034,
})

// you will get an error because these is an undefined variable "c"
Eval("a > c", VarTable{
    "a": 2333,
})

All variables in your expr should be predefined, otherwise you will get an error;

More Examples

Please see ee_test.go :)

Dependencies

EE based on goyacc and nex(https://github.com/blynn/nex);