1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-25 01:32:13 +02:00
ferret/pkg/runtime/expressions/operators/logical.go

114 lines
2.3 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package operators
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type (
LogicalOperatorType int
LogicalOperator struct {
*baseOperator
value LogicalOperatorType
}
)
2018-10-06 00:26:49 +02:00
const (
LogicalOperatorTypeAnd LogicalOperatorType = 0
LogicalOperatorTypeOr LogicalOperatorType = 1
LogicalOperatorTypeNot LogicalOperatorType = 2
2018-09-18 22:42:38 +02:00
)
var logicalOperators = map[string]LogicalOperatorType{
"&&": LogicalOperatorTypeAnd,
"AND": LogicalOperatorTypeAnd,
"||": LogicalOperatorTypeOr,
"OR": LogicalOperatorTypeOr,
"NOT": LogicalOperatorTypeNot,
2018-09-18 22:42:38 +02:00
}
func NewLogicalOperator(
src core.SourceMap,
left core.Expression,
right core.Expression,
operator string,
) (*LogicalOperator, error) {
op, exists := logicalOperators[operator]
if !exists {
return nil, core.Error(core.ErrInvalidArgument, "operator")
2018-09-18 22:42:38 +02:00
}
return &LogicalOperator{
&baseOperator{
src,
left,
right,
},
op,
}, nil
}
func (operator *LogicalOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
if operator.value == LogicalOperatorTypeNot {
2018-09-23 03:06:19 +02:00
val, err := operator.right.Exec(ctx, scope)
2018-09-18 22:42:38 +02:00
2018-09-23 03:06:19 +02:00
if err != nil {
return values.None, core.SourceError(operator.src, err)
}
return Not(val, values.None), nil
2018-09-18 22:42:38 +02:00
}
2018-09-23 03:06:19 +02:00
left, err := operator.left.Exec(ctx, scope)
if err != nil {
return values.None, core.SourceError(operator.src, err)
2018-09-18 22:42:38 +02:00
}
2018-09-23 02:28:33 +02:00
leftBool := values.ToBoolean(left)
if operator.value == LogicalOperatorTypeAnd && leftBool == values.False {
2018-09-23 02:28:33 +02:00
if left.Type() == core.BooleanType {
return values.False, nil
}
return left, nil
2018-09-18 22:42:38 +02:00
}
if operator.value == LogicalOperatorTypeOr && leftBool == values.True {
2018-09-23 02:28:33 +02:00
return left, nil
2018-09-18 22:42:38 +02:00
}
right, err := operator.right.Exec(ctx, scope)
if err != nil {
2018-09-23 03:06:19 +02:00
return values.None, core.SourceError(operator.src, err)
2018-09-18 22:42:38 +02:00
}
2018-09-23 02:28:33 +02:00
return right, nil
2018-09-18 22:42:38 +02:00
}
func (operator *LogicalOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
if operator.value == LogicalOperatorTypeNot {
return Not(right, values.None), nil
}
leftBool := values.ToBoolean(left)
if operator.value == LogicalOperatorTypeAnd && leftBool == values.False {
if left.Type() == core.BooleanType {
return values.False, nil
}
return left, nil
}
if operator.value == LogicalOperatorTypeOr && leftBool == values.True {
return left, nil
}
return right, nil
}