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

This commit is contained in:
2019-07-27 10:51:24 +02:00
parent 8b80b5789d
commit 268f6b59fb
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);