Large overhaul of pointers to shared_ptrs, implemented function evaluation
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -52,7 +52,7 @@ void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationSta
|
||||
auto type = statement->GetType();
|
||||
auto key = statement->GetKey();
|
||||
auto block = statement->GetBlock();
|
||||
auto value = new ScriptFunctionEvalValue(block, *type);
|
||||
auto value = new ScriptFunctionEvalValue(block, type);
|
||||
if (key->IsCreation()){
|
||||
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
|
||||
} else{
|
||||
@@ -66,6 +66,8 @@ EvalValue *Evaluator::EvaluateExpression(BoundExpression *expression) {
|
||||
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
|
||||
case TypeClass ::Bool: return this -> EvaluateBoolExpression(expression);
|
||||
case TypeClass ::String: return this -> EvaluateStringExpression(expression);
|
||||
case TypeClass ::Function: return this->EvaluateFunctionExpression(expression);
|
||||
case TypeClass ::Nil: return this->EvaluateNilExpression(expression);
|
||||
default: throw;
|
||||
}
|
||||
}
|
||||
@@ -81,6 +83,7 @@ NumericEvalValue* Evaluator::EvaluateIntegerExpression(BoundExpression *expressi
|
||||
case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression);
|
||||
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return (NumericEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||
case BoundExpressionKind ::FunctionCall: return (NumericEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
||||
|
||||
case BoundExpressionKind ::LiteralString:
|
||||
case BoundExpressionKind ::LiteralBool:
|
||||
@@ -95,6 +98,7 @@ BooleanEvalValue* Evaluator::EvaluateBoolExpression(BoundExpression *expression)
|
||||
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
|
||||
case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return (BooleanEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||
case BoundExpressionKind ::FunctionCall: return (BooleanEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
||||
|
||||
case BoundExpressionKind::Bad:
|
||||
case BoundExpressionKind::LiteralInteger:
|
||||
@@ -112,7 +116,7 @@ StringEvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression
|
||||
case BoundExpressionKind::Binary:
|
||||
return this -> EvaluateStringBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return (StringEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||
|
||||
case BoundExpressionKind ::FunctionCall: return (StringEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
||||
|
||||
case BoundExpressionKind::Bad:
|
||||
case BoundExpressionKind::LiteralInteger:
|
||||
@@ -121,5 +125,47 @@ StringEvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression
|
||||
case BoundExpressionKind::Unary:
|
||||
throw;
|
||||
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
EvalValue* Evaluator::EvaluateFunctionExpression(BoundExpression * expression){
|
||||
switch (expression->GetKind()){
|
||||
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
|
||||
default: throw;
|
||||
}
|
||||
}
|
||||
EvalValue* Evaluator::EvaluateNilExpression(BoundExpression * expression){
|
||||
switch (expression->GetKind()){
|
||||
case BoundExpressionKind ::FunctionCall:
|
||||
return this->EvaluateFunctionCallExpression(expression);
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
EvalValue* Evaluator::EvaluateFunctionCallExpression(BoundExpression* expression){
|
||||
auto functionCall = (BoundFunctionCallExpression*)expression;
|
||||
auto function = (ScriptFunctionEvalValue*)this->EvaluateExpression(functionCall->GetFunctionExpression());
|
||||
auto boundParameters = functionCall->GetParameters();
|
||||
auto parameters = vector<EvalValue*>(boundParameters.size());
|
||||
for (int i = 0; i < boundParameters.size(); i++){
|
||||
parameters[i] = this->EvaluateExpression(boundParameters[i]);
|
||||
}
|
||||
|
||||
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
||||
auto parameterTypes = type->GetParameterTypes();
|
||||
auto parameterKeys = type->GetParameterKeys();
|
||||
for (int i = 0; i < parameterTypes->size() && i < parameterKeys->size() && i < parameters.size(); i++){
|
||||
auto parameter = parameters[i];
|
||||
auto requiredType = parameterTypes->at(i);
|
||||
if (*parameter->GetType() != requiredType.get()){
|
||||
throw EvaluationException("Passed wrong type to function.");
|
||||
}
|
||||
auto key = parameterKeys->at(i);
|
||||
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
|
||||
}
|
||||
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
||||
return nullptr;
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "EvalValues/EvalValue.hpp"
|
||||
#include "EvalValues/NumericEvalValue.hpp"
|
||||
#include "EvalValues/StringEvalValue.hpp"
|
||||
#include "EvalValues/StringEvalValue.hpp"
|
||||
#include "EvaluationScope/EvaluationScope.hpp"
|
||||
|
||||
using namespace boost;
|
||||
@@ -28,6 +29,8 @@ class Evaluator {
|
||||
NumericEvalValue* EvaluateIntegerExpression(BoundExpression* expression);
|
||||
BooleanEvalValue* EvaluateBoolExpression(BoundExpression* expression);
|
||||
StringEvalValue* EvaluateStringExpression(BoundExpression* expression);
|
||||
EvalValue* EvaluateFunctionExpression(BoundExpression *expression);
|
||||
EvalValue *EvaluateNilExpression(BoundExpression *expression);
|
||||
|
||||
NumericEvalValue* EvaluateIntegerBinary(BoundBinaryExpression* expression);
|
||||
BooleanEvalValue *EvaluateBooleanBinary(BoundBinaryExpression *expression);
|
||||
@@ -35,6 +38,7 @@ class Evaluator {
|
||||
|
||||
NumericEvalValue* EvaluateIntegerUnary(BoundUnaryExpression* expression);
|
||||
BooleanEvalValue *EvaluateBooleanUnary(BoundUnaryExpression *expression);
|
||||
EvalValue *EvaluateFunctionCallExpression(BoundExpression *expression);
|
||||
|
||||
EvalValue *GetVariable(BoundVariableExpression *expression);
|
||||
public:
|
||||
@@ -55,6 +59,8 @@ public:
|
||||
EvaluationScope* GetScope(){
|
||||
return _evaluationScope;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user