diff --git a/src/Evaluator/EvalValues/EvalValue.cpp b/src/Evaluator/EvalValues/EvalValue.cpp index 765b0fc..4954094 100644 --- a/src/Evaluator/EvalValues/EvalValue.cpp +++ b/src/Evaluator/EvalValues/EvalValue.cpp @@ -63,11 +63,10 @@ TEST_CASE( "Evaluate String", "[integration]" ) { auto sc = new u16string(u"\"foo bar\""); auto script = Porygon::Script::Create(*sc); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - size_t size = GetEvalValueStringLength(lastValue); + auto result = script->Evaluate(); + size_t size = GetEvalValueStringLength(result.get()); auto dst = new char16_t[size + 1]{'\0'}; - EvaluateEvalValueString(lastValue, dst, size); + EvaluateEvalValueString(result.get(), dst, size); auto s = u16string(dst); REQUIRE(s == u"foo bar"); delete[] dst; diff --git a/src/Evaluator/Evaluator.cpp b/src/Evaluator/Evaluator.cpp index 950a809..9839323 100644 --- a/src/Evaluator/Evaluator.cpp +++ b/src/Evaluator/Evaluator.cpp @@ -10,16 +10,20 @@ #include "../TableScriptType.hpp" #include "../UserData/UserDataFunction.hpp" #include "EvalValues/NumericalTableEvalValue.hpp" -#include "../Utilities/Random.hpp" using namespace std; using namespace Porygon::Binder; namespace Porygon::Evaluation { - const EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) { + shared_ptr Evaluator::Evaluate(const BoundScriptStatement *statement) { this->_evaluationScope = make_shared(this->_scriptVariables); + auto statements = statement->GetStatements(); + if (statements -> size() == 1 && statements->at(0)->GetKind() == BoundStatementKind::Expression){ + auto expStatement = (BoundExpressionStatement*) statements -> at(0); + return this -> EvaluateExpression(expStatement -> GetExpression()); + } EvaluateBlockStatement(statement); - return this->_returnValue.get(); + return this->_returnValue; } void Evaluator::EvaluateStatement(const BoundStatement *statement) { @@ -67,8 +71,7 @@ namespace Porygon::Evaluation { } void Evaluator::EvaluateExpressionStatement(const BoundExpressionStatement *statement) { - // Save new value - this->_lastValue = this->EvaluateExpression(statement->GetExpression()); + this->EvaluateExpression(statement->GetExpression()); } void Evaluator::EvaluateAssignmentStatement(const BoundAssignmentStatement *statement) { diff --git a/src/Evaluator/Evaluator.hpp b/src/Evaluator/Evaluator.hpp index 8feab27..6da99b5 100644 --- a/src/Evaluator/Evaluator.hpp +++ b/src/Evaluator/Evaluator.hpp @@ -19,7 +19,6 @@ namespace Porygon::Evaluation{ map>* _scriptVariables; bool _hasReturned; bool _hasBroken; - shared_ptr _lastValue; //Porygon::Script* _scriptData; shared_ptr _evaluationScope; @@ -64,14 +63,10 @@ namespace Porygon::Evaluation{ _evaluationScope(nullptr){ } - const EvalValue* Evaluate(const BoundScriptStatement* statement); + shared_ptr Evaluate(const BoundScriptStatement* statement); shared_ptr EvaluateFunction(const GenericFunctionEvalValue *function, const vector ¶meters); - inline const EvalValue* GetLastValue(){ - return _lastValue.get(); - } - }; } diff --git a/src/Script.cpp b/src/Script.cpp index 52a0023..8516500 100644 --- a/src/Script.cpp +++ b/src/Script.cpp @@ -30,7 +30,7 @@ Porygon::Script::Script(const u16string& s) this -> Parse(s); } -const EvalValue* Porygon::Script::Evaluate() { +shared_ptr Porygon::Script::Evaluate() { return _evaluator->Evaluate(_boundScript.get()); } @@ -71,9 +71,9 @@ bool Porygon::Script::HasVariable(const u16string &key) { return f != _scriptVariables->end(); } -const EvalValue *Porygon::Script::GetLastValue() { +/*const EvalValue *Porygon::Script::GetLastValue() { return _evaluator->GetLastValue(); -} +}*/ bool Porygon::Script::HasFunction(const u16string &key) { auto f = _scriptVariables->find(HashedString::CreateLookup(key)); @@ -109,12 +109,10 @@ extern "C" { return Porygon::Script::Create(s); } - void EvaluateScript(Porygon::Script* script){ - script->Evaluate(); - } - - const EvalValue* GetLastValue(Porygon::Script* script){ - return script->GetLastValue(); + const EvalValue* EvaluateScript(Porygon::Script* script){ + auto result = script -> Evaluate(); + auto resultPtr = result.get(); + return resultPtr; } bool HasVariable(Porygon::Script* script, const char16_t* key){ diff --git a/src/Script.hpp b/src/Script.hpp index 52fc453..1b3685f 100644 --- a/src/Script.hpp +++ b/src/Script.hpp @@ -41,9 +41,9 @@ namespace Porygon{ _returnType = t; } - const EvalValue* Evaluate(); + shared_ptr Evaluate(); - const EvalValue* GetLastValue(); + //const EvalValue* GetLastValue(); const EvalValue* GetVariable(const u16string& key); bool HasVariable(const u16string& key); diff --git a/tests/integration/EqualityOperationsTests.cpp b/tests/integration/EqualityOperationsTests.cpp index d2fbc79..94476da 100644 --- a/tests/integration/EqualityOperationsTests.cpp +++ b/tests/integration/EqualityOperationsTests.cpp @@ -7,33 +7,29 @@ using namespace Porygon; TEST_CASE( "True Equals True", "[integration]" ) { auto script = Script::Create("true == true"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "True Not Equals True", "[integration]" ) { auto script = Script::Create("true == false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } TEST_CASE( "True Nequals False", "[integration]" ) { auto script = Script::Create("true ~= false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "True Not Nequals True", "[integration]" ) { auto script = Script::Create("true ~= true"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } @@ -41,134 +37,119 @@ TEST_CASE( "True Not Nequals True", "[integration]" ) { TEST_CASE( "False Equals False", "[integration]" ) { auto script = Script::Create("false == false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "10 Equals 10", "[integration]" ) { auto script = Script::Create("10 == 10"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "10 Not Equals 5", "[integration]" ) { auto script = Script::Create("10 == 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } TEST_CASE( "10 < 5 == false", "[integration]" ) { auto script = Script::Create("10 < 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE_FALSE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE_FALSE(result->EvaluateBool()); delete script; } TEST_CASE( "2 < 60 == true", "[integration]" ) { auto script = Script::Create("2 < 60"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "5 < 5 == false", "[integration]" ) { auto script = Script::Create("5 < 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE_FALSE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE_FALSE(result->EvaluateBool()); delete script; } TEST_CASE( "10 <= 5 == false", "[integration]" ) { auto script = Script::Create("10 <= 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE_FALSE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE_FALSE(result->EvaluateBool()); delete script; } TEST_CASE( "2 <= 60 == true", "[integration]" ) { auto script = Script::Create("2 <= 60"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "5 <= 5 == true", "[integration]" ) { auto script = Script::Create("5 <= 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "10 > 5 == true", "[integration]" ) { auto script = Script::Create("10 > 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "2 > 60 == false", "[integration]" ) { auto script = Script::Create("2 > 60"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE_FALSE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE_FALSE(result->EvaluateBool()); delete script; } TEST_CASE( "5 > 5 == false", "[integration]" ) { auto script = Script::Create("5 > 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE_FALSE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE_FALSE(result->EvaluateBool()); delete script; } TEST_CASE( "10 >= 5 == true", "[integration]" ) { auto script = Script::Create("10 >= 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "2 >= 60 == false", "[integration]" ) { auto script = Script::Create("2 >= 60"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE_FALSE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE_FALSE(result->EvaluateBool()); delete script; } TEST_CASE( "5 >= 5 == true", "[integration]" ) { auto script = Script::Create("5 >= 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } diff --git a/tests/integration/IndexTests.cpp b/tests/integration/IndexTests.cpp index 38829bb..5d2fec6 100644 --- a/tests/integration/IndexTests.cpp +++ b/tests/integration/IndexTests.cpp @@ -6,9 +6,8 @@ using namespace Porygon; TEST_CASE( "String indexing", "[integration]" ) { auto script = Script::Create("'foobar'[4]"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateString() == u"b"); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateString() == u"b"); delete script; } diff --git a/tests/integration/LogicalOperationsTests.cpp b/tests/integration/LogicalOperationsTests.cpp index d6e13da..8850bee 100644 --- a/tests/integration/LogicalOperationsTests.cpp +++ b/tests/integration/LogicalOperationsTests.cpp @@ -6,102 +6,90 @@ using namespace Porygon; TEST_CASE( "Basic True", "[integration]" ) { auto script = Script::Create("true"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "Basic False", "[integration]" ) { auto script = Script::Create("false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } TEST_CASE( "True and True", "[integration]" ) { auto script = Script::Create("true and true"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "True and False", "[integration]" ) { auto script = Script::Create("true and false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } TEST_CASE( "False and True", "[integration]" ) { auto script = Script::Create("false and true"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } TEST_CASE( "False and False", "[integration]" ) { auto script = Script::Create("false and false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } TEST_CASE( "True or True", "[integration]" ) { auto script = Script::Create("true or true"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "True or False", "[integration]" ) { auto script = Script::Create("true or false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "False or True", "[integration]" ) { auto script = Script::Create("false or true"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } TEST_CASE( "False or False", "[integration]" ) { auto script = Script::Create("false or false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } TEST_CASE( "Not True", "[integration]" ) { auto script = Script::Create("not true"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(!lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(!result->EvaluateBool()); delete script; } TEST_CASE( "Not False", "[integration]" ) { auto script = Script::Create("not false"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateBool()); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateBool()); delete script; } diff --git a/tests/integration/NumericalOperationsTests.cpp b/tests/integration/NumericalOperationsTests.cpp index 5d33a10..b91613c 100644 --- a/tests/integration/NumericalOperationsTests.cpp +++ b/tests/integration/NumericalOperationsTests.cpp @@ -6,83 +6,73 @@ using namespace Porygon; TEST_CASE( "Integer Negation", "[integration]" ) { auto script = Script::Create("-60"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateInteger() == -60); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateInteger() == -60); delete script; } TEST_CASE( "Float Negation", "[integration]" ) { auto script = Script::Create("-5.65"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateFloat() == Approx(-5.65)); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateFloat() == Approx(-5.65)); delete script; } TEST_CASE( "Integer Addition", "[integration]" ) { auto script = Script::Create("1 + 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateInteger() == 6); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateInteger() == 6); delete script; } TEST_CASE( "Integer Subtraction", "[integration]" ) { auto script = Script::Create("1 - 5"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateInteger() == -4); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateInteger() == -4); delete script; } TEST_CASE( "Integer Multiplication", "[integration]" ) { auto script = Script::Create("5 * 8"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateInteger() == 40); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateInteger() == 40); delete script; } TEST_CASE( "Integer Division", "[integration]" ) { auto script = Script::Create("40 / 8"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateInteger() == 5); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateInteger() == 5); delete script; } TEST_CASE( "Float Addition", "[integration]" ) { auto script = Script::Create("1.2 + 5.34"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateFloat() == 6.54); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateFloat() == 6.54); delete script; } TEST_CASE( "Float Subtraction", "[integration]" ) { auto script = Script::Create("1.8 - 5.14"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateFloat() == -3.34); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateFloat() == -3.34); delete script; } TEST_CASE( "Float Multiplication", "[integration]" ) { auto script = Script::Create("5.2 * 8.9"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateFloat() == 46.28); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateFloat() == 46.28); delete script; } TEST_CASE( "Float Division", "[integration]" ) { auto script = Script::Create("95.18 / 8.87"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateFloat() == Approx(10.7305524239)); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateFloat() == Approx(10.7305524239)); delete script; } #endif diff --git a/tests/integration/StringOperationsTests.cpp b/tests/integration/StringOperationsTests.cpp index 338d144..bfaad2b 100644 --- a/tests/integration/StringOperationsTests.cpp +++ b/tests/integration/StringOperationsTests.cpp @@ -8,27 +8,24 @@ using namespace Porygon; TEST_CASE( "Simple String", "[integration]" ) { auto script = Script::Create("\"foo bar\""); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateString() == u"foo bar"); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateString() == u"foo bar"); delete script; } TEST_CASE( "String Concat", "[integration]" ) { auto script = Script::Create(R"("foo" + "bar")"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateString() == u"foobar"); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateString() == u"foobar"); delete script; } TEST_CASE( "String Concat 2", "[integration]" ) { auto script = Script::Create("'foo' + 'bar'"); REQUIRE(!script->Diagnostics -> HasErrors()); - script->Evaluate(); - auto lastValue = script->GetLastValue(); - REQUIRE(lastValue->EvaluateString() == u"foobar"); + auto result = script->Evaluate(); + REQUIRE(result->EvaluateString() == u"foobar"); delete script; }