goexpression

package module
v0.0.0-...-f1e7b9f Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2020 License: MIT Imports: 5 Imported by: 0

README

Go Expression

Version: 0.01 Alpha MIT License (MIT)

go expression is a basic math expression parser and evaluator. This project is in alpha phase and may change significantly in the near future.

The purpose is to learn go and use the expression parser in other projects.

Status

Supported

  • Basic Math Operators like '+', '-', '*', '/'
  • Operator precedence, Ex: 1+2*3 = 1+6 = 7
  • grouping () Ex: (1+2)*3
  • variables Ex: 1+x where x is passed as a variable to Eval.
  • only parsing works for;
  • Define a variable Ex: x=6

Parsing only

  • Text values inclused in qoutes.
  • Functions with arguments, ex: myfunc(1,2)
  • Boolean types and expressions like '==', '<', '>', '!', 'and', 'or'

Planned

  • Special keywords like. if, each, etc
  • Calling go functions from the expression

Basic usage

context := map[string]interface{}{
	"x": 5,
	"y": 21,
	"z": 12.5,
}
ans:=goexpression.Eval("1+x*(50-y)/z", context)
fmt.Printf("=%v",ans)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(input string, context map[string]interface{}) float64

Bug(zdebeer): functions is eval from right to left instead from left to right.

Types

type EmptyToken

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

func NewEmptyToken

func NewEmptyToken() *EmptyToken

func (*EmptyToken) Category

func (this *EmptyToken) Category() TokenCategory

func (*EmptyToken) Error

func (this *EmptyToken) Error() error

func (*EmptyToken) SetError

func (this *EmptyToken) SetError(err error)

func (*EmptyToken) String

func (this *EmptyToken) String() string

type ErrorToken

type ErrorToken struct {
	EmptyToken
}

func NewErrorToken

func NewErrorToken(err string) *ErrorToken

type FuncToken

type FuncToken struct {
	EmptyToken
	Name      string
	Arguments []*TreeNode
}

func NewFuncToken

func NewFuncToken(name string) *FuncToken

func (*FuncToken) AddArgument

func (this *FuncToken) AddArgument(arg *TreeNode)

func (*FuncToken) String

func (this *FuncToken) String() string

type GroupToken

type GroupToken struct {
	EmptyToken
	GroupType string
}

func NewGroupToken

func NewGroupToken(group string) *GroupToken

func (*GroupToken) String

func (this *GroupToken) String() string

type IdentityToken

type IdentityToken struct {
	EmptyToken
	Name string
}

func NewIdentityToken

func NewIdentityToken(name string) *IdentityToken

func (*IdentityToken) String

func (this *IdentityToken) String() string

type LRFuncToken

type LRFuncToken struct {
	EmptyToken
	Name string
}

func NewLRFuncToken

func NewLRFuncToken(name string) *LRFuncToken

func (*LRFuncToken) String

func (this *LRFuncToken) String() string

type NumberToken

type NumberToken struct {
	EmptyToken
	Value float64
}

func NewNumberToken

func NewNumberToken(value string) *NumberToken

func (*NumberToken) String

func (this *NumberToken) String() string

type OperatorPrecedence

type OperatorPrecedence [][]string

func (OperatorPrecedence) All

func (this OperatorPrecedence) All() []string

func (OperatorPrecedence) Level

func (this OperatorPrecedence) Level(operator string) int

type OperatorToken

type OperatorToken struct {
	EmptyToken
	Operator string
	// contains filtered or unexported fields
}

func NewOperatorToken

func NewOperatorToken(operator string) *OperatorToken

func (*OperatorToken) Precedence

func (this *OperatorToken) Precedence(operator string) int

OperatorPrecedence return true if the operator argument is lower than the current operator.

func (*OperatorToken) SetOperator

func (this *OperatorToken) SetOperator(operator string)

func (*OperatorToken) String

func (this *OperatorToken) String() string

type TextToken

type TextToken struct {
	EmptyToken
	Text string
}

func NewTextToken

func NewTextToken(text string) *TextToken

func (*TextToken) String

func (this *TextToken) String() string

type Token

type Token interface {
	Category() TokenCategory
	SetError(err error)
	Error() error
	String() string
}

type TokenCategory

type TokenCategory int
const (
	CatOther TokenCategory = iota
	CatFunction
	CatValue
)

type TreeNode

type TreeNode struct {
	Value Token
	// contains filtered or unexported fields
}

func NewTreeNode

func NewTreeNode(value Token) *TreeNode

NewTreeElement Creates a new TreeElement.

func Parse

func Parse(input string) (*TreeNode, error)

func (*TreeNode) Add

func (this *TreeNode) Add(value Token) *TreeNode

Add adds a value to the end of the children items of the current node.

func (*TreeNode) AddElement

func (this *TreeNode) AddElement(element *TreeNode) *TreeNode

Add adds a TreeElement to the end of the children items of the current node.

func (*TreeNode) Items

func (this *TreeNode) Items() []*TreeNode

func (*TreeNode) Last

func (this *TreeNode) Last() Token

func (*TreeNode) LastElement

func (this *TreeNode) LastElement() *TreeNode

func (*TreeNode) Parent

func (this *TreeNode) Parent() *TreeNode

Parent Returns the current element parent

func (*TreeNode) Push

func (this *TreeNode) Push(value Token) *TreeNode

func (*TreeNode) PushElement

func (this *TreeNode) PushElement(element *TreeNode) *TreeNode

Push, removes the current element from its current parent, place the new value in its place and add the current element to the new element. there by pushing the current element down the hierachy. Example: tree: A(B) B.Push(C) tree: A(C(B))

func (*TreeNode) Root

func (this *TreeNode) Root() *TreeNode

func (*TreeNode) String

func (this *TreeNode) String() string

func (*TreeNode) StringContent

func (this *TreeNode) StringContent() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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