2019-05-23 16:50:09 +00:00
|
|
|
|
2019-05-24 13:31:11 +00:00
|
|
|
#include "../Script.hpp"
|
2019-05-23 16:50:09 +00:00
|
|
|
#include "EvaluationException.hpp"
|
|
|
|
#include "Evaluator.hpp"
|
|
|
|
|
2019-05-24 13:31:11 +00:00
|
|
|
long Evaluator::EvaluateIntegerBinary(BoundBinaryExpression *expression) {
|
|
|
|
long leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
|
|
|
|
long rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|