1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/expressions/literals/array.go
Tim Voronov 549b4abd3b
Feature/#5 collect keyword alt (#141)
Implemented COLLECT key word
2018-10-24 21:30:05 -04:00

40 lines
805 B
Go

package literals
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type ArrayLiteral struct {
elements []core.Expression
}
func NewArrayLiteral(size int) *ArrayLiteral {
return &ArrayLiteral{make([]core.Expression, 0, size)}
}
func NewArrayLiteralWith(elements ...core.Expression) *ArrayLiteral {
return &ArrayLiteral{elements}
}
func (l *ArrayLiteral) Push(expression core.Expression) {
l.elements = append(l.elements, expression)
}
func (l *ArrayLiteral) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
arr := values.NewArray(len(l.elements))
for _, el := range l.elements {
val, err := el.Exec(ctx, scope)
if err != nil {
return values.None, err
}
arr.Push(val)
}
return arr, nil
}