1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/expressions/block.go
2018-10-28 01:45:26 -04:00

51 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 BlockExpression struct {
values collections.Iterable
statements []core.Expression
}
func NewBlockExpression(values collections.Iterable) (*BlockExpression, error) {
if values == nil {
return nil, core.Error(core.ErrMissedArgument, "values")
}
return &BlockExpression{
values: values,
statements: make([]core.Expression, 0, 5),
}, nil
}
func (exp *BlockExpression) Add(stmt core.Expression) {
exp.statements = append(exp.statements, stmt)
}
func (exp *BlockExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
for _, stmt := range exp.statements {
_, err := stmt.Exec(ctx, scope)
if err != nil {
return values.None, err
}
}
return values.None, nil
}
func (exp *BlockExpression) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) {
iter, err := exp.values.Iterate(ctx, scope)
if err != nil {
return nil, err
}
return collections.NewTapIterator(iter, exp)
}