This commit is contained in:
@@ -7,28 +7,28 @@
|
||||
namespace Porygon::Evaluation {
|
||||
|
||||
extern "C" {
|
||||
Porygon::TypeClass GetEvalValueTypeClass(EvalValue *v) {
|
||||
Porygon::TypeClass GetEvalValueTypeClass(const EvalValue *v) {
|
||||
return v->GetTypeClass();
|
||||
}
|
||||
|
||||
int64_t EvaluateEvalValueInteger(EvalValue *v) {
|
||||
int64_t EvaluateEvalValueInteger(const EvalValue *v) {
|
||||
return v->EvaluateInteger();
|
||||
}
|
||||
|
||||
double EvaluateEvalValueFloat(EvalValue *v) {
|
||||
double EvaluateEvalValueFloat(const EvalValue *v) {
|
||||
return v->EvaluateFloat();
|
||||
}
|
||||
|
||||
bool EvaluateEvalValueBool(EvalValue *v) {
|
||||
bool EvaluateEvalValueBool(const EvalValue *v) {
|
||||
return v->EvaluateBool();
|
||||
}
|
||||
|
||||
size_t GetEvalValueStringLength(EvalValue *v) {
|
||||
size_t GetEvalValueStringLength(const EvalValue *v) {
|
||||
auto result = v->EvaluateString();
|
||||
return result.size();
|
||||
}
|
||||
|
||||
int EvaluateEvalValueString(EvalValue *v, char16_t* dst, size_t capacity){
|
||||
int EvaluateEvalValueString(const EvalValue *v, char16_t* dst, size_t capacity){
|
||||
auto result = v->EvaluateString();
|
||||
for (int i = 0; i < capacity; i++){
|
||||
dst[i] = result[i];
|
||||
|
||||
@@ -21,46 +21,58 @@ namespace Porygon::Evaluation {
|
||||
|
||||
virtual ~EvalValue() = default;
|
||||
|
||||
virtual const TypeClass GetTypeClass() const = 0;
|
||||
[[nodiscard]]
|
||||
virtual TypeClass GetTypeClass() const = 0;
|
||||
|
||||
virtual const bool operator==(EvalValue *b) const = 0;
|
||||
[[nodiscard]]
|
||||
virtual bool operator==(const EvalValue *b) const = 0;
|
||||
|
||||
virtual const bool operator!=(EvalValue *b) const {
|
||||
[[nodiscard]]
|
||||
virtual bool operator!=(const EvalValue *b) const {
|
||||
return !(this->operator==(b));
|
||||
}
|
||||
|
||||
virtual const shared_ptr<EvalValue> Clone() const = 0;
|
||||
[[nodiscard]]
|
||||
virtual shared_ptr<const EvalValue> Clone() const = 0;
|
||||
|
||||
virtual const long EvaluateInteger() const {
|
||||
[[nodiscard]]
|
||||
virtual long EvaluateInteger() const {
|
||||
throw EvaluationException("Can't evaluate this EvalValue as integer.");
|
||||
}
|
||||
|
||||
virtual const double EvaluateFloat() const {
|
||||
[[nodiscard]]
|
||||
virtual double EvaluateFloat() const {
|
||||
throw EvaluationException("Can't evaluate this EvalValue as float.");
|
||||
}
|
||||
|
||||
virtual const bool EvaluateBool() const {
|
||||
[[nodiscard]]
|
||||
virtual bool EvaluateBool() const {
|
||||
throw EvaluationException("Can't evaluate this EvalValue as bool.");
|
||||
}
|
||||
|
||||
virtual const std::u16string EvaluateString() const {
|
||||
[[nodiscard]]
|
||||
virtual std::u16string EvaluateString() const {
|
||||
throw EvaluationException("Can't evaluate this EvalValue as string.");
|
||||
}
|
||||
|
||||
virtual const std::size_t GetHashCode() const = 0;
|
||||
[[nodiscard]]
|
||||
virtual std::size_t GetHashCode() const = 0;
|
||||
|
||||
virtual const shared_ptr<EvalValue> IndexValue(EvalValue *val) const {
|
||||
[[nodiscard]]
|
||||
virtual shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const {
|
||||
throw EvaluationException("Can't index this EvalValue");
|
||||
}
|
||||
|
||||
virtual const shared_ptr<EvalValue> IndexValue(uint32_t hash) const {
|
||||
[[nodiscard]]
|
||||
virtual shared_ptr<const EvalValue> IndexValue(uint32_t hash) const {
|
||||
throw EvaluationException("Can't index this EvalValue");
|
||||
}
|
||||
|
||||
virtual void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const {
|
||||
virtual void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const {
|
||||
throw EvaluationException("Can't index this EvalValue");
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
virtual Iterator * GetKeyIterator() const{
|
||||
throw EvaluationException("Can't iterate over this EvalValue");
|
||||
}
|
||||
@@ -73,25 +85,30 @@ namespace Porygon::Evaluation {
|
||||
: _value(val) {
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> Clone() const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> Clone() const final {
|
||||
return make_shared<BooleanEvalValue>(_value);
|
||||
}
|
||||
|
||||
inline const TypeClass GetTypeClass() const final {
|
||||
[[nodiscard]]
|
||||
inline TypeClass GetTypeClass() const final {
|
||||
return TypeClass::Bool;
|
||||
}
|
||||
|
||||
inline const bool EvaluateBool() const final {
|
||||
[[nodiscard]]
|
||||
inline bool EvaluateBool() const final {
|
||||
return _value;
|
||||
}
|
||||
|
||||
const bool operator==(EvalValue *b) const final {
|
||||
[[nodiscard]]
|
||||
bool operator==(const EvalValue *b) const final {
|
||||
if (b->GetTypeClass() != TypeClass::Bool)
|
||||
return false;
|
||||
return this->EvaluateBool() == b->EvaluateBool();
|
||||
};
|
||||
|
||||
inline const std::size_t GetHashCode() const final {
|
||||
[[nodiscard]]
|
||||
inline std::size_t GetHashCode() const final {
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <utility>
|
||||
|
||||
#ifndef PORYGONLANG_EVALVALUEHELPER_HPP
|
||||
#define PORYGONLANG_EVALVALUEHELPER_HPP
|
||||
|
||||
|
||||
@@ -5,18 +5,22 @@
|
||||
|
||||
namespace Porygon::Evaluation{
|
||||
class NilEvalValue : public EvalValue{
|
||||
inline const TypeClass GetTypeClass() const final{
|
||||
[[nodiscard]]
|
||||
inline TypeClass GetTypeClass() const final{
|
||||
return TypeClass ::Nil;
|
||||
}
|
||||
inline const bool operator==(EvalValue *b) const final{
|
||||
[[nodiscard]]
|
||||
inline bool operator==(const EvalValue *b) const final{
|
||||
return b->GetTypeClass() == TypeClass ::Nil;
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> Clone() const final{
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> Clone() const final{
|
||||
return make_shared<NilEvalValue>();
|
||||
}
|
||||
|
||||
inline const std::size_t GetHashCode() const final{
|
||||
[[nodiscard]]
|
||||
inline std::size_t GetHashCode() const final{
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2,74 +2,74 @@
|
||||
#include "NumericEvalValue.hpp"
|
||||
|
||||
namespace Porygon::Evaluation {
|
||||
const shared_ptr<NumericEvalValue> NumericEvalValue::operator+(const shared_ptr<NumericEvalValue> &b) const {
|
||||
shared_ptr<const NumericEvalValue> NumericEvalValue::operator+(const shared_ptr<const NumericEvalValue> &b) const {
|
||||
if (this->IsFloat()) {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetIntegerValue());
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() + (double) b->GetIntegerValue());
|
||||
}
|
||||
} else {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<FloatEvalValue>(this->GetIntegerValue() + b->GetFloatValue());
|
||||
return make_shared<FloatEvalValue>((double) this->GetIntegerValue() + b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<IntegerEvalValue>(this->GetIntegerValue() + b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const shared_ptr<NumericEvalValue> NumericEvalValue::operator-(const shared_ptr<NumericEvalValue> &b) const {
|
||||
shared_ptr<const NumericEvalValue> NumericEvalValue::operator-(const shared_ptr<const NumericEvalValue> &b) const {
|
||||
if (this->IsFloat()) {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetIntegerValue());
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() - (double) b->GetIntegerValue());
|
||||
}
|
||||
} else {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<FloatEvalValue>(this->GetIntegerValue() - b->GetFloatValue());
|
||||
return make_shared<FloatEvalValue>((double) this->GetIntegerValue() - b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<IntegerEvalValue>(this->GetIntegerValue() - b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const shared_ptr<NumericEvalValue> NumericEvalValue::operator*(const shared_ptr<NumericEvalValue> &b) const {
|
||||
shared_ptr<const NumericEvalValue> NumericEvalValue::operator*(const shared_ptr<const NumericEvalValue> &b) const {
|
||||
if (this->IsFloat()) {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetIntegerValue());
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() * (double) b->GetIntegerValue());
|
||||
}
|
||||
} else {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<FloatEvalValue>(this->GetIntegerValue() * b->GetFloatValue());
|
||||
return make_shared<FloatEvalValue>((double) this->GetIntegerValue() * b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<IntegerEvalValue>(this->GetIntegerValue() * b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const shared_ptr<NumericEvalValue> NumericEvalValue::operator/(const shared_ptr<NumericEvalValue> &b) const {
|
||||
shared_ptr<const NumericEvalValue> NumericEvalValue::operator/(const shared_ptr<const NumericEvalValue> &b) const {
|
||||
if (this->IsFloat()) {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetIntegerValue());
|
||||
return make_shared<FloatEvalValue>(this->GetFloatValue() / (double) b->GetIntegerValue());
|
||||
}
|
||||
} else {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<FloatEvalValue>(this->GetIntegerValue() / b->GetFloatValue());
|
||||
return make_shared<FloatEvalValue>((double) this->GetIntegerValue() / b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<IntegerEvalValue>(this->GetIntegerValue() / b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const bool NumericEvalValue::operator==(EvalValue *b) const {
|
||||
bool NumericEvalValue::operator==(const EvalValue *b) const {
|
||||
if (b->GetTypeClass() != TypeClass::Number)
|
||||
return false;
|
||||
auto numVal = dynamic_cast<NumericEvalValue*>(b);
|
||||
auto numVal = dynamic_cast<const NumericEvalValue*>(b);
|
||||
if (this->IsFloat() != numVal->IsFloat())
|
||||
return false;
|
||||
|
||||
@@ -80,64 +80,64 @@ namespace Porygon::Evaluation {
|
||||
}
|
||||
}
|
||||
|
||||
const shared_ptr<BooleanEvalValue> NumericEvalValue::operator<(const shared_ptr<NumericEvalValue> &b) const {
|
||||
shared_ptr<const BooleanEvalValue> NumericEvalValue::operator<(const shared_ptr<const NumericEvalValue> &b) const {
|
||||
if (this->IsFloat()) {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetIntegerValue());
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() < (double) b->GetIntegerValue());
|
||||
}
|
||||
} else {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<BooleanEvalValue>(this->GetIntegerValue() < b->GetFloatValue());
|
||||
return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() < b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<BooleanEvalValue>(this->GetIntegerValue() < b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const shared_ptr<BooleanEvalValue> NumericEvalValue::operator<=(const shared_ptr<NumericEvalValue> &b) const {
|
||||
shared_ptr<const BooleanEvalValue> NumericEvalValue::operator<=(const shared_ptr<const NumericEvalValue> &b) const {
|
||||
if (this->IsFloat()) {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetIntegerValue());
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() <=(double) b->GetIntegerValue());
|
||||
}
|
||||
} else {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<BooleanEvalValue>(this->GetIntegerValue() <= b->GetFloatValue());
|
||||
return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() <= b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<BooleanEvalValue>(this->GetIntegerValue() <= b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const shared_ptr<BooleanEvalValue> NumericEvalValue::operator>(const shared_ptr<NumericEvalValue> &b) const {
|
||||
shared_ptr<const BooleanEvalValue> NumericEvalValue::operator>(const shared_ptr<const NumericEvalValue> &b) const {
|
||||
if (this->IsFloat()) {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetIntegerValue());
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() > (double) b->GetIntegerValue());
|
||||
}
|
||||
} else {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<BooleanEvalValue>(this->GetIntegerValue() > b->GetFloatValue());
|
||||
return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() > b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<BooleanEvalValue>(this->GetIntegerValue() > b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const shared_ptr<BooleanEvalValue> NumericEvalValue::operator>=(const shared_ptr<NumericEvalValue> &b) const {
|
||||
shared_ptr<const BooleanEvalValue> NumericEvalValue::operator>=(const shared_ptr<const NumericEvalValue> &b) const {
|
||||
if (this->IsFloat()) {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetIntegerValue());
|
||||
return make_shared<BooleanEvalValue>(this->GetFloatValue() >= (double) b->GetIntegerValue());
|
||||
}
|
||||
} else {
|
||||
if (b->IsFloat()) {
|
||||
return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetFloatValue());
|
||||
return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() >= b->GetFloatValue());
|
||||
} else {
|
||||
return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetIntegerValue());
|
||||
}
|
||||
|
||||
@@ -9,42 +9,48 @@
|
||||
namespace Porygon::Evaluation {
|
||||
class NumericEvalValue : public EvalValue {
|
||||
|
||||
virtual const long GetIntegerValue() const = 0;
|
||||
[[nodiscard]]
|
||||
virtual long GetIntegerValue() const = 0;
|
||||
|
||||
virtual const double GetFloatValue() const = 0;
|
||||
[[nodiscard]]
|
||||
virtual double GetFloatValue() const = 0;
|
||||
|
||||
public:
|
||||
virtual const bool IsFloat() const = 0;
|
||||
[[nodiscard]]
|
||||
virtual bool IsFloat() const = 0;
|
||||
|
||||
inline const TypeClass GetTypeClass() const final {
|
||||
[[nodiscard]]
|
||||
inline TypeClass GetTypeClass() const final {
|
||||
return TypeClass::Number;
|
||||
}
|
||||
|
||||
const shared_ptr<NumericEvalValue> operator+(const shared_ptr<NumericEvalValue> &b) const;
|
||||
shared_ptr<const NumericEvalValue> operator+(const shared_ptr<const NumericEvalValue> &b) const;
|
||||
|
||||
const shared_ptr<NumericEvalValue> operator-(const shared_ptr<NumericEvalValue> &b) const;
|
||||
shared_ptr<const NumericEvalValue> operator-(const shared_ptr<const NumericEvalValue> &b) const;
|
||||
|
||||
const shared_ptr<NumericEvalValue> operator*(const shared_ptr<NumericEvalValue> &b) const;
|
||||
shared_ptr<const NumericEvalValue> operator*(const shared_ptr<const NumericEvalValue> &b) const;
|
||||
|
||||
const shared_ptr<NumericEvalValue> operator/(const shared_ptr<NumericEvalValue> &b) const;
|
||||
shared_ptr<const NumericEvalValue> operator/(const shared_ptr<const NumericEvalValue> &b) const;
|
||||
|
||||
const shared_ptr<BooleanEvalValue> operator<(const shared_ptr<NumericEvalValue> &b) const;
|
||||
shared_ptr<const BooleanEvalValue> operator<(const shared_ptr<const NumericEvalValue> &b) const;
|
||||
|
||||
const shared_ptr<BooleanEvalValue> operator<=(const shared_ptr<NumericEvalValue> &b) const;
|
||||
shared_ptr<const BooleanEvalValue> operator<=(const shared_ptr<const NumericEvalValue> &b) const;
|
||||
|
||||
const shared_ptr<BooleanEvalValue> operator>(const shared_ptr<NumericEvalValue> &b) const;
|
||||
shared_ptr<const BooleanEvalValue> operator>(const shared_ptr<const NumericEvalValue> &b) const;
|
||||
|
||||
const shared_ptr<BooleanEvalValue> operator>=(const shared_ptr<NumericEvalValue> &b) const;
|
||||
shared_ptr<const BooleanEvalValue> operator>=(const shared_ptr<const NumericEvalValue> &b) const;
|
||||
|
||||
const bool operator==(EvalValue *b) const final;
|
||||
bool operator==(const EvalValue *b) const final;
|
||||
};
|
||||
|
||||
class IntegerEvalValue : public NumericEvalValue {
|
||||
const long _value;
|
||||
|
||||
const long GetIntegerValue() const final { return _value; }
|
||||
[[nodiscard]]
|
||||
long GetIntegerValue() const final { return _value; }
|
||||
|
||||
const double GetFloatValue() const final {
|
||||
[[nodiscard]]
|
||||
double GetFloatValue() const final {
|
||||
throw EvaluationException("Attempting to retrieve float from int eval value.");
|
||||
}
|
||||
|
||||
@@ -52,23 +58,28 @@ namespace Porygon::Evaluation {
|
||||
explicit IntegerEvalValue(long value) : _value(value) {
|
||||
}
|
||||
|
||||
inline const bool IsFloat() const final {
|
||||
[[nodiscard]]
|
||||
inline bool IsFloat() const final {
|
||||
return false;
|
||||
}
|
||||
|
||||
inline const long EvaluateInteger() const final {
|
||||
[[nodiscard]]
|
||||
inline long EvaluateInteger() const final {
|
||||
return _value;
|
||||
}
|
||||
|
||||
inline const std::u16string EvaluateString() const final{
|
||||
[[nodiscard]]
|
||||
inline std::u16string EvaluateString() const final{
|
||||
return Utilities::StringUtils::IntToString(_value);
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> Clone() const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> Clone() const final {
|
||||
return make_shared<IntegerEvalValue>(_value);
|
||||
}
|
||||
|
||||
inline const std::size_t GetHashCode() const final {
|
||||
[[nodiscard]]
|
||||
inline std::size_t GetHashCode() const final {
|
||||
return std::hash<long>{}(_value);
|
||||
}
|
||||
};
|
||||
@@ -76,11 +87,13 @@ namespace Porygon::Evaluation {
|
||||
class FloatEvalValue : public NumericEvalValue {
|
||||
const double _value;
|
||||
|
||||
inline const long GetIntegerValue() const final {
|
||||
[[nodiscard]]
|
||||
inline long GetIntegerValue() const final {
|
||||
throw EvaluationException("Attempting to retrieve float from int eval value.");
|
||||
}
|
||||
|
||||
inline const double GetFloatValue() const final {
|
||||
[[nodiscard]]
|
||||
inline double GetFloatValue() const final {
|
||||
return _value;
|
||||
}
|
||||
|
||||
@@ -88,19 +101,23 @@ namespace Porygon::Evaluation {
|
||||
explicit FloatEvalValue(double value) : _value(value) {
|
||||
}
|
||||
|
||||
inline const bool IsFloat() const final {
|
||||
[[nodiscard]]
|
||||
inline bool IsFloat() const final {
|
||||
return true;
|
||||
}
|
||||
|
||||
inline const double EvaluateFloat() const final {
|
||||
[[nodiscard]]
|
||||
inline double EvaluateFloat() const final {
|
||||
return _value;
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> Clone() const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> Clone() const final {
|
||||
return make_shared<FloatEvalValue>(_value);
|
||||
}
|
||||
|
||||
inline const std::size_t GetHashCode() const final {
|
||||
[[nodiscard]]
|
||||
inline std::size_t GetHashCode() const final {
|
||||
return std::hash<double>{}(_value);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -10,55 +10,63 @@ using namespace std;
|
||||
|
||||
namespace Porygon::Evaluation {
|
||||
class NumericalTableEvalValue : public EvalValue {
|
||||
const shared_ptr<vector<shared_ptr<EvalValue>>> _table;
|
||||
const shared_ptr<vector<shared_ptr<const EvalValue>>> _table;
|
||||
const size_t _hash;
|
||||
|
||||
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<EvalValue>>> table, size_t hash)
|
||||
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<const EvalValue>>> table, size_t hash)
|
||||
: _table(std::move(table)),
|
||||
_hash(hash)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<EvalValue>>> table) :
|
||||
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<const EvalValue>>> table) :
|
||||
_table(std::move(table)),
|
||||
_hash(rand())
|
||||
{
|
||||
}
|
||||
|
||||
inline const TypeClass GetTypeClass() const final {
|
||||
[[nodiscard]]
|
||||
inline TypeClass GetTypeClass() const final {
|
||||
return TypeClass::Table;
|
||||
}
|
||||
|
||||
inline const size_t GetHashCode() const final {
|
||||
[[nodiscard]]
|
||||
inline size_t GetHashCode() const final {
|
||||
return _hash;
|
||||
}
|
||||
|
||||
inline const bool operator==(EvalValue *b) const final {
|
||||
[[nodiscard]]
|
||||
inline bool operator==(const EvalValue *b) const final {
|
||||
return this->_hash == b->GetHashCode();
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> Clone() const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> Clone() const final {
|
||||
return shared_ptr<EvalValue>(new NumericalTableEvalValue(_table, _hash));
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
|
||||
const auto index = val->EvaluateInteger() - 1;
|
||||
return this->_table->at(index);
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> IndexValue(uint32_t hash) const final {
|
||||
return this->_table->at(hash - 1);
|
||||
}
|
||||
|
||||
inline void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const final {
|
||||
inline void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const final {
|
||||
auto index = key->EvaluateInteger();
|
||||
this->_table->at(index - 1) = value;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
Iterator * GetKeyIterator() const final;
|
||||
|
||||
inline const shared_ptr<vector<shared_ptr<EvalValue>>> GetTable() const{
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<vector<shared_ptr<const EvalValue>>> GetTable() const{
|
||||
return _table;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -20,16 +20,16 @@ namespace Porygon::Evaluation {
|
||||
};
|
||||
|
||||
class EvaluationScriptFunctionOption : public GenericFunctionOption{
|
||||
const std::shared_ptr<BoundBlockStatement> _innerBlock;
|
||||
const std::shared_ptr<const BoundBlockStatement> _innerBlock;
|
||||
const std::shared_ptr<EvaluationScope> _scope;
|
||||
|
||||
public:
|
||||
EvaluationScriptFunctionOption(shared_ptr<BoundBlockStatement> innerBlock, shared_ptr<EvaluationScope> scope)
|
||||
EvaluationScriptFunctionOption(shared_ptr<const BoundBlockStatement> innerBlock, shared_ptr<EvaluationScope> scope)
|
||||
: _innerBlock(std::move(innerBlock)), _scope(std::move(scope)) {
|
||||
}
|
||||
~EvaluationScriptFunctionOption() final = default;
|
||||
|
||||
inline const std::shared_ptr<BoundBlockStatement> &GetInnerBlock() const {
|
||||
inline std::shared_ptr<const BoundBlockStatement> GetInnerBlock() const {
|
||||
return _innerBlock;
|
||||
}
|
||||
|
||||
@@ -40,47 +40,53 @@ namespace Porygon::Evaluation {
|
||||
|
||||
class GenericFunctionEvalValue : public EvalValue{
|
||||
protected:
|
||||
const shared_ptr<GenericFunctionScriptType> _type;
|
||||
const shared_ptr<const GenericFunctionScriptType> _type;
|
||||
const size_t _hash;
|
||||
vector<shared_ptr<GenericFunctionOption>> _options;
|
||||
vector<shared_ptr<GenericFunctionOption>>* _options;
|
||||
public:
|
||||
GenericFunctionEvalValue(shared_ptr<GenericFunctionScriptType> type, size_t hash)
|
||||
GenericFunctionEvalValue(shared_ptr<const GenericFunctionScriptType> type, size_t hash)
|
||||
: _type(move(type)),
|
||||
_hash(hash){
|
||||
_hash(hash), _options(new vector<shared_ptr<GenericFunctionOption>>()){
|
||||
}
|
||||
|
||||
const shared_ptr<EvalValue> Clone() const final {
|
||||
GenericFunctionEvalValue(const GenericFunctionEvalValue& _) = delete;
|
||||
GenericFunctionEvalValue() = delete;
|
||||
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);
|
||||
for (const auto& o: *_options){
|
||||
t->_options->push_back(o);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
inline void RegisterOption(GenericFunctionOption* option){
|
||||
_options.push_back(shared_ptr<GenericFunctionOption>(option));
|
||||
inline void RegisterOption(GenericFunctionOption* option) const{
|
||||
_options->push_back(shared_ptr<GenericFunctionOption>(option));
|
||||
}
|
||||
|
||||
inline const std::shared_ptr<ScriptType> GetType() const {
|
||||
inline std::shared_ptr<const ScriptType> GetType() const {
|
||||
return _type;
|
||||
}
|
||||
|
||||
inline const TypeClass GetTypeClass() const final {
|
||||
inline TypeClass GetTypeClass() const final {
|
||||
return TypeClass::Function;
|
||||
}
|
||||
|
||||
const bool operator==(EvalValue *b) const final {
|
||||
bool operator==(const EvalValue *b) const final {
|
||||
if (b->GetTypeClass() != TypeClass::Function)
|
||||
return false;
|
||||
return this->_hash == ((GenericFunctionEvalValue *) b)->_hash;
|
||||
};
|
||||
|
||||
inline const std::size_t GetHashCode() const final {
|
||||
inline std::size_t GetHashCode() const final {
|
||||
return _hash;
|
||||
}
|
||||
|
||||
inline const shared_ptr<GenericFunctionOption> GetOption(const size_t id) const{
|
||||
return this->_options.at(id);
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const GenericFunctionOption> GetOption(const size_t id) const{
|
||||
return this->_options->at(id);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -17,31 +17,35 @@ namespace Porygon::Evaluation {
|
||||
_hash = Utilities::HashedString::ConstHash(_value.c_str());
|
||||
}
|
||||
|
||||
inline const TypeClass GetTypeClass() const final {
|
||||
[[nodiscard]]
|
||||
inline TypeClass GetTypeClass() const final {
|
||||
return TypeClass::String;
|
||||
}
|
||||
|
||||
const bool operator==(EvalValue *b) const final {
|
||||
bool operator==(const EvalValue *b) const final {
|
||||
if (b->GetTypeClass() != TypeClass::String)
|
||||
return false;
|
||||
return this->_hash == b->GetHashCode();
|
||||
};
|
||||
|
||||
inline const u16string EvaluateString() const final {
|
||||
[[nodiscard]]
|
||||
inline u16string EvaluateString() const final {
|
||||
return _value;
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> Clone() const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> Clone() const final {
|
||||
return make_shared<StringEvalValue>(_value);
|
||||
}
|
||||
|
||||
const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final {
|
||||
shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
|
||||
// Porygon is 1-indexed, so we convert to that.
|
||||
auto l = val->EvaluateInteger() - 1;
|
||||
return make_shared<StringEvalValue>(u16string(1, _value[l]));
|
||||
return make_shared<const StringEvalValue>(u16string(1, _value[l]));
|
||||
}
|
||||
|
||||
inline const std::size_t GetHashCode() const final {
|
||||
[[nodiscard]]
|
||||
inline std::size_t GetHashCode() const final {
|
||||
return _hash;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,68 +4,79 @@
|
||||
#include <utility>
|
||||
#include <map>
|
||||
#include "EvalValue.hpp"
|
||||
#include "../../Utilities/Random.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Porygon::Evaluation {
|
||||
class TableEvalValue : public EvalValue {
|
||||
const shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> _table;
|
||||
const shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> _table;
|
||||
const size_t _hash;
|
||||
|
||||
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> table, size_t hash)
|
||||
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> table, size_t hash)
|
||||
: _table(std::move(table)),
|
||||
_hash(hash)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> table) :
|
||||
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> table) :
|
||||
_table(std::move(table)),
|
||||
_hash(rand())
|
||||
_hash(Utilities::Random::Get())
|
||||
{
|
||||
}
|
||||
|
||||
inline const TypeClass GetTypeClass() const final {
|
||||
[[nodiscard]]
|
||||
inline TypeClass GetTypeClass() const final {
|
||||
return TypeClass::Table;
|
||||
}
|
||||
|
||||
inline const size_t GetHashCode() const final {
|
||||
[[nodiscard]]
|
||||
inline size_t GetHashCode() const final {
|
||||
return _hash;
|
||||
}
|
||||
|
||||
inline const bool operator==(EvalValue *b) const final {
|
||||
[[nodiscard]]
|
||||
inline bool operator==(const EvalValue *b) const final {
|
||||
return this->_hash == b->GetHashCode();
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> Clone() const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> Clone() const final {
|
||||
return shared_ptr<EvalValue>(new TableEvalValue(_table, _hash));
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
|
||||
const auto stringKey = val->EvaluateString();
|
||||
return this->_table->at(Utilities::HashedString::CreateLookup(stringKey));
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> IndexValue(uint32_t hash) const final {
|
||||
return this->_table->at(Utilities::HashedString::CreateLookup(hash));
|
||||
}
|
||||
|
||||
inline const shared_ptr<EvalValue> IndexValue(const char *val) const {
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const EvalValue> IndexValue(const char *val) const {
|
||||
auto hash = Utilities::HashedString::ConstHash(val);
|
||||
return this->_table->at(Utilities::HashedString::CreateLookup(hash));
|
||||
}
|
||||
|
||||
inline void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const final {
|
||||
inline void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const final {
|
||||
auto hash = key->GetHashCode();
|
||||
this->_table->at(Utilities::HashedString::CreateLookup(hash)) = value;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
Iterator * GetKeyIterator() const final;
|
||||
|
||||
inline const _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> GetTableIterator() const{
|
||||
[[nodiscard]]
|
||||
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> GetTableIterator() const{
|
||||
return _table->cbegin();
|
||||
};
|
||||
inline const _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> GetTableIteratorEnd() const{
|
||||
[[nodiscard]]
|
||||
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> GetTableIteratorEnd() const{
|
||||
return _table->cend();
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user