1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-11-27 22:08:15 +02:00
Files
ferret/pkg/runtime/expressions/literals/array.go
Tim Voronov bd6463fa29 Bench (#683)
* Extended public API

* Refactored event loop

* Refactored parser

* Fixed unit tests

* Added --dry-run opt

* Added total alloc

* Added profiler

* Fixed driver registration
2021-10-16 17:24:54 -04:00

40 lines
804 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
}