Large rework of tables
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-09-15 13:08:11 +02:00
parent e89782f921
commit 458274f370
19 changed files with 360 additions and 271 deletions

View File

@@ -77,6 +77,12 @@ namespace Porygon::Evaluation {
throw EvaluationException(err.str());
}
virtual void SetIndexValue(const Utilities::HashedString *key, const EvalValue* value) const {
std::stringstream err;
err << "Can't index this EvalValue: " << ToString() << " with key: " << value->ToString();
throw EvaluationException(err.str());
}
[[nodiscard]]
virtual Iterator * GetKeyIterator() const{
throw EvaluationException("Can't iterate over this EvalValue");

View File

@@ -1,21 +0,0 @@
#include "NumericalTableEvalValue.hpp"
#include "../Iterator/NumericalKeyIterator.hpp"
#include "../../Utilities/Random.hpp"
inline Porygon::Evaluation::Iterator *Porygon::Evaluation::NumericalTableEvalValue::GetKeyIterator() const {
return new NumericalKeyIterator(this);
}
Porygon::Evaluation::NumericalTableEvalValue::NumericalTableEvalValue(shared_ptr<vector<EvalValuePointer>> table) :
_table(std::move(table)),
_hash(Utilities::Random::Get())
{
}
Porygon::Evaluation::EvalValue *
Porygon::Evaluation::NumericalTableEvalValue::UnaryOperation(Porygon::Binder::BoundUnaryOperation operation) const {
if (operation == Porygon::Binder::BoundUnaryOperation::Count){
return new NumericEvalValue(static_cast<int64_t>(this->_table->size()));
}
return EvalValue::UnaryOperation(operation);
}

View File

@@ -1,84 +0,0 @@
#ifndef PORYGONLANG_NUMERICALTABLEEVALVALUE_HPP
#define PORYGONLANG_NUMERICALTABLEEVALVALUE_HPP
#include <utility>
#include <map>
#include "EvalValue.hpp"
#include "../EvalValuePointer.hpp"
using namespace std;
namespace Porygon::Evaluation {
class NumericalTableEvalValue : public EvalValue {
const shared_ptr<vector<EvalValuePointer>> _table;
const size_t _hash;
explicit NumericalTableEvalValue(shared_ptr<vector<EvalValuePointer>> table, size_t hash)
: _table(std::move(table)),
_hash(hash)
{
}
public:
explicit NumericalTableEvalValue(shared_ptr<vector<EvalValuePointer>> table);
[[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::Table;
}
[[nodiscard]]
inline size_t GetHashCode() const final {
return _hash;
}
[[nodiscard]]
inline bool operator==(const EvalValue *b) const final {
return this->_hash == b->GetHashCode();
}
bool operator!=(const EvalValue *b) const override {
return !operator==(b);
}
[[nodiscard]]
inline EvalValue* Clone() const final {
return new NumericalTableEvalValue(_table, _hash);
}
[[nodiscard]]
inline const EvalValue* IndexValue(const EvalValue *val) const final {
const auto index = val->EvaluateInteger() - 1;
return this->_table->at(index).Clone();
}
[[nodiscard]]
inline EvalValue* IndexValue(const Utilities::HashedString* hash) const final {
return this->_table->at(hash->GetHash() - 1)-> Clone();
}
inline void SetIndexValue(const EvalValue *key, const EvalValue* value) const final {
auto index = key->EvaluateInteger();
index--;
if (this->_table->size() <= index){
this->_table->resize(index);
}
this->_table->at(index) = value;
}
[[nodiscard]]
Iterator * GetKeyIterator() const final;
[[nodiscard]]
inline shared_ptr<vector<EvalValuePointer>> GetTable() const{
return _table;
}
[[nodiscard]] EvalValue *UnaryOperation(Binder::BoundUnaryOperation operation) const override;;
};
}
#undef iteratorKind
#endif //PORYGONLANG_NUMERICALTABLEEVALVALUE_HPP

View File

@@ -1,8 +1,9 @@
#include "TableEvalValue.hpp"
#include "../Iterator/SimpleKeyIterator.hpp"
#include "NumericEvalValue.hpp"
#include "../Iterator/NumericalKeyIterator.hpp"
inline Porygon::Evaluation::Iterator * Porygon::Evaluation::TableEvalValue::GetKeyIterator() const {
Porygon::Evaluation::Iterator * Porygon::Evaluation::TableEvalValue::GetKeyIterator() const {
return new TableKeyIterator(this);
}
@@ -13,3 +14,20 @@ Porygon::Evaluation::TableEvalValue::UnaryOperation(Porygon::Binder::BoundUnaryO
}
return EvalValue::UnaryOperation(operation);
}
void Porygon::Evaluation::TableEvalValue::SetIndexValue(const Porygon::Utilities::HashedString *key,
const Porygon::Evaluation::EvalValue *value) const {
auto insert = _table->insert({*key, value});
if (!insert.second) {
_table->at(*key).ClearAssign(value);
}
}
Porygon::Evaluation::Iterator *Porygon::Evaluation::NumericTableEvalValue::GetKeyIterator() const {
return new NumericalKeyIterator(this);
}
Porygon::Evaluation::EvalValue *Porygon::Evaluation::NumericTableEvalValue::Clone() const {
return new NumericTableEvalValue(_table, _hash);
}

View File

@@ -11,15 +11,15 @@ using namespace std;
namespace Porygon::Evaluation {
class TableEvalValue : public EvalValue {
const shared_ptr<map<Utilities::HashedString, EvalValuePointer>> _table;
const size_t _hash;
protected:
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, EvalValuePointer>> table, size_t hash)
: _table(std::move(table)),
_hash(hash)
{
}
const shared_ptr<map<Utilities::HashedString, EvalValuePointer>> _table;
const size_t _hash;
public:
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, EvalValuePointer>> table) :
_table(std::move(table)),
@@ -47,7 +47,7 @@ namespace Porygon::Evaluation {
}
[[nodiscard]]
inline EvalValue* Clone() const final {
inline EvalValue* Clone() const override {
return new TableEvalValue(_table, _hash);
}
@@ -70,11 +70,15 @@ namespace Porygon::Evaluation {
inline void SetIndexValue(const EvalValue *key, const EvalValue* value) const final {
auto hash = key->GetHashCode();
this->_table->at(Utilities::HashedString::CreateLookup(hash)).ClearAssign(value);
auto lookup = Utilities::HashedString::CreateLookup(hash);
auto insert = _table->insert({lookup, value});
if (!insert.second) {
_table->at(lookup).ClearAssign(value);
}
}
[[nodiscard]]
Iterator * GetKeyIterator() const final;
Iterator * GetKeyIterator() const override;
[[nodiscard]]
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, EvalValuePointer>> GetTableIterator() const{
@@ -85,7 +89,26 @@ namespace Porygon::Evaluation {
return _table->cend();
}
EvalValue *UnaryOperation(Binder::BoundUnaryOperation operation) const override;;
[[nodiscard]] EvalValue *UnaryOperation(Binder::BoundUnaryOperation operation) const override;
void SetIndexValue(const Utilities::HashedString *key, const EvalValue *value) const override;
};
class NumericTableEvalValue : public TableEvalValue{
explicit NumericTableEvalValue(shared_ptr<map<Utilities::HashedString, EvalValuePointer>> table, size_t hash)
: TableEvalValue(std::move(table), hash)
{
}
public:
explicit NumericTableEvalValue(shared_ptr<map<Utilities::HashedString, EvalValuePointer>> table)
:TableEvalValue(std::move(table))
{
}
[[nodiscard]] Iterator *GetKeyIterator() const final;
[[nodiscard]] EvalValue *Clone() const override;
};
}

View File

@@ -11,7 +11,6 @@
#include "../Binder/BoundExpressions/BoundRequireExpression.hpp"
#include "../ScriptTypes/TableScriptType.hpp"
#include "../UserData/UserDataFunction.hpp"
#include "EvalValues/NumericalTableEvalValue.hpp"
#include "../UserData/UserDataValue.hpp"
using namespace std;
@@ -93,12 +92,21 @@ namespace Porygon::Evaluation {
void Evaluator::EvaluateIndexAssignmentStatement(const BoundIndexAssignmentStatement *statement) {
auto indexExpression = statement->GetIndexExpression();
auto value = this->EvaluateExpression(statement->GetValueExpression());
auto index = ((BoundIndexExpression *) indexExpression);
auto table = this->EvaluateExpression(index->GetIndexableExpression());
auto key = this->EvaluateExpression(index->GetIndexExpression());
table->SetIndexValue(key.Get(), value.Take());
if (indexExpression->GetKind() == BoundExpressionKind::Index){
auto index = dynamic_cast<const BoundIndexExpression*>(indexExpression);
auto table = this->EvaluateExpression(index->GetIndexableExpression());
auto key = this->EvaluateExpression(index->GetIndexExpression());
table->SetIndexValue(key.Get(), value.Take());
}
else{
auto index = dynamic_cast<const BoundPeriodIndexExpression*>(indexExpression);
auto table = this->EvaluateExpression(index->GetIndexableExpression());
auto key = index->GetIndex();
table->SetIndexValue(key, value.Take());
}
}
void Evaluator::EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement) {
auto type = statement->GetType();
auto key = statement->GetKey();
@@ -396,20 +404,22 @@ namespace Porygon::Evaluation {
EvalValuePointer Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundNumericalTableExpression *) expression;
auto valueExpressions = tableExpression->GetExpressions();
auto values = new vector<EvalValuePointer>(valueExpressions->size());
auto values = new map<Utilities::HashedString, EvalValuePointer>();
for (size_t i = 0; i < valueExpressions->size(); i++) {
auto val = this->EvaluateExpression(valueExpressions->at(i));
values->at(i) = val.Take();
auto k = Utilities::StringUtils::IntToString(i + 1);
auto s = Utilities::HashedString(new u16string(k));
values->insert({s, val});
}
auto valuesPointer = shared_ptr<vector<EvalValuePointer>>(values);
return new NumericalTableEvalValue(valuesPointer);
auto valuesPointer = shared_ptr<map<Utilities::HashedString, EvalValuePointer>>(values);
return new NumericTableEvalValue(valuesPointer);
}
EvalValuePointer Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundTableExpression *) expression;
const auto& baseType = tableExpression -> GetType();
auto type = dynamic_pointer_cast<const TableScriptType>(baseType);
auto declaredVars = type->GetValues();
auto declaredVars = type->GetContentTypes();
auto variables = make_shared<map<Utilities::HashedString, EvalValuePointer>>();
for (const auto& i : *declaredVars) {
variables->insert({i.first, nullptr});

View File

@@ -1 +1,6 @@
#include "NumericalKeyIterator.hpp"
const Porygon::Evaluation::EvalValue *Porygon::Evaluation::NumericalKeyIterator::GetCurrent() {
auto s = *_iterator->first.GetString();
return new NumericEvalValue(Utilities::StringUtils::ParseInteger(s));
}

View File

@@ -1,34 +1,15 @@
#ifndef PORYGONLANG_NUMERICALKEYITERATOR_HPP
#define PORYGONLANG_NUMERICALKEYITERATOR_HPP
#include "Iterator.hpp"
#include "../EvalValues/NumericalTableEvalValue.hpp"
#include "../EvalValues/NumericEvalValue.hpp"
#include "SimpleKeyIterator.hpp"
namespace Porygon::Evaluation{
class NumericalKeyIterator : public Iterator{
const shared_ptr<vector<EvalValuePointer>> _vec;
const size_t _size;
size_t _position = 0;
class NumericalKeyIterator : public TableKeyIterator{
public:
explicit NumericalKeyIterator(const NumericalTableEvalValue* table)
: _vec(table->GetTable()), _size(_vec->size() + 1){}
const EvalValue *GetCurrent() override;
explicit NumericalKeyIterator(const TableEvalValue* table)
: TableKeyIterator(table){}
inline const EvalValue* GetCurrent() final{
return new NumericEvalValue(static_cast<int64_t >(_position));
}
inline bool MoveNext() final{
_position++;
return _position != _size;
}
inline void Reset() final{
_position = 0;
}
};
}
#endif //PORYGONLANG_NUMERICALKEYITERATOR_HPP

View File

@@ -8,6 +8,7 @@
namespace Porygon::Evaluation{
class TableKeyIterator : public Iterator{
protected:
_Rb_tree_const_iterator<pair<const Utilities::HashedString, EvalValuePointer>> _iterator;
_Rb_tree_const_iterator<pair<const Utilities::HashedString, EvalValuePointer>> _end;
bool _hasStarted = false;
@@ -15,7 +16,7 @@ namespace Porygon::Evaluation{
explicit TableKeyIterator(const TableEvalValue* table)
: _iterator(table->GetTableIterator()), _end(table->GetTableIteratorEnd()){}
inline const EvalValue* GetCurrent() final{
inline const EvalValue* GetCurrent() override {
return new StringEvalValue(*_iterator->first.GetString());
}