Large overhaul of pointers to shared_ptrs, implemented function evaluation

This commit is contained in:
2019-06-01 19:20:31 +02:00
parent 8b70eed516
commit 4408cf00cd
17 changed files with 261 additions and 129 deletions

View File

@@ -6,12 +6,13 @@
#include "../EvaluationException.hpp"
#include <string>
#include <sstream>
#include <memory>
class EvalValue{
public:
EvalValue() = default;
virtual ~EvalValue() = default;
virtual ScriptType* GetType() = 0;
virtual std::shared_ptr<ScriptType> GetType() = 0;
virtual bool operator ==(EvalValue* b) = 0;
@@ -37,22 +38,18 @@ public:
class BooleanEvalValue : public EvalValue{
bool _value;
ScriptType* _type;
std::shared_ptr<ScriptType> _type;
public:
explicit BooleanEvalValue(bool val){
_value = val;
_type = new ScriptType(TypeClass::Bool);
_type = std::make_shared<ScriptType>(TypeClass::Bool);
}
EvalValue* Clone() final{
return new BooleanEvalValue(_value);
}
~BooleanEvalValue() final{
delete _type;
}
ScriptType* GetType() final{
std::shared_ptr<ScriptType> GetType() final{
return _type;
};

View File

@@ -11,13 +11,10 @@ class NumericEvalValue : public EvalValue{
virtual double GetFloatValue() = 0;
protected:
ScriptType* _type;
std::shared_ptr<ScriptType> _type;
public:
~NumericEvalValue() override{
delete _type;
};
virtual const bool IsFloat() = 0;
ScriptType* GetType() override {
std::shared_ptr<ScriptType> GetType() override {
return _type;
}
@@ -34,7 +31,7 @@ class IntegerEvalValue : public NumericEvalValue{
double GetFloatValue() final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
public:
explicit IntegerEvalValue(long value){
_type = new NumericScriptType(true, false);
_type = std::make_shared<NumericScriptType>(true, false);
_value = value;
}
const bool IsFloat() final{
@@ -62,7 +59,7 @@ class FloatEvalValue : public NumericEvalValue{
double GetFloatValue() final{return _value;}
public:
explicit FloatEvalValue(double value){
_type = new NumericScriptType(true, true);
_type = std::make_shared<NumericScriptType>(true, true);
_value = value;
}
const bool IsFloat() final{

View File

@@ -1,3 +1,5 @@
#include <utility>
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
@@ -11,44 +13,32 @@
class ScriptFunctionEvalValue : public EvalValue{
std::shared_ptr<BoundBlockStatement> _innerBlock;
FunctionScriptType _type;
std::shared_ptr<FunctionScriptType> _type;
public:
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, FunctionScriptType type)
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<FunctionScriptType> type)
: _type(std::move(type))
{
_innerBlock = std::move(innerBlock);
}
std::shared_ptr<ScriptType> GetType() final{
return _type;
}
EvalValue* Clone() final{
return new ScriptFunctionEvalValue(_innerBlock, _type);
}
ScriptType* GetType() final{
return &_type;
};
bool operator ==(EvalValue* b) final{
if (b->GetType()->GetClass() != TypeClass::Function)
return false;
return this->_innerBlock == ((ScriptFunctionEvalValue*)b)->_innerBlock;
};
EvalValue* EvaluateFunction(Evaluator* evaluator, const vector<EvalValue*>& parameters){
auto parameterTypes = _type.GetParameterTypes();
auto parameterKeys = _type.GetParameterKeys();
auto scope = evaluator->GetScope();
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
auto requiredType = parameterTypes[i];
if (parameter->GetType() != requiredType.get()){
throw EvaluationException("Passed wrong type to function.");
}
auto key = parameterKeys[i];
scope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
}
evaluator->EvaluateBlockStatement(_innerBlock.get());
return nullptr;
std::shared_ptr<BoundBlockStatement> GetInnerBlock(){
return _innerBlock;
}
};

View File

@@ -9,17 +9,14 @@ using namespace std;
class StringEvalValue : public EvalValue{
string _value;
ScriptType* _type;
std::shared_ptr<ScriptType> _type;
public:
explicit StringEvalValue(string s){
_value = move(s);
_type = new ScriptType(TypeClass::String);
}
~StringEvalValue() final{
delete _type;
_type = std::make_shared<ScriptType>(TypeClass::String);
}
ScriptType* GetType() final{
std::shared_ptr<ScriptType> GetType() final{
return _type;
};
bool operator ==(EvalValue* b) final{