Skip to content

Commit bdf627f

Browse files
author
dengsgo
committed
增加 注册函数说明
1 parent 407b72d commit bdf627f

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

README.md

+43-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/dengsgo/math-engine.svg?branch=master)](https://travis-ci.org/dengsgo/math-engine) [![Go Report Card](https://goreportcard.com/badge/github.com/dengsgo/math-engine)](https://goreportcard.com/report/github.com/dengsgo/math-engine) [![godoc.org](https://godoc.org/github.com/dengsgo/math-engine/engine?status.svg)](https://godoc.org/github.com/dengsgo/math-engine/engine)
44

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

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

@@ -12,8 +12,9 @@
1212
- `123_345_456 * 1.5 - 2 ^ 4`
1313
- `-4 * 6 + 2e2 - 1.6e-3`
1414
- `sin(pi/2)+cos(45-45*1)+tan(pi/4)`
15-
- `99+abs(-1)-ceil(88.8)+floor(88.8)`
16-
- `max(min(2^3, 3^2), 10*1.5-7)`
15+
- `99+abs(-1)-ceil(88.8)+floor(88.8)`
16+
- `max(min(2^3, 3^2), 10*1.5-7)`
17+
- `double(6) + 3` , `double`是一个自定义的函数
1718

1819
### Demo
1920

@@ -48,6 +49,7 @@
4849
| `max(x, y)` | x, y 中的较大值 | max(2, 3) = 3 |
4950
| `min(x, y)` | x, y 中的较小值 | min(2, 3) = 2 |
5051
| `noerr(x)` | 计算 x 出错时返回 0 | noerr(1 / 1) = 1, noerr( 1/ 0 ) = 0 |
52+
| `double(x)` | 返回 x 的双倍值,这是一个自定义的函数示例,你可以注册任意的自定义函数到引擎中 | double(6) = 12 |
5153

5254

5355
## Usage
@@ -60,7 +62,7 @@ go get -u github.com/dengsgo/math-engine
6062
```go
6163
import "github.com/dengsgo/math-engine/engine"
6264
```
63-
e.g. 1 直接调用解析执行函数 :
65+
e.g. 1 常规用法: 直接调用解析执行函数 :
6466

6567
```go
6668
import "github.com/dengsgo/math-engine/engine"
@@ -78,7 +80,7 @@ func main() {
7880

7981

8082

81-
e.g. 2 依次调用函数,手动执行 :
83+
e.g. 2 高级用法: 依次调用函数,手动执行 :
8284

8385
```go
8486
import "github.com/dengsgo/math-engine/engine"
@@ -142,7 +144,41 @@ func main() {
142144
}
143145
```
144146

147+
## Register Function
145148

149+
`v4.0` 开始, `math-engine` 提供了自定义函数注册到引擎的能力。你可以把常用的函数注册到引擎中,然后就能像内置函数一样在输入的数学表达式中使用。
150+
151+
e.g
152+
153+
```go
154+
// RegFunction is Top level function
155+
// the same function name only needs to be registered once.
156+
// double is register function name.
157+
// 1 is a number of parameter signatures.
158+
// func(expr ...engine.ExprAST) float64 is your function.
159+
engine.RegFunction("double", 1, func(expr ...engine.ExprAST) float64 {
160+
// you can use the index value directly according to the number of parameters
161+
// without worrying about crossing the boundary.
162+
// use ExprASTResult to get the result of the ExprAST structure.
163+
return engine.ExprASTResult(expr[0]) * 2
164+
})
165+
```
166+
167+
然后你就可以在输入的表达式中使用这个函数 `double`:
168+
169+
```go
170+
exp := "double(6) + 2"
171+
r, err := engine.ParseAndExec("double(6) + 2")
172+
if err != nil {
173+
panic(err)
174+
}
175+
fmt.Printf("double(6) + 2 = %f\n", r) // will print : double(6) + 2 = 14.000000
176+
```
177+
178+
注意事项:
179+
- 注册的函数名只能是英文字母(区分大小写);
180+
- 每一个函数名只能且只需注册一次;
181+
- 注册的函数逻辑中如果有 panic ,需要程序自己捕获处理;
146182

147183
## Compile
148184

@@ -178,6 +214,8 @@ go build
178214
- [x] 三角函数 e.g. `sin, cos, tan, cot, sec, csc`
179215
- [x] 常量 pi
180216
- [x] 辅助函数 e.g. `abs, ceil, floor, sqrt, cbrt, max, min, noerr`
217+
- [x] 提供自定义函数注册功能,注册后可以在表达式中使用
218+
- [x] 精确的数据计算
181219
- [x] 友好的错误消息 e.g.
182220
```bash
183221
input /> 123+89-0.0.9
@@ -189,7 +227,3 @@ want '(' or '0-9' but get '0.0.9'
189227
------------
190228
```
191229

192-
### 待实现
193-
194-
- [ ] 精确浮点计算
195-
- [ ] 更多辅助函数

0 commit comments

Comments
 (0)