#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP #define PORYGONLANG_BOUNDEXPRESSION_HPP #include #include #include #include "../../ScriptType.hpp" #include "../BoundOperators.hpp" #include "../BoundVariables/BoundVariableKey.hpp" using namespace std; enum class BoundExpressionKind{ Bad, LiteralInteger, LiteralFloat, LiteralString, LiteralBool, Variable, Unary, Binary, FunctionCall, Index, PeriodIndex, NumericalTable, Table, }; class BoundExpression{ const unsigned int _start; const unsigned int _length; const shared_ptr _type; public: BoundExpression(unsigned int start, unsigned int length, shared_ptr type) : _start(start), _length(length), _type(std::move(type)) { } virtual ~BoundExpression() = default; virtual const BoundExpressionKind GetKind() const = 0; virtual const std::shared_ptr& GetType() const{ return _type; }; const unsigned int GetStartPosition() const{ return _start; } const unsigned int GetLength() const{ return _length; } const unsigned int GetEndPosition() const{ return _start + _length - 1; } }; class BoundBadExpression : public BoundExpression{ public: BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared(TypeClass::Error)){} const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Bad; } }; class BoundLiteralIntegerExpression : public BoundExpression{ const long _value; public: BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared(true, false)), _value(value) { } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::LiteralInteger; } const long GetValue() const{ return _value; } }; class BoundLiteralFloatExpression : public BoundExpression{ const double _value; public: BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared(true, true)), _value(value) { } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::LiteralFloat; } const double GetValue() const{ return _value; } }; class BoundLiteralStringExpression : public BoundExpression{ const u16string _value; public: BoundLiteralStringExpression(u16string value, unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared(true, HashedString::ConstHash(value.c_str()))), _value(value) { } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::LiteralString; } const u16string GetValue() const{ return _value; } }; class BoundLiteralBoolExpression : public BoundExpression{ const bool _value; public: BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared(TypeClass::Bool)), _value(value) { } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::LiteralBool; } const bool GetValue() const{ return _value; } }; class BoundVariableExpression : public BoundExpression{ const BoundVariableKey* _key; public: BoundVariableExpression(BoundVariableKey* key, shared_ptr type, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(type)), _key(key) { } ~BoundVariableExpression() override{ delete _key; } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Variable; } const BoundVariableKey* GetKey() const{ return _key; } }; class BoundBinaryExpression : public BoundExpression { const BoundExpression* _left; const BoundExpression* _right; const BoundBinaryOperation _operation; public: BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(result)), _left(left), _right(right), _operation(op) { } ~BoundBinaryExpression() final{ delete _left; delete _right; } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Binary; } const BoundExpression* GetLeft() const{ return _left; } const BoundExpression* GetRight() const{ return _right; } const BoundBinaryOperation GetOperation() const{ return _operation; } }; class BoundUnaryExpression : public BoundExpression { const BoundExpression* _operand; const BoundUnaryOperation _operation; public: BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, result), _operand(operand), _operation(op) { } ~BoundUnaryExpression() final{ delete _operand; } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Unary; } const BoundExpression* GetOperand() const{ return _operand; } const BoundUnaryOperation GetOperation() const{ return _operation; } }; class BoundFunctionCallExpression : public BoundExpression { const BoundExpression* _functionExpression; const vector _parameters; public: BoundFunctionCallExpression(BoundExpression *functionExpression, vector parameters, shared_ptr 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; } } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::FunctionCall; } const BoundExpression* GetFunctionExpression() const{ return _functionExpression; } const vector* GetParameters() const{ return &_parameters; } }; class BoundIndexExpression : public BoundExpression { BoundExpression* _indexableExpression; BoundExpression* _indexExpression; public: BoundIndexExpression(BoundExpression* indexableExpression, BoundExpression* indexExpression, shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _indexExpression(indexExpression) {} ~BoundIndexExpression() final{ delete _indexableExpression; delete _indexExpression; } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Index; } BoundExpression* GetIndexableExpression() const{ return _indexableExpression; } BoundExpression* GetIndexExpression() const{ return _indexExpression; } }; class BoundPeriodIndexExpression : public BoundExpression { BoundExpression* _indexableExpression; HashedString _index; public: BoundPeriodIndexExpression(BoundExpression* indexableExpression, HashedString index, shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _index(index) {} ~BoundPeriodIndexExpression() final{ delete _indexableExpression; } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::PeriodIndex; } const BoundExpression* GetIndexableExpression() const{ return _indexableExpression; } const HashedString GetIndex() const{ return _index; } }; class BoundNumericalTableExpression : public BoundExpression{ const vector _expressions; public: BoundNumericalTableExpression(vector expressions, shared_ptr type, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(type)), _expressions(std::move(expressions)) {} ~BoundNumericalTableExpression() final{ for (auto e: _expressions){ delete e; } } const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::NumericalTable; } const vector* GetExpressions() const{ return &_expressions; } }; #endif //PORYGONLANG_BOUNDEXPRESSION_HPP