public override ToyExpression Reduce(ToyContext context) { var reduced1 = _operand1.Reduce(context); var reduced2 = _operand2.Reduce(context); var number1 = reduced1 as ToyNumberValue; var number2 = reduced2 as ToyNumberValue; if (number1 != null && number2 != null) { switch (_operator) { case ToyOperatorType.Add: return(new ToyNumberValue(number1.Value + number2.Value)); case ToyOperatorType.Sub: return(new ToyNumberValue(number1.Value - number2.Value)); case ToyOperatorType.Mul: return(new ToyNumberValue(number1.Value * number2.Value)); case ToyOperatorType.Div: return(new ToyNumberValue(number1.Value / number2.Value)); default: throw new ArgumentOutOfRangeException(); } } return(new ToyBinaryExpression(_operator, reduced1, reduced2)); }
public override ToyExpression Reduce(ToyContext context) { var reduced = _expression.Reduce(context); var number = reduced as ToyNumberValue; if (number != null) { return(new ToyNumberValue(-number.Value)); } return(new ToyNegateExpression(reduced)); }