expression

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

README

expression

Another expression parser and executor Better version of https://github.com/neonxp/lexpr

usage

import "go.neonxp.ru/expression"
...
e := expression.New()
result, err := e.Eval(`2 + 2`) // 4, nil
...

defaults

Default operators and functions: /defaults.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFunctions = map[string]func(stack *Stack) error{
	"max": func(stack *Stack) error {
		a := stack.Pop()
		b := stack.Pop()
		n1, isInt1 := a.Int()
		if !isInt1 {
			return fmt.Errorf("Token %s must be integer", a.Literal)
		}
		n2, isInt2 := b.Int()
		if !isInt2 {
			return fmt.Errorf("Token %s must be integer", b.Literal)
		}
		r := n2
		if n2 < n1 {
			r = n1
		}
		stack.Push(Token{
			Token:   token.INT,
			Literal: strconv.Itoa(r),
			Pos:     b.Pos,
		})
		return nil
	},
	"min": func(stack *Stack) error {
		a := stack.Pop()
		b := stack.Pop()
		n1, isInt1 := a.Int()
		if !isInt1 {
			return fmt.Errorf("Token %s must be integer", a.Literal)
		}
		n2, isInt2 := b.Int()
		if !isInt2 {
			return fmt.Errorf("Token %s must be integer", b.Literal)
		}
		r := n2
		if n2 > n1 {
			r = n1
		}
		stack.Push(Token{
			Token:   token.INT,
			Literal: strconv.Itoa(r),
			Pos:     b.Pos,
		})
		return nil
	},
}
View Source
var DefaultOperators = map[token.Token]Operator{
	token.ADD: {
		// contains filtered or unexported fields
	},
	token.SUB: {
		// contains filtered or unexported fields
	},
	token.MUL: {
		// contains filtered or unexported fields
	},
	token.QUO: {
		// contains filtered or unexported fields
	},
	token.REM: {
		// contains filtered or unexported fields
	},

	token.AND: {
		// contains filtered or unexported fields
	},
	token.OR: {
		// contains filtered or unexported fields
	},
	token.XOR: {
		// contains filtered or unexported fields
	},
	token.SHL: {
		// contains filtered or unexported fields
	},
	token.SHR: {
		// contains filtered or unexported fields
	},

	token.LAND: {
		// contains filtered or unexported fields
	},
	token.LOR: {
		// contains filtered or unexported fields
	},
	token.EQL: {
		// contains filtered or unexported fields
	},
	token.LSS: {
		// contains filtered or unexported fields
	},
	token.GTR: {
		// contains filtered or unexported fields
	},
	token.NEQ: {
		// contains filtered or unexported fields
	},
	token.LEQ: {
		// contains filtered or unexported fields
	},
	token.GEQ: {
		// contains filtered or unexported fields
	},
	token.NOT: {
		// contains filtered or unexported fields
	},
}

Functions

This section is empty.

Types

type Evaluator

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

func New

func New() *Evaluator

func (*Evaluator) Eval

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

func (*Evaluator) ToPRN

func (e *Evaluator) ToPRN(in <-chan Token) chan Token

type Operator

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

type Stack

type Stack []Token

func (*Stack) Empty

func (s *Stack) Empty() bool

func (*Stack) Head

func (s *Stack) Head() (item *Token)

func (*Stack) Pop

func (s *Stack) Pop() (item Token)

func (*Stack) Push

func (s *Stack) Push(item Token)

type Token

type Token struct {
	Token   token.Token
	Literal string
	Pos     int
}

func (*Token) Error

func (t *Token) Error() error

func (*Token) Float

func (t *Token) Float() float64

func (*Token) Int

func (t *Token) Int() (int, bool)

func (*Token) IsError

func (t *Token) IsError() bool

func (*Token) IsFunc

func (t *Token) IsFunc() bool

func (*Token) IsNumber

func (t *Token) IsNumber() bool

func (*Token) IsOperator

func (t *Token) IsOperator() bool

func (*Token) IsSeparator

func (t *Token) IsSeparator() bool

func (*Token) LP

func (t *Token) LP() bool

func (*Token) RP

func (t *Token) RP() bool

Jump to

Keyboard shortcuts

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