2
2
3
3
[ ![ 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 )
4
4
5
- 使用 Go 实现的数学表达式解析计算引擎库,无任何依赖,相对比较完整的完成了数学表达式解析执行 ,包括词法分析、语法分析、构建AST、运行。
5
+ 使用 Go 实现的数学表达式解析计算引擎库,它小巧, 无任何依赖,具有扩展性(比如可以注册自己的函数到引擎中),比较完整的完成了数学表达式解析执行 ,包括词法分析、语法分析、构建AST、运行。
6
6
7
7
` go get -u github.com/dengsgo/math-engine `
8
8
12
12
- ` 123_345_456 * 1.5 - 2 ^ 4 `
13
13
- ` -4 * 6 + 2e2 - 1.6e-3 `
14
14
- ` 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 ` 是一个自定义的函数
17
18
18
19
### Demo
19
20
48
49
| ` max(x, y) ` | x, y 中的较大值 | max(2, 3) = 3 |
49
50
| ` min(x, y) ` | x, y 中的较小值 | min(2, 3) = 2 |
50
51
| ` noerr(x) ` | 计算 x 出错时返回 0 | noerr(1 / 1) = 1, noerr( 1/ 0 ) = 0 |
52
+ | ` double(x) ` | 返回 x 的双倍值,这是一个自定义的函数示例,你可以注册任意的自定义函数到引擎中 | double(6) = 12 |
51
53
52
54
53
55
## Usage
@@ -60,7 +62,7 @@ go get -u github.com/dengsgo/math-engine
60
62
``` go
61
63
import " github.com/dengsgo/math-engine/engine"
62
64
```
63
- e.g. 1 直接调用解析执行函数 :
65
+ e.g. 1 常规用法: 直接调用解析执行函数 :
64
66
65
67
``` go
66
68
import " github.com/dengsgo/math-engine/engine"
@@ -78,7 +80,7 @@ func main() {
78
80
79
81
80
82
81
- e.g. 2 依次调用函数,手动执行 :
83
+ e.g. 2 高级用法: 依次调用函数,手动执行 :
82
84
83
85
``` go
84
86
import " github.com/dengsgo/math-engine/engine"
@@ -142,7 +144,41 @@ func main() {
142
144
}
143
145
```
144
146
147
+ ## Register Function
145
148
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 ,需要程序自己捕获处理;
146
182
147
183
## Compile
148
184
@@ -178,6 +214,8 @@ go build
178
214
- [x] 三角函数 e.g. ` sin, cos, tan, cot, sec, csc `
179
215
- [x] 常量 pi
180
216
- [x] 辅助函数 e.g. ` abs, ceil, floor, sqrt, cbrt, max, min, noerr `
217
+ - [x] 提供自定义函数注册功能,注册后可以在表达式中使用
218
+ - [x] 精确的数据计算
181
219
- [x] 友好的错误消息 e.g.
182
220
``` bash
183
221
input /> 123+89-0.0.9
@@ -189,7 +227,3 @@ want '(' or '0-9' but get '0.0.9'
189
227
------------
190
228
```
191
229
192
- ### 待实现
193
-
194
- - [ ] 精确浮点计算
195
- - [ ] 更多辅助函数
0 commit comments