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 17:14:30 +00:00
|
|
|
#include "EvalValues/NumericEvalValue.hpp"
|
2019-05-25 14:15:20 +00:00
|
|
|
#include "EvalValues/StringEvalValue.hpp"
|
2019-05-23 16:50:09 +00:00
|
|
|
|
2019-06-01 19:38:39 +00:00
|
|
|
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerBinary(BoundBinaryExpression *expression) {
|
|
|
|
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
|
|
|
|
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
|
2019-05-24 13:31:11 +00:00
|
|
|
|
2019-05-24 17:14:30 +00:00
|
|
|
NumericEvalValue* result;
|
2019-05-24 13:31:11 +00:00
|
|
|
switch (expression->GetOperation()){
|
2019-05-24 17:14:30 +00:00
|
|
|
case BoundBinaryOperation ::Addition:
|
2019-06-01 19:38:39 +00:00
|
|
|
result = leftValue.get() -> operator+ (rightValue.get());
|
2019-05-24 17:14:30 +00:00
|
|
|
break;
|
|
|
|
case BoundBinaryOperation::Subtraction:
|
2019-06-01 19:38:39 +00:00
|
|
|
result = leftValue.get() -> operator- (rightValue.get());
|
2019-05-24 17:14:30 +00:00
|
|
|
break;
|
|
|
|
case BoundBinaryOperation::Multiplication:
|
2019-06-01 19:38:39 +00:00
|
|
|
result = leftValue.get() -> operator* (rightValue.get());
|
2019-05-24 17:14:30 +00:00
|
|
|
break;
|
|
|
|
case BoundBinaryOperation::Division:
|
2019-06-01 19:38:39 +00:00
|
|
|
result = leftValue.get() -> operator/ (rightValue.get());
|
2019-05-24 17:14:30 +00:00
|
|
|
break;
|
2019-05-24 13:31:11 +00:00
|
|
|
default:
|
2019-05-24 17:14:30 +00:00
|
|
|
throw EvaluationException("Can't evaluate operation to numeric");
|
2019-05-24 13:31:11 +00:00
|
|
|
}
|
2019-06-01 19:38:39 +00:00
|
|
|
return shared_ptr<NumericEvalValue>(result);
|
2019-05-25 11:30:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:38:39 +00:00
|
|
|
shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(BoundBinaryExpression* expression){
|
2019-05-25 11:30:20 +00:00
|
|
|
switch (expression->GetOperation()){
|
2019-05-25 11:57:43 +00:00
|
|
|
case BoundBinaryOperation::Equality:
|
|
|
|
{
|
2019-06-01 19:38:39 +00:00
|
|
|
auto leftValue = this -> EvaluateExpression(expression->GetLeft());
|
|
|
|
auto rightValue = this -> EvaluateExpression(expression->GetRight());
|
|
|
|
bool equals = leftValue.get()->operator==(rightValue.get());
|
|
|
|
return make_shared<BooleanEvalValue>(equals);
|
2019-05-25 11:57:43 +00:00
|
|
|
}
|
2019-05-25 12:17:52 +00:00
|
|
|
case BoundBinaryOperation::Inequality:
|
|
|
|
{
|
2019-06-01 19:38:39 +00:00
|
|
|
auto leftValue = this -> EvaluateExpression(expression->GetLeft());
|
|
|
|
auto rightValue = this -> EvaluateExpression(expression->GetRight());
|
|
|
|
bool equals = leftValue.get()->operator!=(rightValue.get());
|
|
|
|
return make_shared<BooleanEvalValue>(equals);
|
2019-05-25 12:17:52 +00:00
|
|
|
}
|
2019-05-25 11:30:20 +00:00
|
|
|
case BoundBinaryOperation::LogicalAnd:
|
|
|
|
{
|
2019-06-01 19:38:39 +00:00
|
|
|
auto leftValue = this -> EvaluateBoolExpression(expression->GetLeft());
|
2019-05-25 11:30:20 +00:00
|
|
|
if (!leftValue->EvaluateBool()) return leftValue;
|
2019-06-01 19:38:39 +00:00
|
|
|
auto rightValue = this -> EvaluateBoolExpression(expression->GetRight());
|
2019-05-25 11:30:20 +00:00
|
|
|
return rightValue;
|
|
|
|
}
|
|
|
|
case BoundBinaryOperation::LogicalOr:
|
|
|
|
{
|
2019-06-01 19:38:39 +00:00
|
|
|
auto leftValue = this -> EvaluateBoolExpression(expression->GetLeft());
|
2019-05-25 11:30:20 +00:00
|
|
|
if (leftValue->EvaluateBool()) return leftValue;
|
2019-06-01 19:38:39 +00:00
|
|
|
auto rightValue = this -> EvaluateBoolExpression(expression->GetRight());
|
2019-05-25 11:30:20 +00:00
|
|
|
return rightValue;
|
|
|
|
}
|
2019-05-25 11:57:43 +00:00
|
|
|
default:
|
|
|
|
throw EvaluationException("Can't evaluate operation to boolean");
|
2019-05-25 11:30:20 +00:00
|
|
|
}
|
2019-05-25 14:15:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:38:39 +00:00
|
|
|
shared_ptr<StringEvalValue> Evaluator::EvaluateStringBinary(BoundBinaryExpression* expression){
|
2019-05-25 14:15:20 +00:00
|
|
|
if (expression->GetOperation() != BoundBinaryOperation::Concatenation)
|
|
|
|
throw;
|
|
|
|
std::ostringstream strs;
|
|
|
|
auto left = this -> EvaluateStringExpression(expression->GetLeft());
|
2019-06-05 15:46:46 +00:00
|
|
|
strs << *left->EvaluateString();
|
2019-05-25 14:15:20 +00:00
|
|
|
auto right = this -> EvaluateExpression(expression->GetRight());
|
2019-06-05 15:46:46 +00:00
|
|
|
strs << *right->EvaluateString();
|
2019-06-01 19:38:39 +00:00
|
|
|
return make_shared<StringEvalValue>(strs.str());
|
2019-05-24 13:31:11 +00:00
|
|
|
}
|