PorygonLang/src/Evaluator/BinaryEvaluation.cpp

76 lines
3.2 KiB
C++

#include "../Script.hpp"
#include "EvaluationException.hpp"
#include "Evaluator.hpp"
#include "EvalValues/NumericEvalValue.hpp"
#include "EvalValues/StringEvalValue.hpp"
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerBinary(BoundBinaryExpression *expression) {
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
NumericEvalValue* result;
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;
default:
throw EvaluationException("Can't evaluate operation to numeric");
}
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);
}
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);
}
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());
}