splitted go and expr function bindings in dedicated source files
This commit is contained in:
parent
9df9ad5dd1
commit
33d70d6d1a
44
bind-expr-functions.go
Normal file
44
bind-expr-functions.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
|
// All rights reserved.
|
||||||
|
|
||||||
|
// function.go
|
||||||
|
package expr
|
||||||
|
|
||||||
|
// ---- Linking with Expr functions
|
||||||
|
type exprFunctor struct {
|
||||||
|
baseFunctor
|
||||||
|
params []ExprFuncParam
|
||||||
|
expr Expr
|
||||||
|
defCtx ExprContext
|
||||||
|
}
|
||||||
|
|
||||||
|
// func newExprFunctor(e Expr, params []string, ctx ExprContext) *exprFunctor {
|
||||||
|
// return &exprFunctor{expr: e, params: params, defCtx: ctx}
|
||||||
|
// }
|
||||||
|
|
||||||
|
func newExprFunctor(e Expr, params []ExprFuncParam, 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]
|
||||||
|
if funcArg, ok := arg.(Functor); ok {
|
||||||
|
// ctx.RegisterFunc(p, functor, 0, -1)
|
||||||
|
paramSpecs := funcArg.GetParams()
|
||||||
|
ctx.RegisterFunc(p.Name(), funcArg, TypeAny, paramSpecs)
|
||||||
|
} else {
|
||||||
|
ctx.UnsafeSetVar(p.Name(), arg)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ctx.UnsafeSetVar(p.Name(), nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result, err = functor.expr.Eval(ctx)
|
||||||
|
return
|
||||||
|
}
|
19
bind-go-functions.go
Normal file
19
bind-go-functions.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
|
// All rights reserved.
|
||||||
|
|
||||||
|
// bind-go-function.go
|
||||||
|
package expr
|
||||||
|
|
||||||
|
// ---- Linking with Go functions
|
||||||
|
type golangFunctor struct {
|
||||||
|
baseFunctor
|
||||||
|
f FuncTemplate
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGolangFunctor(f FuncTemplate) *golangFunctor {
|
||||||
|
return &golangFunctor{f: f}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (functor *golangFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||||
|
return functor.f(ctx, name, args)
|
||||||
|
}
|
53
function.go
53
function.go
@ -42,59 +42,6 @@ func (functor *baseFunctor) GetFunc() ExprFunc {
|
|||||||
return functor.info
|
return functor.info
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Linking with Go functions
|
|
||||||
type golangFunctor struct {
|
|
||||||
baseFunctor
|
|
||||||
f FuncTemplate
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGolangFunctor(f FuncTemplate) *golangFunctor {
|
|
||||||
return &golangFunctor{f: f}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (functor *golangFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) {
|
|
||||||
return functor.f(ctx, name, args)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- Linking with Expr functions
|
|
||||||
type exprFunctor struct {
|
|
||||||
baseFunctor
|
|
||||||
params []ExprFuncParam
|
|
||||||
expr Expr
|
|
||||||
defCtx ExprContext
|
|
||||||
}
|
|
||||||
|
|
||||||
// func newExprFunctor(e Expr, params []string, ctx ExprContext) *exprFunctor {
|
|
||||||
// return &exprFunctor{expr: e, params: params, defCtx: ctx}
|
|
||||||
// }
|
|
||||||
|
|
||||||
func newExprFunctor(e Expr, params []ExprFuncParam, 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]
|
|
||||||
if funcArg, ok := arg.(Functor); ok {
|
|
||||||
// ctx.RegisterFunc(p, functor, 0, -1)
|
|
||||||
paramSpecs := funcArg.GetParams()
|
|
||||||
ctx.RegisterFunc(p.Name(), funcArg, TypeAny, paramSpecs)
|
|
||||||
} else {
|
|
||||||
ctx.UnsafeSetVar(p.Name(), arg)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ctx.UnsafeSetVar(p.Name(), nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result, err = functor.expr.Eval(ctx)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- Function Parameters
|
// ---- Function Parameters
|
||||||
type paramFlags uint16
|
type paramFlags uint16
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user