2018-09-18 22:42:38 +02:00
|
|
|
package expressions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-28 07:45:26 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/collections"
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlockExpression struct {
|
2018-10-28 07:45:26 +02:00
|
|
|
values collections.Iterable
|
2018-09-18 22:42:38 +02:00
|
|
|
statements []core.Expression
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
func NewBlockExpression(values collections.Iterable) (*BlockExpression, error) {
|
|
|
|
if values == nil {
|
|
|
|
return nil, core.Error(core.ErrMissedArgument, "values")
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
return &BlockExpression{
|
|
|
|
values: values,
|
|
|
|
statements: make([]core.Expression, 0, 5),
|
|
|
|
}, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
func (exp *BlockExpression) Add(stmt core.Expression) {
|
|
|
|
exp.statements = append(exp.statements, stmt)
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
func (exp *BlockExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
|
2018-12-22 06:14:41 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return values.None, core.ErrTerminated
|
|
|
|
default:
|
|
|
|
for _, stmt := range exp.statements {
|
|
|
|
_, err := stmt.Exec(ctx, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
2018-10-28 07:45:26 +02:00
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
return values.None, nil
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
func (exp *BlockExpression) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) {
|
2018-12-22 06:14:41 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, core.ErrTerminated
|
|
|
|
default:
|
|
|
|
iter, err := exp.values.Iterate(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
return collections.NewTapIterator(iter, exp)
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|