Mark evalValues as const
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-17 17:43:54 +02:00
parent d91caa7f32
commit 21d3329c55
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
14 changed files with 204 additions and 215 deletions

View File

@ -490,7 +490,7 @@ BoundExpression* Binder::BindPeriodIndexExpression(const PeriodIndexExpression*
BoundExpression* Binder::BindNumericalTableExpression(const ParsedNumericalTableExpression* expression){ BoundExpression* Binder::BindNumericalTableExpression(const ParsedNumericalTableExpression* expression){
auto expressions = expression->GetExpressions(); auto expressions = expression->GetExpressions();
auto boundExpressions = vector<BoundExpression*>(expressions-> size()); auto boundExpressions = vector<const BoundExpression*>(expressions-> size());
shared_ptr<ScriptType> valueType = nullptr; shared_ptr<ScriptType> valueType = nullptr;
if (!boundExpressions.empty()){ if (!boundExpressions.empty()){
boundExpressions[0] = this -> BindExpression(expressions -> at(0)); boundExpressions[0] = this -> BindExpression(expressions -> at(0));

View File

@ -1,4 +1,3 @@
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP #ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
#define PORYGONLANG_BOUNDEXPRESSION_HPP #define PORYGONLANG_BOUNDEXPRESSION_HPP
@ -107,7 +106,7 @@ public:
class BoundLiteralStringExpression : public BoundExpression{ class BoundLiteralStringExpression : public BoundExpression{
const u16string _value; const u16string _value;
public: public:
BoundLiteralStringExpression(u16string value, unsigned int start, unsigned int length) BoundLiteralStringExpression(const u16string& value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<StringScriptType>(true, HashedString::ConstHash(value.c_str()))), : BoundExpression(start, length, make_shared<StringScriptType>(true, HashedString::ConstHash(value.c_str()))),
_value(value) _value(value)
{ {
@ -203,7 +202,7 @@ class BoundUnaryExpression : public BoundExpression {
const BoundUnaryOperation _operation; const BoundUnaryOperation _operation;
public: public:
BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, shared_ptr<ScriptType> result, unsigned int start, unsigned int length) BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, shared_ptr<ScriptType> result, unsigned int start, unsigned int length)
: BoundExpression(start, length, result), : BoundExpression(start, length, std::move(result)),
_operand(operand), _operand(operand),
_operation(op) _operation(op)
{ {
@ -232,7 +231,7 @@ class BoundFunctionCallExpression : public BoundExpression {
public: public:
BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters, shared_ptr<ScriptType> result, BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length) unsigned int start, unsigned int length)
: BoundExpression(start, length, result), _functionExpression(functionExpression), _parameters(std::move(parameters)) {} : BoundExpression(start, length, std::move(result)), _functionExpression(functionExpression), _parameters(std::move(parameters)) {}
~BoundFunctionCallExpression() final{ ~BoundFunctionCallExpression() final{
delete _functionExpression; delete _functionExpression;
@ -255,8 +254,8 @@ public:
}; };
class BoundIndexExpression : public BoundExpression { class BoundIndexExpression : public BoundExpression {
BoundExpression* _indexableExpression; const BoundExpression* _indexableExpression;
BoundExpression* _indexExpression; const BoundExpression* _indexExpression;
public: public:
BoundIndexExpression(BoundExpression* indexableExpression, BoundExpression* indexExpression, shared_ptr<ScriptType> result, BoundIndexExpression(BoundExpression* indexableExpression, BoundExpression* indexExpression, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length) unsigned int start, unsigned int length)
@ -271,18 +270,18 @@ public:
return BoundExpressionKind ::Index; return BoundExpressionKind ::Index;
} }
BoundExpression* GetIndexableExpression() const{ const BoundExpression* GetIndexableExpression() const{
return _indexableExpression; return _indexableExpression;
} }
BoundExpression* GetIndexExpression() const{ const BoundExpression* GetIndexExpression() const{
return _indexExpression; return _indexExpression;
} }
}; };
class BoundPeriodIndexExpression : public BoundExpression { class BoundPeriodIndexExpression : public BoundExpression {
BoundExpression* _indexableExpression; const BoundExpression* _indexableExpression;
HashedString _index; const HashedString _index;
public: public:
BoundPeriodIndexExpression(BoundExpression* indexableExpression, HashedString index, shared_ptr<ScriptType> result, BoundPeriodIndexExpression(BoundExpression* indexableExpression, HashedString index, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length) unsigned int start, unsigned int length)
@ -306,9 +305,9 @@ public:
}; };
class BoundNumericalTableExpression : public BoundExpression{ class BoundNumericalTableExpression : public BoundExpression{
const vector<BoundExpression*> _expressions; const vector<const BoundExpression*> _expressions;
public: public:
BoundNumericalTableExpression(vector<BoundExpression*> expressions, shared_ptr<ScriptType> type, unsigned int start, unsigned int length) BoundNumericalTableExpression(vector<const BoundExpression*> expressions, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)), : BoundExpression(start, length, std::move(type)),
_expressions(std::move(expressions)) _expressions(std::move(expressions))
{} {}
@ -323,7 +322,7 @@ public:
return BoundExpressionKind ::NumericalTable; return BoundExpressionKind ::NumericalTable;
} }
const vector<BoundExpression*>* GetExpressions() const{ const vector<const BoundExpression*>* GetExpressions() const{
return &_expressions; return &_expressions;
} }
}; };

View File

@ -7,7 +7,7 @@
#include "../BoundStatements/BoundStatement.hpp" #include "../BoundStatements/BoundStatement.hpp"
class BoundTableExpression : public BoundExpression{ class BoundTableExpression : public BoundExpression{
BoundBlockStatement* _block; const BoundBlockStatement* _block;
public: public:
BoundTableExpression(BoundBlockStatement* block, shared_ptr<ScriptType> type, unsigned int start, unsigned int length) BoundTableExpression(BoundBlockStatement* block, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)){ : BoundExpression(start, length, std::move(type)){
@ -22,7 +22,7 @@ public:
return BoundExpressionKind ::Table; return BoundExpressionKind ::Table;
} }
BoundBlockStatement* GetBlock(){ const BoundBlockStatement* GetBlock() const {
return _block; return _block;
} }
}; };

View File

@ -5,31 +5,25 @@
#include "EvalValues/NumericEvalValue.hpp" #include "EvalValues/NumericEvalValue.hpp"
#include "EvalValues/StringEvalValue.hpp" #include "EvalValues/StringEvalValue.hpp"
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) { const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) {
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft()); auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight()); auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
NumericEvalValue* result;
switch (expression->GetOperation()){ switch (expression->GetOperation()){
case BoundBinaryOperation ::Addition: case BoundBinaryOperation ::Addition:
result = leftValue.get() -> operator+ (rightValue.get()); return leftValue.get() -> operator+ (rightValue);
break;
case BoundBinaryOperation::Subtraction: case BoundBinaryOperation::Subtraction:
result = leftValue.get() -> operator- (rightValue.get()); return leftValue.get() -> operator- (rightValue);
break;
case BoundBinaryOperation::Multiplication: case BoundBinaryOperation::Multiplication:
result = leftValue.get() -> operator* (rightValue.get()); return leftValue.get() -> operator* (rightValue);
break;
case BoundBinaryOperation::Division: case BoundBinaryOperation::Division:
result = leftValue.get() -> operator/ (rightValue.get()); return leftValue.get() -> operator/ (rightValue);
break;
default: default:
throw EvaluationException("Can't evaluate operation to numeric"); throw EvaluationException("Can't evaluate operation to numeric");
} }
return shared_ptr<NumericEvalValue>(result);
} }
shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression* expression){ const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression *expression){
switch (expression->GetOperation()){ switch (expression->GetOperation()){
case BoundBinaryOperation::Equality: case BoundBinaryOperation::Equality:
{ {
@ -49,29 +43,25 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryE
{ {
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft()); auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight()); auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
BooleanEvalValue* b = leftValue->operator<(rightValue.get()); return leftValue->operator<(rightValue);
return shared_ptr<BooleanEvalValue>(b);
} }
case BoundBinaryOperation ::LessThanEquals: case BoundBinaryOperation ::LessThanEquals:
{ {
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft()); auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight()); auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
BooleanEvalValue* b = leftValue->operator<=(rightValue.get()); return leftValue->operator<=(rightValue);
return shared_ptr<BooleanEvalValue>(b);
} }
case BoundBinaryOperation ::GreaterThan: case BoundBinaryOperation ::GreaterThan:
{ {
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft()); auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight()); auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
BooleanEvalValue* b = leftValue->operator>(rightValue.get()); return leftValue->operator>(rightValue);
return shared_ptr<BooleanEvalValue>(b);
} }
case BoundBinaryOperation ::GreaterThanEquals: case BoundBinaryOperation ::GreaterThanEquals:
{ {
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft()); auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight()); auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
BooleanEvalValue* b = leftValue->operator>=(rightValue.get()); return leftValue->operator>=(rightValue);
return shared_ptr<BooleanEvalValue>(b);
} }
case BoundBinaryOperation::LogicalAnd: case BoundBinaryOperation::LogicalAnd:
@ -93,13 +83,13 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryE
} }
} }
shared_ptr<StringEvalValue> Evaluator::EvaluateStringBinary(const BoundBinaryExpression* expression){ const shared_ptr<StringEvalValue> Evaluator::EvaluateStringBinary(const BoundBinaryExpression *expression){
if (expression->GetOperation() != BoundBinaryOperation::Concatenation) if (expression->GetOperation() != BoundBinaryOperation::Concatenation)
throw; throw;
std::basic_ostringstream<char16_t > strs; std::basic_ostringstream<char16_t > stringStream;
auto left = this -> EvaluateStringExpression(expression->GetLeft()); auto left = this -> EvaluateStringExpression(expression->GetLeft());
strs << *left->EvaluateString(); stringStream << *left->EvaluateString();
auto right = this -> EvaluateExpression(expression->GetRight()); auto right = this -> EvaluateExpression(expression->GetRight());
strs << *right->EvaluateString(); stringStream << *right->EvaluateString();
return make_shared<StringEvalValue>(strs.str()); return make_shared<StringEvalValue>(stringStream.str());
} }

View File

@ -12,40 +12,40 @@ class EvalValue{
public: public:
EvalValue() = default; EvalValue() = default;
virtual ~EvalValue() = default; virtual ~EvalValue() = default;
virtual const TypeClass GetTypeClass() = 0; virtual const TypeClass GetTypeClass() const = 0;
virtual bool operator ==(EvalValue* b) = 0; virtual const bool operator ==(EvalValue* b) const = 0;
virtual bool operator !=(EvalValue*b){ virtual const bool operator !=(EvalValue*b) const{
return ! (this->operator==(b)); return ! (this->operator==(b));
} }
virtual shared_ptr<EvalValue> Clone() = 0; virtual const shared_ptr<EvalValue> Clone() const = 0;
virtual long EvaluateInteger() const{ virtual const long EvaluateInteger() const{
throw EvaluationException("Can't evaluate this EvalValue as integer."); throw EvaluationException("Can't evaluate this EvalValue as integer.");
} }
virtual double EvaluateFloat() const{ virtual const double EvaluateFloat() const{
throw EvaluationException("Can't evaluate this EvalValue as float."); throw EvaluationException("Can't evaluate this EvalValue as float.");
} }
virtual bool EvaluateBool() const{ virtual const bool EvaluateBool() const{
throw EvaluationException("Can't evaluate this EvalValue as bool."); throw EvaluationException("Can't evaluate this EvalValue as bool.");
} }
virtual const std::u16string* EvaluateString() const { virtual const std::u16string* EvaluateString() const {
throw EvaluationException("Can't evaluate this EvalValue as string."); throw EvaluationException("Can't evaluate this EvalValue as string.");
} }
virtual std::size_t GetHashCode() = 0; virtual const std::size_t GetHashCode() const = 0;
virtual shared_ptr<EvalValue> IndexValue(EvalValue* val){ virtual const shared_ptr<EvalValue> IndexValue(EvalValue* val) const{
throw EvaluationException("Can't index this EvalValue"); throw EvaluationException("Can't index this EvalValue");
} }
virtual shared_ptr<EvalValue> IndexValue(uint32_t hash){ virtual const shared_ptr<EvalValue> IndexValue(uint32_t hash) const{
throw EvaluationException("Can't index this EvalValue"); throw EvaluationException("Can't index this EvalValue");
} }
virtual void SetIndexValue(EvalValue *key, shared_ptr<EvalValue> value){ virtual void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue>& value) const{
throw EvaluationException("Can't index this EvalValue"); throw EvaluationException("Can't index this EvalValue");
} }
}; };
@ -58,25 +58,25 @@ public:
{ {
} }
shared_ptr<EvalValue> Clone() final{ const shared_ptr<EvalValue> Clone() const final{
return make_shared<BooleanEvalValue>(_value); return make_shared<BooleanEvalValue>(_value);
} }
const TypeClass GetTypeClass() final{ const TypeClass GetTypeClass() const final{
return TypeClass ::Bool; return TypeClass ::Bool;
} }
bool EvaluateBool() const final{ const bool EvaluateBool() const final{
return _value; return _value;
} }
bool operator ==(EvalValue* b) final{ const bool operator ==(EvalValue* b) const final{
if (b->GetTypeClass() != TypeClass::Bool) if (b->GetTypeClass() != TypeClass::Bool)
return false; return false;
return this->EvaluateBool() == b->EvaluateBool(); return this->EvaluateBool() == b->EvaluateBool();
}; };
std::size_t GetHashCode() final{ const std::size_t GetHashCode() const final{
return _value; return _value;
} }
}; };

View File

@ -1,71 +1,71 @@
#include "NumericEvalValue.hpp" #include "NumericEvalValue.hpp"
NumericEvalValue *NumericEvalValue::operator+(NumericEvalValue *b) { const shared_ptr<NumericEvalValue> NumericEvalValue::operator+(const shared_ptr<NumericEvalValue>& b) const {
if (this->IsFloat()){ if (this->IsFloat()){
if (b->IsFloat()){ if (b->IsFloat()){
return new FloatEvalValue(this->GetFloatValue() + b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetFloatValue());
} else{ } else{
return new FloatEvalValue(this->GetFloatValue() + b->GetIntegerValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()){ if (b->IsFloat()){
return new FloatEvalValue(this->GetIntegerValue() + b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetIntegerValue() + b->GetFloatValue());
} else{ } else{
return new IntegerEvalValue(this->GetIntegerValue() + b->GetIntegerValue()); return make_shared<IntegerEvalValue>(this->GetIntegerValue() + b->GetIntegerValue());
} }
} }
} }
NumericEvalValue *NumericEvalValue::operator-(NumericEvalValue *b) { const shared_ptr<NumericEvalValue> NumericEvalValue::operator-(const shared_ptr<NumericEvalValue>& b) const {
if (this->IsFloat()){ if (this->IsFloat()){
if (b->IsFloat()){ if (b->IsFloat()){
return new FloatEvalValue(this->GetFloatValue() - b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetFloatValue());
} else{ } else{
return new FloatEvalValue(this->GetFloatValue() - b->GetIntegerValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()){ if (b->IsFloat()){
return new FloatEvalValue(this->GetIntegerValue() - b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetIntegerValue() - b->GetFloatValue());
} else{ } else{
return new IntegerEvalValue(this->GetIntegerValue() - b->GetIntegerValue()); return make_shared<IntegerEvalValue>(this->GetIntegerValue() - b->GetIntegerValue());
} }
} }
} }
NumericEvalValue *NumericEvalValue::operator*(NumericEvalValue *b) { const shared_ptr<NumericEvalValue> NumericEvalValue::operator*(const shared_ptr<NumericEvalValue>& b) const {
if (this->IsFloat()){ if (this->IsFloat()){
if (b->IsFloat()){ if (b->IsFloat()){
return new FloatEvalValue(this->GetFloatValue() * b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetFloatValue());
} else{ } else{
return new FloatEvalValue(this->GetFloatValue() * b->GetIntegerValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()){ if (b->IsFloat()){
return new FloatEvalValue(this->GetIntegerValue() * b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetIntegerValue() * b->GetFloatValue());
} else{ } else{
return new IntegerEvalValue(this->GetIntegerValue() * b->GetIntegerValue()); return make_shared<IntegerEvalValue>(this->GetIntegerValue() * b->GetIntegerValue());
} }
} }
} }
NumericEvalValue *NumericEvalValue::operator/(NumericEvalValue *b) { const shared_ptr<NumericEvalValue> NumericEvalValue::operator/(const shared_ptr<NumericEvalValue>& b) const {
if (this->IsFloat()){ if (this->IsFloat()){
if (b->IsFloat()){ if (b->IsFloat()){
return new FloatEvalValue(this->GetFloatValue() / b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetFloatValue());
} else{ } else{
return new FloatEvalValue(this->GetFloatValue() / b->GetIntegerValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()){ if (b->IsFloat()){
return new FloatEvalValue(this->GetIntegerValue() / b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetIntegerValue() / b->GetFloatValue());
} else{ } else{
return new IntegerEvalValue(this->GetIntegerValue() / b->GetIntegerValue()); return make_shared<IntegerEvalValue>(this->GetIntegerValue() / b->GetIntegerValue());
} }
} }
} }
bool NumericEvalValue::operator==(EvalValue *b) { const bool NumericEvalValue::operator==(EvalValue *b) const {
if (b->GetTypeClass() != TypeClass::Number) if (b->GetTypeClass() != TypeClass::Number)
return false; return false;
auto numVal = (NumericEvalValue*)b; auto numVal = (NumericEvalValue*)b;
@ -79,66 +79,66 @@ bool NumericEvalValue::operator==(EvalValue *b) {
} }
} }
BooleanEvalValue *NumericEvalValue::operator<(NumericEvalValue *b) { const shared_ptr<BooleanEvalValue> NumericEvalValue::operator<(const shared_ptr<NumericEvalValue>& b) const {
if (this->IsFloat()){ if (this->IsFloat()){
if (b->IsFloat()){ if (b->IsFloat()){
return new BooleanEvalValue(this->GetFloatValue() < b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetFloatValue());
} else{ } else{
return new BooleanEvalValue(this->GetFloatValue() < b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()){ if (b->IsFloat()){
return new BooleanEvalValue(this->GetIntegerValue() < b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() < b->GetFloatValue());
} else{ } else{
return new BooleanEvalValue(this->GetIntegerValue() < b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() < b->GetIntegerValue());
} }
} }
} }
BooleanEvalValue *NumericEvalValue::operator<=(NumericEvalValue *b) { const shared_ptr<BooleanEvalValue> NumericEvalValue::operator<=(const shared_ptr<NumericEvalValue>& b) const {
if (this->IsFloat()){ if (this->IsFloat()){
if (b->IsFloat()){ if (b->IsFloat()){
return new BooleanEvalValue(this->GetFloatValue() <= b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetFloatValue());
} else{ } else{
return new BooleanEvalValue(this->GetFloatValue() <= b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()){ if (b->IsFloat()){
return new BooleanEvalValue(this->GetIntegerValue() <= b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() <= b->GetFloatValue());
} else{ } else{
return new BooleanEvalValue(this->GetIntegerValue() <= b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() <= b->GetIntegerValue());
} }
} }
} }
BooleanEvalValue *NumericEvalValue::operator>(NumericEvalValue *b) { const shared_ptr<BooleanEvalValue> NumericEvalValue::operator>(const shared_ptr<NumericEvalValue>& b) const {
if (this->IsFloat()){ if (this->IsFloat()){
if (b->IsFloat()){ if (b->IsFloat()){
return new BooleanEvalValue(this->GetFloatValue() > b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetFloatValue());
} else{ } else{
return new BooleanEvalValue(this->GetFloatValue() > b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()){ if (b->IsFloat()){
return new BooleanEvalValue(this->GetIntegerValue() > b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() > b->GetFloatValue());
} else{ } else{
return new BooleanEvalValue(this->GetIntegerValue() > b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() > b->GetIntegerValue());
} }
} }
} }
BooleanEvalValue *NumericEvalValue::operator>=(NumericEvalValue *b) { const shared_ptr<BooleanEvalValue> NumericEvalValue::operator>=(const shared_ptr<NumericEvalValue>& b) const {
if (this->IsFloat()){ if (this->IsFloat()){
if (b->IsFloat()){ if (b->IsFloat()){
return new BooleanEvalValue(this->GetFloatValue() >= b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetFloatValue());
} else{ } else{
return new BooleanEvalValue(this->GetFloatValue() >= b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()){ if (b->IsFloat()){
return new BooleanEvalValue(this->GetIntegerValue() >= b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetFloatValue());
} else{ } else{
return new BooleanEvalValue(this->GetIntegerValue() >= b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetIntegerValue());
} }
} }
} }

View File

@ -7,73 +7,71 @@
class NumericEvalValue : public EvalValue{ class NumericEvalValue : public EvalValue{
virtual long GetIntegerValue() = 0; virtual const long GetIntegerValue() const = 0;
virtual double GetFloatValue() = 0; virtual const double GetFloatValue() const = 0;
public: public:
virtual const bool IsFloat() = 0; virtual const bool IsFloat() const = 0;
const TypeClass GetTypeClass() final{ const TypeClass GetTypeClass() const final{
return TypeClass ::Number; return TypeClass ::Number;
} }
NumericEvalValue* operator +(NumericEvalValue* b); const shared_ptr<NumericEvalValue> operator +(const shared_ptr<NumericEvalValue>& b) const;
NumericEvalValue* operator -(NumericEvalValue* b); const shared_ptr<NumericEvalValue> operator -(const shared_ptr<NumericEvalValue>& b) const ;
NumericEvalValue* operator *(NumericEvalValue* b); const shared_ptr<NumericEvalValue> operator *(const shared_ptr<NumericEvalValue>& b) const ;
NumericEvalValue* operator /(NumericEvalValue* b); const shared_ptr<NumericEvalValue> operator /(const shared_ptr<NumericEvalValue>& b) const ;
BooleanEvalValue* operator <(NumericEvalValue* b); const shared_ptr<BooleanEvalValue> operator <(const shared_ptr<NumericEvalValue>& b) const ;
BooleanEvalValue* operator <=(NumericEvalValue* b); const shared_ptr<BooleanEvalValue> operator <=(const shared_ptr<NumericEvalValue>& b)const ;
BooleanEvalValue* operator >(NumericEvalValue* b); const shared_ptr<BooleanEvalValue> operator >(const shared_ptr<NumericEvalValue>& b) const ;
BooleanEvalValue* operator >=(NumericEvalValue* b); const shared_ptr<BooleanEvalValue> operator >=(const shared_ptr<NumericEvalValue>& b) const ;
bool operator ==(EvalValue* b) final; const bool operator ==(EvalValue* b) const final;
}; };
class IntegerEvalValue : public NumericEvalValue{ class IntegerEvalValue : public NumericEvalValue{
long _value; const long _value;
long GetIntegerValue() final{return _value;} const long GetIntegerValue() const final{return _value;}
double GetFloatValue() final{ throw EvaluationException("Attempting to retrieve float from int eval value."); } const double GetFloatValue() const final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
public: public:
explicit IntegerEvalValue(long value){ explicit IntegerEvalValue(long value) :_value(value){
_value = value;
} }
const bool IsFloat() final{ const bool IsFloat() const final{
return false; return false;
} }
long EvaluateInteger() const final{ const long EvaluateInteger() const final{
return _value; return _value;
} }
shared_ptr<EvalValue> Clone() final{ const shared_ptr<EvalValue> Clone() const final{
return make_shared<IntegerEvalValue>(_value); return make_shared<IntegerEvalValue>(_value);
} }
std::size_t GetHashCode() final{ const std::size_t GetHashCode() const final{
return std::hash<long>{}(_value); return std::hash<long>{}(_value);
} }
}; };
class FloatEvalValue : public NumericEvalValue{ class FloatEvalValue : public NumericEvalValue{
double _value; const double _value;
long GetIntegerValue() final{ throw EvaluationException("Attempting to retrieve float from int eval value."); } const long GetIntegerValue() const final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
double GetFloatValue() final{return _value;} const double GetFloatValue() const final{return _value;}
public: public:
explicit FloatEvalValue(double value){ explicit FloatEvalValue(double value) :_value(value){
_value = value;
} }
const bool IsFloat() final{ const bool IsFloat() const final{
return true; return true;
} }
double EvaluateFloat() const final{ const double EvaluateFloat() const final{
return _value; return _value;
} }
shared_ptr<EvalValue> Clone() final{ const shared_ptr<EvalValue> Clone() const final{
return make_shared<FloatEvalValue>(_value); return make_shared<FloatEvalValue>(_value);
} }
std::size_t GetHashCode() final{ const std::size_t GetHashCode() const final{
return std::hash<double >{}(_value); return std::hash<double >{}(_value);
} }
}; };

View File

@ -1,7 +1,5 @@
#include <utility> #include <utility>
#include <utility>
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP #ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP #define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
@ -15,57 +13,58 @@
class ScriptFunctionEvalValue : public EvalValue{ class ScriptFunctionEvalValue : public EvalValue{
std::shared_ptr<BoundBlockStatement> _innerBlock; const std::shared_ptr<BoundBlockStatement> _innerBlock;
std::shared_ptr<FunctionScriptType> _type; const std::shared_ptr<FunctionScriptType> _type;
std::shared_ptr<EvaluationScope> _scope; const std::shared_ptr<EvaluationScope> _scope;
std::size_t _hash; const std::size_t _hash;
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<EvaluationScope> scope, explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<EvaluationScope> scope,
std::shared_ptr<FunctionScriptType> type, size_t hash) std::shared_ptr<FunctionScriptType> type, size_t hash)
: _type(std::move(type)) : _type(std::move(type)),
_innerBlock(std::move(innerBlock)),
_scope(std::move(scope)),
_hash(hash)
{ {
_innerBlock = std::move(innerBlock);
_scope = std::move(scope);
_hash = hash;
} }
public: public:
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<EvaluationScope> scope, explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<EvaluationScope> scope,
std::shared_ptr<FunctionScriptType> type) std::shared_ptr<FunctionScriptType> type)
: _type(std::move(type)) : _type(std::move(type)),
_innerBlock(std::move(innerBlock)),
_scope(std::move(scope)),
_hash(rand())
{ {
_innerBlock = std::move(innerBlock);
_scope = std::move(scope);
_hash = rand();
} }
std::shared_ptr<ScriptType> GetType() const{ const std::shared_ptr<ScriptType> GetType() const{
return _type; return _type;
} }
const TypeClass GetTypeClass() final{ const TypeClass GetTypeClass() const final{
return TypeClass ::Function; return TypeClass ::Function;
} }
shared_ptr<EvalValue> Clone() final{ const shared_ptr<EvalValue> Clone() const final{
// We don't run make_shared here as it can't call private constructors
return shared_ptr<ScriptFunctionEvalValue>(new ScriptFunctionEvalValue(_innerBlock, _scope, _type, _hash)); return shared_ptr<ScriptFunctionEvalValue>(new ScriptFunctionEvalValue(_innerBlock, _scope, _type, _hash));
} }
bool operator ==(EvalValue* b) final{ const bool operator ==(EvalValue* b) const final{
if (b->GetTypeClass() != TypeClass::Function) if (b->GetTypeClass() != TypeClass::Function)
return false; return false;
return this->_hash == ((ScriptFunctionEvalValue*)b)->_hash; return this->_hash == ((ScriptFunctionEvalValue*)b)->_hash;
}; };
std::shared_ptr<BoundBlockStatement> GetInnerBlock() const{ const std::shared_ptr<BoundBlockStatement>& GetInnerBlock() const{
return _innerBlock; return _innerBlock;
} }
std::size_t GetHashCode() final{ const std::size_t GetHashCode() const final{
return _hash; return _hash;
} }
std::shared_ptr<EvaluationScope> GetScope() const{ const std::shared_ptr<EvaluationScope>& GetScope() const{
return _scope; return _scope;
} }
}; };

View File

@ -17,11 +17,11 @@ public:
_hash = HashedString::ConstHash (_value.c_str()); _hash = HashedString::ConstHash (_value.c_str());
} }
const TypeClass GetTypeClass() final{ const TypeClass GetTypeClass() const final{
return TypeClass ::String; return TypeClass ::String;
} }
bool operator ==(EvalValue* b) final{ const bool operator ==(EvalValue* b) const final{
if (b->GetTypeClass() != TypeClass::String) if (b->GetTypeClass() != TypeClass::String)
return false; return false;
return this->_hash == b->GetHashCode(); return this->_hash == b->GetHashCode();
@ -31,17 +31,17 @@ public:
return &_value; return &_value;
} }
shared_ptr<EvalValue> Clone() final{ const shared_ptr<EvalValue> Clone() const final{
return make_shared<StringEvalValue>(_value); return make_shared<StringEvalValue>(_value);
} }
shared_ptr<EvalValue> IndexValue(EvalValue* val) final{ const shared_ptr<EvalValue> IndexValue(EvalValue* val) const final{
// Porygon is 1-indexed, so we convert to that. // Porygon is 1-indexed, so we convert to that.
auto l = val->EvaluateInteger() - 1; auto l = val->EvaluateInteger() - 1;
return make_shared<StringEvalValue>(u16string(1, _value[l])); return make_shared<StringEvalValue>(u16string(1, _value[l]));
} }
std::size_t GetHashCode() final{ const std::size_t GetHashCode() const final{
return _hash; return _hash;
} }
}; };

View File

@ -20,37 +20,37 @@ public:
_hash = rand(); _hash = rand();
} }
const TypeClass GetTypeClass() final{ const TypeClass GetTypeClass() const final{
return TypeClass ::Table; return TypeClass ::Table;
} }
size_t GetHashCode() final{ const size_t GetHashCode() const final{
return _hash; return _hash;
} }
bool operator ==(EvalValue* b) final{ const bool operator ==(EvalValue* b) const final{
return this -> _hash == b->GetHashCode(); return this -> _hash == b->GetHashCode();
} }
shared_ptr<EvalValue> Clone() final{ const shared_ptr<EvalValue> Clone() const final{
return shared_ptr<EvalValue>(new TableEvalValue(_table, _hash)); return shared_ptr<EvalValue>(new TableEvalValue(_table, _hash));
} }
shared_ptr<EvalValue> IndexValue(EvalValue* val) final{ const shared_ptr<EvalValue> IndexValue(EvalValue* val) const final{
auto hash = val->GetHashCode(); auto hash = val->GetHashCode();
return this -> _table->at(hash); return this -> _table->at(hash);
} }
shared_ptr<EvalValue> IndexValue(uint32_t hash) final{ const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final{
return this -> _table->at(hash); return this -> _table->at(hash);
} }
shared_ptr<EvalValue> IndexValue(const char* val){ const shared_ptr<EvalValue> IndexValue(const char* val) const {
auto hash = HashedString::ConstHash(val); auto hash = HashedString::ConstHash(val);
return this -> _table -> at(hash); return this -> _table -> at(hash);
} }
void SetIndexValue(EvalValue *key, shared_ptr<EvalValue> value) final{ void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue>& value) const final{
auto hash = key->GetHashCode(); auto hash = key->GetHashCode();
this -> _table->at(hash) = value; this -> _table->at(hash) = value;
} }

View File

@ -104,7 +104,7 @@ void Evaluator::EvaluateConditionalStatement(const BoundConditionalStatement *st
} }
} }
shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) { const shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
auto type = expression -> GetType(); auto type = expression -> GetType();
switch (type->GetClass()){ switch (type->GetClass()){
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression); case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
@ -118,7 +118,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expre
} }
} }
shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression* expression){ const shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression *expression){
auto variable = this->_evaluationScope->GetVariable(expression->GetKey()); auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
if (variable == nullptr){ if (variable == nullptr){
throw EvaluationException("Variable not found"); throw EvaluationException("Variable not found");
@ -126,7 +126,7 @@ shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression* expr
return variable->Clone(); return variable->Clone();
} }
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) { const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
switch (expression->GetKind()){ switch (expression->GetKind()){
case BoundExpressionKind ::LiteralInteger: return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression*)expression)->GetValue()); case BoundExpressionKind ::LiteralInteger: return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression*)expression)->GetValue());
case BoundExpressionKind ::LiteralFloat: return make_shared<FloatEvalValue>(((BoundLiteralFloatExpression*)expression)->GetValue()); case BoundExpressionKind ::LiteralFloat: return make_shared<FloatEvalValue>(((BoundLiteralFloatExpression*)expression)->GetValue());
@ -146,7 +146,7 @@ shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExp
} }
} }
shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) { const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind ::LiteralBool: return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression*)expression)->GetValue()); case BoundExpressionKind ::LiteralBool: return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression*)expression)->GetValue());
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression); case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
@ -167,7 +167,7 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpres
} }
} }
shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) { const shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind ::LiteralString: case BoundExpressionKind ::LiteralString:
return make_shared<StringEvalValue>(((BoundLiteralStringExpression*)expression)->GetValue()); return make_shared<StringEvalValue>(((BoundLiteralStringExpression*)expression)->GetValue());
@ -190,7 +190,7 @@ shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpre
} }
} }
shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression * expression){ const shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression *expression){
switch (expression->GetKind()){ switch (expression->GetKind()){
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression); case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression); case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
@ -198,7 +198,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpressio
default: throw; default: throw;
} }
} }
shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression * expression){ const shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression){
switch (expression->GetKind()){ switch (expression->GetKind()){
case BoundExpressionKind ::FunctionCall: case BoundExpressionKind ::FunctionCall:
return this->EvaluateFunctionCallExpression(expression); return this->EvaluateFunctionCallExpression(expression);
@ -206,7 +206,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression * e
return nullptr; return nullptr;
} }
} }
shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression * expression){ const shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *expression){
switch (expression->GetKind()){ switch (expression->GetKind()){
case BoundExpressionKind ::FunctionCall: case BoundExpressionKind ::FunctionCall:
return this->EvaluateFunctionCallExpression(expression); return this->EvaluateFunctionCallExpression(expression);
@ -223,7 +223,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *
shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression* expression){ const shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression){
auto functionCall = (BoundFunctionCallExpression*)expression; auto functionCall = (BoundFunctionCallExpression*)expression;
auto function = dynamic_pointer_cast<ScriptFunctionEvalValue>(this->EvaluateExpression(functionCall->GetFunctionExpression())); auto function = dynamic_pointer_cast<ScriptFunctionEvalValue>(this->EvaluateExpression(functionCall->GetFunctionExpression()));
@ -253,7 +253,8 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpre
return r; return r;
} }
shared_ptr<EvalValue> Evaluator::EvaluateFunction(const ScriptFunctionEvalValue *function, const vector<EvalValue *>& parameters) { const shared_ptr<EvalValue> Evaluator::EvaluateFunction(const ScriptFunctionEvalValue *function,
const vector<EvalValue *> &parameters) {
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType()); auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
auto parameterTypes = type->GetParameterTypes(); auto parameterTypes = type->GetParameterTypes();
auto parameterKeys = type->GetParameterKeys(); auto parameterKeys = type->GetParameterKeys();
@ -274,21 +275,21 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(const ScriptFunctionEvalValue
return r; return r;
} }
shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) { const shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
auto indexExpression = (BoundIndexExpression*)expression; auto indexExpression = (BoundIndexExpression*)expression;
auto index = this -> EvaluateExpression(indexExpression->GetIndexExpression()); auto index = this -> EvaluateExpression(indexExpression->GetIndexExpression());
auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression()); auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression());
return indexable -> IndexValue(index.get()) -> Clone(); return indexable -> IndexValue(index.get()) -> Clone();
} }
shared_ptr<EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) { const shared_ptr<EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
auto indexExpression = (BoundPeriodIndexExpression*)expression; auto indexExpression = (BoundPeriodIndexExpression*)expression;
auto index = indexExpression -> GetIndex().GetHash(); auto index = indexExpression -> GetIndex().GetHash();
auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression()); auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression());
return indexable -> IndexValue(index) -> Clone(); return indexable -> IndexValue(index) -> Clone();
} }
shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) { const shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundNumericalTableExpression*)expression; auto tableExpression = (BoundNumericalTableExpression*)expression;
auto valueExpressions = tableExpression->GetExpressions(); auto valueExpressions = tableExpression->GetExpressions();
auto values = new unordered_map<uint32_t, shared_ptr<EvalValue>>(valueExpressions->size()); auto values = new unordered_map<uint32_t, shared_ptr<EvalValue>>(valueExpressions->size());
@ -300,7 +301,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpre
return make_shared<TableEvalValue>(valuesPointer); return make_shared<TableEvalValue>(valuesPointer);
} }
shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) { const shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundTableExpression*)expression; auto tableExpression = (BoundTableExpression*)expression;
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType()); auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
auto declaredVars = type -> GetValues(); auto declaredVars = type -> GetValues();
@ -316,7 +317,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpre
return make_shared<TableEvalValue>(variables); return make_shared<TableEvalValue>(variables);
} }
shared_ptr<EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) { const shared_ptr<EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) {
switch (expression->GetKind()){ switch (expression->GetKind()){
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression); case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this -> EvaluateIndexExpression(expression); case BoundExpressionKind ::Index: return this -> EvaluateIndexExpression(expression);

View File

@ -31,28 +31,28 @@ class Evaluator {
void EvaluateReturnStatement(const BoundReturnStatement *statement); void EvaluateReturnStatement(const BoundReturnStatement *statement);
void EvaluateConditionalStatement(const BoundConditionalStatement *statement); void EvaluateConditionalStatement(const BoundConditionalStatement *statement);
shared_ptr<EvalValue> EvaluateExpression(const BoundExpression* expression); const shared_ptr<EvalValue> EvaluateExpression(const BoundExpression *expression);
shared_ptr<NumericEvalValue> EvaluateIntegerExpression(const BoundExpression* expression); const shared_ptr<NumericEvalValue> EvaluateIntegerExpression(const BoundExpression *expression);
shared_ptr<BooleanEvalValue> EvaluateBoolExpression(const BoundExpression* expression); const shared_ptr<BooleanEvalValue> EvaluateBoolExpression(const BoundExpression *expression);
shared_ptr<StringEvalValue> EvaluateStringExpression(const BoundExpression* expression); const shared_ptr<StringEvalValue> EvaluateStringExpression(const BoundExpression *expression);
shared_ptr<EvalValue> EvaluateFunctionExpression(const BoundExpression *expression); const shared_ptr<EvalValue> EvaluateFunctionExpression(const BoundExpression *expression);
shared_ptr<EvalValue>EvaluateNilExpression(const BoundExpression *expression); const shared_ptr<EvalValue> EvaluateNilExpression(const BoundExpression *expression);
shared_ptr<EvalValue>EvaluateTableExpression(const BoundExpression *expression); const shared_ptr<EvalValue> EvaluateTableExpression(const BoundExpression *expression);
shared_ptr<NumericEvalValue> EvaluateIntegerBinary(const BoundBinaryExpression* expression); const shared_ptr<NumericEvalValue> EvaluateIntegerBinary(const BoundBinaryExpression *expression);
shared_ptr<BooleanEvalValue> EvaluateBooleanBinary(const BoundBinaryExpression *expression); const shared_ptr<BooleanEvalValue> EvaluateBooleanBinary(const BoundBinaryExpression *expression);
shared_ptr<StringEvalValue> EvaluateStringBinary(const BoundBinaryExpression *expression); const shared_ptr<StringEvalValue> EvaluateStringBinary(const BoundBinaryExpression *expression);
shared_ptr<NumericEvalValue> EvaluateIntegerUnary(const BoundUnaryExpression* expression); const shared_ptr<NumericEvalValue> EvaluateIntegerUnary(const BoundUnaryExpression *expression);
shared_ptr<BooleanEvalValue> EvaluateBooleanUnary(const BoundUnaryExpression *expression); const shared_ptr<BooleanEvalValue> EvaluateBooleanUnary(const BoundUnaryExpression *expression);
shared_ptr<EvalValue> EvaluateFunctionCallExpression(const BoundExpression *expression); const shared_ptr<EvalValue> EvaluateFunctionCallExpression(const BoundExpression *expression);
shared_ptr<EvalValue> EvaluateIndexExpression(const BoundExpression* expression); const shared_ptr<EvalValue> EvaluateIndexExpression(const BoundExpression *expression);
shared_ptr<EvalValue> EvaluatePeriodIndexExpression(const BoundExpression* expression); const shared_ptr<EvalValue> EvaluatePeriodIndexExpression(const BoundExpression *expression);
shared_ptr<EvalValue> EvaluateNumericTableExpression(const BoundExpression* expression); const shared_ptr<EvalValue> EvaluateNumericTableExpression(const BoundExpression *expression);
shared_ptr<EvalValue> EvaluateComplexTableExpression(const BoundExpression *expression); const shared_ptr<EvalValue> EvaluateComplexTableExpression(const BoundExpression *expression);
shared_ptr<EvalValue> EvaluateUserDataExpression(const BoundExpression *expression); const shared_ptr<EvalValue> EvaluateUserDataExpression(const BoundExpression *expression);
shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression); const shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression);
public: public:
explicit Evaluator(Script* script){ explicit Evaluator(Script* script){
_scriptData = script; _scriptData = script;
@ -62,7 +62,8 @@ public:
} }
EvalValue* Evaluate(const BoundScriptStatement* statement); EvalValue* Evaluate(const BoundScriptStatement* statement);
shared_ptr<EvalValue> EvaluateFunction(const ScriptFunctionEvalValue *function, const vector<EvalValue *>& parameters); const shared_ptr<EvalValue> EvaluateFunction(const ScriptFunctionEvalValue *function,
const vector<EvalValue *> &parameters);
EvalValue* GetLastValue(){ EvalValue* GetLastValue(){
return _lastValue.get(); return _lastValue.get();

View File

@ -4,7 +4,7 @@
#include "EvaluationException.hpp" #include "EvaluationException.hpp"
#include "../Script.hpp" #include "../Script.hpp"
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) { const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()){ switch (expression->GetOperation()){
case BoundUnaryOperation::Negation: case BoundUnaryOperation::Negation:
{ {
@ -22,7 +22,7 @@ shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExp
} }
} }
shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) { const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()){ switch (expression->GetOperation()){
case BoundUnaryOperation::LogicalNegation: case BoundUnaryOperation::LogicalNegation:
{ {

View File

@ -1,5 +1,4 @@
#ifndef PORYGONLANG_USERDATAVALUE_HPP #ifndef PORYGONLANG_USERDATAVALUE_HPP
#define PORYGONLANG_USERDATAVALUE_HPP #define PORYGONLANG_USERDATAVALUE_HPP
@ -9,49 +8,51 @@
#include "UserDataStorage.hpp" #include "UserDataStorage.hpp"
class UserDataValue : public EvalValue{ class UserDataValue : public EvalValue{
shared_ptr<UserData> _userData; const shared_ptr<UserData> _userData;
void* _obj; void* _obj;
public: public:
UserDataValue(shared_ptr<UserData> userData, void* obj){ UserDataValue(shared_ptr<UserData> userData, void* obj)
_userData = std::move(userData); : _userData(std::move(userData))
{
_obj = obj; _obj = obj;
} }
UserDataValue(uint32_t userDataId, void* obj){ UserDataValue(uint32_t userDataId, void* obj)
_userData = UserDataStorage::GetUserDataType(userDataId); : _userData(UserDataStorage::GetUserDataType(userDataId))
{
_obj = obj; _obj = obj;
} }
const TypeClass GetTypeClass() final{ const TypeClass GetTypeClass() const final{
return TypeClass ::UserData; return TypeClass ::UserData;
} }
bool operator ==(EvalValue* b) final { const bool operator ==(EvalValue* b) const final {
if (b->GetTypeClass() != TypeClass::UserData) if (b->GetTypeClass() != TypeClass::UserData)
return false; return false;
return _obj == ((UserDataValue*)b)->_obj; return _obj == ((UserDataValue*)b)->_obj;
} }
shared_ptr<EvalValue> Clone() final{ const shared_ptr<EvalValue> Clone() const final{
return make_shared<UserDataValue>(_userData, _obj); return make_shared<UserDataValue>(_userData, _obj);
} }
std::size_t GetHashCode() final{ const std::size_t GetHashCode() const final{
return reinterpret_cast<intptr_t>(_obj); return reinterpret_cast<intptr_t>(_obj);
} }
shared_ptr<EvalValue> IndexValue(EvalValue* val) final { const shared_ptr<EvalValue> IndexValue(EvalValue* val) const final {
auto fieldId = val->GetHashCode(); auto fieldId = val->GetHashCode();
auto field = _userData->GetField(fieldId); auto field = _userData->GetField(fieldId);
return shared_ptr<EvalValue>(field->Get(_obj)); return shared_ptr<EvalValue>(field->Get(_obj));
} }
shared_ptr<EvalValue> IndexValue(uint32_t hash) final{ const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final{
auto field = _userData->GetField(hash); auto field = _userData->GetField(hash);
return shared_ptr<EvalValue>(field->Get(_obj)); return shared_ptr<EvalValue>(field->Get(_obj));
} }
void SetIndexValue(EvalValue *key, shared_ptr<EvalValue> value) final{ void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue>& value) const final{
auto fieldId = key->GetHashCode(); auto fieldId = key->GetHashCode();
auto field = _userData->GetField(fieldId); auto field = _userData->GetField(fieldId);
field -> Set(_obj, value.get()); field -> Set(_obj, value.get());