mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-16 11:37:36 +02:00
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package literals
|
|
|
|
import (
|
|
"context"
|
|
"github.com/MontFerret/ferret/pkg/runtime/collections"
|
|
"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) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) {
|
|
arr, err := l.doExec(ctx, scope)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return collections.NewArrayIterator(arr), nil
|
|
}
|
|
|
|
func (l *ArrayLiteral) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
|
|
arr, err := l.doExec(ctx, scope)
|
|
|
|
if err != nil {
|
|
return values.None, err
|
|
}
|
|
|
|
return arr, nil
|
|
}
|
|
|
|
func (l *ArrayLiteral) doExec(ctx context.Context, scope *core.Scope) (*values.Array, error) {
|
|
arr := values.NewArray(len(l.elements))
|
|
|
|
for _, el := range l.elements {
|
|
val, err := el.Exec(ctx, scope)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
arr.Push(val)
|
|
}
|
|
|
|
return arr, nil
|
|
}
|