1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/runtime/expressions/func_call.go

72 lines
1.4 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package expressions
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/collections"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type FunctionCallExpression struct {
src core.SourceMap
fun core.Function
args []core.Expression
}
func NewFunctionCallExpression(
src core.SourceMap,
fun core.Function,
args ...core.Expression,
) (*FunctionCallExpression, error) {
if fun == nil {
return nil, core.Error(core.ErrMissedArgument, "function")
}
return &FunctionCallExpression{src, fun, args}, nil
}
func (e *FunctionCallExpression) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) {
value, err := e.Exec(ctx, scope)
if err != nil {
return nil, core.SourceError(e.src, err)
}
iter, err := collections.ToIterator(value)
if err != nil {
return nil, core.SourceError(e.src, err)
}
return iter, nil
}
func (e *FunctionCallExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
2018-09-23 10:33:20 +02:00
var out core.Value
var err error
2018-09-18 22:42:38 +02:00
if len(e.args) == 0 {
2018-09-23 10:33:20 +02:00
out, err = e.fun(ctx)
} else {
args := make([]core.Value, len(e.args))
2018-09-18 22:42:38 +02:00
2018-09-23 10:33:20 +02:00
for idx, arg := range e.args {
out, err := arg.Exec(ctx, scope)
2018-09-18 22:42:38 +02:00
2018-09-23 10:33:20 +02:00
if err != nil {
return values.None, core.SourceError(e.src, err)
}
2018-09-18 22:42:38 +02:00
2018-09-23 10:33:20 +02:00
args[idx] = out
2018-09-18 22:42:38 +02:00
}
2018-09-23 10:33:20 +02:00
out, err = e.fun(ctx, args...)
}
if err != nil {
return values.None, core.SourceError(e.src, err)
2018-09-18 22:42:38 +02:00
}
2018-09-23 10:33:20 +02:00
return out, nil
2018-09-18 22:42:38 +02:00
}