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"
|
|
|
|
)
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
type (
|
|
|
|
MathOperatorType string
|
|
|
|
MathOperator struct {
|
|
|
|
*baseOperator
|
|
|
|
fn OperatorFunc
|
|
|
|
leftOnly bool
|
|
|
|
}
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
const (
|
|
|
|
MathOperatorTypeAdd MathOperatorType = "+"
|
|
|
|
MathOperatorTypeSubtract MathOperatorType = "-"
|
|
|
|
MathOperatorTypeMultiply MathOperatorType = "*"
|
|
|
|
MathOperatorTypeDivide MathOperatorType = "/"
|
|
|
|
MathOperatorTypeModulus MathOperatorType = "%"
|
|
|
|
MathOperatorTypeIncrement MathOperatorType = "++"
|
|
|
|
MathOperatorTypeDecrement MathOperatorType = "--"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mathOperators = map[MathOperatorType]OperatorFunc{
|
|
|
|
MathOperatorTypeAdd: Add,
|
|
|
|
MathOperatorTypeSubtract: Subtract,
|
|
|
|
MathOperatorTypeMultiply: Multiply,
|
|
|
|
MathOperatorTypeDivide: Divide,
|
|
|
|
MathOperatorTypeModulus: Modulus,
|
|
|
|
MathOperatorTypeIncrement: Increment,
|
|
|
|
MathOperatorTypeDecrement: Decrement,
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMathOperator(
|
|
|
|
src core.SourceMap,
|
|
|
|
left core.Expression,
|
|
|
|
right core.Expression,
|
2018-10-07 23:54:02 +02:00
|
|
|
operator MathOperatorType,
|
2018-09-18 22:42:38 +02:00
|
|
|
) (*MathOperator, error) {
|
|
|
|
fn, exists := mathOperators[operator]
|
|
|
|
|
|
|
|
if !exists {
|
2018-10-07 23:54:02 +02:00
|
|
|
return nil, core.Error(core.ErrInvalidArgument, "operator type")
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var leftOnly bool
|
|
|
|
|
|
|
|
if operator == "++" || operator == "--" {
|
|
|
|
leftOnly = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MathOperator{
|
|
|
|
&baseOperator{src, left, right},
|
|
|
|
fn,
|
|
|
|
leftOnly,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (operator *MathOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
|
|
|
|
left, err := operator.left.Exec(ctx, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if operator.leftOnly {
|
2018-10-07 23:54:02 +02:00
|
|
|
return operator.Eval(ctx, left, values.None)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 *MathOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
|
|
|
|
if operator.leftOnly {
|
|
|
|
return operator.fn(left, values.None), nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
return operator.fn(left, right), nil
|
|
|
|
}
|