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){
auto expressions = expression->GetExpressions();
auto boundExpressions = vector<BoundExpression*>(expressions-> size());
auto boundExpressions = vector<const BoundExpression*>(expressions-> size());
shared_ptr<ScriptType> valueType = nullptr;
if (!boundExpressions.empty()){
boundExpressions[0] = this -> BindExpression(expressions -> at(0));

View File

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

View File

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

View File

@ -5,31 +5,25 @@
#include "EvalValues/NumericEvalValue.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 rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
NumericEvalValue* result;
switch (expression->GetOperation()){
case BoundBinaryOperation ::Addition:
result = leftValue.get() -> operator+ (rightValue.get());
break;
return leftValue.get() -> operator+ (rightValue);
case BoundBinaryOperation::Subtraction:
result = leftValue.get() -> operator- (rightValue.get());
break;
return leftValue.get() -> operator- (rightValue);
case BoundBinaryOperation::Multiplication:
result = leftValue.get() -> operator* (rightValue.get());
break;
return leftValue.get() -> operator* (rightValue);
case BoundBinaryOperation::Division:
result = leftValue.get() -> operator/ (rightValue.get());
break;
return leftValue.get() -> operator/ (rightValue);
default:
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()){
case BoundBinaryOperation::Equality:
{
@ -49,29 +43,25 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryE
{
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
BooleanEvalValue* b = leftValue->operator<(rightValue.get());
return shared_ptr<BooleanEvalValue>(b);
return leftValue->operator<(rightValue);
}
case BoundBinaryOperation ::LessThanEquals:
{
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
BooleanEvalValue* b = leftValue->operator<=(rightValue.get());
return shared_ptr<BooleanEvalValue>(b);
return leftValue->operator<=(rightValue);
}
case BoundBinaryOperation ::GreaterThan:
{
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
BooleanEvalValue* b = leftValue->operator>(rightValue.get());
return shared_ptr<BooleanEvalValue>(b);
return leftValue->operator>(rightValue);
}
case BoundBinaryOperation ::GreaterThanEquals:
{
auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight());
BooleanEvalValue* b = leftValue->operator>=(rightValue.get());
return shared_ptr<BooleanEvalValue>(b);
return leftValue->operator>=(rightValue);
}
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)
throw;
std::basic_ostringstream<char16_t > strs;
std::basic_ostringstream<char16_t > stringStream;
auto left = this -> EvaluateStringExpression(expression->GetLeft());
strs << *left->EvaluateString();
stringStream << *left->EvaluateString();
auto right = this -> EvaluateExpression(expression->GetRight());
strs << *right->EvaluateString();
return make_shared<StringEvalValue>(strs.str());
stringStream << *right->EvaluateString();
return make_shared<StringEvalValue>(stringStream.str());
}

View File

@ -12,40 +12,40 @@ class EvalValue{
public:
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));
}
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.");
}
virtual double EvaluateFloat() const{
virtual const double EvaluateFloat() const{
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.");
}
virtual const std::u16string* EvaluateString() const {
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");
}
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");
}
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");
}
};
@ -58,25 +58,25 @@ public:
{
}
shared_ptr<EvalValue> Clone() final{
const shared_ptr<EvalValue> Clone() const final{
return make_shared<BooleanEvalValue>(_value);
}
const TypeClass GetTypeClass() final{
const TypeClass GetTypeClass() const final{
return TypeClass ::Bool;
}
bool EvaluateBool() const final{
const bool EvaluateBool() const final{
return _value;
}
bool operator ==(EvalValue* b) final{
const bool operator ==(EvalValue* b) const final{
if (b->GetTypeClass() != TypeClass::Bool)
return false;
return this->EvaluateBool() == b->EvaluateBool();
};
std::size_t GetHashCode() final{
const std::size_t GetHashCode() const final{
return _value;
}
};

View File

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

View File

@ -1,7 +1,5 @@
#include <utility>
#include <utility>
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
@ -15,57 +13,58 @@
class ScriptFunctionEvalValue : public EvalValue{
std::shared_ptr<BoundBlockStatement> _innerBlock;
std::shared_ptr<FunctionScriptType> _type;
std::shared_ptr<EvaluationScope> _scope;
std::size_t _hash;
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<EvaluationScope> scope,
const std::shared_ptr<BoundBlockStatement> _innerBlock;
const std::shared_ptr<FunctionScriptType> _type;
const std::shared_ptr<EvaluationScope> _scope;
const std::size_t _hash;
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<EvaluationScope> scope,
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:
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)
: _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;
}
const TypeClass GetTypeClass() final{
const TypeClass GetTypeClass() const final{
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));
}
bool operator ==(EvalValue* b) final{
const bool operator ==(EvalValue* b) const final{
if (b->GetTypeClass() != TypeClass::Function)
return false;
return this->_hash == ((ScriptFunctionEvalValue*)b)->_hash;
};
std::shared_ptr<BoundBlockStatement> GetInnerBlock() const{
const std::shared_ptr<BoundBlockStatement>& GetInnerBlock() const{
return _innerBlock;
}
std::size_t GetHashCode() final{
const std::size_t GetHashCode() const final{
return _hash;
}
std::shared_ptr<EvaluationScope> GetScope() const{
const std::shared_ptr<EvaluationScope>& GetScope() const{
return _scope;
}
};

View File

@ -17,11 +17,11 @@ public:
_hash = HashedString::ConstHash (_value.c_str());
}
const TypeClass GetTypeClass() final{
const TypeClass GetTypeClass() const final{
return TypeClass ::String;
}
bool operator ==(EvalValue* b) final{
const bool operator ==(EvalValue* b) const final{
if (b->GetTypeClass() != TypeClass::String)
return false;
return this->_hash == b->GetHashCode();
@ -31,17 +31,17 @@ public:
return &_value;
}
shared_ptr<EvalValue> Clone() final{
const shared_ptr<EvalValue> Clone() const final{
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.
auto l = val->EvaluateInteger() - 1;
return make_shared<StringEvalValue>(u16string(1, _value[l]));
}
std::size_t GetHashCode() final{
const std::size_t GetHashCode() const final{
return _hash;
}
};

View File

@ -20,37 +20,37 @@ public:
_hash = rand();
}
const TypeClass GetTypeClass() final{
const TypeClass GetTypeClass() const final{
return TypeClass ::Table;
}
size_t GetHashCode() final{
const size_t GetHashCode() const final{
return _hash;
}
bool operator ==(EvalValue* b) final{
const bool operator ==(EvalValue* b) const final{
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));
}
shared_ptr<EvalValue> IndexValue(EvalValue* val) final{
const shared_ptr<EvalValue> IndexValue(EvalValue* val) const final{
auto hash = val->GetHashCode();
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);
}
shared_ptr<EvalValue> IndexValue(const char* val){
const shared_ptr<EvalValue> IndexValue(const char* val) const {
auto hash = HashedString::ConstHash(val);
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();
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();
switch (type->GetClass()){
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());
if (variable == nullptr){
throw EvaluationException("Variable not found");
@ -126,7 +126,7 @@ shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression* expr
return variable->Clone();
}
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
switch (expression->GetKind()){
case BoundExpressionKind ::LiteralInteger: return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression*)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()) {
case BoundExpressionKind ::LiteralBool: return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression*)expression)->GetValue());
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()) {
case BoundExpressionKind ::LiteralString:
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()){
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
@ -198,7 +198,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpressio
default: throw;
}
}
shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression * expression){
const shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression){
switch (expression->GetKind()){
case BoundExpressionKind ::FunctionCall:
return this->EvaluateFunctionCallExpression(expression);
@ -206,7 +206,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression * e
return nullptr;
}
}
shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression * expression){
const shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *expression){
switch (expression->GetKind()){
case BoundExpressionKind ::FunctionCall:
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 function = dynamic_pointer_cast<ScriptFunctionEvalValue>(this->EvaluateExpression(functionCall->GetFunctionExpression()));
@ -253,7 +253,8 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpre
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 parameterTypes = type->GetParameterTypes();
auto parameterKeys = type->GetParameterKeys();
@ -274,21 +275,21 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(const ScriptFunctionEvalValue
return r;
}
shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
const shared_ptr<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();
}
shared_ptr<EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
const shared_ptr<EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
auto indexExpression = (BoundPeriodIndexExpression*)expression;
auto index = indexExpression -> GetIndex().GetHash();
auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression());
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 valueExpressions = tableExpression->GetExpressions();
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);
}
shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
const shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundTableExpression*)expression;
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
auto declaredVars = type -> GetValues();
@ -316,7 +317,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpre
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()){
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this -> EvaluateIndexExpression(expression);

View File

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

View File

@ -4,7 +4,7 @@
#include "EvaluationException.hpp"
#include "../Script.hpp"
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) {
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()){
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()){
case BoundUnaryOperation::LogicalNegation:
{

View File

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