mirror of
https://github.com/MontFerret/ferret.git
synced 2025-08-13 19:52:52 +02:00
@@ -59,7 +59,7 @@ func NewArrayOperator(
|
||||
}
|
||||
|
||||
if IsValidArrayOperatorType(aotype) == false {
|
||||
return nil, core.Error(core.ErrInvalidArgument, "operator type")
|
||||
return nil, core.Error(core.ErrInvalidArgument, "operator")
|
||||
}
|
||||
|
||||
if comparator == nil {
|
||||
|
@@ -30,7 +30,7 @@ func NewEqualityOperator(
|
||||
fn, exists := equalityOperators[operator]
|
||||
|
||||
if !exists {
|
||||
return nil, core.Error(core.ErrInvalidArgument, "aotype")
|
||||
return nil, core.Error(core.ErrInvalidArgument, "operator")
|
||||
}
|
||||
|
||||
return &EqualityOperator{
|
||||
|
@@ -37,7 +37,7 @@ func NewLogicalOperator(
|
||||
op, exists := logicalOperators[operator]
|
||||
|
||||
if !exists {
|
||||
return nil, core.Error(core.ErrInvalidArgument, "value")
|
||||
return nil, core.Error(core.ErrInvalidArgument, "operator")
|
||||
}
|
||||
|
||||
return &LogicalOperator{
|
||||
|
@@ -299,3 +299,35 @@ func Decrement(left, _ core.Value) core.Value {
|
||||
|
||||
return values.None
|
||||
}
|
||||
|
||||
func Negative(value, _ core.Value) core.Value {
|
||||
err := core.ValidateType(value, core.IntType, core.FloatType)
|
||||
|
||||
if err != nil {
|
||||
return values.ZeroInt
|
||||
}
|
||||
|
||||
if value.Type() == core.IntType {
|
||||
return -value.(values.Int)
|
||||
}
|
||||
|
||||
return -value.(values.Float)
|
||||
}
|
||||
|
||||
func Positive(value, _ core.Value) core.Value {
|
||||
err := core.ValidateType(value, core.IntType, core.FloatType)
|
||||
|
||||
if err != nil {
|
||||
return values.ZeroInt
|
||||
}
|
||||
|
||||
if value.Type() == core.IntType {
|
||||
return +value.(values.Int)
|
||||
}
|
||||
|
||||
return +value.(values.Float)
|
||||
}
|
||||
|
||||
func ToBoolean(value, _ core.Value) core.Value {
|
||||
return values.ToBoolean(value)
|
||||
}
|
||||
|
66
pkg/runtime/expressions/operators/unary.go
Normal file
66
pkg/runtime/expressions/operators/unary.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package operators
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
)
|
||||
|
||||
type (
|
||||
UnaryOperatorType string
|
||||
UnaryOperator struct {
|
||||
*baseOperator
|
||||
fn OperatorFunc
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
UnaryOperatorTypeNoop UnaryOperatorType = ""
|
||||
UnaryOperatorTypeNot UnaryOperatorType = "!"
|
||||
UnaryOperatorTypeNot2 UnaryOperatorType = "NOT"
|
||||
UnaryOperatorTypeNegative UnaryOperatorType = "-"
|
||||
UnaryOperatorTypePositive UnaryOperatorType = "+"
|
||||
)
|
||||
|
||||
var unaryOperators = map[UnaryOperatorType]OperatorFunc{
|
||||
UnaryOperatorTypeNoop: ToBoolean,
|
||||
UnaryOperatorTypeNot: Not,
|
||||
UnaryOperatorTypeNot2: Not,
|
||||
UnaryOperatorTypeNegative: Negative,
|
||||
UnaryOperatorTypePositive: Positive,
|
||||
}
|
||||
|
||||
func NewUnaryOperator(
|
||||
src core.SourceMap,
|
||||
exp core.Expression,
|
||||
operator UnaryOperatorType,
|
||||
) (*UnaryOperator, error) {
|
||||
fn, exists := unaryOperators[operator]
|
||||
|
||||
if !exists {
|
||||
return nil, core.Error(core.ErrInvalidArgument, "operator")
|
||||
}
|
||||
|
||||
return &UnaryOperator{
|
||||
&baseOperator{
|
||||
src,
|
||||
exp,
|
||||
nil,
|
||||
},
|
||||
fn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (operator *UnaryOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
|
||||
value, err := operator.left.Exec(ctx, scope)
|
||||
|
||||
if err != nil {
|
||||
return nil, core.SourceError(operator.src, err)
|
||||
}
|
||||
|
||||
return operator.Eval(ctx, value, nil)
|
||||
}
|
||||
|
||||
func (operator *UnaryOperator) Eval(_ context.Context, left, _ core.Value) (core.Value, error) {
|
||||
return operator.fn(left, values.None), nil
|
||||
}
|
Reference in New Issue
Block a user