mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-23 01:24:30 +02:00
1af8b37a0f
* New type system * Fixed dot notation for HTML elements
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package operators
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
|
)
|
|
|
|
type RangeOperator struct {
|
|
*baseOperator
|
|
}
|
|
|
|
func NewRangeOperator(
|
|
src core.SourceMap,
|
|
left core.Expression,
|
|
right core.Expression,
|
|
) (*RangeOperator, error) {
|
|
if left == nil {
|
|
return nil, core.Error(core.ErrMissedArgument, "left expression")
|
|
}
|
|
|
|
if right == nil {
|
|
return nil, core.Error(core.ErrMissedArgument, "right expression")
|
|
}
|
|
|
|
return &RangeOperator{&baseOperator{src, left, right}}, nil
|
|
}
|
|
|
|
func (operator *RangeOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
|
|
left, err := operator.left.Exec(ctx, scope)
|
|
|
|
if err != nil {
|
|
return values.None, core.SourceError(operator.src, err)
|
|
}
|
|
|
|
right, err := operator.right.Exec(ctx, scope)
|
|
|
|
if err != nil {
|
|
return values.None, core.SourceError(operator.src, err)
|
|
}
|
|
|
|
return operator.Eval(ctx, left, right)
|
|
}
|
|
|
|
func (operator *RangeOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
|
|
err := core.ValidateType(left, types.Int, types.Float)
|
|
|
|
if err != nil {
|
|
return values.None, core.SourceError(operator.src, err)
|
|
}
|
|
|
|
err = core.ValidateType(right, types.Int, types.Float)
|
|
|
|
if err != nil {
|
|
return values.None, core.SourceError(operator.src, err)
|
|
}
|
|
|
|
var start int
|
|
var end int
|
|
|
|
if left.Type() == types.Float {
|
|
start = int(left.(values.Float))
|
|
} else {
|
|
start = int(left.(values.Int))
|
|
}
|
|
|
|
if right.Type() == types.Float {
|
|
end = int(right.(values.Float))
|
|
} else {
|
|
end = int(right.(values.Int))
|
|
}
|
|
|
|
arr := values.NewArray(10)
|
|
|
|
for i := start; i <= end; i++ {
|
|
arr.Push(values.NewInt(i))
|
|
}
|
|
|
|
return arr, nil
|
|
}
|