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

@@ -1,10 +1,13 @@
#include <utility>
#include <utility>
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
#define PORYGONLANG_BOUNDEXPRESSION_HPP
#include <string>
#include <memory>
#include "../../ScriptType.hpp"
#include "../BoundOperators.hpp"
#include "../BoundVariables/BoundVariableKey.hpp"
@@ -22,24 +25,23 @@ enum class BoundExpressionKind{
Unary,
Binary,
FunctionCall,
};
class BoundExpression{
unsigned int _start;
unsigned int _length;
ScriptType* _type;
std::shared_ptr<ScriptType> _type;
public:
BoundExpression(unsigned int start, unsigned int length, ScriptType* type){
BoundExpression(unsigned int start, unsigned int length, std::shared_ptr<ScriptType> type){
_start = start;
_length = length;
_type = type;
}
virtual ~BoundExpression(){
delete _type;
};
virtual ~BoundExpression() = default;
virtual BoundExpressionKind GetKind() = 0;
virtual ScriptType* GetType(){
virtual std::shared_ptr<ScriptType> GetType(){
return _type;
};
@@ -56,7 +58,7 @@ public:
class BoundBadExpression : public BoundExpression{
public:
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, new ScriptType(TypeClass::Error)){}
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Error)){}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Bad;
@@ -67,7 +69,7 @@ class BoundLiteralIntegerExpression : public BoundExpression{
long _value;
public:
BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length)
: BoundExpression(start, length, new NumericScriptType(true, false)){
: BoundExpression(start, length, make_shared<NumericScriptType>(true, false)){
_value = value;
}
@@ -84,7 +86,7 @@ class BoundLiteralFloatExpression : public BoundExpression{
double _value;
public:
BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length)
: BoundExpression(start, length, new NumericScriptType(true, true)){
: BoundExpression(start, length, make_shared<NumericScriptType>(true, true)){
_value = value;
}
@@ -101,7 +103,7 @@ class BoundLiteralStringExpression : public BoundExpression{
string _value;
public:
BoundLiteralStringExpression(string value, unsigned int start, unsigned int length)
: BoundExpression(start, length, new ScriptType(TypeClass::String)){
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::String)){
_value = std::move(value);
}
@@ -118,7 +120,7 @@ class BoundLiteralBoolExpression : public BoundExpression{
bool _value;
public:
BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length)
: BoundExpression(start, length, new ScriptType(TypeClass::Bool)){
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Bool)){
_value = value;
}
@@ -134,20 +136,15 @@ public:
class BoundVariableExpression : public BoundExpression{
int _scope;
int _id;
ScriptType _type;
public:
BoundVariableExpression(int scope, int id, const ScriptType& type, unsigned int start, unsigned int length)
: BoundExpression(start, length, nullptr), _type(type){
BoundVariableExpression(int scope, int id, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)){
_scope = scope;
_id = id;
}
~BoundVariableExpression() override = default;
ScriptType* GetType() final{
return &_type;
};
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Variable;
}
@@ -166,7 +163,7 @@ class BoundBinaryExpression : public BoundExpression {
BoundExpression* _right;
BoundBinaryOperation _operation;
public:
BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, ScriptType* result,
BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, result){
_left = left;
@@ -199,7 +196,7 @@ class BoundUnaryExpression : public BoundExpression {
BoundExpression* _operand;
BoundUnaryOperation _operation;
public:
BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, ScriptType* result, unsigned int start, unsigned int length)
BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, shared_ptr<ScriptType> result, unsigned int start, unsigned int length)
:BoundExpression(start, length, result){
_operand = operand;
_operation = op;
@@ -222,6 +219,33 @@ public:
}
};
class BoundFunctionCallExpression : public BoundExpression {
BoundExpression* _functionExpression;
vector<BoundExpression*> _parameters;
public:
BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, result), _functionExpression(functionExpression), _parameters(std::move(parameters)) {}
~BoundFunctionCallExpression() final{
delete _functionExpression;
for (auto p : _parameters){
delete p;
}
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::FunctionCall;
}
BoundExpression* GetFunctionExpression(){
return _functionExpression;
}
vector<BoundExpression*> GetParameters(){
return _parameters;
}
};
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP