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

Index

Examples

Package Files

evaluator.go s-expression.go

Variables

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

Code:

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

Output:

true
true

Code:

s := `(!(a b )())`
exp, err := parse(s)
if err != nil {
    return
}
exp.dump(0)

Output:

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

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

Directories

PathSynopsis
functionPackage function provides basic functions which implement Funcer interface

Package evaluator imports 8 packages (graph). Updated 2018-02-17. Refresh now. Tools for package owners.