From 8dcf31cb4090385625a9d1963587d0ab796de46b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 22 Sep 2019 11:11:22 +0200 Subject: [PATCH] Extern support for getting data from Table EvalValues --- src/Evaluator/EvalValues/TableEvalValue.cpp | 67 ++++++++++++++------- src/Evaluator/EvalValues/TableEvalValue.hpp | 21 ++++++- src/ScriptTypes/TableScriptType.hpp | 5 +- 3 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/Evaluator/EvalValues/TableEvalValue.cpp b/src/Evaluator/EvalValues/TableEvalValue.cpp index 3ce6906..509dd05 100644 --- a/src/Evaluator/EvalValues/TableEvalValue.cpp +++ b/src/Evaluator/EvalValues/TableEvalValue.cpp @@ -3,31 +3,52 @@ #include "NumericEvalValue.hpp" #include "../Iterator/NumericalKeyIterator.hpp" -Porygon::Evaluation::Iterator * Porygon::Evaluation::TableEvalValue::GetKeyIterator() const { - return new TableKeyIterator(this); -} - -Porygon::Evaluation::EvalValue * -Porygon::Evaluation::TableEvalValue::UnaryOperation(Porygon::Binder::BoundUnaryOperation operation) const { - if (operation == Porygon::Binder::BoundUnaryOperation::Count){ - return new NumericEvalValue(static_cast(this->_table->size())); +namespace Porygon::Evaluation { + Porygon::Evaluation::Iterator *Porygon::Evaluation::TableEvalValue::GetKeyIterator() const { + return new TableKeyIterator(this); } - 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::EvalValue * + Porygon::Evaluation::TableEvalValue::UnaryOperation(Porygon::Binder::BoundUnaryOperation operation) const { + if (operation == Porygon::Binder::BoundUnaryOperation::Count) { + return new NumericEvalValue(static_cast(this->_table->size())); + } + 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); + } + +extern "C"{ + TableScriptType::TableType GetTableType(TableEvalValue* table){ + return table->GetTableType(); + } + + size_t GetTableLength(TableEvalValue* table){ + return table->GetLength(); + } + + EvalValue* IndexTableHash(TableEvalValue* table, uint32_t hash){ + return table->IndexValue(hash); + } + + EvalValue* IndexTableObj(TableEvalValue* table, EvalValue* key){ + return table->IndexValue(key->GetHashCode()); } } - -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); -} +} \ No newline at end of file diff --git a/src/Evaluator/EvalValues/TableEvalValue.hpp b/src/Evaluator/EvalValues/TableEvalValue.hpp index 2655731..c386bef 100644 --- a/src/Evaluator/EvalValues/TableEvalValue.hpp +++ b/src/Evaluator/EvalValues/TableEvalValue.hpp @@ -4,8 +4,9 @@ #include #include #include "EvalValue.hpp" -#include "../../Utilities/Random.hpp" #include "../EvalValuePointer.hpp" +#include "../../Utilities/Random.hpp" +#include "../../ScriptTypes/TableScriptType.hpp" using namespace std; @@ -62,6 +63,12 @@ namespace Porygon::Evaluation { return this->_table->at(*hash)->Clone(); } + [[nodiscard]] + inline EvalValue* IndexValue(uint32_t hash) const { + return this->_table->at(Utilities::HashedString::CreateLookup(hash))->Clone(); + } + + [[nodiscard]] inline const EvalValue* IndexValue(const char *val) const { auto hash = Utilities::HashedString::ConstHash(val); @@ -92,6 +99,14 @@ namespace Porygon::Evaluation { [[nodiscard]] EvalValue *UnaryOperation(Binder::BoundUnaryOperation operation) const override; void SetIndexValue(const Utilities::HashedString *key, const EvalValue *value) const override; + + [[nodiscard]] virtual TableScriptType::TableType GetTableType() const{ + return TableScriptType::TableType ::StringKeyed; + } + + [[nodiscard]] size_t GetLength() const{ + return _table->size(); + } }; class NumericTableEvalValue : public TableEvalValue{ @@ -109,6 +124,10 @@ namespace Porygon::Evaluation { [[nodiscard]] Iterator *GetKeyIterator() const final; [[nodiscard]] EvalValue *Clone() const override; + + [[nodiscard]] TableScriptType::TableType GetTableType() const final{ + return TableScriptType::TableType ::Numerical; + } }; } diff --git a/src/ScriptTypes/TableScriptType.hpp b/src/ScriptTypes/TableScriptType.hpp index 4e32e71..367c15f 100644 --- a/src/ScriptTypes/TableScriptType.hpp +++ b/src/ScriptTypes/TableScriptType.hpp @@ -3,19 +3,21 @@ #define PORYGONLANG_TABLESCRIPTTYPE_HPP #include #include +#include #include "../Binder/BoundVariables/BoundVariable.hpp" #include "../Exception.hpp" namespace Porygon{ class TableScriptType : public ScriptType{ + public: enum TableType{ Unknown, Numerical, StringKeyed, Dictionary }; - + private: using ContentTypes = unordered_map>*; using KeyValueType = std::pair, shared_ptr>; @@ -173,6 +175,7 @@ namespace Porygon{ }; } } + }; }