gocalc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2015 License: MIT Imports: 3 Imported by: 0

README

gocalc

mathematical expression evaluator in go

GoDoc Build Status Code Coverage

Documentation

Overview

Package gocalc is for evaluating mathematical expressions at runtime.

See https://github.com/justinsacbibit/gocalc-cl-sample for a sample console app that uses gocalc.

func main() {
  expression, err := gocalc.NewExpr("1 + 2")
  if err != nil {
    fmt.Println(err)
    return
  }

  result, err := expression.Evaluate(nil, nil)
  if err != nil {
    fmt.Println(err)
    return
  }

  fmt.Println(result)
}
Example (CompileError)
_, err := NewExpr("1 +")
if err != nil {
	// A compilation error has occurred
}
Output:

Example (EvaluationError)
expression, _ := NewExpr("a")

_, err := expression.Evaluate(nil, nil)
if err != nil {
	// An evaluation error has occurred
}
Output:

Example (Float)
expression, _ := NewExpr("3.25 * 10")

result, _ := expression.Evaluate(nil, nil)

fmt.Println(result)
Output:

32.5
Example (Function)
expression, _ := NewExpr("sub2(5)")

result, _ := expression.Evaluate(nil, func(fn string, args ...func() interface{}) (result interface{}, handled bool) {
	switch fn {
	case "sub2":
		if len(args) != 1 {
			panic(EvaluationError("sub2 requires 1 argument"))
		}

		arg := args[0]().(int64)
		return arg - 2, true
	}

	return nil, false
})

fmt.Println(result)
Output:

3
Example (Identifier)
expression, _ := NewExpr("a * 2")

result, _ := expression.Evaluate(func(id string) interface{} {
	switch id {
	case "a":
		return 2
	}

	return nil
}, nil)

fmt.Println(result)
Output:

4
Example (Simple)
expression, _ := NewExpr("1 + 2")

result, _ := expression.Evaluate(nil, nil)

fmt.Println(result)
Output:

3

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EvaluationError

type EvaluationError string

EvaluationError is the type of an expression evaluation error.

func (EvaluationError) Error

func (e EvaluationError) Error() string

Error is EvaluationError's implementation of the error interface.

type Expression

type Expression struct {
	// contains filtered or unexported fields
}

An Expression is used to compile and evaluate a string representation of a mathematical expression. Multiple goroutines can use an Expression, as parameters and functions are not stored within the Expression, and are instead resolved during each evaluation.

func NewExpr

func NewExpr(expr string) (*Expression, error)

NewExpr initializes and returns an Expression given the string representation, or an error if compilation failed.

func (*Expression) Evaluate

func (e *Expression) Evaluate(p ParamResolver, f FuncHandler) (result interface{}, err error)

Evaluate evaluates an Expression. If any parameters or function are found, Evaluate will call the appropriate resolver. The evaluation result is returned, or an error if evaluation failed.

type FuncHandler

type FuncHandler func(string, ...func() interface{}) (result interface{}, handled bool)

FuncHandler handles evaluates a function within an Expression, given parameters (which are wrapped in a function for lazy evaluation). FuncHandlers may make calls to panic() with an EvaluationError.

type ParamResolver

type ParamResolver func(string) (value interface{})

ParamResolver resolves the values of any identifiers within an Expression.

Jump to

Keyboard shortcuts

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