Skip to content

Commit 407b72d

Browse files
author
dengsgo
committed
增加 自定义函数注册
1 parent e6dc263 commit 407b72d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

engine/util_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ func TestParseAndExecTrigonometric(t *testing.T) {
106106
}
107107
}
108108

109+
func TestRegFunction(t *testing.T) {
110+
RegFunction("double", 1, func(expr ...ExprAST) float64 {
111+
return ExprASTResult(expr[0]) * 2
112+
})
113+
r, err := ParseAndExec("double(6)")
114+
if r != 12 {
115+
t.Error(err, "RegFunction errors when register new funtion")
116+
}
117+
}
118+
109119
func TestParseAndExecError(t *testing.T) {
110120
exprs := []string{
111121
"(",

main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package main
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/dengsgo/math-engine/engine"
76
"os"
87
"strings"
98
"time"
9+
10+
"github.com/dengsgo/math-engine/engine"
1011
)
1112

1213
func main() {
@@ -15,6 +16,9 @@ func main() {
1516

1617
// input loop
1718
func loop() {
19+
engine.RegFunction("double", 1, func(expr ...engine.ExprAST) float64 {
20+
return engine.ExprASTResult(expr[0]) * 2
21+
})
1822
for {
1923
fmt.Print("input /> ")
2024
f := bufio.NewReader(os.Stdin)

0 commit comments

Comments
 (0)