Rework of memory handling in Evaluation
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-07-27 17:59:42 +02:00
parent 268f6b59fb
commit ccc6e297f2
32 changed files with 496 additions and 461 deletions

View File

@@ -64,9 +64,9 @@ TEST_CASE( "Evaluate String", "[integration]" ) {
auto script = Porygon::Script::Create(*sc);
REQUIRE(!script->Diagnostics -> HasErrors());
auto result = script->Evaluate();
size_t size = GetEvalValueStringLength(result.get());
size_t size = GetEvalValueStringLength(result.Get());
auto dst = new char16_t[size + 1]{'\0'};
EvaluateEvalValueString(result.get(), dst, size);
EvaluateEvalValueString(result.Get(), dst, size);
auto s = u16string(dst);
REQUIRE(s == u"foo bar");
delete[] dst;

View File

@@ -12,6 +12,7 @@ namespace Porygon::Evaluation{
class Iterator;
}
#include "../Iterator/Iterator.hpp"
#include "../../Binder/BoundOperators.hpp"
namespace Porygon::Evaluation {
@@ -33,7 +34,7 @@ namespace Porygon::Evaluation {
}
[[nodiscard]]
virtual shared_ptr<const EvalValue> Clone() const = 0;
virtual EvalValue* Clone() const = 0;
[[nodiscard]]
virtual long EvaluateInteger() const {
@@ -59,16 +60,16 @@ namespace Porygon::Evaluation {
virtual std::size_t GetHashCode() const = 0;
[[nodiscard]]
virtual shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const {
virtual const EvalValue* IndexValue(const EvalValue *val) const {
throw EvaluationException("Can't index this EvalValue");
}
[[nodiscard]]
virtual shared_ptr<const EvalValue> IndexValue(uint32_t hash) const {
virtual const EvalValue* IndexValue(uint32_t hash) const {
throw EvaluationException("Can't index this EvalValue");
}
virtual void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const {
virtual void SetIndexValue(const EvalValue *key, const EvalValue* value) const {
throw EvaluationException("Can't index this EvalValue");
}
@@ -76,6 +77,16 @@ namespace Porygon::Evaluation {
virtual Iterator * GetKeyIterator() const{
throw EvaluationException("Can't iterate over this EvalValue");
}
[[nodiscard]]
virtual EvalValue* BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue* b) const{
throw EvaluationException("Binary operations are not implemented for this type.");
}
[[nodiscard]]
virtual EvalValue* UnaryOperation(Binder::BoundUnaryOperation operation) const{
throw EvaluationException("Unary operations are not implemented for this type.");
}
};
class BooleanEvalValue : public EvalValue {
@@ -86,8 +97,8 @@ namespace Porygon::Evaluation {
}
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<BooleanEvalValue>(_value);
inline EvalValue* Clone() const final {
return new BooleanEvalValue(_value);
}
[[nodiscard]]
@@ -111,6 +122,25 @@ namespace Porygon::Evaluation {
inline std::size_t GetHashCode() const final {
return _value;
}
[[nodiscard]]
inline EvalValue* BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue* b) const final{
auto bVal = b -> EvaluateBool();
switch (operation){
case Binder::BoundBinaryOperation::LogicalAnd: return new BooleanEvalValue(_value && bVal);
case Binder::BoundBinaryOperation::LogicalOr: return new BooleanEvalValue(_value || bVal);
default:
throw;
}
}
[[nodiscard]]
EvalValue* UnaryOperation(Binder::BoundUnaryOperation operation) const final{
switch (operation){
case Binder::BoundUnaryOperation::LogicalNegation: return new BooleanEvalValue(!_value);
default: throw;
}
}
};
}

View File

@@ -15,8 +15,8 @@ namespace Porygon::Evaluation{
}
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final{
return make_shared<NilEvalValue>();
inline EvalValue* Clone() const final{
return new NilEvalValue();
}
[[nodiscard]]

View File

@@ -143,4 +143,70 @@ namespace Porygon::Evaluation {
}
}
}
EvalValue *IntegerEvalValue::BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue *b) const {
auto right = dynamic_cast<const NumericEvalValue*>(b);
if (right->IsFloat()){
auto rightVal = right->EvaluateFloat();
switch (operation){
case Binder::BoundBinaryOperation::Addition: return new FloatEvalValue((double)_value + rightVal);
case Binder::BoundBinaryOperation::Subtraction: return new FloatEvalValue((double)_value - rightVal);
case Binder::BoundBinaryOperation::Multiplication: return new FloatEvalValue((double)_value * rightVal);
case Binder::BoundBinaryOperation::Division: return new FloatEvalValue((double)_value / rightVal);
case Binder::BoundBinaryOperation::LessThan: return new BooleanEvalValue((double)_value < rightVal);
case Binder::BoundBinaryOperation::LessThanEquals: return new BooleanEvalValue((double)_value <= rightVal);
case Binder::BoundBinaryOperation::GreaterThan: return new BooleanEvalValue((double)_value > rightVal);
case Binder::BoundBinaryOperation::GreaterThanEquals: return new BooleanEvalValue((double)_value >= rightVal);
default:
throw;
}
} else{
auto rightVal = right->EvaluateInteger();
switch (operation){
case Binder::BoundBinaryOperation::Addition: return new IntegerEvalValue(_value + rightVal);
case Binder::BoundBinaryOperation::Subtraction: return new IntegerEvalValue(_value - rightVal);
case Binder::BoundBinaryOperation::Multiplication: return new IntegerEvalValue(_value * rightVal);
case Binder::BoundBinaryOperation::Division: return new IntegerEvalValue(_value / rightVal);
case Binder::BoundBinaryOperation::LessThan: return new BooleanEvalValue(_value < rightVal);
case Binder::BoundBinaryOperation::LessThanEquals: return new BooleanEvalValue(_value <= rightVal);
case Binder::BoundBinaryOperation::GreaterThan: return new BooleanEvalValue(_value > rightVal);
case Binder::BoundBinaryOperation::GreaterThanEquals: return new BooleanEvalValue(_value >= rightVal);
default:
throw;
}
}
}
EvalValue *FloatEvalValue::BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue *b) const {
auto right = dynamic_cast<const NumericEvalValue*>(b);
if (right->IsFloat()){
auto rightVal = right->EvaluateFloat();
switch (operation){
case Binder::BoundBinaryOperation::Addition: return new FloatEvalValue(_value + rightVal);
case Binder::BoundBinaryOperation::Subtraction: return new FloatEvalValue(_value - rightVal);
case Binder::BoundBinaryOperation::Multiplication: return new FloatEvalValue(_value * rightVal);
case Binder::BoundBinaryOperation::Division: return new FloatEvalValue(_value / rightVal);
case Binder::BoundBinaryOperation::LessThan: return new BooleanEvalValue(_value < rightVal);
case Binder::BoundBinaryOperation::LessThanEquals: return new BooleanEvalValue(_value <= rightVal);
case Binder::BoundBinaryOperation::GreaterThan: return new BooleanEvalValue(_value > rightVal);
case Binder::BoundBinaryOperation::GreaterThanEquals: return new BooleanEvalValue(_value >= rightVal);
default:
throw;
}
} else{
auto rightVal = right->EvaluateInteger();
switch (operation){
case Binder::BoundBinaryOperation::Addition: return new IntegerEvalValue((long)_value + rightVal);
case Binder::BoundBinaryOperation::Subtraction: return new IntegerEvalValue((long)_value - rightVal);
case Binder::BoundBinaryOperation::Multiplication: return new IntegerEvalValue((long)_value * rightVal);
case Binder::BoundBinaryOperation::Division: return new IntegerEvalValue((long)_value / rightVal);
case Binder::BoundBinaryOperation::LessThan: return new BooleanEvalValue(_value < rightVal);
case Binder::BoundBinaryOperation::LessThanEquals: return new BooleanEvalValue(_value <= rightVal);
case Binder::BoundBinaryOperation::GreaterThan: return new BooleanEvalValue(_value > rightVal);
case Binder::BoundBinaryOperation::GreaterThanEquals: return new BooleanEvalValue(_value >= rightVal);
default:
throw;
}
}
}
}

View File

@@ -74,14 +74,25 @@ namespace Porygon::Evaluation {
}
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<IntegerEvalValue>(_value);
inline EvalValue* Clone() const final {
return new IntegerEvalValue(_value);
}
[[nodiscard]]
inline std::size_t GetHashCode() const final {
return std::hash<long>{}(_value);
}
[[nodiscard]]
EvalValue* BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue* b) const final;
[[nodiscard]]
EvalValue* UnaryOperation(Binder::BoundUnaryOperation operation) const final{
switch (operation){
case Binder::BoundUnaryOperation::Negation: return new IntegerEvalValue(-_value);
default: throw;
}
}
};
class FloatEvalValue : public NumericEvalValue {
@@ -112,14 +123,25 @@ namespace Porygon::Evaluation {
}
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<FloatEvalValue>(_value);
inline EvalValue* Clone() const final {
return new FloatEvalValue(_value);
}
[[nodiscard]]
inline std::size_t GetHashCode() const final {
return std::hash<double>{}(_value);
}
[[nodiscard]]
inline EvalValue* BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue* b) const final;
[[nodiscard]]
EvalValue* UnaryOperation(Binder::BoundUnaryOperation operation) const final{
switch (operation){
case Binder::BoundUnaryOperation::Negation: return new FloatEvalValue(-_value);
default: throw;
}
}
};
}

View File

@@ -4,3 +4,9 @@
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(rand())
{
}

View File

@@ -5,26 +5,23 @@
#include <utility>
#include <map>
#include "EvalValue.hpp"
#include "../EvalValuePointer.hpp"
using namespace std;
namespace Porygon::Evaluation {
class NumericalTableEvalValue : public EvalValue {
const shared_ptr<vector<shared_ptr<const EvalValue>>> _table;
const shared_ptr<vector<EvalValuePointer>> _table;
const size_t _hash;
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<const EvalValue>>> table, 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<shared_ptr<const EvalValue>>> table) :
_table(std::move(table)),
_hash(rand())
{
}
explicit NumericalTableEvalValue(shared_ptr<vector<EvalValuePointer>> table);
[[nodiscard]]
inline TypeClass GetTypeClass() const final {
@@ -42,22 +39,22 @@ namespace Porygon::Evaluation {
}
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return shared_ptr<EvalValue>(new NumericalTableEvalValue(_table, _hash));
inline EvalValue* Clone() const final {
return new NumericalTableEvalValue(_table, _hash);
}
[[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
inline const EvalValue* IndexValue(const EvalValue *val) const final {
const auto index = val->EvaluateInteger() - 1;
return this->_table->at(index);
return this->_table->at(index).Clone();
}
[[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(uint32_t hash) const final {
return this->_table->at(hash - 1);
inline EvalValue* IndexValue(uint32_t hash) const final {
return this->_table->at(hash - 1)-> Clone();
}
inline void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const final {
inline void SetIndexValue(const EvalValue *key, const EvalValue* value) const final {
auto index = key->EvaluateInteger();
this->_table->at(index - 1) = value;
}
@@ -66,7 +63,7 @@ namespace Porygon::Evaluation {
Iterator * GetKeyIterator() const final;
[[nodiscard]]
inline shared_ptr<vector<shared_ptr<const EvalValue>>> GetTable() const{
inline shared_ptr<vector<EvalValuePointer>> GetTable() const{
return _table;
};
};

View File

@@ -42,15 +42,15 @@ namespace Porygon::Evaluation {
protected:
const shared_ptr<const GenericFunctionScriptType> _type;
const size_t _hash;
vector<shared_ptr<GenericFunctionOption>>* _options;
shared_ptr<vector<shared_ptr<GenericFunctionOption>>> _options;
GenericFunctionEvalValue(shared_ptr<const GenericFunctionScriptType> type, size_t hash,
shared_ptr<vector<shared_ptr<GenericFunctionOption>>> options)
:_type(std::move(type)), _hash(hash), _options(std::move(options))
{}
public:
GenericFunctionEvalValue(shared_ptr<const GenericFunctionScriptType> type, size_t hash)
: _type(move(type)),
_hash(hash), _options(new vector<shared_ptr<GenericFunctionOption>>()){
}
~GenericFunctionEvalValue() final{
delete _options;
_hash(hash), _options(make_shared<vector<shared_ptr<GenericFunctionOption>>>()){
}
GenericFunctionEvalValue(const GenericFunctionEvalValue& _) = delete;
@@ -58,19 +58,16 @@ namespace Porygon::Evaluation {
GenericFunctionEvalValue& operator =(GenericFunctionEvalValue v) = delete;
[[nodiscard]]
shared_ptr<const EvalValue> Clone() const final {
auto t = make_shared<GenericFunctionEvalValue>(_type, _hash);
for (const auto& o: *_options){
t->_options->push_back(o);
}
return t;
EvalValue* Clone() const final {
return new GenericFunctionEvalValue(_type, _hash, _options);
}
inline void RegisterOption(GenericFunctionOption* option) const{
_options->push_back(shared_ptr<GenericFunctionOption>(option));
}
inline std::shared_ptr<const ScriptType> GetType() const {
[[nodiscard]]
inline std::shared_ptr<const GenericFunctionScriptType> GetType() const {
return _type;
}

View File

@@ -34,20 +34,31 @@ namespace Porygon::Evaluation {
}
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<StringEvalValue>(_value);
inline EvalValue* Clone() const final {
return new StringEvalValue(_value);
}
shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
EvalValue* IndexValue(const EvalValue *val) const final {
// Porygon is 1-indexed, so we convert to that.
auto l = val->EvaluateInteger() - 1;
return make_shared<const StringEvalValue>(u16string(1, _value[l]));
return new StringEvalValue(u16string(1, _value[l]));
}
[[nodiscard]]
inline std::size_t GetHashCode() const final {
return _hash;
}
[[nodiscard]]
inline EvalValue* BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue* b) const final{
if (operation != Binder::BoundBinaryOperation::Concatenation){
throw EvaluationException("Binary operation not supported for strings.");
}
std::basic_ostringstream<char16_t> stringStream;
stringStream << _value;
stringStream << b->EvaluateString();
return new StringEvalValue(stringStream.str());
}
};
}

View File

@@ -5,22 +5,23 @@
#include <map>
#include "EvalValue.hpp"
#include "../../Utilities/Random.hpp"
#include "../EvalValuePointer.hpp"
using namespace std;
namespace Porygon::Evaluation {
class TableEvalValue : public EvalValue {
const shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> _table;
const shared_ptr<map<Utilities::HashedString, EvalValuePointer>> _table;
const size_t _hash;
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> table, size_t hash)
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, EvalValuePointer>> table, size_t hash)
: _table(std::move(table)),
_hash(hash)
{
}
public:
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> table) :
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, EvalValuePointer>> table) :
_table(std::move(table)),
_hash(Utilities::Random::Get())
{
@@ -42,41 +43,41 @@ namespace Porygon::Evaluation {
}
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return shared_ptr<EvalValue>(new TableEvalValue(_table, _hash));
inline EvalValue* Clone() const final {
return new TableEvalValue(_table, _hash);
}
[[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
inline EvalValue* IndexValue(const EvalValue *val) const final {
const auto stringKey = val->EvaluateString();
return this->_table->at(Utilities::HashedString::CreateLookup(stringKey));
return this->_table->at(Utilities::HashedString::CreateLookup(stringKey))->Clone();
}
[[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(uint32_t hash) const final {
return this->_table->at(Utilities::HashedString::CreateLookup(hash));
inline EvalValue* IndexValue(uint32_t hash) const final {
return this->_table->at(Utilities::HashedString::CreateLookup(hash))->Clone();
}
[[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(const char *val) const {
inline const EvalValue* IndexValue(const char *val) const {
auto hash = Utilities::HashedString::ConstHash(val);
return this->_table->at(Utilities::HashedString::CreateLookup(hash));
return this->_table->at(Utilities::HashedString::CreateLookup(hash)).Clone();
}
inline void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const final {
inline void SetIndexValue(const EvalValue *key, const EvalValue* value) const final {
auto hash = key->GetHashCode();
this->_table->at(Utilities::HashedString::CreateLookup(hash)) = value;
this->_table->at(Utilities::HashedString::CreateLookup(hash)).ClearAssign(value);
}
[[nodiscard]]
Iterator * GetKeyIterator() const final;
[[nodiscard]]
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> GetTableIterator() const{
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, EvalValuePointer>> GetTableIterator() const{
return _table->cbegin();
};
[[nodiscard]]
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> GetTableIteratorEnd() const{
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, EvalValuePointer>> GetTableIteratorEnd() const{
return _table->cend();
};
};