#include "../Script.hpp" #include "EvaluationException.hpp" #include "Evaluator.hpp" #include "EvalValues/NumericEvalValue.hpp" #include "EvalValues/StringEvalValue.hpp" shared_ptr 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(result); } shared_ptr 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(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(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 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(strs.str()); }