From 5910cbbfa92c9dc1a7d26158031ac2df658cdca1 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 13 Jun 2019 18:14:59 +0200 Subject: [PATCH] Make parsed statements constant during binding --- src/Binder/Binder.cpp | 60 +++--- src/Binder/Binder.hpp | 32 +-- .../ParsedExpressions/ParsedExpression.hpp | 189 ++++++++++-------- .../ParsedTableExpression.hpp | 10 +- .../ParsedStatements/ParsedStatement.hpp | 132 ++++++------ src/Parser/Parser.cpp | 16 +- src/Parser/Parser.hpp | 6 +- tests/parser/ParserTests.cpp | 8 +- 8 files changed, 238 insertions(+), 215 deletions(-) diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index 4cad000..cf3056c 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -1,9 +1,11 @@ +#include + #include "Binder.hpp" #include "../TableScriptType.hpp" #include "BoundExpressions/BoundTableExpression.hpp" #include -BoundScriptStatement *Binder::Bind(Script* script, ParsedScriptStatement *s, BoundScope* scriptScope) { +BoundScriptStatement *Binder::Bind(Script* script, const ParsedScriptStatement *s, BoundScope* scriptScope) { auto binder = Binder(); binder._scriptData = script; @@ -20,7 +22,7 @@ Binder::~Binder() { delete _scope; } -BoundStatement* Binder::BindStatement(ParsedStatement* statement){ +BoundStatement* Binder::BindStatement(const ParsedStatement* statement){ switch (statement -> GetKind()) { case ParsedStatementKind ::Script: throw; // This shouldn't happen. case ParsedStatementKind ::Block: return this -> BindBlockStatement(statement); @@ -34,7 +36,7 @@ BoundStatement* Binder::BindStatement(ParsedStatement* statement){ } } -BoundStatement *Binder::BindBlockStatement(ParsedStatement *statement) { +BoundStatement *Binder::BindBlockStatement(const ParsedStatement *statement) { auto statements = ((ParsedBlockStatement*)statement)->GetStatements(); vector boundStatements (statements->size()); this->_scope->GoInnerScope(); @@ -45,12 +47,12 @@ BoundStatement *Binder::BindBlockStatement(ParsedStatement *statement) { return new BoundBlockStatement(boundStatements); } -BoundStatement *Binder::BindExpressionStatement(ParsedStatement *statement) { +BoundStatement *Binder::BindExpressionStatement(const ParsedStatement *statement) { auto exp = ((ParsedExpressionStatement*)statement)->GetExpression(); return new BoundExpressionStatement(this -> BindExpression(exp)); } -BoundStatement* Binder::BindAssignmentStatement(ParsedStatement *statement){ +BoundStatement* Binder::BindAssignmentStatement(const ParsedStatement *statement){ auto s = (ParsedAssignmentStatement*) statement; auto boundExpression = this->BindExpression(s->GetExpression()); VariableAssignment assignment = @@ -76,16 +78,16 @@ std::shared_ptr ParseTypeIdentifier(HashedString s){ } } -BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statement) { +BoundStatement *Binder::BindFunctionDeclarationStatement(const ParsedStatement *statement) { auto functionStatement = (ParsedFunctionDeclarationStatement*) statement; auto parameters = functionStatement->GetParameters(); - auto parameterTypes = vector>(parameters.size()); - auto parameterKeys = vector>(parameters.size()); + auto parameterTypes = vector>(parameters->size()); + auto parameterKeys = vector>(parameters->size()); auto scopeIndex = this->_scope->GetCurrentScope(); this->_scope->GoInnerScope(); - for (int i = 0; i < parameters.size(); i++){ - auto var = parameters[i]; + for (int i = 0; i < parameters->size(); i++){ + auto var = parameters -> at(i); auto parsedType = ParseTypeIdentifier(var->GetType()); parameterTypes.at(i) = parsedType; auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType); @@ -114,7 +116,7 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statem return new BoundFunctionDeclarationStatement(type, assignment.GetKey(), (BoundBlockStatement*)boundBlock); } -BoundStatement *Binder::BindReturnStatement(ParsedStatement* statement){ +BoundStatement *Binder::BindReturnStatement(const ParsedStatement* statement){ auto expression = ((ParsedReturnStatement*)statement)->GetExpression(); shared_ptr currentReturnType; if (this->_currentFunction == nullptr){ @@ -143,7 +145,7 @@ BoundStatement *Binder::BindReturnStatement(ParsedStatement* statement){ return new BoundReturnStatement(boundExpression); } -BoundStatement *Binder::BindConditionalStatement(ParsedStatement* statement) { +BoundStatement *Binder::BindConditionalStatement(const ParsedStatement* statement) { auto conditionalStatement = (ParsedConditionalStatement*)statement; auto boundCondition = this -> BindExpression(conditionalStatement -> GetCondition()); if (boundCondition->GetType() -> GetClass() != TypeClass::Bool){ @@ -158,7 +160,7 @@ BoundStatement *Binder::BindConditionalStatement(ParsedStatement* statement) { return new BoundConditionalStatement(boundCondition, boundBlock, elseStatement); } -BoundExpression* Binder::BindExpression(ParsedExpression* expression){ +BoundExpression* Binder::BindExpression(const ParsedExpression* expression){ switch (expression -> GetKind()){ case ParsedExpressionKind ::LiteralInteger: return new BoundLiteralIntegerExpression(((LiteralIntegerExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength()); @@ -193,7 +195,7 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){ } } -BoundExpression* Binder::BindVariableExpression(VariableExpression* expression){ +BoundExpression* Binder::BindVariableExpression(const VariableExpression* expression){ auto key = expression->GetValue(); auto scope = this->_scope->Exists(key.GetHash()); if (scope == -1){ @@ -205,7 +207,7 @@ BoundExpression* Binder::BindVariableExpression(VariableExpression* expression){ return new BoundVariableExpression(new BoundVariableKey(key.GetHash(), scope, false), type, expression->GetStartPosition(), expression->GetLength()); } -BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){ +BoundExpression* Binder::BindBinaryOperator(const BinaryExpression* expression){ auto boundLeft = this -> BindExpression(expression->GetLeft()); auto boundRight = this -> BindExpression(expression->GetRight()); @@ -326,7 +328,7 @@ BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){ return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } -BoundExpression* Binder::BindUnaryOperator(UnaryExpression* expression){ +BoundExpression* Binder::BindUnaryOperator(const UnaryExpression* expression){ auto operand = this -> BindExpression(expression->GetOperand()); auto operandType = operand -> GetType(); switch (expression->GetOperatorKind()){ @@ -359,7 +361,7 @@ BoundExpression* Binder::BindUnaryOperator(UnaryExpression* expression){ } -BoundExpression* Binder::BindFunctionCall(FunctionCallExpression* expression){ +BoundExpression* Binder::BindFunctionCall(const FunctionCallExpression* expression){ auto functionExpression = BindExpression(expression->GetFunction()); auto type = functionExpression->GetType(); if (type->GetClass() != TypeClass::Function){ @@ -370,14 +372,14 @@ BoundExpression* Binder::BindFunctionCall(FunctionCallExpression* expression){ auto functionType = std::dynamic_pointer_cast(type); auto parameterTypes = functionType->GetParameterTypes(); auto givenParameters = expression->GetParameters(); - if (parameterTypes.size() != givenParameters.size()){ + if (parameterTypes.size() != givenParameters->size()){ this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterCountMismatch, expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } - vector boundParameters = vector(givenParameters.size()); - for (int i = 0; i < givenParameters.size(); i++){ - auto parameter = givenParameters[i]; + vector boundParameters = vector(givenParameters->size()); + for (int i = 0; i < givenParameters->size(); i++){ + auto parameter = givenParameters -> at(i); auto boundParameter = this -> BindExpression(parameter); if (boundParameter->GetType().get()->operator!=(parameterTypes.at(i).get())){ this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterTypeMismatch, parameter->GetStartPosition(), @@ -391,7 +393,7 @@ BoundExpression* Binder::BindFunctionCall(FunctionCallExpression* expression){ expression->GetStartPosition(), expression->GetLength()); } -BoundExpression *Binder::BindIndexExpression(IndexExpression *expression) { +BoundExpression *Binder::BindIndexExpression(const IndexExpression *expression) { auto indexer = this->BindExpression(expression->GetIndexer()); auto index = this->BindExpression(expression->GetIndex()); @@ -404,15 +406,15 @@ BoundExpression *Binder::BindIndexExpression(IndexExpression *expression) { return new BoundIndexExpression(indexer, index, resultType, expression->GetStartPosition(), expression->GetLength()); } -BoundExpression* Binder::BindNumericalTableExpression(ParsedNumericalTableExpression* expression){ +BoundExpression* Binder::BindNumericalTableExpression(const ParsedNumericalTableExpression* expression){ auto expressions = expression->GetExpressions(); - auto boundExpressions = vector(expressions.size()); + auto boundExpressions = vector(expressions-> size()); shared_ptr valueType = nullptr; if (!boundExpressions.empty()){ - boundExpressions[0] = this -> BindExpression(expressions[0]); + boundExpressions[0] = this -> BindExpression(expressions -> at(0)); valueType = boundExpressions[0] -> GetType(); - for (int i = 1; i < expressions.size(); i++){ - boundExpressions[i] = this -> BindExpression(expressions[i]); + for (int i = 1; i < expressions->size(); i++){ + boundExpressions[i] = this -> BindExpression(expressions -> at(i)); if (boundExpressions[i] -> GetType().get()->operator!=(valueType.get())){ this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTableValueType, boundExpressions[i]->GetStartPosition(), boundExpressions[i]->GetLength()); @@ -426,7 +428,7 @@ BoundExpression* Binder::BindNumericalTableExpression(ParsedNumericalTableExpres return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(), expression->GetLength()); } -BoundExpression *Binder::BindTableExpression(ParsedTableExpression *expression) { +BoundExpression *Binder::BindTableExpression(const ParsedTableExpression *expression) { auto tableScope = new unordered_map(); auto innerScope = new BoundScope(tableScope); auto currentScope = this -> _scope; @@ -434,7 +436,7 @@ BoundExpression *Binder::BindTableExpression(ParsedTableExpression *expression) auto block = this -> BindBlockStatement(expression -> GetBlock()); this -> _scope = currentScope; - auto tableType = shared_ptr(new TableScriptType(tableScope, innerScope->GetLocalVariableCount())); + auto tableType = std::make_shared(tableScope, innerScope->GetLocalVariableCount()); delete innerScope; return new BoundTableExpression((BoundBlockStatement*)block, tableType, expression->GetStartPosition(), expression->GetLength()); diff --git a/src/Binder/Binder.hpp b/src/Binder/Binder.hpp index 4fd60ab..aa10a5e 100644 --- a/src/Binder/Binder.hpp +++ b/src/Binder/Binder.hpp @@ -16,24 +16,24 @@ class Binder { ~Binder(); - BoundStatement *BindStatement(ParsedStatement *statement); - BoundStatement *BindBlockStatement(ParsedStatement *statement); - BoundStatement *BindExpressionStatement(ParsedStatement *statement); - BoundStatement *BindAssignmentStatement(ParsedStatement *statement); - BoundStatement *BindFunctionDeclarationStatement(ParsedStatement * statement); - BoundStatement *BindReturnStatement(ParsedStatement *statement); - BoundStatement *BindConditionalStatement(ParsedStatement *statement); + BoundStatement *BindStatement(const ParsedStatement *statement); + BoundStatement *BindBlockStatement(const ParsedStatement *statement); + BoundStatement *BindExpressionStatement(const ParsedStatement *statement); + BoundStatement *BindAssignmentStatement(const ParsedStatement *statement); + BoundStatement *BindFunctionDeclarationStatement(const ParsedStatement * statement); + BoundStatement *BindReturnStatement(const ParsedStatement *statement); + BoundStatement *BindConditionalStatement(const ParsedStatement *statement); - BoundExpression *BindExpression(ParsedExpression *expression); - BoundExpression *BindVariableExpression(VariableExpression *expression); - BoundExpression *BindBinaryOperator(BinaryExpression *expression); - BoundExpression *BindUnaryOperator(UnaryExpression *expression); - BoundExpression *BindFunctionCall(FunctionCallExpression *expression); - BoundExpression *BindIndexExpression(IndexExpression *expression); - BoundExpression *BindNumericalTableExpression(ParsedNumericalTableExpression *expression); - BoundExpression *BindTableExpression(ParsedTableExpression * expression); + BoundExpression *BindExpression(const ParsedExpression *expression); + BoundExpression *BindVariableExpression(const VariableExpression *expression); + BoundExpression *BindBinaryOperator(const BinaryExpression *expression); + BoundExpression *BindUnaryOperator(const UnaryExpression *expression); + BoundExpression *BindFunctionCall(const FunctionCallExpression *expression); + BoundExpression *BindIndexExpression(const IndexExpression *expression); + BoundExpression *BindNumericalTableExpression(const ParsedNumericalTableExpression *expression); + BoundExpression *BindTableExpression(const ParsedTableExpression * expression); public: - static BoundScriptStatement* Bind(Script* script, ParsedScriptStatement* s, BoundScope* scriptScope); + static BoundScriptStatement* Bind(Script* script, const ParsedScriptStatement* s, BoundScope* scriptScope); }; diff --git a/src/Parser/ParsedExpressions/ParsedExpression.hpp b/src/Parser/ParsedExpressions/ParsedExpression.hpp index c0accbb..7656b6c 100644 --- a/src/Parser/ParsedExpressions/ParsedExpression.hpp +++ b/src/Parser/ParsedExpressions/ParsedExpression.hpp @@ -1,3 +1,5 @@ +#include + #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 _function; - vector> _parameters; + const std::unique_ptr _function; + const vector _parameters; public: - FunctionCallExpression(ParsedExpression* function, vector parameters, - unsigned int start, unsigned int length) : ParsedExpression(start, length){ - _function = std::unique_ptr(function); - for (int i = 0; i < parameters.size(); i++){ - _parameters.push_back(std::unique_ptr(parameters[i])); + FunctionCallExpression(ParsedExpression* function, vector 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 GetParameters(){ - auto par = vector(_parameters.size(), nullptr); - for (int i = 0; i < _parameters.size(); i++){ - par[i] = _parameters[i].get(); - } - return par; + const vector* GetParameters() const{ + return &_parameters; } }; class IndexExpression : public ParsedExpression{ - std::unique_ptr _indexerExpression; - std::unique_ptr _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(indexer); - _indexExpression = std::unique_ptr(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 _expressions; + vector _expressions; public: - ParsedNumericalTableExpression(vector expressions, unsigned int start, unsigned int length) + ParsedNumericalTableExpression(vector expressions, unsigned int start, unsigned int length) : ParsedExpression(start, length){ _expressions = std::move(expressions); } @@ -286,11 +301,11 @@ public: } } - vector GetExpressions(){ - return _expressions; + const vector* GetExpressions() const{ + return &_expressions; } - ParsedExpressionKind GetKind() final{ + const ParsedExpressionKind GetKind() const final{ return ParsedExpressionKind::NumericalTable; } }; diff --git a/src/Parser/ParsedExpressions/ParsedTableExpression.hpp b/src/Parser/ParsedExpressions/ParsedTableExpression.hpp index f565ab7..3018385 100644 --- a/src/Parser/ParsedExpressions/ParsedTableExpression.hpp +++ b/src/Parser/ParsedExpressions/ParsedTableExpression.hpp @@ -6,22 +6,22 @@ #include "../ParsedStatements/ParsedStatement.hpp" class ParsedTableExpression : public ParsedExpression{ - ParsedBlockStatement* _block; + const ParsedBlockStatement* _block; public: ParsedTableExpression(ParsedBlockStatement* block, unsigned int start, unsigned int length) - : ParsedExpression(start, length){ - _block = block; + : ParsedExpression(start, length), _block(block) + { } ~ParsedTableExpression() final{ delete _block; } - ParsedBlockStatement* GetBlock(){ + const ParsedBlockStatement* GetBlock() const{ return _block; } - ParsedExpressionKind GetKind() final{ + const ParsedExpressionKind GetKind() const final{ return ParsedExpressionKind::Table; } }; diff --git a/src/Parser/ParsedStatements/ParsedStatement.hpp b/src/Parser/ParsedStatements/ParsedStatement.hpp index 784d8a7..1b57a8f 100644 --- a/src/Parser/ParsedStatements/ParsedStatement.hpp +++ b/src/Parser/ParsedStatements/ParsedStatement.hpp @@ -1,5 +1,7 @@ #include +#include + #ifndef PORYGONLANG_PARSEDSTATEMENT_HPP #define PORYGONLANG_PARSEDSTATEMENT_HPP @@ -23,25 +25,25 @@ enum class ParsedStatementKind{ }; class ParsedStatement { - unsigned int _start; - unsigned int _length; + const unsigned int _start; + const unsigned int _length; public: - ParsedStatement(unsigned int start, unsigned int length){ - _start = start; - _length = length; + ParsedStatement(unsigned int start, unsigned int length) + :_start(start), _length(length) + { } virtual ~ParsedStatement() = default; - virtual ParsedStatementKind GetKind() = 0; + virtual const ParsedStatementKind GetKind() const = 0; - unsigned int GetStartPosition(){ + const unsigned int GetStartPosition() const { return _start; } - unsigned int GetLength(){ + const unsigned int GetLength() const { return _length; } - unsigned int GetEndPosition(){ + const unsigned int GetEndPosition() const { return _start + _length - 1; } }; @@ -49,20 +51,22 @@ public: class ParsedBadStatement : public ParsedStatement{ public: ParsedBadStatement(unsigned int start, unsigned int length) : ParsedStatement(start, length){}; - ParsedStatementKind GetKind() final{ + const ParsedStatementKind GetKind() const final{ return ParsedStatementKind ::Bad; } }; class ParsedBlockStatement : public ParsedStatement{ - std::vector _statements; + const std::vector _statements; public: - explicit ParsedBlockStatement(std::vector statements) - : ParsedStatement(statements.front()->GetStartPosition(), statements.back()->GetEndPosition() - statements.front()->GetStartPosition()){ - _statements = std::move(statements); - } - ParsedBlockStatement(std::vector statements, unsigned int start) : ParsedStatement(start, 0){ - _statements = std::move(statements); + explicit ParsedBlockStatement(std::vector statements) + : ParsedStatement(statements.front()->GetStartPosition(), statements.back()->GetEndPosition() - statements.front()->GetStartPosition()), + _statements(statements) + {} + + ParsedBlockStatement(std::vector statements, unsigned int start) : ParsedStatement(start, 0), + _statements(std::move(statements)) + { } @@ -70,50 +74,52 @@ public: for (auto s: _statements){ delete s; } - _statements.clear(); } - ParsedStatementKind GetKind() override{ + const ParsedStatementKind GetKind() const override { return ParsedStatementKind ::Block; } - std::vector* GetStatements(){ + const std::vector* GetStatements() const{ return &_statements; } }; class ParsedScriptStatement : public ParsedBlockStatement{ public: - explicit ParsedScriptStatement(vector statements) : ParsedBlockStatement(move(statements)){} + explicit ParsedScriptStatement(vector statements) : ParsedBlockStatement(move(statements)){} - ParsedStatementKind GetKind() final{ + const ParsedStatementKind GetKind() const final{ return ParsedStatementKind ::Script; } }; class ParsedExpressionStatement : public ParsedStatement{ - ParsedExpression* _expression; + const ParsedExpression* _expression; public: - explicit ParsedExpressionStatement(ParsedExpression* expression) : ParsedStatement(expression->GetStartPosition(), expression->GetLength()){ - _expression = expression; + explicit ParsedExpressionStatement(ParsedExpression* expression) + : ParsedStatement(expression->GetStartPosition(), expression->GetLength()), + _expression(expression) + { } + ~ParsedExpressionStatement() override { delete _expression; } - ParsedStatementKind GetKind() final{ + const ParsedStatementKind GetKind() const final{ return ParsedStatementKind ::Expression; } - ParsedExpression* GetExpression(){ + const ParsedExpression* GetExpression() const{ return _expression; } }; class ParsedFunctionDeclarationStatement : public ParsedStatement{ - HashedString _identifier; - vector _parameters; - ParsedBlockStatement* _block; + const HashedString _identifier; + const vector _parameters; + const ParsedBlockStatement* _block; public: ParsedFunctionDeclarationStatement(HashedString identifier, vector parameters, ParsedBlockStatement* block, unsigned int start, unsigned int length) @@ -126,90 +132,90 @@ public: delete _block; } - ParsedStatementKind GetKind() final{ + const ParsedStatementKind GetKind() const final{ return ParsedStatementKind ::FunctionDeclaration; } - HashedString GetIdentifier(){ + const HashedString GetIdentifier() const{ return _identifier; } - vector GetParameters(){ - return _parameters; + const vector* GetParameters() const{ + return &_parameters; } - ParsedBlockStatement* GetBlock(){ + const ParsedBlockStatement* GetBlock() const{ return _block; } }; class ParsedAssignmentStatement : public ParsedStatement{ - bool _local; - HashedString _identifier; - ParsedExpression* _expression; + const bool _local; + const HashedString _identifier; + const ParsedExpression* _expression; public: - ParsedAssignmentStatement(bool local, std::string identifier, ParsedExpression* expression, unsigned int start, unsigned int length) - : ParsedStatement(start, length), _identifier(HashedString(std::move(identifier))) + ParsedAssignmentStatement(bool local, const std::string& identifier, ParsedExpression* expression, unsigned int start, unsigned int length) + : ParsedStatement(start, length), _local(local), _identifier(HashedString(identifier)), _expression(expression) { - _local = local; - _expression = expression; } + ~ParsedAssignmentStatement() final{ delete _expression; } - ParsedStatementKind GetKind() final{ + const ParsedStatementKind GetKind() const final{ return ParsedStatementKind ::Assignment; } - bool IsLocal(){ + const bool IsLocal() const{ return _local; } - HashedString GetIdentifier(){ + const HashedString GetIdentifier() const{ return _identifier; } - ParsedExpression* GetExpression(){ + const ParsedExpression* GetExpression() const{ return _expression; } }; class ParsedReturnStatement : public ParsedStatement{ - ParsedExpression* _expression; + const ParsedExpression* _expression; public: - ParsedReturnStatement(ParsedExpression* expression, unsigned int start, unsigned int length) : ParsedStatement(start, length){ - _expression = expression; + ParsedReturnStatement(ParsedExpression* expression, unsigned int start, unsigned int length) + : ParsedStatement(start, length), + _expression(expression) + { } ~ParsedReturnStatement() final{ delete _expression; } - ParsedStatementKind GetKind() final{ + const ParsedStatementKind GetKind() const final{ return ParsedStatementKind ::Return; } - ParsedExpression* GetExpression(){ + const ParsedExpression* GetExpression() const{ return _expression; } }; class ParsedConditionalStatement : public ParsedStatement{ - ParsedExpression* _condition; - ParsedStatement* _block; + const ParsedExpression* _condition; + const ParsedStatement* _block; // This can be either else if or else - ParsedStatement* _elseStatement; + const ParsedStatement* _elseStatement; public: ParsedConditionalStatement(ParsedExpression* condition, ParsedStatement* block, unsigned int start, unsigned int length) - : ParsedStatement(start, length){ - _condition = condition; - _block = block; - _elseStatement = nullptr; + : ParsedStatement(start, length), _condition(condition), _block(block), _elseStatement(nullptr) + { } ParsedConditionalStatement(ParsedExpression* condition, ParsedStatement* block, ParsedStatement* nextStatement, unsigned int start, unsigned int length) - : ParsedStatement(start, length){ + : ParsedStatement(start, length), _condition(condition), _block(block), _elseStatement(nextStatement) + { _condition = condition; _block = block; _elseStatement = nextStatement; @@ -221,19 +227,19 @@ public: delete _elseStatement; } - ParsedStatementKind GetKind() final{ + const ParsedStatementKind GetKind() const final{ return ParsedStatementKind ::Conditional; } - ParsedExpression* GetCondition(){ + const ParsedExpression* GetCondition() const { return _condition; } - ParsedStatement* GetBlock(){ + const ParsedStatement* GetBlock() const { return _block; } - ParsedStatement* GetElseStatement(){ + const ParsedStatement* GetElseStatement() const { return _elseStatement; } }; diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index c3829b9..a2b302a 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -1,3 +1,5 @@ +#include + #include #include "Parser.hpp" @@ -9,7 +11,7 @@ ParsedScriptStatement* Parser::Parse() { - vector statements; + vector statements; while (this->_position < this->_tokens.size()){ auto next = this -> Next(); if (next->GetKind() == TokenKind::EndOfFile){ @@ -73,8 +75,8 @@ ParsedStatement *Parser::ParseAssignment(IToken *current) { return new ParsedAssignmentStatement(isLocal, ((IdentifierToken*)identifier) -> Value, expression, start, expression->GetEndPosition() - start); } -ParsedStatement *Parser::ParseBlock(const vector& endTokens) { - vector statements; +ParsedStatement *Parser::ParseBlock(const vector& endTokens, const vector& openStatements) { + auto statements = openStatements; auto start = this->_position; while (this->_position < this->_tokens.size()){ auto next = this -> Next(); @@ -331,7 +333,7 @@ ParsedExpression *Parser::ParseParenthesizedExpression(IToken *current) { ParsedExpression *Parser::ParseFunctionCallExpression(ParsedExpression* functionExpression) { this -> Next(); // consume the open parenthesis - vector parameters; + vector parameters; auto peeked = this -> Peek(); auto peekedKind = peeked->GetKind(); if (peekedKind == TokenKind::CloseParenthesis){ @@ -379,7 +381,7 @@ ParsedExpression* Parser::ParseTableExpression(IToken* current){ if (firstItem->GetKind() == ParsedStatementKind::Expression && (this->Peek()->GetKind() == TokenKind::CommaToken )){ auto expr = ((ParsedExpressionStatement*)firstItem)->GetExpression(); - auto expressions = vector{expr}; + auto expressions = vector{expr}; auto n = this -> Next(); // consume the comma bool hasErrors = false; while (n->GetKind() != TokenKind::CloseCurlyBracket){ @@ -398,9 +400,7 @@ ParsedExpression* Parser::ParseTableExpression(IToken* current){ } // Otherwise we have a more complex table, which can be defined by a block else { - auto block = (ParsedBlockStatement*)this -> ParseBlock({TokenKind ::CloseCurlyBracket}); - auto statements = block->GetStatements(); - statements->insert(statements->begin(), firstItem); + auto block = (ParsedBlockStatement*)this -> ParseBlock({TokenKind ::CloseCurlyBracket}, {firstItem}); auto closeToken = this -> PeekAt(-1); return new ParsedTableExpression(block, start, closeToken->GetEndPosition() - start); } diff --git a/src/Parser/Parser.hpp b/src/Parser/Parser.hpp index 684dea7..bc42328 100644 --- a/src/Parser/Parser.hpp +++ b/src/Parser/Parser.hpp @@ -27,7 +27,7 @@ class Parser { ParsedStatement* ParseStatement(IToken* current); ParsedStatement* ParseAssignment(IToken* current); - ParsedStatement *ParseBlock(const vector& endTokens); + ParsedStatement *ParseBlock(const vector& endTokens, const vector& openStatements = {}); ParsedStatement* ParseFunctionDeclaration(IToken* current); ParsedStatement *ParseReturnStatement(IToken *current); ParsedStatement *ParseIfStatement(IToken *current); @@ -41,8 +41,8 @@ class Parser { ParsedExpression *ParseIndexExpression(ParsedExpression *indexingExpression); public: ParsedScriptStatement* Parse(); - explicit Parser(vector tokens, Script* scriptData){ - _tokens = std::move(tokens); + explicit Parser(const vector& tokens, Script* scriptData){ + _tokens = tokens; _position = 0; ScriptData = scriptData; } diff --git a/tests/parser/ParserTests.cpp b/tests/parser/ParserTests.cpp index ee9d785..97bfcfd 100644 --- a/tests/parser/ParserTests.cpp +++ b/tests/parser/ParserTests.cpp @@ -216,10 +216,10 @@ TEST_CASE( "Parse function declaration", "[parser]" ){ auto functionDeclaration = (ParsedFunctionDeclarationStatement*)firstStatement; REQUIRE(functionDeclaration->GetIdentifier() == HashedString("foo")); auto parameters = functionDeclaration->GetParameters(); - CHECK(parameters[0]->GetType() == HashedString("number")); - CHECK(parameters[0]->GetIdentifier() == HashedString("bar")); - CHECK(parameters[1]->GetType() == HashedString("number")); - CHECK(parameters[1]->GetIdentifier() == HashedString("par")); + CHECK(parameters -> at(0) ->GetType() == HashedString("number")); + CHECK(parameters -> at(0) ->GetIdentifier() == HashedString("bar")); + CHECK(parameters -> at(1) ->GetType() == HashedString("number")); + CHECK(parameters -> at(1) ->GetIdentifier() == HashedString("par")); }