PorygonLang/src/Binder/BoundExpressions/BoundExpression.hpp

281 lines
7.0 KiB
C++
Raw Normal View History

#include <utility>
#include <utility>
#include <utility>
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
#define PORYGONLANG_BOUNDEXPRESSION_HPP
#include <string>
#include <memory>
#include "../../ScriptType.hpp"
2019-05-21 20:15:51 +00:00
#include "../BoundOperators.hpp"
#include "../BoundVariables/BoundVariableKey.hpp"
using namespace std;
enum class BoundExpressionKind{
Bad,
LiteralInteger,
LiteralFloat,
LiteralString,
LiteralBool,
Variable,
Unary,
Binary,
FunctionCall,
Index,
};
class BoundExpression{
unsigned int _start;
unsigned int _length;
std::shared_ptr<ScriptType> _type;
public:
BoundExpression(unsigned int start, unsigned int length, std::shared_ptr<ScriptType> type){
_start = start;
_length = length;
_type = std::move(type);
}
virtual ~BoundExpression() = default;
virtual BoundExpressionKind GetKind() = 0;
virtual std::shared_ptr<ScriptType> GetType(){
return _type;
};
unsigned int GetStartPosition(){
return _start;
}
unsigned int GetLength(){
return _length;
}
2019-05-21 20:15:51 +00:00
unsigned int GetEndPosition(){
return _start + _length - 1;
}
};
class BoundBadExpression : public BoundExpression{
public:
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Error)){}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Bad;
}
};
class BoundLiteralIntegerExpression : public BoundExpression{
long _value;
public:
BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<NumericScriptType>(true, false)){
_value = value;
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::LiteralInteger;
}
long GetValue(){
return _value;
}
};
class BoundLiteralFloatExpression : public BoundExpression{
double _value;
public:
BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<NumericScriptType>(true, true)){
_value = value;
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::LiteralFloat;
}
double GetValue(){
return _value;
}
};
class BoundLiteralStringExpression : public BoundExpression{
string _value;
public:
BoundLiteralStringExpression(string value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::String)){
_value = std::move(value);
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::LiteralString;
}
string GetValue(){
return _value;
}
};
class BoundLiteralBoolExpression : public BoundExpression{
bool _value;
public:
BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Bool)){
_value = value;
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::LiteralBool;
}
bool GetValue(){
return _value;
}
};
class BoundVariableExpression : public BoundExpression{
int _scope;
int _id;
public:
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;
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Variable;
}
int GetScope(){
return _scope;
}
int GetId(){
return _id;
}
};
2019-05-21 20:15:51 +00:00
class BoundBinaryExpression : public BoundExpression {
BoundExpression* _left;
BoundExpression* _right;
2019-05-22 10:22:52 +00:00
BoundBinaryOperation _operation;
2019-05-21 20:15:51 +00:00
public:
BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, shared_ptr<ScriptType> result,
2019-05-22 10:29:29 +00:00
unsigned int start, unsigned int length)
: BoundExpression(start, length, result){
2019-05-21 20:15:51 +00:00
_left = left;
_right = right;
2019-05-22 10:22:52 +00:00
_operation = op;
2019-05-21 20:15:51 +00:00
}
~BoundBinaryExpression() final{
delete _left;
delete _right;
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Binary;
}
2019-05-23 16:50:09 +00:00
BoundExpression* GetLeft(){
return _left;
}
BoundExpression* GetRight(){
return _right;
}
BoundBinaryOperation GetOperation(){
return _operation;
}
2019-05-21 20:15:51 +00:00
};
2019-05-22 10:22:52 +00:00
class BoundUnaryExpression : public BoundExpression {
BoundExpression* _operand;
BoundUnaryOperation _operation;
public:
BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, shared_ptr<ScriptType> result, unsigned int start, unsigned int length)
2019-05-22 10:22:52 +00:00
:BoundExpression(start, length, result){
_operand = operand;
_operation = op;
}
~BoundUnaryExpression() final{
delete _operand;
}
2019-05-22 10:22:52 +00:00
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Unary;
}
2019-05-25 12:59:12 +00:00
BoundExpression* GetOperand(){
return _operand;
}
BoundUnaryOperation GetOperation(){
return _operation;
}
2019-05-22 10:22:52 +00:00
};
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;
}
};
class BoundIndexExpression : public BoundExpression {
BoundExpression* _indexableExpression;
BoundExpression* _indexExpression;
public:
BoundIndexExpression(BoundExpression* indexableExpression, BoundExpression* indexExpression, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _indexExpression(indexExpression) {}
~BoundIndexExpression() final{
delete _indexableExpression;
delete _indexExpression;
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Index;
}
BoundExpression* GetIndexableExpression(){
return _indexableExpression;
}
BoundExpression* GetIndexExpression(){
return _indexExpression;
}
};
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP