Reworked evaluation to use internal type instead of boost::any
This commit is contained in:
@@ -2,76 +2,30 @@
|
||||
#include "../Script.hpp"
|
||||
#include "EvaluationException.hpp"
|
||||
#include "Evaluator.hpp"
|
||||
#include "EvalValues/NumericEvalValue.hpp"
|
||||
|
||||
long Evaluator::EvaluateIntegerBinary(BoundBinaryExpression *expression) {
|
||||
long leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
|
||||
long rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
|
||||
NumericEvalValue* Evaluator::EvaluateIntegerBinary(BoundBinaryExpression *expression) {
|
||||
NumericEvalValue* leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
|
||||
NumericEvalValue* rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
|
||||
|
||||
NumericEvalValue* result;
|
||||
switch (expression->GetOperation()){
|
||||
case BoundBinaryOperation ::Addition: return leftValue + rightValue;
|
||||
case BoundBinaryOperation ::Subtraction: return leftValue - rightValue;
|
||||
case BoundBinaryOperation ::Multiplication: return leftValue * rightValue;
|
||||
case BoundBinaryOperation ::Division: return leftValue / rightValue;
|
||||
|
||||
case BoundBinaryOperation ::Addition:
|
||||
result = leftValue -> operator+ (rightValue);
|
||||
break;
|
||||
case BoundBinaryOperation::Subtraction:
|
||||
result = leftValue -> operator- (rightValue);
|
||||
break;
|
||||
case BoundBinaryOperation::Multiplication:
|
||||
result = leftValue -> operator* (rightValue);
|
||||
break;
|
||||
case BoundBinaryOperation::Division:
|
||||
result = leftValue -> operator/ (rightValue);
|
||||
break;
|
||||
default:
|
||||
throw EvaluationException("Can't evaluate operation to integer");
|
||||
}
|
||||
}
|
||||
|
||||
double EvaluateBinaryOperation(double l, double r, BoundBinaryOperation op){
|
||||
switch (op){
|
||||
case BoundBinaryOperation ::Addition: return l + r;
|
||||
case BoundBinaryOperation ::Subtraction: return l - r;
|
||||
case BoundBinaryOperation ::Multiplication: return l * r;
|
||||
case BoundBinaryOperation ::Division: return l / r;
|
||||
|
||||
default:
|
||||
throw EvaluationException("Can't evaluate operation to float");
|
||||
}
|
||||
}
|
||||
|
||||
double EvaluateBinaryOperation(double l, long r, BoundBinaryOperation op){
|
||||
switch (op){
|
||||
case BoundBinaryOperation ::Addition: return l + r;
|
||||
case BoundBinaryOperation ::Subtraction: return l - r;
|
||||
case BoundBinaryOperation ::Multiplication: return l * r;
|
||||
case BoundBinaryOperation ::Division: return l / r;
|
||||
|
||||
default:
|
||||
throw EvaluationException("Can't evaluate operation to float");
|
||||
}
|
||||
}
|
||||
|
||||
double EvaluateBinaryOperation(long l, double r, BoundBinaryOperation op){
|
||||
switch (op){
|
||||
case BoundBinaryOperation ::Addition: return l + r;
|
||||
case BoundBinaryOperation ::Subtraction: return l - r;
|
||||
case BoundBinaryOperation ::Multiplication: return l * r;
|
||||
case BoundBinaryOperation ::Division: return l / r;
|
||||
|
||||
default:
|
||||
throw EvaluationException("Can't evaluate operation to float");
|
||||
}
|
||||
}
|
||||
|
||||
double Evaluator::EvaluateFloatBinary(BoundBinaryExpression *expression) {
|
||||
auto left = expression->GetLeft();
|
||||
auto right = expression->GetRight();
|
||||
auto leftType = (NumericScriptType*)left->GetType();
|
||||
auto rightType = (NumericScriptType*)right->GetType();
|
||||
if (leftType->IsFloat()){
|
||||
double leftValue = this -> EvaluateFloatExpression(left);
|
||||
if (rightType->IsFloat()){
|
||||
double rightValue = this -> EvaluateFloatExpression(right);
|
||||
return EvaluateBinaryOperation(leftValue, rightValue, expression->GetOperation());
|
||||
} else{
|
||||
long rightValue = this -> EvaluateIntegerExpression(right);
|
||||
return EvaluateBinaryOperation(leftValue, rightValue, expression->GetOperation());
|
||||
}
|
||||
} else{
|
||||
long leftValue = this-> EvaluateIntegerExpression(left);
|
||||
// If the left is an integer, we know the right must be a float, otherwise we'd be evaluating as integer;
|
||||
double rightValue = this -> EvaluateFloatExpression(right);
|
||||
return EvaluateBinaryOperation(leftValue, rightValue, expression->GetOperation());
|
||||
throw EvaluationException("Can't evaluate operation to numeric");
|
||||
}
|
||||
delete leftValue;
|
||||
delete rightValue;
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user