eval

package
v1.67.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2022 License: MPL-2.0 Imports: 4 Imported by: 6

README

evaluator

Simple expression evaluator

Status

The float64eval package contains the operators and functions for a version that uses float64 numbers, while the f64d4eval package contains the operators and functions for a version that used fixed-point numbers (specifically, the github.com/richardwilkes/toolbox/xmath/fixed.F64d4 type).

Support for other numeric types is easily added. Currently, however, only the two numeric types are currently implemented.

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/richardwilkes/toolbox/eval/f64d4eval"
)

func main() {
	e := f64d4eval.NewEvaluator(nil, true)
	result, err := e.Evaluate("1 + sqrt(2)")
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}
Output:

2.4142

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NextArg

func NextArg(args string) (arg, remaining string)

NextArg provides extraction of the next argument from an arguments string passed to a Function. An empty string will be returned if no argument remains.

Types

type Evaluator

type Evaluator struct {
	Resolver  VariableResolver
	Operators []*Operator
	Functions map[string]Function
	// contains filtered or unexported fields
}

Evaluator is used to evaluate an expression. If you do not have any variables that will be resolved, you can leave Resolver unset. StdOperators() and StdFunctions() can be used to populate the Operators and Functions fields.

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(expression string) (interface{}, error)

Evaluate an expression.

func (*Evaluator) EvaluateNew

func (e *Evaluator) EvaluateNew(expression string) (interface{}, error)

EvaluateNew reuses the Resolver, Operators, and Functions from this Evaluator to create a new Evaluator and then resolves an expression with it.

type Function

type Function func(evaluator *Evaluator, arguments string) (interface{}, error)

Function provides a signature for a Function.

type OpFunc

type OpFunc func(left, right interface{}) (interface{}, error)

OpFunc provides a signature for an Operator's Evaluate function.

type Operator

type Operator struct {
	Symbol        string
	Precedence    int
	Evaluate      OpFunc
	EvaluateUnary UnaryOpFunc
}

Operator provides an operator implementation for the Evaluator.

func Add

func Add(f OpFunc, unary UnaryOpFunc) *Operator

Add +

func And

func And(f OpFunc) *Operator

And &&

func CloseParen

func CloseParen() *Operator

CloseParen )

func Divide

func Divide(f OpFunc) *Operator

Divide /

func Equal

func Equal(f OpFunc) *Operator

Equal ==

func GreaterThan

func GreaterThan(f OpFunc) *Operator

GreaterThan >

func GreaterThanOrEqual

func GreaterThanOrEqual(f OpFunc) *Operator

GreaterThanOrEqual >=

func LessThan

func LessThan(f OpFunc) *Operator

LessThan <

func LessThanOrEqual

func LessThanOrEqual(f OpFunc) *Operator

LessThanOrEqual <=

func Modulo

func Modulo(f OpFunc) *Operator

Modulo %

func Multiply

func Multiply(f OpFunc) *Operator

Multiply *

func Not

func Not(f UnaryOpFunc) *Operator

Not !

func NotEqual

func NotEqual(f OpFunc) *Operator

NotEqual !=

func OpenParen

func OpenParen() *Operator

OpenParen (

func Or

func Or(f OpFunc) *Operator

Or ||

func Power

func Power(f OpFunc) *Operator

Power ^

func Subtract

func Subtract(f OpFunc, unary UnaryOpFunc) *Operator

Subtract -

type UnaryOpFunc

type UnaryOpFunc func(arg interface{}) (interface{}, error)

UnaryOpFunc provides a signature for an Operator's EvaluateUnary function.

type VariableResolver

type VariableResolver interface {
	ResolveVariable(variableName string) string
}

VariableResolver is used to resolve variables in expressions into their values.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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