libMath

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Overview

math-engine

--------

数学表达式解析计算引擎库

使用 Go 实现的数学表达式解析计算引擎库,它小巧,无任何依赖,具有扩展性(比如可以注册自己的函数到引擎中),比较完整的完成了数学表达式解析执行,包括词法分析、语法分析、构建AST、运行。

`go get -u github.com/dengsgo/math-engine`

能够处理的表达式样例: - `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)` - `max(min(2^3, 3^2), 10*1.5-7)` - `double(6) + 3` , `double`是一个自定义的函数

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 float64) 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 name: be register function name. the same function name only needs to be registered once. argc: this is a number of parameter signatures. should be -1, 0, or a positive integer

-1 variable-length argument; >=0 fixed numbers argument

fun: function handler

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