Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加注册自定义函数的能力 #6

Open
dengsgo opened this issue May 1, 2020 · 5 comments
Open

增加注册自定义函数的能力 #6

dengsgo opened this issue May 1, 2020 · 5 comments
Labels

Comments

@dengsgo
Copy link
Owner

dengsgo commented May 1, 2020

可以把常用的函数注册到引擎中,然后就能像内置函数一样在输入的数学表达式中使用。

e.g

  engine.RegFunction("double", 1, func(expr ...engine.ExprAST) float64 {
    return engine.ExprASTResult(expr[0]) * 2
  })

然后就可以在输入的表达式中使用这个函数 double:

exp := "double(6) + 2"
r, err := engine.ParseAndExec("double(6) + 2")
if err != nil {
  panic(err)
}
fmt.Printf("double(6) + 2 = %f\n", r) // will print : double(6) + 2 = 14.000000
@dengsgo
Copy link
Owner Author

dengsgo commented May 1, 2020

已实现.

@dengsgo dengsgo added the feature label May 1, 2020
@dengsgo
Copy link
Owner Author

dengsgo commented Jul 24, 2020

update: 注册的函数名可以是英文字母和数字,但必须英文字母开头,且区分大小写。

@songzhilai
Copy link

使用该RegFunction函数注册时,能否不设置参数长度,或者能否允许我重复注册同一个函数

@dengsgo
Copy link
Owner Author

dengsgo commented Sep 14, 2021

使用该RegFunction函数注册时,能否不设置参数长度,或者能否允许我重复注册同一个函数

重复注册同名函数是不会被支持的。
对于可变参数的函数,暂时还不支持。后续会迭代这个需求,可以持续关注一下~

@dengsgo
Copy link
Owner Author

dengsgo commented Oct 23, 2021

v3.3.0 增加对可选长度参数的自定义函数的注册支持

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants