Initial work on iterators, rework of variable handling by including actual string
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-06-23 15:58:14 +02:00
parent 1a84661c79
commit 76b8ba3ebc
25 changed files with 185 additions and 78 deletions

View File

@@ -83,9 +83,9 @@ namespace Porygon::Evaluation {
throw;
std::basic_ostringstream<char16_t> stringStream;
auto left = this->EvaluateStringExpression(expression->GetLeft());
stringStream << *left->EvaluateString();
stringStream << left->EvaluateString();
auto right = this->EvaluateExpression(expression->GetRight());
stringStream << *right->EvaluateString();
stringStream << right->EvaluateString();
return make_shared<StringEvalValue>(stringStream.str());
}
}

View File

@@ -24,7 +24,7 @@ namespace Porygon::Evaluation {
}
const char16_t *EvaluateEvalValueString(EvalValue *v) {
return v->EvaluateString()->c_str();
return (new u16string(v->EvaluateString()))->c_str();
}

View File

@@ -37,7 +37,7 @@ namespace Porygon::Evaluation {
throw EvaluationException("Can't evaluate this EvalValue as bool.");
}
virtual const std::u16string *EvaluateString() const {
virtual const std::u16string EvaluateString() const {
throw EvaluationException("Can't evaluate this EvalValue as string.");
}

View File

@@ -4,6 +4,8 @@
#include <sstream>
#include "EvalValue.hpp"
#include "../../Utilities/StringUtils.hpp"
namespace Porygon::Evaluation {
class NumericEvalValue : public EvalValue {
@@ -58,6 +60,10 @@ namespace Porygon::Evaluation {
return _value;
}
const std::u16string EvaluateString() const final{
return Utilities::StringUtils::IntToString(_value);
}
const shared_ptr<EvalValue> Clone() const final {
return make_shared<IntegerEvalValue>(_value);
}

View File

@@ -28,8 +28,8 @@ namespace Porygon::Evaluation {
return this->_hash == b->GetHashCode();
};
const u16string *EvaluateString() const final {
return &_value;
const u16string EvaluateString() const final {
return _value;
}
const shared_ptr<EvalValue> Clone() const final {

View File

@@ -1,23 +1,23 @@
#ifndef PORYGONLANG_TABLEEVALVALUE_HPP
#define PORYGONLANG_TABLEEVALVALUE_HPP
#include <utility>
#include <unordered_map>
#include <map>
#include "EvalValue.hpp"
using namespace std;
namespace Porygon::Evaluation {
class TableEvalValue : public EvalValue {
shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> _table;
shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> _table;
size_t _hash;
explicit TableEvalValue(shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> table, size_t hash) {
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> table, size_t hash) {
_table = std::move(table);
_hash = hash;
}
public:
explicit TableEvalValue(shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> table) {
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> table) {
_table = std::move(table);
_hash = rand();
}
@@ -39,23 +39,30 @@ namespace Porygon::Evaluation {
}
const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final {
auto hash = val->GetHashCode();
return this->_table->at(hash);
const auto stringKey = val->EvaluateString();
return this->_table->at(Utilities::HashedString(stringKey));
}
const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final {
return this->_table->at(hash);
return this->_table->at(Utilities::HashedString(hash));
}
const shared_ptr<EvalValue> IndexValue(const char *val) const {
auto hash = Utilities::HashedString::ConstHash(val);
return this->_table->at(hash);
return this->_table->at(Utilities::HashedString(hash));
}
void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const final {
auto hash = key->GetHashCode();
this->_table->at(hash) = value;
this->_table->at(Utilities::HashedString(hash)) = value;
}
const _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> GetTableIterator() const{
return _table->cbegin();
};
const _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> GetTableIteratorEnd() const{
return _table->cend();
};
};
}

View File

@@ -3,10 +3,10 @@
#include <memory>
namespace Porygon::Evaluation {
EvaluationScope::EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>> *scriptVariables,
EvaluationScope::EvaluationScope(map<Utilities::HashedString, shared_ptr<EvalValue>> *scriptVariables,
int localVariableCount) {
_scriptScope = scriptVariables;
_localScope = unordered_map<uint64_t, shared_ptr<EvalValue>>(localVariableCount);
_localScope = map<uint64_t, shared_ptr<EvalValue>>();
}
void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value) {

View File

@@ -2,17 +2,17 @@
#ifndef PORYGONLANG_EVALUATIONSCOPE_HPP
#define PORYGONLANG_EVALUATIONSCOPE_HPP
#include <unordered_map>
#include <map>
#include <vector>
#include "../EvalValues/EvalValue.hpp"
using namespace Porygon::Binder;
namespace Porygon::Evaluation {
class EvaluationScope {
unordered_map<uint32_t, shared_ptr<EvalValue>> *_scriptScope;
unordered_map<uint64_t, shared_ptr<EvalValue>> _localScope;
map<Utilities::HashedString, shared_ptr<EvalValue>> *_scriptScope;
map<uint64_t, shared_ptr<EvalValue>> _localScope;
public:
explicit EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>> *scriptVariables, int deepestScope);
explicit EvaluationScope(map<Utilities::HashedString, shared_ptr<EvalValue>> *scriptVariables, int deepestScope);
~EvaluationScope() = default;

View File

@@ -10,6 +10,7 @@
#include "../Binder/BoundExpressions/BoundTableExpression.hpp"
#include "../TableScriptType.hpp"
#include "../UserData/UserDataFunction.hpp"
#include "../Utilities/StringUtils.hpp"
using namespace std;
using namespace Porygon::Binder;
@@ -388,12 +389,13 @@ namespace Porygon::Evaluation {
const shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundNumericalTableExpression *) expression;
auto valueExpressions = tableExpression->GetExpressions();
auto values = new unordered_map<uint32_t, shared_ptr<EvalValue>>(valueExpressions->size());
auto values = new map<Utilities::HashedString, shared_ptr<EvalValue>>();
for (int i = 0; i < valueExpressions->size(); i++) {
auto val = this->EvaluateExpression(valueExpressions->at(i));
values->insert({i + 1, val});
auto key = Utilities::StringUtils::IntToString(i + 1);
values->insert({Utilities::HashedString(key), val});
}
auto valuesPointer = shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>>(values);
auto valuesPointer = shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>>(values);
return make_shared<TableEvalValue>(valuesPointer);
}
@@ -401,7 +403,7 @@ namespace Porygon::Evaluation {
auto tableExpression = (BoundTableExpression *) expression;
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
auto declaredVars = type->GetValues();
auto variables = make_shared<unordered_map<uint32_t, shared_ptr<EvalValue>>>(declaredVars->size());
auto variables = make_shared<map<Utilities::HashedString, shared_ptr<EvalValue>>>();
for (auto i : *declaredVars) {
variables->insert({i.first, nullptr});
}

View File

@@ -16,7 +16,7 @@ using namespace std;
namespace Porygon::Evaluation{
class Evaluator {
shared_ptr<EvalValue> _returnValue;
unordered_map<uint32_t, shared_ptr<EvalValue>>* _scriptVariables;
map<Utilities::HashedString, shared_ptr<EvalValue>>* _scriptVariables;
bool _hasReturned;
shared_ptr<EvalValue> _lastValue;
@@ -56,7 +56,7 @@ namespace Porygon::Evaluation{
const shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression);
public:
explicit Evaluator(unordered_map<uint32_t, shared_ptr<EvalValue>>* scriptVariables){
explicit Evaluator(map<Utilities::HashedString, shared_ptr<EvalValue>>* scriptVariables){
_scriptVariables = scriptVariables;
_hasReturned = false;
_returnValue = nullptr;

View File

@@ -0,0 +1,2 @@
#include "Iterator.hpp"

View File

@@ -0,0 +1,43 @@
#ifndef PORYGONLANG_ITERATOR_HPP
#define PORYGONLANG_ITERATOR_HPP
#include <memory>
#include "../EvalValues/EvalValue.hpp"
#include "../EvalValues/TableEvalValue.hpp"
#include "../EvalValues/StringEvalValue.hpp"
using namespace std;
namespace Porygon::Evaluation{
class Iterator {
public:
virtual shared_ptr<EvalValue> GetCurrent() = 0;
virtual bool MoveNext() = 0;
virtual void Reset() = 0;
};
class TableKeyIterator : Iterator{
_Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> _iterator;
_Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> _end;
public:
TableKeyIterator(shared_ptr<TableEvalValue> table)
: _iterator(table->GetTableIterator()), _end(table->GetTableIteratorEnd()){}
shared_ptr<EvalValue> GetCurrent() final{
return make_shared<StringEvalValue>(_iterator->first.GetString());
}
bool MoveNext() final{
std::advance(_iterator, 1);
return _iterator != _end;
}
void Reset(){
throw EvaluationException("Can't reset table key iterator");
}
};
}
#endif //PORYGONLANG_ITERATOR_HPP