Added equality operation for eval values
This commit is contained in:
parent
4a4a71ca73
commit
d6a6e116fe
|
@ -34,7 +34,15 @@ NumericEvalValue* Evaluator::EvaluateIntegerBinary(BoundBinaryExpression *expres
|
|||
|
||||
BooleanEvalValue* Evaluator::EvaluateBooleanBinary(BoundBinaryExpression* expression){
|
||||
switch (expression->GetOperation()){
|
||||
case BoundBinaryOperation::Equality:break;
|
||||
case BoundBinaryOperation::Equality:
|
||||
{
|
||||
EvalValue* leftValue = this -> EvaluateExpression(expression->GetLeft());
|
||||
EvalValue* rightValue = this -> EvaluateExpression(expression->GetRight());
|
||||
bool equals = leftValue->operator==(rightValue);
|
||||
delete leftValue;
|
||||
delete rightValue;
|
||||
return new BooleanEvalValue(equals);
|
||||
}
|
||||
case BoundBinaryOperation::LogicalAnd:
|
||||
{
|
||||
BooleanEvalValue* leftValue = this -> EvaluateBoolExpression(expression->GetLeft());
|
||||
|
@ -51,5 +59,7 @@ BooleanEvalValue* Evaluator::EvaluateBooleanBinary(BoundBinaryExpression* expres
|
|||
BooleanEvalValue* rightValue = this -> EvaluateBoolExpression(expression->GetRight());
|
||||
return rightValue;
|
||||
}
|
||||
default:
|
||||
throw EvaluationException("Can't evaluate operation to boolean");
|
||||
}
|
||||
}
|
|
@ -11,6 +11,12 @@ public:
|
|||
virtual ~EvalValue() = default;
|
||||
virtual ScriptType* GetType() = 0;
|
||||
|
||||
virtual bool operator ==(EvalValue* b) = 0;
|
||||
|
||||
virtual bool operator !=(EvalValue*b){
|
||||
return ! (this->operator==(b));
|
||||
}
|
||||
|
||||
virtual long EvaluateInteger(){
|
||||
throw EvaluationException("Can't evaluate this EvalValue as integer.");
|
||||
}
|
||||
|
@ -45,6 +51,12 @@ public:
|
|||
bool EvaluateBool() final{
|
||||
return _value;
|
||||
}
|
||||
|
||||
bool operator ==(EvalValue* b) final{
|
||||
if (b->GetType()->GetClass() != TypeClass::Bool)
|
||||
return false;
|
||||
return this->EvaluateBool() == b->EvaluateBool();
|
||||
};
|
||||
};
|
||||
|
||||
#endif //PORYGONLANG_EVALVALUE_HPP
|
||||
|
|
|
@ -64,3 +64,17 @@ NumericEvalValue *NumericEvalValue::operator/(NumericEvalValue *b) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool NumericEvalValue::operator==(EvalValue *b) {
|
||||
if (b->GetType()->GetClass() != TypeClass::Number)
|
||||
return false;
|
||||
auto numVal = (NumericEvalValue*)b;
|
||||
if (this->IsFloat() != numVal->IsFloat())
|
||||
return false;
|
||||
|
||||
if (this->IsFloat()){
|
||||
return this->EvaluateFloat() == numVal->EvaluateFloat();
|
||||
} else{
|
||||
return this->EvaluateInteger() == numVal->EvaluateInteger();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ public:
|
|||
NumericEvalValue* operator -(NumericEvalValue* b);
|
||||
NumericEvalValue* operator *(NumericEvalValue* b);
|
||||
NumericEvalValue* operator /(NumericEvalValue* b);
|
||||
bool operator ==(EvalValue* b) final;
|
||||
};
|
||||
|
||||
class IntegerEvalValue : public NumericEvalValue{
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
|
||||
#ifdef TESTS_BUILD
|
||||
#include <catch.hpp>
|
||||
#include "../src/Script.hpp"
|
||||
|
||||
TEST_CASE( "True Equals True", "[integration]" ) {
|
||||
Script script = Script::Create("true == true");
|
||||
REQUIRE(!script.Diagnostics -> HasErrors());
|
||||
script.Evaluate();
|
||||
auto lastValue = script.GetLastValue();
|
||||
REQUIRE(lastValue->EvaluateBool());
|
||||
}
|
||||
TEST_CASE( "True Not Equals True", "[integration]" ) {
|
||||
Script script = Script::Create("true == false");
|
||||
REQUIRE(!script.Diagnostics -> HasErrors());
|
||||
script.Evaluate();
|
||||
auto lastValue = script.GetLastValue();
|
||||
REQUIRE(!lastValue->EvaluateBool());
|
||||
}
|
||||
|
||||
TEST_CASE( "False Equals False", "[integration]" ) {
|
||||
Script script = Script::Create("false == false");
|
||||
REQUIRE(!script.Diagnostics -> HasErrors());
|
||||
script.Evaluate();
|
||||
auto lastValue = script.GetLastValue();
|
||||
REQUIRE(lastValue->EvaluateBool());
|
||||
}
|
||||
|
||||
TEST_CASE( "10 Equals 10", "[integration]" ) {
|
||||
Script script = Script::Create("10 == 10");
|
||||
REQUIRE(!script.Diagnostics -> HasErrors());
|
||||
script.Evaluate();
|
||||
auto lastValue = script.GetLastValue();
|
||||
REQUIRE(lastValue->EvaluateBool());
|
||||
}
|
||||
TEST_CASE( "10 Not Equals 5", "[integration]" ) {
|
||||
Script script = Script::Create("10 == 5");
|
||||
REQUIRE(!script.Diagnostics -> HasErrors());
|
||||
script.Evaluate();
|
||||
auto lastValue = script.GetLastValue();
|
||||
REQUIRE(!lastValue->EvaluateBool());
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue