2018-09-18 22:42:38 +02:00
|
|
|
package operators
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
EqualityOperator struct {
|
|
|
|
*baseOperator
|
2018-10-07 23:54:02 +02:00
|
|
|
fn OperatorFunc
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
var equalityOperators = map[string]OperatorFunc{
|
2018-09-18 22:42:38 +02:00
|
|
|
"==": Equal,
|
|
|
|
"!=": NotEqual,
|
|
|
|
">": Greater,
|
|
|
|
"<": Less,
|
|
|
|
">=": GreaterOrEqual,
|
|
|
|
"<=": LessOrEqual,
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEqualityOperator(
|
|
|
|
src core.SourceMap,
|
|
|
|
left core.Expression,
|
|
|
|
right core.Expression,
|
|
|
|
operator string,
|
|
|
|
) (*EqualityOperator, error) {
|
|
|
|
fn, exists := equalityOperators[operator]
|
|
|
|
|
|
|
|
if !exists {
|
2018-10-07 23:54:02 +02:00
|
|
|
return nil, core.Error(core.ErrInvalidArgument, "aotype")
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &EqualityOperator{
|
|
|
|
&baseOperator{src, left, right},
|
|
|
|
fn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (operator *EqualityOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
|
|
|
|
left, err := operator.left.Exec(ctx, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
right, err := operator.right.Exec(ctx, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
return operator.Eval(ctx, left, right)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (operator *EqualityOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
return operator.fn(left, right), nil
|
|
|
|
}
|