PorygonLang/src/Evaluator/BinaryEvaluation.cpp

76 lines
3.2 KiB
C++
Raw Normal View History

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