Make parsed statements constant during binding
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#include <utility>
|
||||
|
||||
#ifndef PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||
#define PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||
|
||||
@@ -28,26 +30,28 @@ enum class ParsedExpressionKind{
|
||||
};
|
||||
|
||||
class ParsedExpression {
|
||||
unsigned int _position;
|
||||
unsigned int _length;
|
||||
const unsigned int _position;
|
||||
const unsigned int _length;
|
||||
public:
|
||||
ParsedExpression(unsigned int position, unsigned int length){
|
||||
_position = position;
|
||||
_length = length;
|
||||
ParsedExpression(unsigned int position, unsigned int length)
|
||||
: _position(position),
|
||||
_length(length)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~ParsedExpression() = default;
|
||||
|
||||
virtual ParsedExpressionKind GetKind() = 0;
|
||||
virtual const ParsedExpressionKind GetKind() const = 0;
|
||||
|
||||
unsigned int GetStartPosition(){
|
||||
const unsigned int GetStartPosition() const{
|
||||
return _position;
|
||||
}
|
||||
|
||||
unsigned int GetEndPosition(){
|
||||
const unsigned int GetEndPosition() const{
|
||||
return _position + _length - 1;
|
||||
}
|
||||
|
||||
unsigned int GetLength(){
|
||||
const unsigned int GetLength() const{
|
||||
return _length;
|
||||
}
|
||||
};
|
||||
@@ -56,75 +60,83 @@ class BadExpression : public ParsedExpression{
|
||||
public:
|
||||
BadExpression(unsigned int position, unsigned int length) : ParsedExpression(position, length){}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final {
|
||||
return ParsedExpressionKind::Bad;
|
||||
}
|
||||
};
|
||||
|
||||
class LiteralIntegerExpression : public ParsedExpression{
|
||||
long _value;
|
||||
const long _value;
|
||||
public:
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::LiteralInteger;
|
||||
}
|
||||
explicit LiteralIntegerExpression(IntegerToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){
|
||||
_value = token->Value;
|
||||
explicit LiteralIntegerExpression(IntegerToken* token)
|
||||
: ParsedExpression(token -> GetStartPosition(), token -> GetLength()),
|
||||
_value(token -> Value)
|
||||
{
|
||||
}
|
||||
|
||||
long GetValue(){
|
||||
const long GetValue() const{
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
class LiteralFloatExpression : public ParsedExpression{
|
||||
double _value;
|
||||
const double _value;
|
||||
public:
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::LiteralFloat;
|
||||
}
|
||||
explicit LiteralFloatExpression(FloatToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){
|
||||
_value = token->Value;
|
||||
explicit LiteralFloatExpression(FloatToken* token)
|
||||
: ParsedExpression(token -> GetStartPosition(), token -> GetLength()),
|
||||
_value(token -> Value)
|
||||
{
|
||||
}
|
||||
|
||||
double GetValue(){
|
||||
const double GetValue() const{
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
class LiteralStringExpression : public ParsedExpression{
|
||||
string _value;
|
||||
const string _value;
|
||||
public:
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::LiteralString;
|
||||
}
|
||||
explicit LiteralStringExpression(StringToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){
|
||||
_value = std::move(token->Value);
|
||||
explicit LiteralStringExpression(StringToken* token)
|
||||
: ParsedExpression(token -> GetStartPosition(), token -> GetLength()),
|
||||
_value(std::move(token -> Value))
|
||||
{
|
||||
}
|
||||
|
||||
string GetValue(){
|
||||
const string& GetValue() const{
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
class LiteralBoolExpression : public ParsedExpression{
|
||||
bool _value;
|
||||
const bool _value;
|
||||
public:
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::LiteralBool;
|
||||
}
|
||||
explicit LiteralBoolExpression(IToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){
|
||||
_value = token -> GetKind() == TokenKind::TrueKeyword;
|
||||
explicit LiteralBoolExpression(IToken* token)
|
||||
: ParsedExpression(token -> GetStartPosition(), token -> GetLength()),
|
||||
_value(token -> GetKind() == TokenKind::TrueKeyword)
|
||||
{
|
||||
}
|
||||
|
||||
bool GetValue(){
|
||||
const bool GetValue() const{
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
class VariableExpression : public ParsedExpression{
|
||||
HashedString _value;
|
||||
const HashedString _value;
|
||||
public:
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::Variable;
|
||||
}
|
||||
explicit VariableExpression(IdentifierToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength())
|
||||
@@ -132,150 +144,153 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
HashedString GetValue(){
|
||||
const HashedString GetValue() const{
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class ParenthesizedExpression : public ParsedExpression{
|
||||
ParsedExpression* _expression;
|
||||
const ParsedExpression* _expression;
|
||||
public:
|
||||
~ParenthesizedExpression() override {
|
||||
delete _expression;
|
||||
}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::Parenthesized;
|
||||
}
|
||||
|
||||
explicit ParenthesizedExpression(ParsedExpression* innerExpression, unsigned int start, unsigned int length)
|
||||
: ParsedExpression(start, length){
|
||||
_expression = innerExpression;
|
||||
: ParsedExpression(start, length), _expression(innerExpression){
|
||||
}
|
||||
|
||||
ParsedExpression* GetInnerExpression(){
|
||||
const ParsedExpression* GetInnerExpression() const{
|
||||
return _expression;
|
||||
}
|
||||
};
|
||||
|
||||
class UnaryExpression : public ParsedExpression{
|
||||
UnaryOperatorKind _kind;
|
||||
ParsedExpression* _operand;
|
||||
const UnaryOperatorKind _kind;
|
||||
const ParsedExpression* _operand;
|
||||
public:
|
||||
~UnaryExpression() override {
|
||||
delete _operand;
|
||||
}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::Unary;
|
||||
}
|
||||
|
||||
UnaryExpression(UnaryOperatorKind kind, ParsedExpression* operand, unsigned int start, unsigned int length)
|
||||
: ParsedExpression(start, length){
|
||||
_kind = kind;
|
||||
_operand = operand;
|
||||
: ParsedExpression(start, length),
|
||||
_kind(kind), _operand(operand)
|
||||
{
|
||||
}
|
||||
|
||||
UnaryOperatorKind GetOperatorKind(){
|
||||
const UnaryOperatorKind GetOperatorKind() const{
|
||||
return _kind;
|
||||
}
|
||||
|
||||
ParsedExpression* GetOperand(){
|
||||
const ParsedExpression* GetOperand() const{
|
||||
return _operand;
|
||||
}
|
||||
};
|
||||
|
||||
class BinaryExpression : public ParsedExpression{
|
||||
BinaryOperatorKind _kind;
|
||||
ParsedExpression* _left;
|
||||
ParsedExpression* _right;
|
||||
const BinaryOperatorKind _kind;
|
||||
const ParsedExpression* _left;
|
||||
const ParsedExpression* _right;
|
||||
public:
|
||||
~BinaryExpression() override {
|
||||
delete _left;
|
||||
delete _right;
|
||||
}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const 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;
|
||||
: ParsedExpression(start, length),
|
||||
_kind(kind), _left(left), _right(right)
|
||||
{
|
||||
}
|
||||
|
||||
BinaryOperatorKind GetOperatorKind(){
|
||||
const BinaryOperatorKind GetOperatorKind() const{
|
||||
return _kind;
|
||||
}
|
||||
|
||||
ParsedExpression* GetLeft() {
|
||||
const ParsedExpression* GetLeft() const{
|
||||
return _left;
|
||||
}
|
||||
|
||||
ParsedExpression* GetRight() {
|
||||
const ParsedExpression* GetRight() const{
|
||||
return _right;
|
||||
}
|
||||
};
|
||||
|
||||
class FunctionCallExpression : public ParsedExpression{
|
||||
std::unique_ptr<ParsedExpression> _function;
|
||||
vector<std::unique_ptr<ParsedExpression>> _parameters;
|
||||
const std::unique_ptr<ParsedExpression> _function;
|
||||
const vector<const 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]));
|
||||
FunctionCallExpression(ParsedExpression* function, vector<const ParsedExpression*> parameters, unsigned int start, unsigned int length)
|
||||
: ParsedExpression(start, length),
|
||||
_function(function), _parameters(std::move(parameters))
|
||||
{
|
||||
}
|
||||
|
||||
~FunctionCallExpression() final{
|
||||
for (auto p : _parameters){
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::FunctionCall;
|
||||
}
|
||||
|
||||
ParsedExpression* GetFunction(){
|
||||
const ParsedExpression* GetFunction() const{
|
||||
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;
|
||||
const vector<const ParsedExpression*>* GetParameters() const{
|
||||
return &_parameters;
|
||||
}
|
||||
};
|
||||
|
||||
class IndexExpression : public ParsedExpression{
|
||||
std::unique_ptr<ParsedExpression> _indexerExpression;
|
||||
std::unique_ptr<ParsedExpression> _indexExpression;
|
||||
const ParsedExpression* _indexerExpression;
|
||||
const 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);
|
||||
:ParsedExpression(start, length),
|
||||
_indexerExpression(indexer), _indexExpression(index)
|
||||
{
|
||||
}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
~IndexExpression() final{
|
||||
delete _indexerExpression;
|
||||
delete _indexExpression;
|
||||
}
|
||||
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::Indexer;
|
||||
}
|
||||
|
||||
ParsedExpression* GetIndexer(){
|
||||
return _indexerExpression.get();
|
||||
const ParsedExpression* GetIndexer() const{
|
||||
return _indexerExpression;
|
||||
}
|
||||
|
||||
ParsedExpression* GetIndex(){
|
||||
return _indexExpression.get();
|
||||
const ParsedExpression* GetIndex() const{
|
||||
return _indexExpression;
|
||||
}
|
||||
};
|
||||
|
||||
class ParsedNumericalTableExpression : public ParsedExpression{
|
||||
vector<ParsedExpression*> _expressions;
|
||||
vector<const ParsedExpression*> _expressions;
|
||||
public:
|
||||
ParsedNumericalTableExpression(vector<ParsedExpression*> expressions, unsigned int start, unsigned int length)
|
||||
ParsedNumericalTableExpression(vector<const ParsedExpression*> expressions, unsigned int start, unsigned int length)
|
||||
: ParsedExpression(start, length){
|
||||
_expressions = std::move(expressions);
|
||||
}
|
||||
@@ -286,11 +301,11 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
vector<ParsedExpression*> GetExpressions(){
|
||||
return _expressions;
|
||||
const vector<const ParsedExpression*>* GetExpressions() const{
|
||||
return &_expressions;
|
||||
}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
const ParsedExpressionKind GetKind() const final{
|
||||
return ParsedExpressionKind::NumericalTable;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user