NewArithmetic compiles an expression of the form "expr1 op expr2" for the arithmetic operators +, -, *, /
(sctx *super.Context, op string, lhs, rhs Evaluator)
| 424 | // NewArithmetic compiles an expression of the form "expr1 op expr2" |
| 425 | // for the arithmetic operators +, -, *, / |
| 426 | func NewArithmetic(sctx *super.Context, op string, lhs, rhs Evaluator) (Evaluator, error) { |
| 427 | n := newNumeric(sctx, lhs, rhs) |
| 428 | switch op { |
| 429 | case "+": |
| 430 | return &Add{sctx: sctx, operands: n}, nil |
| 431 | case "-": |
| 432 | return &Subtract{sctx: sctx, operands: n}, nil |
| 433 | case "*": |
| 434 | return &Multiply{sctx: sctx, operands: n}, nil |
| 435 | case "/": |
| 436 | return &Divide{sctx: sctx, operands: n}, nil |
| 437 | case "%": |
| 438 | return &Modulo{sctx: sctx, operands: n}, nil |
| 439 | } |
| 440 | return nil, fmt.Errorf("unknown arithmetic operator: %s", op) |
| 441 | } |
| 442 | |
| 443 | func (a *Add) Eval(this super.Value) super.Value { |
| 444 | lhsVal, rhsVal, typ, errVal := a.operands.evalAndPromote(this) |
no test coverage detected