eval

package module
v0.0.0-...-46db3d1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2022 License: BSD-3-Clause Imports: 6 Imported by: 1

README

eval

Go Report Card Go Reference

Package eval provides numerical and boolean expression evaluation in Go. It is still a work in progress. Eval is based off of Knetic/govaluate and it has some improvements like supporting expressions that lack multiplication symbols and parentheses.

Example Usage

package main

import (
    "fmt"

    "github.com/eqgo/eval"
)

func main() {
    ctx := eval.DefaultContext()
    ctx.Set("x", 3.0)

    expr := eval.New("xcospi")
    err := expr.Compile(ctx)
    if err != nil {
        panic(err)
    }
    res, err := expr.Eval(ctx)
    if err != nil {
        panic(err)
    }
    fmt.Println(res) // -3
}

Documentation

Overview

Package eval provides mathematical expression evaluation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comp

type Comp int

Comp represents the value of a COMP token

const (
	// =
	EQUAL Comp = iota
	// !=
	NOTEQUAL
	// >
	GREATER
	// <
	LESS
	// >=
	GEQ
	// <=
	LEQ
)

Comp Constants

func (Comp) String

func (c Comp) String() string

type Context

type Context struct {
	Vars  Vars
	Funcs Funcs
}

Context is context that is given when compiling and evaluating expressions. Context contains variables and functions that can be used in expressions.

func DefaultContext

func DefaultContext() *Context

DefaultContext returns a context with standard math constants, standard math functions, and basic logic functions

func LogicalContext

func LogicalContext() *Context

LogicalContext returns a context with basic logical functions

func MathContext

func MathContext() *Context

MathContext returns a context with the standard math functions and constants

func NewContext

func NewContext() *Context

NewContext makes a new context

func NewContextFrom

func NewContextFrom(from *Context) *Context

NewContextFrom makes a new context by copying the other context

func (*Context) Copy

func (c *Context) Copy(other *Context)

Copy copies the variables and functions of the other context and does not delete existing variables and functions

func (*Context) CopyFuncs

func (c *Context) CopyFuncs(other *Context)

CopyFuncs copies the funcs from the other context and does not delete existing functions

func (*Context) CopyVars

func (c *Context) CopyVars(other *Context)

CopyVars copies the vars from the other context and does not delete existing variables

func (*Context) Delete

func (c *Context) Delete(name string)

Delete deletes the given name from both the vars and funcs

func (*Context) DeleteFunc

func (c *Context) DeleteFunc(name string)

DeleteFunc deletes the given function

func (*Context) DeleteVar

func (c *Context) DeleteVar(name string)

DeleteVar deletes the given variable

func (*Context) Reset

func (c *Context) Reset()

Reset resets the variables and functions

func (*Context) ResetFuncs

func (c *Context) ResetFuncs()

ResetFuncs resets the functions

func (*Context) ResetVars

func (c *Context) ResetVars()

ResetVars resets the variables

func (*Context) Set

func (c *Context) Set(name string, value any)

Set calls c.Funcs.Set if value is type Func, otherwise it calls c.Vars.Set

func (*Context) SetFunc

func (c *Context) SetFunc(name string, value Func)

SetFunc sets the value of the given function to the given value

func (*Context) SetFuncsTo

func (c *Context) SetFuncsTo(other *Context)

SetFuncsTo sets the funcs of the context to the funcs of the other context

func (*Context) SetTo

func (c *Context) SetTo(other *Context)

SetTo sets the context to the other context

func (*Context) SetVar

func (c *Context) SetVar(name string, value Var)

SetVar sets the value of the given variable to the given value

func (*Context) SetVarsTo

func (c *Context) SetVarsTo(other *Context)

SetVarsTo sets the vars of the context to the vars of the other context

type Expr

type Expr struct {
	Expr   string
	Tokens []Token
	// contains filtered or unexported fields
}

Expr is an expression that can be evaluated

func New

func New(expr string) *Expr

New is an alias for NewExpr

func NewExpr

func NewExpr(expr string) *Expr

NewExpr makes a new expression from the given expression string

func (*Expr) Compile

func (ex *Expr) Compile(ctx *Context) error

Compile compiles the expression with the given context

func (*Expr) Eval

func (ex *Expr) Eval(ctx *Context) (any, error)

Eval evaluates the expression with the given context

func (*Expr) Set

func (ex *Expr) Set(expr string)

Set sets the expression of the expression

type Func

type Func func(args ...any) (any, error)

A Func is a function that can be used in expressions. Functions take any number of arguments of any type, and return one argument of any type and an error. Use eval/fun to easily make functions.

func NewFunc1

func NewFunc1[I, O any](f func(I) O) Func

NewFunc1 makes a function that can be used in expressions from a function that takes a single argument and returns a single value.

func NewFunc2

func NewFunc2[I1, I2, O any](f func(I1, I2) O) Func

NewFunc2 makes a function that can be used in expressions from a function that takes two arguments and returns a single value.

func NewFunc3

func NewFunc3[I1, I2, I3, O any](f func(I1, I2, I3) O) Func

NewFunc3 makes a function that can be used in expressions from a function that takes three arguments and returns a single value.

func NewFuncV

func NewFuncV[I, O any](f func(...I) O) Func

NewFuncV makes a function that can be used in expressions from a function that takes a variadic input and returns a single value.

type Funcs

type Funcs map[string]Func

Funcs is a group of named functions that can be used when evaluating expressions.

func DefaultFuncs

func DefaultFuncs() Funcs

DefaultFuncs returns a set of functions containing all of the standard math functions, and basic logic functions

func LogicalFuncs

func LogicalFuncs() Funcs

LogicalFuncs returns a set of basic logical functions

func MathFuncs

func MathFuncs() Funcs

MathFuncs returns a set of functions with all of the standard math functions

func NewFuncs

func NewFuncs() Funcs

NewFuncs makes a new empty set of funcs

func NewFuncsFrom

func NewFuncsFrom(from Funcs) Funcs

NewFuncsFrom makes a new set of functions by copying the other functions

func (Funcs) Copy

func (f Funcs) Copy(other Funcs)

Copy copies the given functions to v. It does not remove the existing functions in v, but it will override any functions with the same name as copied functions.

func (Funcs) Delete

func (f Funcs) Delete(name string)

Delete removes the function with the given name.

func (Funcs) Reset

func (f Funcs) Reset()

Reset resets the functions to an empty map.

func (Funcs) Set

func (f Funcs) Set(name string, value Func)

Set sets the value of the given name to the given value.

func (Funcs) SetTo

func (f Funcs) SetTo(other Funcs)

SetTo sets the functions to the other given functions.

type LogOp

type LogOp int

LogOp represents the value of a LOGOP token

const (
	// &
	AND LogOp = iota
	// |
	OR
)

LogOp Constants

func (LogOp) String

func (l LogOp) String() string

type LogPre

type LogPre int

LogPre represents the value of a LOGPRE token

const (
	// !
	NOT LogPre = iota
)

LogPre Constants

func (LogPre) String

func (l LogPre) String() string

type NumOp

type NumOp int

NumOp represents the value of a NUMOP token

const (
	// +
	ADD NumOp = iota
	// -
	SUB
	// *
	MUL
	// /
	DIV
	// ^
	POW
	// %
	MOD
)

NumOp Constants

func (NumOp) String

func (n NumOp) String() string

type NumPre

type NumPre int

NumPre represents the value of a NUMPRE token

const (
	// -
	NEG NumPre = iota
)

NumPre Constants

func (NumPre) String

func (n NumPre) String() string

type Token

type Token struct {
	Type  TokenType
	Value any
}

Token is a Token in an expression

func Tokens

func Tokens(expr string, ctx *Context) ([]Token, error)

Tokens returns the tokens for the given expression string with the given context

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int

TokenType represents the type of an expression token

const (
	// Value is type Var
	VAR TokenType = iota
	// Value is type Func
	FUNC
	// float64
	NUM
	// literal
	BOOL
	// ({[
	LEFT
	// )}]
	RIGHT
	// ,
	SEP
	// Value is type NumOp
	NUMOP
	// Value is type NumPre
	NUMPRE
	// Value is type Comp
	COMP
	// Value is type LogOp
	LOGOP
	// Value is type LogPre
	LOGPRE
)

TokenType Constants

func (TokenType) String

func (t TokenType) String() string

type Var

type Var any

A Var is a variable that can be used in expressions. Variables can be of any type and hold any value.

type Vars

type Vars map[string]Var

Vars is a group of named variables that can be used in expressions.

func DefaultVars

func DefaultVars() Vars

DefaultVars returns a set of variables containing all of the standard math constants

func MathConsts

func MathConsts() Vars

MathConsts returns a set of variables with all of the standard math constants

func NewVars

func NewVars() Vars

NewVars makes a new empty set of vars

func NewVarsFrom

func NewVarsFrom(from Vars) Vars

NewVarsFrom makes a new set of variables by copying the other variables

func (Vars) Copy

func (v Vars) Copy(other Vars)

Copy copies the given variables to v. It does not remove the existing variables in v, but it will override any variables with the same name as copied variables.

func (Vars) Delete

func (v Vars) Delete(name string)

Delete removes the variable with the given name.

func (Vars) Reset

func (v Vars) Reset()

Reset resets the variables to an empty map.

func (Vars) Set

func (v Vars) Set(name string, value Var)

Set sets the value of the given name to the given value.

func (Vars) SetTo

func (v Vars) SetTo(other Vars)

SetTo sets the variables to the other given variables.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL