github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/literals/array.go (about)

     1  package literals
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  )
     9  
    10  type ArrayLiteral struct {
    11  	elements []core.Expression
    12  }
    13  
    14  func NewArrayLiteral(size int) *ArrayLiteral {
    15  	return &ArrayLiteral{make([]core.Expression, 0, size)}
    16  }
    17  
    18  func NewArrayLiteralWith(elements []core.Expression) *ArrayLiteral {
    19  	return &ArrayLiteral{elements}
    20  }
    21  
    22  func (l *ArrayLiteral) Push(expression core.Expression) {
    23  	l.elements = append(l.elements, expression)
    24  }
    25  
    26  func (l *ArrayLiteral) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
    27  	arr := values.NewArray(len(l.elements))
    28  
    29  	for _, el := range l.elements {
    30  		val, err := el.Exec(ctx, scope)
    31  
    32  		if err != nil {
    33  			return values.None, err
    34  		}
    35  
    36  		arr.Push(val)
    37  	}
    38  
    39  	return arr, nil
    40  }