1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-19 21:28:32 +02:00
ferret/pkg/runtime/expressions/for_while.go
Tim Voronov bd07b84736
Feature/#262 while loop (#567)
* Added new syntax and iterator

* Added FOR-WHILE loop

* Added 'FOR-DO-WHILE loop'
2020-11-10 19:16:22 -05:00

47 lines
1.1 KiB
Go

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 ForWhileIterableExpression struct {
src core.SourceMap
mode collections.WhileMode
condition core.Expression
valVariable string
}
func NewForWhileIterableExpression(
src core.SourceMap,
mode collections.WhileMode,
valVariable string,
condition core.Expression,
) (collections.Iterable, error) {
if condition == nil {
return nil, core.Error(core.ErrMissedArgument, "condition")
}
return &ForWhileIterableExpression{
src: src,
mode: mode,
valVariable: valVariable,
condition: condition,
}, nil
}
func (iterable *ForWhileIterableExpression) Iterate(_ context.Context, _ *core.Scope) (collections.Iterator, error) {
return collections.NewWhileIterator(iterable.mode, iterable.valVariable, func(ctx context.Context, scope *core.Scope) (bool, error) {
res, err := iterable.condition.Exec(ctx, scope)
if err != nil {
return false, err
}
return res == values.True, nil
})
}