Expr functions now act as closures

This commit is contained in:
2024-05-30 07:13:26 +02:00
parent d84e690ef3
commit f41ea96d17
4 changed files with 29 additions and 16 deletions
+9 -4
View File
@@ -34,7 +34,7 @@ func (functor *baseFunctor) GetFunc() ExprFunc {
return functor.info
}
// ---- Linking with the functions of Go
// ---- Linking with Go functions
type golangFunctor struct {
baseFunctor
f FuncTemplate
@@ -48,18 +48,23 @@ func (functor *golangFunctor) Invoke(ctx ExprContext, name string, args []any) (
return functor.f(ctx, name, args)
}
// ---- Linking with the functions of Expr
// ---- Linking with Expr functions
type exprFunctor struct {
baseFunctor
params []string
expr Expr
defCtx ExprContext
}
func newExprFunctor(e Expr, params []string) *exprFunctor {
return &exprFunctor{expr: e, params: params}
func newExprFunctor(e Expr, params []string, ctx ExprContext) *exprFunctor {
return &exprFunctor{expr: e, params: params, defCtx: ctx}
}
func (functor *exprFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) {
if functor.defCtx != nil {
ctx.Merge(functor.defCtx)
}
for i, p := range functor.params {
if i < len(args) {
arg := args[i]