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
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "../Script.hpp"
|
||||
#include "EvaluationScope/EvaluationScope.hpp"
|
||||
#include "EvalValues/ScriptFunctionEvalValue.hpp"
|
||||
#include "EvalValues/TableEvalValue.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -97,6 +98,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateExpression(BoundExpression *expression)
|
||||
case TypeClass ::String: return this -> EvaluateStringExpression(expression);
|
||||
case TypeClass ::Function: return this->EvaluateFunctionExpression(expression);
|
||||
case TypeClass ::Nil: return this->EvaluateNilExpression(expression);
|
||||
case TypeClass ::Table: return this-> EvaluateTableExpression(expression);
|
||||
default: throw;
|
||||
}
|
||||
}
|
||||
@@ -119,6 +121,7 @@ shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(BoundExpressio
|
||||
case BoundExpressionKind ::LiteralString:
|
||||
case BoundExpressionKind ::LiteralBool:
|
||||
case BoundExpressionKind ::Bad:
|
||||
case BoundExpressionKind::NumericalTable:
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -136,6 +139,7 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(BoundExpression *
|
||||
case BoundExpressionKind::LiteralInteger:
|
||||
case BoundExpressionKind::LiteralFloat:
|
||||
case BoundExpressionKind::LiteralString:
|
||||
case BoundExpressionKind::NumericalTable:
|
||||
throw;
|
||||
|
||||
}
|
||||
@@ -156,6 +160,7 @@ shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(BoundExpression
|
||||
case BoundExpressionKind::LiteralFloat:
|
||||
case BoundExpressionKind::LiteralBool:
|
||||
case BoundExpressionKind::Unary:
|
||||
case BoundExpressionKind::NumericalTable:
|
||||
throw;
|
||||
|
||||
}
|
||||
@@ -175,6 +180,19 @@ shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(BoundExpression * express
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(BoundExpression * expression){
|
||||
switch (expression->GetKind()){
|
||||
case BoundExpressionKind ::FunctionCall:
|
||||
return this->EvaluateFunctionCallExpression(expression);
|
||||
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
|
||||
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
|
||||
case BoundExpressionKind ::NumericalTable: return this-> EvaluateNumericTableExpression(expression);
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression* expression){
|
||||
@@ -239,6 +257,18 @@ shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(BoundExpression *expres
|
||||
auto indexExpression = (BoundIndexExpression*)expression;
|
||||
auto index = this -> EvaluateExpression(indexExpression->GetIndexExpression());
|
||||
auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression());
|
||||
return shared_ptr<EvalValue>(indexable -> IndexValue(index.get()));
|
||||
return indexable -> IndexValue(index.get()) -> Clone();
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(BoundExpression *expression) {
|
||||
auto tableExpression = (BoundNumericalTableExpression*)expression;
|
||||
auto valueExpressions = tableExpression->GetExpressions();
|
||||
auto values = new unordered_map<size_t, shared_ptr<EvalValue>>(valueExpressions.size());
|
||||
for (int i = 0; i < valueExpressions.size(); i++){
|
||||
auto val = this -> EvaluateExpression(valueExpressions[i]);
|
||||
values -> insert({i + 1, val});
|
||||
}
|
||||
auto valuesPointer = shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>>(values);
|
||||
return make_shared<TableEvalValue>(valuesPointer, tableExpression->GetType());
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ class Evaluator {
|
||||
shared_ptr<StringEvalValue> EvaluateStringExpression(BoundExpression* expression);
|
||||
shared_ptr<EvalValue> EvaluateFunctionExpression(BoundExpression *expression);
|
||||
shared_ptr<EvalValue>EvaluateNilExpression(BoundExpression *expression);
|
||||
shared_ptr<EvalValue>EvaluateTableExpression(BoundExpression *expression);
|
||||
|
||||
shared_ptr<NumericEvalValue> EvaluateIntegerBinary(BoundBinaryExpression* expression);
|
||||
shared_ptr<BooleanEvalValue> EvaluateBooleanBinary(BoundBinaryExpression *expression);
|
||||
@@ -45,6 +46,7 @@ class Evaluator {
|
||||
shared_ptr<BooleanEvalValue> EvaluateBooleanUnary(BoundUnaryExpression *expression);
|
||||
shared_ptr<EvalValue> EvaluateFunctionCallExpression(BoundExpression *expression);
|
||||
shared_ptr<EvalValue> EvaluateIndexExpression(BoundExpression* expression);
|
||||
shared_ptr<EvalValue> EvaluateNumericTableExpression(BoundExpression* expression);
|
||||
|
||||
shared_ptr<EvalValue> GetVariable(BoundVariableExpression *expression);
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user