evaluator: github.com/nullne/evaluator Index | Examples | Files | Directories

package evaluator

import "github.com/nullne/evaluator"

Package evaluator evaluates an expression in the form of s-expression

Package evaluator treats the element within expression with three types, each type has its own array form:

- number

For convenience, we treat float64, int64 and so on as type of number. For example, float 100.0 is equal to int 100, but not euqal to string "100"

- string

character string quoted with `, ', or " are treated as type of string. You can convert type string to any other defined type you like by type convert functions which are mentioned later

- function or variable

character string without quotes are regarded as type of function or variable which depends on whether this function exists. For example in expression (age birthdate), both age and birthdate is unquoted. age is type of function because we have registered a function named age, while birthdate is type of variable for not found. The program will come to errors if there is neither parameter nor function named birthdate when evaluating



Package Files

evaluator.go s-expression.go


var (
    // ErrNotFound means the unknow string within the expression cannot be Get from neither functions or params
    ErrNotFound = errors.New("neither function not variable found")
    // ErrInvalidResult means the invalid result type expected with the real output
    ErrInvalidResult = errors.New("invalid result type")
var (
    // ErrUnexpectedEnd occurs most time with the missing parenthesis
    ErrUnexpectedEnd = errors.New("unexpected end")
    // ErrNilInput means the input is nil
    ErrNilInput = errors.New("nil input")
    // ErrLeftOverText indicates there are some of the expression are left pared
    ErrLeftOverText = errors.New("left over text")
    // ErrUnmatchedParenthesis indicated the mismatching parenthesis
    ErrUnmatchedParenthesis = errors.New("unmatched parenthesis")

func Eval Uses

func Eval(expr string, params Params) (interface{}, error)

Eval is a handy encapsulation to parse the expression and evaluate it

func EvalBool Uses

func EvalBool(expr string, params Params) (bool, error)

EvalBool is same as Eval but return a boolean result instead of interface type

type Expression Uses

type Expression struct {
    // contains filtered or unexported fields

Expression stands for an expression which can be evaluated by passing required params


exp, err := New(`(eq gender 'male')`)
if err != nil {
params := MapParams{"gender": "male"}
res, err := exp.Eval(params)
if err != nil {
res, err = exp.EvalBool(params)
if err != nil {




s := `(!(a b )())`
exp, err := parse(s)
if err != nil {


evaluator.list: 3 elements: [! [a b] []]
   evaluator.varString: !
   evaluator.list: 2 elements: [a b]
      evaluator.varString: a
      evaluator.varString: b
   evaluator.list: 0 elements: []

func New Uses

func New(expr string) (Expression, error)

New will return a Expression by parsing the given expression string

func (Expression) Eval Uses

func (e Expression) Eval(params Params) (interface{}, error)

Eval evaluates the Expression with params and return the real value in the type of interface

func (Expression) EvalBool Uses

func (e Expression) EvalBool(params Params) (bool, error)

EvalBool invokes method Eval and does boolean type assertion, return ErrInvalidResult if the type of result is not boolean

func (Expression) Properties Uses

func (e Expression) Properties() []string

Properties returns the field names in an Expression. e.g. Expression constructed by `(or (and (between age 18 80) (eq gender "male") )` returns "age", "gender" by calling Properties.

type MapParams Uses

type MapParams map[string]interface{}

MapParams is a simple map implementation of Params interface

func (MapParams) Get Uses

func (p MapParams) Get(name string) (interface{}, error)

Get is the only method required by Params interface

type Params Uses

type Params interface {
    Get(name string) (interface{}, error)

Params defines a Get method which gets required param for the expression


functionPackage function provides basic functions which implement Funcer interface

Package evaluator imports 8 packages (graph). Updated 2020-10-22. Refresh now. Tools for package owners.