Removed LastValue variable, as it can be better emulated with basic return values

This commit is contained in:
Deukhoofd 2019-07-27 10:51:24 +02:00
parent 8b80b5789d
commit 268f6b59fb
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
10 changed files with 111 additions and 161 deletions

View File

@ -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;

View File

@ -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<const EvalValue> Evaluator::Evaluate(const BoundScriptStatement *statement) {
this->_evaluationScope = make_shared<EvaluationScope>(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) {

View File

@ -19,7 +19,6 @@ namespace Porygon::Evaluation{
map<Utilities::HashedString, shared_ptr<const EvalValue>>* _scriptVariables;
bool _hasReturned;
bool _hasBroken;
shared_ptr<const EvalValue> _lastValue;
//Porygon::Script* _scriptData;
shared_ptr<EvaluationScope> _evaluationScope;
@ -64,14 +63,10 @@ namespace Porygon::Evaluation{
_evaluationScope(nullptr){
}
const EvalValue* Evaluate(const BoundScriptStatement* statement);
shared_ptr<const EvalValue> Evaluate(const BoundScriptStatement* statement);
shared_ptr<const EvalValue> EvaluateFunction(const GenericFunctionEvalValue *function,
const vector<EvalValue *> &parameters);
inline const EvalValue* GetLastValue(){
return _lastValue.get();
}
};
}

View File

@ -30,7 +30,7 @@ Porygon::Script::Script(const u16string& s)
this -> Parse(s);
}
const EvalValue* Porygon::Script::Evaluate() {
shared_ptr<const EvalValue> 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){

View File

@ -41,9 +41,9 @@ namespace Porygon{
_returnType = t;
}
const EvalValue* Evaluate();
shared_ptr<const EvalValue> Evaluate();
const EvalValue* GetLastValue();
//const EvalValue* GetLastValue();
const EvalValue* GetVariable(const u16string& key);
bool HasVariable(const u16string& key);

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}