engine

package
v3.1.1-0...-01b3a64 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

math-engine

--------

数学表达式计算引擎

使用 Go 实现的数学表达式解析计算引擎,无任何依赖,相对比较完整的完成了数学表达式解析执行,包括词法分析、语法分析、构建AST、运行。

能够处理的表达式样例:

1+127-21+(3-4)*6/2.5

(88+(1+8)*6)/2+99

123_345_456 * 1.5 - 2 ^ 4

-4 * 6 + 2e2 - 1.6e-3

sin(pi/2)+cos(45-45*1)+tan(pi/4)

99+abs(-1)-ceil(88.8)+floor(88.8)

Index

Constants

View Source
const (
	RadianMode = iota
	AngleMode
)
View Source
const (
	Identifier = iota
	// e.g. 50
	Literal
	// e.g. + - * /
	Operator
	// ,
	COMMA
)

Variables

View Source
var TrigonometricMode = RadianMode

enum "RadianMode", "AngleMode"

Functions

func ErrPos

func ErrPos(s string, pos int) string

func ExprASTResult

func ExprASTResult(expr ExprAST) float64

ExprASTResult is a Top level function AST traversal if an arithmetic runtime error occurs, a panic exception is thrown

func Float64ToStr

func Float64ToStr(f float64) string

Float64ToStr float64 -> string

func ParseAndExec

func ParseAndExec(s string) (r float64, err error)

Top level function Analytical expression and execution err is not nil if an error occurs (including arithmetic runtime errors)

func Pow

func Pow(x float64, n int) float64

the integer power of a number

func RegFunction

func RegFunction(name string, argc int, fun func(...ExprAST) float64) error

RegFunction is Top level function register a new function to use in expressions

Types

type AST

type AST struct {
	Tokens []*Token

	Err error
	// contains filtered or unexported fields
}

func NewAST

func NewAST(toks []*Token, s string) *AST

func (*AST) ParseExpression

func (a *AST) ParseExpression() ExprAST

type BinaryExprAST

type BinaryExprAST struct {
	Op string
	Lhs,
	Rhs ExprAST
}

type ExprAST

type ExprAST interface {
	// contains filtered or unexported methods
}

type FunCallerExprAST

type FunCallerExprAST struct {
	Name string
	Arg  []ExprAST
}

type NumberExprAST

type NumberExprAST struct {
	Val float64
	Str string
}

type Parser

type Parser struct {
	Source string
	// contains filtered or unexported fields
}

type Token

type Token struct {
	// raw characters
	Tok string
	// type with Literal/Operator
	Type,
	Flag int

	Offset int
}

func Parse

func Parse(s string) ([]*Token, error)

Jump to

Keyboard shortcuts

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