Implements basic numerical tables
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -35,7 +35,9 @@ public:
|
||||
throw EvaluationException("Can't evaluate this EvalValue as string.");
|
||||
}
|
||||
|
||||
virtual EvalValue* IndexValue(EvalValue* val){
|
||||
virtual std::size_t GetHashCode() = 0;
|
||||
|
||||
virtual shared_ptr<EvalValue> IndexValue(EvalValue* val){
|
||||
throw EvaluationException("Can't index this EvalValue");
|
||||
}
|
||||
};
|
||||
@@ -66,6 +68,10 @@ public:
|
||||
return false;
|
||||
return this->EvaluateBool() == b->EvaluateBool();
|
||||
};
|
||||
|
||||
std::size_t GetHashCode() final{
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //PORYGONLANG_EVALVALUE_HPP
|
||||
|
||||
@@ -49,6 +49,10 @@ public:
|
||||
shared_ptr<EvalValue> Clone() final{
|
||||
return make_shared<IntegerEvalValue>(_value);
|
||||
}
|
||||
|
||||
std::size_t GetHashCode() final{
|
||||
return std::hash<long>{}(_value);
|
||||
}
|
||||
};
|
||||
|
||||
class FloatEvalValue : public NumericEvalValue{
|
||||
@@ -71,6 +75,10 @@ public:
|
||||
shared_ptr<EvalValue> Clone() final{
|
||||
return make_shared<FloatEvalValue>(_value);
|
||||
}
|
||||
|
||||
std::size_t GetHashCode() final{
|
||||
return std::hash<double >{}(_value);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //PORYGONLANG_NUMERICEVALVALUE_HPP
|
||||
|
||||
@@ -14,11 +14,20 @@
|
||||
class ScriptFunctionEvalValue : public EvalValue{
|
||||
std::shared_ptr<BoundBlockStatement> _innerBlock;
|
||||
std::shared_ptr<FunctionScriptType> _type;
|
||||
std::size_t _hash;
|
||||
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<FunctionScriptType> type, size_t hash)
|
||||
: _type(std::move(type))
|
||||
{
|
||||
_innerBlock = std::move(innerBlock);
|
||||
_hash = hash;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<FunctionScriptType> type)
|
||||
: _type(std::move(type))
|
||||
{
|
||||
_innerBlock = std::move(innerBlock);
|
||||
_hash = rand();
|
||||
}
|
||||
|
||||
std::shared_ptr<ScriptType> GetType() final{
|
||||
@@ -26,20 +35,25 @@ public:
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Clone() final{
|
||||
return make_shared<ScriptFunctionEvalValue>(_innerBlock, _type);
|
||||
return shared_ptr<ScriptFunctionEvalValue>(new ScriptFunctionEvalValue(_innerBlock, _type, _hash));
|
||||
}
|
||||
|
||||
|
||||
bool operator ==(EvalValue* b) final{
|
||||
if (b->GetType()->GetClass() != TypeClass::Function)
|
||||
return false;
|
||||
return this->_innerBlock == ((ScriptFunctionEvalValue*)b)->_innerBlock;
|
||||
return this->_hash == ((ScriptFunctionEvalValue*)b)->_hash;
|
||||
};
|
||||
|
||||
std::shared_ptr<BoundBlockStatement> GetInnerBlock(){
|
||||
return _innerBlock;
|
||||
}
|
||||
|
||||
std::size_t GetHashCode(){
|
||||
return _hash;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -9,11 +9,13 @@ using namespace std;
|
||||
|
||||
class StringEvalValue : public EvalValue{
|
||||
string _value;
|
||||
size_t _hash;
|
||||
std::shared_ptr<ScriptType> _type;
|
||||
public:
|
||||
explicit StringEvalValue(string s){
|
||||
_value = move(s);
|
||||
_type = std::make_shared<ScriptType>(TypeClass::String);
|
||||
_hash = std::hash<string>{}(_value);
|
||||
}
|
||||
|
||||
std::shared_ptr<ScriptType> GetType() final{
|
||||
@@ -22,7 +24,7 @@ public:
|
||||
bool operator ==(EvalValue* b) final{
|
||||
if (b->GetType()->GetClass() != TypeClass::String)
|
||||
return false;
|
||||
return this->_value == *b->EvaluateString();
|
||||
return this->_hash == b->GetHashCode();
|
||||
};
|
||||
|
||||
string* EvaluateString() final{
|
||||
@@ -33,10 +35,14 @@ public:
|
||||
return make_shared<StringEvalValue>(_value);
|
||||
}
|
||||
|
||||
EvalValue* IndexValue(EvalValue* val) final{
|
||||
shared_ptr<EvalValue> IndexValue(EvalValue* val) final{
|
||||
// Porygon is 1-indexed, so we convert to that.
|
||||
auto l = val->EvaluateInteger() - 1;
|
||||
return new StringEvalValue(string(1, _value[l]));
|
||||
return make_shared<StringEvalValue>(string(1, _value[l]));
|
||||
}
|
||||
|
||||
std::size_t GetHashCode() final{
|
||||
return _hash;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
2
src/Evaluator/EvalValues/TableEvalValue.cpp
Normal file
2
src/Evaluator/EvalValues/TableEvalValue.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
#include "TableEvalValue.hpp"
|
||||
51
src/Evaluator/EvalValues/TableEvalValue.hpp
Normal file
51
src/Evaluator/EvalValues/TableEvalValue.hpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <utility>
|
||||
|
||||
#ifndef PORYGONLANG_TABLEEVALVALUE_HPP
|
||||
#define PORYGONLANG_TABLEEVALVALUE_HPP
|
||||
#include <utility>
|
||||
#include <unordered_map>
|
||||
#include "EvalValue.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class TableEvalValue : public EvalValue {
|
||||
shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> _table;
|
||||
shared_ptr<ScriptType> _type;
|
||||
size_t _hash;
|
||||
|
||||
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table, shared_ptr<ScriptType> type, size_t hash){
|
||||
_table = std::move(table);
|
||||
_type = std::move(type);
|
||||
_hash = hash;
|
||||
}
|
||||
public:
|
||||
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table, shared_ptr<ScriptType> type){
|
||||
_table = std::move(table);
|
||||
_type = std::move(type);
|
||||
_hash = rand();
|
||||
}
|
||||
|
||||
std::shared_ptr<ScriptType> GetType() final{
|
||||
return _type;
|
||||
}
|
||||
|
||||
size_t GetHashCode() final{
|
||||
return _hash;
|
||||
}
|
||||
|
||||
bool operator ==(EvalValue* b) final{
|
||||
return this -> _hash == b->GetHashCode();
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Clone() final{
|
||||
return shared_ptr<EvalValue>(new TableEvalValue(_table, _type, _hash));
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> IndexValue(EvalValue* val) final{
|
||||
auto hash = val->GetHashCode();
|
||||
return this -> _table->at(hash);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //PORYGONLANG_TABLEEVALVALUE_HPP
|
||||
Reference in New Issue
Block a user