eval

package
v1.113.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MPL-2.0 Imports: 12 Imported by: 6

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/richardwilkes/toolbox/eval"
	"github.com/richardwilkes/toolbox/xmath/fixed"
)

func main() {
	e := eval.NewFixedEvaluator[fixed.D4](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 FixedFrom added in v1.68.1

func FixedFrom[T fixed.Dx](arg any) (f64.Int[T], error)

FixedFrom attempts to convert the arg into one of the fixed.F64 types.

func FixedFunctions added in v1.68.0

func FixedFunctions[T fixed.Dx]() map[string]Function

FixedFunctions returns standard functions that work with 64-bit fixed-point values.

func FloatFunctions added in v1.68.0

func FloatFunctions[T constraints.Float]() map[string]Function

FloatFunctions returns standard functions that work with constraints.Float.

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 NewFixedEvaluator added in v1.68.0

func NewFixedEvaluator[T fixed.Dx](resolver VariableResolver, divideByZeroReturnsZero bool) *Evaluator

NewFixedEvaluator creates a new evaluator whose number type is one of the fixed types.

func NewFloatEvaluator added in v1.68.0

func NewFloatEvaluator[T constraints.Float](resolver VariableResolver, divideByZeroReturnsZero bool) *Evaluator

NewFloatEvaluator creates a new evaluator whose number type is one of the constraints.Float types.

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(expression string) (any, error)

Evaluate an expression.

func (*Evaluator) EvaluateNew

func (e *Evaluator) EvaluateNew(expression string) (any, 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) (any, error)

Function provides a signature for a Function.

type OpFunc

type OpFunc func(left, right any) (any, 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 CloseParen

func CloseParen() *Operator

CloseParen )

func Divide

func Divide(f OpFunc) *Operator

Divide /

func Equal

func Equal(f OpFunc) *Operator

Equal ==

func FixedOperators added in v1.68.0

func FixedOperators[T fixed.Dx](divideByZeroReturnsZero bool) []*Operator

FixedOperators returns standard operators that work with 64-bit fixed-point values.

func FloatOperators added in v1.68.0

func FloatOperators[T constraints.Float](divideByZeroReturnsZero bool) []*Operator

FloatOperators returns standard operators that work with floating point values.

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 LogicalAnd added in v1.68.0

func LogicalAnd(f OpFunc) *Operator

LogicalAnd &&

func LogicalOr added in v1.68.0

func LogicalOr(f OpFunc) *Operator

LogicalOr ||

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 Power

func Power(f OpFunc) *Operator

Power ^

func Subtract

func Subtract(f OpFunc, unary UnaryOpFunc) *Operator

Subtract -

type UnaryOpFunc

type UnaryOpFunc func(arg any) (any, 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.

Jump to

Keyboard shortcuts

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