Implements expression parsing
This commit is contained in:
142
src/Parser/ParsedExpressions/ParsedExpression.hpp
Normal file
142
src/Parser/ParsedExpressions/ParsedExpression.hpp
Normal file
@@ -0,0 +1,142 @@
|
||||
|
||||
#ifndef PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||
#define PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||
|
||||
#include "../Token.hpp"
|
||||
#include "../UnaryOperatorKind.hpp"
|
||||
#include "../BinaryOperatorKind.hpp"
|
||||
|
||||
enum class ParsedExpressionKind{
|
||||
LiteralInteger,
|
||||
LiteralFloat,
|
||||
LiteralString,
|
||||
LiteralBool,
|
||||
|
||||
Unary,
|
||||
Binary,
|
||||
};
|
||||
|
||||
class ParsedExpression {
|
||||
unsigned int _position;
|
||||
unsigned int _length;
|
||||
public:
|
||||
virtual ParsedExpressionKind GetKind() = 0;
|
||||
|
||||
ParsedExpression(unsigned int position, unsigned int length){
|
||||
_position = position;
|
||||
_length = length;
|
||||
}
|
||||
|
||||
unsigned int GetStartPosition(){
|
||||
return _position;
|
||||
}
|
||||
|
||||
unsigned int GetEndPosition(){
|
||||
return _position + _length - 1;
|
||||
}
|
||||
|
||||
unsigned int GetLength(){
|
||||
return _length;
|
||||
}
|
||||
};
|
||||
|
||||
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 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 UnaryExpression : public ParsedExpression{
|
||||
UnaryOperatorKind _kind;
|
||||
ParsedExpression* _operand;
|
||||
public:
|
||||
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:
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||
Reference in New Issue
Block a user