Large cleanup
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
2019-07-25 17:23:54 +02:00
parent e639a2c170
commit e2a0c35992
58 changed files with 700 additions and 539 deletions

View File

@@ -8,7 +8,7 @@
using namespace Porygon::Binder;
namespace Porygon::Evaluation {
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) {
shared_ptr<const NumericEvalValue> Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) {
auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this->EvaluateIntegerExpression(expression->GetRight());
@@ -26,7 +26,7 @@ namespace Porygon::Evaluation {
}
}
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression *expression) {
shared_ptr<const BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression *expression) {
switch (expression->GetOperation()) {
case BoundBinaryOperation::Equality: {
auto leftValue = this->EvaluateExpression(expression->GetLeft());
@@ -78,7 +78,7 @@ namespace Porygon::Evaluation {
}
}
const shared_ptr<StringEvalValue> Evaluator::EvaluateStringBinary(const BoundBinaryExpression *expression) {
shared_ptr<const StringEvalValue> Evaluator::EvaluateStringBinary(const BoundBinaryExpression *expression) {
if (expression->GetOperation() != BoundBinaryOperation::Concatenation)
throw;
std::basic_ostringstream<char16_t> stringStream;

View File

@@ -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];

View File

@@ -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;
}
};

View File

@@ -1,5 +1,3 @@
#include <utility>
#ifndef PORYGONLANG_EVALVALUEHELPER_HPP
#define PORYGONLANG_EVALVALUEHELPER_HPP

View File

@@ -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;
}
};

View File

@@ -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());
}

View File

@@ -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);
}
};

View File

@@ -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;
};
};

View File

@@ -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);
}
};
}

View File

@@ -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;
}
};

View File

@@ -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();
};
};

View File

@@ -4,14 +4,14 @@
#include <memory>
namespace Porygon::Evaluation {
EvaluationScope::EvaluationScope(map<Utilities::HashedString, shared_ptr<EvalValue>> *scriptVariables,
int localVariableCount) : _scriptScope(scriptVariables),
_localScope(map<uint64_t, shared_ptr<EvalValue>>()) {
EvaluationScope::EvaluationScope(map<Utilities::HashedString, shared_ptr<const EvalValue>> *scriptVariables)
: _scriptScope(scriptVariables),
_localScope(map<uint64_t, shared_ptr<const EvalValue>>()) {
}
void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value) {
void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue>& value) {
if (key->GetScopeId() == 0) {
_scriptScope->at(key->GetIdentifier()) = value;
_scriptScope->at(*key->GetIdentifier()) = value;
} else {
auto insert = _localScope.insert({key->GetHash(), value});
if (!insert.second) {
@@ -20,20 +20,20 @@ namespace Porygon::Evaluation {
}
}
void EvaluationScope::SetVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value) {
void EvaluationScope::SetVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue> &value) {
if (key->GetScopeId() == 0) {
_scriptScope->at(key->GetIdentifier()) = value;
_scriptScope->at(*key->GetIdentifier()) = value;
} else {
_localScope[key->GetHash()] = value;
}
}
shared_ptr<EvalValue> EvaluationScope::GetVariable(const BoundVariableKey *key) {
shared_ptr<const EvalValue> EvaluationScope::GetVariable(const BoundVariableKey *key) {
auto scopeId = key -> GetScopeId();
if (scopeId== 0) {
return _scriptScope->at(key->GetIdentifier());
return _scriptScope->at(*key->GetIdentifier());
} else if(scopeId == -2){
return StandardLibraries::StaticScope::GetVariable(key->GetIdentifier());
return StandardLibraries::StaticScope::GetVariable(*key->GetIdentifier());
} else {
return _localScope[key->GetHash()];
}

View File

@@ -9,18 +9,18 @@ using namespace Porygon::Binder;
namespace Porygon::Evaluation {
class EvaluationScope {
map<Utilities::HashedString, shared_ptr<EvalValue>> *_scriptScope;
map<uint64_t, shared_ptr<EvalValue>> _localScope;
map<Utilities::HashedString, shared_ptr<const EvalValue>> *_scriptScope;
map<uint64_t, shared_ptr<const EvalValue>> _localScope;
public:
explicit EvaluationScope(map<Utilities::HashedString, shared_ptr<EvalValue>> *scriptVariables, int deepestScope);
explicit EvaluationScope(map<Utilities::HashedString, shared_ptr<const EvalValue>> *scriptVariables);
~EvaluationScope() = default;
void CreateVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value);
void CreateVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue>&value);
void SetVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value);
void SetVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue> &value);
shared_ptr<EvalValue> GetVariable(const BoundVariableKey *key);
shared_ptr<const EvalValue> GetVariable(const BoundVariableKey *key);
};
}

View File

@@ -1,9 +1,7 @@
#include <utility>
#include <memory>
#include "Evaluator.hpp"
#include "EvaluationException.hpp"
#include "../Script.hpp"
#include "EvaluationScope/EvaluationScope.hpp"
#include "EvalValues/ScriptFunctionEvalValue.hpp"
#include "EvalValues/TableEvalValue.hpp"
@@ -11,17 +9,15 @@
#include "../Binder/BoundExpressions/BoundFunctionCallExpression.hpp"
#include "../TableScriptType.hpp"
#include "../UserData/UserDataFunction.hpp"
#include "../Utilities/StringUtils.hpp"
#include "EvalValues/NumericalTableEvalValue.hpp"
#include "../FunctionScriptType.hpp"
#include "../Utilities/Random.hpp"
using namespace std;
using namespace Porygon::Binder;
namespace Porygon::Evaluation {
EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) {
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptVariables,
statement->GetLocalVariableCount());
const EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) {
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptVariables);
EvaluateBlockStatement(statement);
return this->_returnValue.get();
}
@@ -101,11 +97,11 @@ namespace Porygon::Evaluation {
auto option = new Evaluation::EvaluationScriptFunctionOption(block, this->_evaluationScope);
if (key->IsCreation()) {
auto value = make_shared<GenericFunctionEvalValue>(type, rand());
auto value = make_shared<GenericFunctionEvalValue>(type, Utilities::Random::Get());
value->RegisterOption(option);
this->_evaluationScope->CreateVariable(key, value);
} else {
auto var = dynamic_pointer_cast<GenericFunctionEvalValue>(this -> _evaluationScope ->GetVariable(key));
auto var = dynamic_pointer_cast<const GenericFunctionEvalValue>(this -> _evaluationScope ->GetVariable(key));
var->RegisterOption(option);
this->_evaluationScope->SetVariable(key, var);
}
@@ -223,7 +219,7 @@ namespace Porygon::Evaluation {
// Expressions //
/////////////////
const shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
auto type = expression->GetType();
switch (type->GetClass()) {
case TypeClass::Number:
@@ -245,7 +241,7 @@ namespace Porygon::Evaluation {
}
}
const shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression *expression) {
shared_ptr<const EvalValue> Evaluator::GetVariable(const BoundVariableExpression *expression) {
auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
if (variable == nullptr) {
throw EvaluationException("Variable not found");
@@ -253,7 +249,7 @@ namespace Porygon::Evaluation {
return variable->Clone();
}
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
shared_ptr<const NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::LiteralInteger:
return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression *) expression)->GetValue());
@@ -264,21 +260,21 @@ namespace Porygon::Evaluation {
case BoundExpressionKind::Binary:
return this->EvaluateIntegerBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<NumericEvalValue>(
return dynamic_pointer_cast<const NumericEvalValue>(
this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateIndexExpression(expression));
return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluatePeriodIndexExpression(expression));
return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default:
throw;
}
}
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) {
shared_ptr<const BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::LiteralBool:
return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression *) expression)->GetValue());
@@ -287,14 +283,14 @@ namespace Porygon::Evaluation {
case BoundExpressionKind::Binary:
return this->EvaluateBooleanBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<BooleanEvalValue>(
return dynamic_pointer_cast<const BooleanEvalValue>(
this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateIndexExpression(expression));
return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluatePeriodIndexExpression(expression));
return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default:
throw;
@@ -302,20 +298,20 @@ namespace Porygon::Evaluation {
}
}
const shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) {
shared_ptr<const StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::LiteralString:
return make_shared<StringEvalValue>(((BoundLiteralStringExpression *) expression)->GetValue());
return make_shared<StringEvalValue>(*((BoundLiteralStringExpression *) expression)->GetValue());
case BoundExpressionKind::Binary:
return this->EvaluateStringBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<StringEvalValue>(this->GetVariable((BoundVariableExpression *) expression));
return dynamic_pointer_cast<const StringEvalValue>(this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
return dynamic_pointer_cast<const StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluateIndexExpression(expression));
return dynamic_pointer_cast<const StringEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluatePeriodIndexExpression(expression));
return dynamic_pointer_cast<const StringEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default:
throw;
@@ -323,7 +319,7 @@ namespace Porygon::Evaluation {
}
}
const shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression);
@@ -336,7 +332,7 @@ namespace Porygon::Evaluation {
}
}
const shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::FunctionCall:
return this->EvaluateFunctionCallExpression(expression);
@@ -345,7 +341,7 @@ namespace Porygon::Evaluation {
}
}
const shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::FunctionCall:
return this->EvaluateFunctionCallExpression(expression);
@@ -365,19 +361,19 @@ namespace Porygon::Evaluation {
}
const shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
auto functionCall = (BoundFunctionCallExpression *) expression;
auto function = dynamic_pointer_cast<GenericFunctionEvalValue>(
auto function = dynamic_pointer_cast<const GenericFunctionEvalValue>(
this->EvaluateExpression(functionCall->GetFunctionExpression()));
auto boundParameters = functionCall->GetParameters();
auto parameters = vector<shared_ptr<EvalValue>>(boundParameters->size());
for (int i = 0; i < boundParameters->size(); i++) {
auto parameters = vector<shared_ptr<const EvalValue>>(boundParameters->size());
for (size_t i = 0; i < boundParameters->size(); i++) {
parameters[i] = this->EvaluateExpression(boundParameters->at(i));
}
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType());
auto func = dynamic_pointer_cast<GenericFunctionEvalValue>(function);
auto type = std::dynamic_pointer_cast<const GenericFunctionScriptType>(function->GetType());
auto func = dynamic_pointer_cast<const GenericFunctionEvalValue>(function);
auto option = functionCall ->GetFunctionOption();
auto opt = func->GetOption(option->GetOptionId());
if (option -> IsScriptFunction()){
@@ -385,7 +381,7 @@ namespace Porygon::Evaluation {
auto scriptFunctionType = dynamic_cast<const ScriptFunctionOption*>(option);
auto parameterKeys = scriptFunctionType->GetParameterKeys();
auto originalScope = this->_evaluationScope;
auto scriptOption = dynamic_pointer_cast<EvaluationScriptFunctionOption>(opt);
auto scriptOption = dynamic_pointer_cast<const EvaluationScriptFunctionOption>(opt);
this->_evaluationScope = scriptOption->GetScope();
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) {
@@ -401,25 +397,25 @@ namespace Porygon::Evaluation {
this->_returnValue = nullptr;
return r;
} else{
auto scriptOption = dynamic_pointer_cast<UserData::UserDataFunction>(opt);
EvalValue* arr[parameters.size()];
for (int i = 0; i < parameters.size(); i++){
auto scriptOption = dynamic_pointer_cast<const UserData::UserDataFunction>(opt);
const EvalValue* arr[parameters.size()];
for (size_t i = 0; i < parameters.size(); i++){
arr[i] = parameters[i].get();
}
return shared_ptr<EvalValue>(scriptOption -> Call(arr, parameters.size()));
return shared_ptr<const EvalValue>(scriptOption -> Call(arr, parameters.size()));
}
}
const shared_ptr<EvalValue> Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function,
shared_ptr<const EvalValue> Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function,
const vector<EvalValue *> &parameters) {
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType());
auto type = std::dynamic_pointer_cast<const GenericFunctionScriptType>(function->GetType());
auto option = dynamic_cast<const ScriptFunctionOption*>(type->GetFirstOption());
auto parameterTypes = option->GetParameterTypes();
auto parameterKeys = option->GetParameterKeys();
auto originalScope = this->_evaluationScope;
auto scriptOption = dynamic_pointer_cast<EvaluationScriptFunctionOption>(function->GetOption(0));
auto scriptOption = dynamic_pointer_cast<const EvaluationScriptFunctionOption>(function->GetOption(0));
this->_evaluationScope = scriptOption->GetScope();
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) {
@@ -435,41 +431,41 @@ namespace Porygon::Evaluation {
return r;
}
const shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
auto indexExpression = (BoundIndexExpression *) expression;
auto index = this->EvaluateExpression(indexExpression->GetIndexExpression());
auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression());
return indexable->IndexValue(index.get())->Clone();
return indexable->IndexValue(index.get());
}
const shared_ptr<EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
auto indexExpression = (BoundPeriodIndexExpression *) expression;
auto index = indexExpression->GetIndex().GetHash();
auto index = indexExpression->GetIndex()->GetHash();
auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression());
return indexable->IndexValue(index)->Clone();
}
const shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundNumericalTableExpression *) expression;
auto valueExpressions = tableExpression->GetExpressions();
auto values = new vector<shared_ptr<EvalValue>>(valueExpressions->size());
for (int i = 0; i < valueExpressions->size(); i++) {
auto values = new vector<shared_ptr<const EvalValue>>(valueExpressions->size());
for (size_t i = 0; i < valueExpressions->size(); i++) {
auto val = this->EvaluateExpression(valueExpressions->at(i));
values->at(i) = val;
}
auto valuesPointer = shared_ptr<vector<shared_ptr<EvalValue>>>(values);
auto valuesPointer = shared_ptr<vector<shared_ptr<const EvalValue>>>(values);
return make_shared<NumericalTableEvalValue>(valuesPointer);
}
const shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundTableExpression *) expression;
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
auto type = dynamic_pointer_cast<const TableScriptType>(tableExpression->GetType());
auto declaredVars = type->GetValues();
auto variables = make_shared<map<Utilities::HashedString, shared_ptr<EvalValue>>>();
auto variables = make_shared<map<Utilities::HashedString, shared_ptr<const EvalValue>>>();
for (const auto& i : *declaredVars) {
variables->insert({i.first, nullptr});
}
auto evaluator = make_shared<EvaluationScope>(variables.get(), type->GetLocalVariableCount());
auto evaluator = make_shared<EvaluationScope>(variables.get());
auto currentEvaluator = this->_evaluationScope;
this->_evaluationScope = evaluator;
this->EvaluateBlockStatement(tableExpression->GetBlock());
@@ -477,7 +473,7 @@ namespace Porygon::Evaluation {
return make_shared<TableEvalValue>(variables);
}
const shared_ptr<EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) {
shared_ptr<const EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression);

View File

@@ -15,11 +15,11 @@ using namespace std;
namespace Porygon::Evaluation{
class Evaluator {
shared_ptr<EvalValue> _returnValue;
map<Utilities::HashedString, shared_ptr<EvalValue>>* _scriptVariables;
shared_ptr<const EvalValue> _returnValue;
map<Utilities::HashedString, shared_ptr<const EvalValue>>* _scriptVariables;
bool _hasReturned;
bool _hasBroken;
shared_ptr<EvalValue> _lastValue;
shared_ptr<const EvalValue> _lastValue;
//Porygon::Script* _scriptData;
shared_ptr<EvaluationScope> _evaluationScope;
@@ -36,39 +36,39 @@ namespace Porygon::Evaluation{
void EvaluateGenericForStatement(const BoundGenericForStatement *statement);
void EvaluateWhileStatement(const BoundWhileStatement *statement);
const shared_ptr<EvalValue> EvaluateExpression(const BoundExpression *expression);
const shared_ptr<NumericEvalValue> EvaluateIntegerExpression(const BoundExpression *expression);
const shared_ptr<BooleanEvalValue> EvaluateBoolExpression(const BoundExpression *expression);
const shared_ptr<StringEvalValue> EvaluateStringExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateFunctionExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateNilExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateTableExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluateExpression(const BoundExpression *expression);
shared_ptr<const NumericEvalValue> EvaluateIntegerExpression(const BoundExpression *expression);
shared_ptr<const BooleanEvalValue> EvaluateBoolExpression(const BoundExpression *expression);
shared_ptr<const StringEvalValue> EvaluateStringExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluateFunctionExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluateNilExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluateTableExpression(const BoundExpression *expression);
const shared_ptr<NumericEvalValue> EvaluateIntegerBinary(const BoundBinaryExpression *expression);
const shared_ptr<BooleanEvalValue> EvaluateBooleanBinary(const BoundBinaryExpression *expression);
const shared_ptr<StringEvalValue> EvaluateStringBinary(const BoundBinaryExpression *expression);
shared_ptr<const NumericEvalValue> EvaluateIntegerBinary(const BoundBinaryExpression *expression);
shared_ptr<const BooleanEvalValue> EvaluateBooleanBinary(const BoundBinaryExpression *expression);
shared_ptr<const StringEvalValue> EvaluateStringBinary(const BoundBinaryExpression *expression);
const shared_ptr<NumericEvalValue> EvaluateIntegerUnary(const BoundUnaryExpression *expression);
const shared_ptr<BooleanEvalValue> EvaluateBooleanUnary(const BoundUnaryExpression *expression);
const shared_ptr<EvalValue> EvaluateFunctionCallExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateIndexExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluatePeriodIndexExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateNumericTableExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateComplexTableExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateUserDataExpression(const BoundExpression *expression);
shared_ptr<const NumericEvalValue> EvaluateIntegerUnary(const BoundUnaryExpression *expression);
shared_ptr<const BooleanEvalValue> EvaluateBooleanUnary(const BoundUnaryExpression *expression);
shared_ptr<const EvalValue> EvaluateFunctionCallExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluateIndexExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluatePeriodIndexExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluateNumericTableExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluateUserDataExpression(const BoundExpression *expression);
shared_ptr<const EvalValue> EvaluateComplexTableExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression);
shared_ptr<const EvalValue> GetVariable(const BoundVariableExpression *expression);
public:
explicit Evaluator(map<Utilities::HashedString, shared_ptr<EvalValue>>* scriptVariables)
explicit Evaluator(map<Utilities::HashedString, shared_ptr<const EvalValue>>* scriptVariables)
: _scriptVariables(scriptVariables), _hasReturned(false), _hasBroken(false), _returnValue(nullptr),
_evaluationScope(nullptr){
}
EvalValue* Evaluate(const BoundScriptStatement* statement);
const shared_ptr<EvalValue> EvaluateFunction(const GenericFunctionEvalValue *function,
const EvalValue* Evaluate(const BoundScriptStatement* statement);
shared_ptr<const EvalValue> EvaluateFunction(const GenericFunctionEvalValue *function,
const vector<EvalValue *> &parameters);
EvalValue* GetLastValue(){
inline const EvalValue* GetLastValue(){
return _lastValue.get();
}

View File

@@ -9,7 +9,7 @@
namespace Porygon::Evaluation{
class NumericalKeyIterator : public Iterator{
const shared_ptr<vector<shared_ptr<EvalValue>>> _vec;
const shared_ptr<vector<shared_ptr<const EvalValue>>> _vec;
const size_t _size;
long _position = 0;
public:

View File

@@ -8,8 +8,8 @@
namespace Porygon::Evaluation{
class TableKeyIterator : public 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;
_Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> _iterator;
_Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> _end;
bool _hasStarted = false;
public:
explicit TableKeyIterator(const TableEvalValue* table)

View File

@@ -5,7 +5,7 @@
#include "../Script.hpp"
namespace Porygon::Evaluation {
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) {
shared_ptr<const NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()) {
case BoundUnaryOperation::Negation: {
auto operandValue = EvaluateIntegerExpression(expression->GetOperand());
@@ -22,7 +22,7 @@ namespace Porygon::Evaluation {
}
}
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) {
shared_ptr<const BooleanEvalValue> Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()) {
case BoundUnaryOperation::LogicalNegation: {
auto val = EvaluateBoolExpression(expression->GetOperand());