300 lines
7.6 KiB
C++
300 lines
7.6 KiB
C++
#ifndef PORYGONLANG_PARSEDEXPRESSION_HPP
|
|
#define PORYGONLANG_PARSEDEXPRESSION_HPP
|
|
|
|
#include <utility>
|
|
#include <memory>
|
|
|
|
#include "../Token.hpp"
|
|
#include "../UnaryOperatorKind.hpp"
|
|
#include "../BinaryOperatorKind.hpp"
|
|
#include "../../Utilities/HashedString.hpp"
|
|
|
|
enum class ParsedExpressionKind{
|
|
Bad,
|
|
|
|
LiteralInteger,
|
|
LiteralFloat,
|
|
LiteralString,
|
|
LiteralBool,
|
|
Variable,
|
|
|
|
Unary,
|
|
Binary,
|
|
Parenthesized,
|
|
FunctionCall,
|
|
Indexer,
|
|
NumericalTable,
|
|
Table,
|
|
};
|
|
|
|
class ParsedExpression {
|
|
unsigned int _position;
|
|
unsigned int _length;
|
|
public:
|
|
ParsedExpression(unsigned int position, unsigned int length){
|
|
_position = position;
|
|
_length = length;
|
|
}
|
|
virtual ~ParsedExpression() = default;
|
|
|
|
virtual ParsedExpressionKind GetKind() = 0;
|
|
|
|
unsigned int GetStartPosition(){
|
|
return _position;
|
|
}
|
|
|
|
unsigned int GetEndPosition(){
|
|
return _position + _length - 1;
|
|
}
|
|
|
|
unsigned int GetLength(){
|
|
return _length;
|
|
}
|
|
};
|
|
|
|
class BadExpression : public ParsedExpression{
|
|
public:
|
|
BadExpression(unsigned int position, unsigned int length) : ParsedExpression(position, length){}
|
|
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::Bad;
|
|
}
|
|
};
|
|
|
|
class LiteralIntegerExpression : public ParsedExpression{
|
|
long _value;
|
|
public:
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::LiteralInteger;
|
|
}
|
|
explicit LiteralIntegerExpression(IntegerToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){
|
|
_value = token->Value;
|
|
}
|
|
|
|
long GetValue(){
|
|
return _value;
|
|
}
|
|
};
|
|
|
|
class LiteralFloatExpression : public ParsedExpression{
|
|
double _value;
|
|
public:
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::LiteralFloat;
|
|
}
|
|
explicit LiteralFloatExpression(FloatToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){
|
|
_value = token->Value;
|
|
}
|
|
|
|
double GetValue(){
|
|
return _value;
|
|
}
|
|
};
|
|
|
|
class LiteralStringExpression : public ParsedExpression{
|
|
string _value;
|
|
public:
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::LiteralString;
|
|
}
|
|
explicit LiteralStringExpression(StringToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){
|
|
_value = std::move(token->Value);
|
|
}
|
|
|
|
string GetValue(){
|
|
return _value;
|
|
}
|
|
};
|
|
|
|
class LiteralBoolExpression : public ParsedExpression{
|
|
bool _value;
|
|
public:
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::LiteralBool;
|
|
}
|
|
explicit LiteralBoolExpression(IToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){
|
|
_value = token -> GetKind() == TokenKind::TrueKeyword;
|
|
}
|
|
|
|
bool GetValue(){
|
|
return _value;
|
|
}
|
|
};
|
|
|
|
class VariableExpression : public ParsedExpression{
|
|
HashedString _value;
|
|
public:
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::Variable;
|
|
}
|
|
explicit VariableExpression(IdentifierToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength())
|
|
, _value(HashedString(token -> Value))
|
|
{
|
|
}
|
|
|
|
HashedString GetValue(){
|
|
return _value;
|
|
}
|
|
};
|
|
|
|
|
|
class ParenthesizedExpression : public ParsedExpression{
|
|
ParsedExpression* _expression;
|
|
public:
|
|
~ParenthesizedExpression() override {
|
|
delete _expression;
|
|
}
|
|
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::Parenthesized;
|
|
}
|
|
|
|
explicit ParenthesizedExpression(ParsedExpression* innerExpression, unsigned int start, unsigned int length)
|
|
: ParsedExpression(start, length){
|
|
_expression = innerExpression;
|
|
}
|
|
|
|
ParsedExpression* GetInnerExpression(){
|
|
return _expression;
|
|
}
|
|
};
|
|
|
|
class UnaryExpression : public ParsedExpression{
|
|
UnaryOperatorKind _kind;
|
|
ParsedExpression* _operand;
|
|
public:
|
|
~UnaryExpression() override {
|
|
delete _operand;
|
|
}
|
|
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::Unary;
|
|
}
|
|
|
|
UnaryExpression(UnaryOperatorKind kind, ParsedExpression* operand, unsigned int start, unsigned int length)
|
|
: ParsedExpression(start, length){
|
|
_kind = kind;
|
|
_operand = operand;
|
|
}
|
|
|
|
UnaryOperatorKind GetOperatorKind(){
|
|
return _kind;
|
|
}
|
|
|
|
ParsedExpression* GetOperand(){
|
|
return _operand;
|
|
}
|
|
};
|
|
|
|
class BinaryExpression : public ParsedExpression{
|
|
BinaryOperatorKind _kind;
|
|
ParsedExpression* _left;
|
|
ParsedExpression* _right;
|
|
public:
|
|
~BinaryExpression() override {
|
|
delete _left;
|
|
delete _right;
|
|
}
|
|
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::Binary;
|
|
}
|
|
|
|
BinaryExpression(BinaryOperatorKind kind, ParsedExpression* left, ParsedExpression* right, unsigned int start,
|
|
unsigned int length)
|
|
: ParsedExpression(start, length){
|
|
_kind = kind;
|
|
_left = left;
|
|
_right = right;
|
|
}
|
|
|
|
BinaryOperatorKind GetOperatorKind(){
|
|
return _kind;
|
|
}
|
|
|
|
ParsedExpression* GetLeft() {
|
|
return _left;
|
|
}
|
|
|
|
ParsedExpression* GetRight() {
|
|
return _right;
|
|
}
|
|
};
|
|
|
|
class FunctionCallExpression : public ParsedExpression{
|
|
std::unique_ptr<ParsedExpression> _function;
|
|
vector<std::unique_ptr<ParsedExpression>> _parameters;
|
|
public:
|
|
FunctionCallExpression(ParsedExpression* function, vector<ParsedExpression*> parameters,
|
|
unsigned int start, unsigned int length) : ParsedExpression(start, length){
|
|
_function = std::unique_ptr<ParsedExpression>(function);
|
|
for (int i = 0; i < parameters.size(); i++){
|
|
_parameters.push_back(std::unique_ptr<ParsedExpression>(parameters[i]));
|
|
}
|
|
}
|
|
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::FunctionCall;
|
|
}
|
|
|
|
ParsedExpression* GetFunction(){
|
|
return _function.get();
|
|
}
|
|
vector<ParsedExpression*> GetParameters(){
|
|
auto par = vector<ParsedExpression*>(_parameters.size(), nullptr);
|
|
for (int i = 0; i < _parameters.size(); i++){
|
|
par[i] = _parameters[i].get();
|
|
}
|
|
return par;
|
|
}
|
|
};
|
|
|
|
class IndexExpression : public ParsedExpression{
|
|
std::unique_ptr<ParsedExpression> _indexerExpression;
|
|
std::unique_ptr<ParsedExpression> _indexExpression;
|
|
public:
|
|
IndexExpression(ParsedExpression* indexer, ParsedExpression* index, unsigned int start, unsigned int length)
|
|
:ParsedExpression(start, length){
|
|
_indexerExpression = std::unique_ptr<ParsedExpression>(indexer);
|
|
_indexExpression = std::unique_ptr<ParsedExpression>(index);
|
|
}
|
|
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::Indexer;
|
|
}
|
|
|
|
ParsedExpression* GetIndexer(){
|
|
return _indexerExpression.get();
|
|
}
|
|
|
|
ParsedExpression* GetIndex(){
|
|
return _indexExpression.get();
|
|
}
|
|
};
|
|
|
|
class ParsedNumericalTableExpression : public ParsedExpression{
|
|
vector<ParsedExpression*> _expressions;
|
|
public:
|
|
ParsedNumericalTableExpression(vector<ParsedExpression*> expressions, unsigned int start, unsigned int length)
|
|
: ParsedExpression(start, length){
|
|
_expressions = std::move(expressions);
|
|
}
|
|
|
|
~ParsedNumericalTableExpression() final{
|
|
for (auto s: _expressions){
|
|
delete s;
|
|
}
|
|
}
|
|
|
|
vector<ParsedExpression*> GetExpressions(){
|
|
return _expressions;
|
|
}
|
|
|
|
ParsedExpressionKind GetKind() final{
|
|
return ParsedExpressionKind::NumericalTable;
|
|
}
|
|
};
|
|
|
|
|
|
#endif //PORYGONLANG_PARSEDEXPRESSION_HPP
|