Skip to content

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

@dengsgo

Description

@dengsgo

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions