1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-08-13 19:52:52 +02:00

Bug/binary expression (#135)

Added boolean binary operator
This commit is contained in:
Tim Voronov
2018-10-17 11:41:40 -04:00
committed by GitHub
parent dd13878f80
commit e5ca63bcdb
16 changed files with 1289 additions and 887 deletions

View File

@@ -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 {

View File

@@ -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{

View File

@@ -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{

View File

@@ -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)
}

View 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
}