Mark evalValues as const
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-17 17:43:54 +02:00
parent d91caa7f32
commit 21d3329c55
14 changed files with 204 additions and 215 deletions

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