Properly clear up memory of parsed results

This commit is contained in:
Deukhoofd 2019-05-21 18:36:31 +02:00
parent 1e5cd010a1
commit 2fe6f570ec
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 35 additions and 20 deletions

View File

@ -23,12 +23,13 @@ class ParsedExpression {
unsigned int _position;
unsigned int _length;
public:
virtual ParsedExpressionKind GetKind() = 0;
ParsedExpression(unsigned int position, unsigned int length){
_position = position;
_length = length;
}
virtual ~ParsedExpression() = default;
virtual ParsedExpressionKind GetKind() = 0;
unsigned int GetStartPosition(){
return _position;
@ -100,6 +101,10 @@ public:
class ParenthesizedExpression : public ParsedExpression{
ParsedExpression* _expression;
public:
~ParenthesizedExpression() override {
delete _expression;
}
ParsedExpressionKind GetKind() final{
return ParsedExpressionKind::Parenthesized;
}
@ -118,6 +123,10 @@ class UnaryExpression : public ParsedExpression{
UnaryOperatorKind _kind;
ParsedExpression* _operand;
public:
~UnaryExpression() override {
delete _operand;
}
ParsedExpressionKind GetKind() final{
return ParsedExpressionKind::Unary;
}
@ -142,6 +151,11 @@ class BinaryExpression : public ParsedExpression{
ParsedExpression* _left;
ParsedExpression* _right;
public:
~BinaryExpression() override {
delete _left;
delete _right;
}
ParsedExpressionKind GetKind() final{
return ParsedExpressionKind::Binary;
}

View File

@ -1,16 +0,0 @@
#ifndef PORYGONLANG_PARSEDSCRIPTSTATEMENT_HPP
#define PORYGONLANG_PARSEDSCRIPTSTATEMENT_HPP
#include "ParsedStatement.hpp"
class ParsedScriptStatement : public ParsedBlockStatement{
public:
explicit ParsedScriptStatement(vector<ParsedStatement*> statements) : ParsedBlockStatement(move(statements)){}
ParsedStatementKind GetKind() final{
return ParsedStatementKind ::Script;
}
};
#endif //PORYGONLANG_PARSEDSCRIPTSTATEMENT_HPP

View File

@ -24,6 +24,7 @@ public:
_start = start;
_length = length;
}
virtual ~ParsedStatement() = default;
unsigned int GetStartPosition(){
return _start;
@ -41,6 +42,12 @@ public:
: ParsedStatement(statements.front()->GetStartPosition(), statements.back()->GetEndPosition() - statements.front()->GetStartPosition()){
_statements = std::move(statements);
}
~ParsedBlockStatement() override {
for (auto s: _statements){
delete s;
}
_statements.clear();
}
ParsedStatementKind GetKind() override{
return ParsedStatementKind ::Block;
@ -51,12 +58,24 @@ public:
}
};
class ParsedScriptStatement : public ParsedBlockStatement{
public:
explicit ParsedScriptStatement(vector<ParsedStatement*> statements) : ParsedBlockStatement(move(statements)){}
ParsedStatementKind GetKind() final{
return ParsedStatementKind ::Script;
}
};
class ParsedExpressionStatement : public ParsedStatement{
ParsedExpression* _expression;
public:
explicit ParsedExpressionStatement(ParsedExpression* expression) : ParsedStatement(expression->GetStartPosition(), expression->GetLength()){
_expression = expression;
}
~ParsedExpressionStatement() override {
delete _expression;
}
ParsedStatementKind GetKind() final{
return ParsedStatementKind ::Expression;

View File

@ -2,7 +2,6 @@
#include "Parser.hpp"
#include "UnaryOperatorKind.hpp"
#include "BinaryOperatorKind.hpp"
#include "ParsedScriptStatement.hpp"
ParsedScriptStatement* Parser::Parse() {

View File

@ -6,7 +6,6 @@
#include "ParsedStatements/ParsedStatement.hpp"
#include "../Script.hpp"
#include "ParsedScriptStatement.hpp"
enum class OperatorPrecedence {
No,