diff --git a/include/ast.h b/include/ast.h index 4ec0082..65aa911 100644 --- a/include/ast.h +++ b/include/ast.h @@ -51,6 +51,10 @@ struct BinaryExpression final : public AbstractSyntaxTree { AbstractSyntaxTree *right{}; Token op; BinaryExpression(AbstractSyntaxTree *left, AbstractSyntaxTree *right, Token op); + ~BinaryExpression() override { + delete left; + delete right; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -63,6 +67,9 @@ struct UnaryExpression final : public AbstractSyntaxTree { AbstractSyntaxTree *expression; Token op; UnaryExpression(AbstractSyntaxTree *expression, Token op, Side side); + ~UnaryExpression() override { + delete expression; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -74,6 +81,10 @@ struct Declaration final : public AbstractSyntaxTree { Declaration(AbstractSyntaxTree *type, Node identifier, AbstractSyntaxTree *value); Declaration(AbstractSyntaxTree *type, Node identifier); Declaration(Node identifier, AbstractSyntaxTree *value); + ~Declaration() override { + if (type.has_value()) delete type.value(); + if (value.has_value()) delete value.value(); + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -81,6 +92,10 @@ struct Declaration final : public AbstractSyntaxTree { struct ScopedBody : public AbstractSyntaxTree { std::vector body; explicit ScopedBody(const std::vector &body); + ~ScopedBody() override { + for (auto &node : body) + delete node; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -89,12 +104,20 @@ struct FunctionDeclaration : public AbstractSyntaxTree { AbstractSyntaxTree *returnType; std::vector arguments; FunctionDeclaration(AbstractSyntaxTree *returnType, const std::vector &arguments); + ~FunctionDeclaration() override { + delete returnType; + for (auto &arg : arguments) + delete arg; + } bool operator==(const AbstractSyntaxTree &other) const override; }; struct ReturnStatement : public AbstractSyntaxTree { AbstractSyntaxTree *expression; explicit ReturnStatement(AbstractSyntaxTree *expression); + ~ReturnStatement() override { + delete expression; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -103,6 +126,10 @@ struct TypeCast : public AbstractSyntaxTree { AbstractSyntaxTree *expression; AbstractSyntaxTree *type; TypeCast(AbstractSyntaxTree *expression, AbstractSyntaxTree *type); + ~TypeCast() override { + delete expression; + delete type; + } bool operator==(const AbstractSyntaxTree &other) const override; }; @@ -110,6 +137,10 @@ struct FunctionCall : public AbstractSyntaxTree { Node identifier; std::vector arguments; FunctionCall(Node identifier, const std::vector &arguments); + ~FunctionCall() override { + for (auto &arg : arguments) + delete arg; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -120,6 +151,11 @@ struct IfStatement : public AbstractSyntaxTree { std::optional elseBody; IfStatement(AbstractSyntaxTree *condition, AbstractSyntaxTree *thenBody, AbstractSyntaxTree *elseBody); IfStatement(AbstractSyntaxTree *condition, AbstractSyntaxTree *thenBody); + ~IfStatement() override { + delete condition; + delete thenBody; + if (elseBody.has_value()) delete elseBody.value(); + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -128,6 +164,10 @@ struct WhileStatement : public AbstractSyntaxTree { AbstractSyntaxTree *condition; AbstractSyntaxTree *body; WhileStatement(AbstractSyntaxTree *condition, AbstractSyntaxTree *body); + ~WhileStatement() override { + delete condition; + delete body; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -138,6 +178,12 @@ struct ForLoop : public AbstractSyntaxTree { AbstractSyntaxTree *step; AbstractSyntaxTree *body; ForLoop(AbstractSyntaxTree *initialization, AbstractSyntaxTree *condition, AbstractSyntaxTree *step, AbstractSyntaxTree *body); + ~ForLoop() override { + delete initialization; + delete condition; + delete step; + delete body; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -145,6 +191,10 @@ struct ForLoop : public AbstractSyntaxTree { struct List : public AbstractSyntaxTree { std::vector elements; explicit List(const std::vector &elements); + ~List() override { + for (auto &element : elements) + delete element; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -152,6 +202,9 @@ struct List : public AbstractSyntaxTree { struct ArrayType : public AbstractSyntaxTree { AbstractSyntaxTree *type; explicit ArrayType(AbstractSyntaxTree *type); + ~ArrayType() override { + delete type; + } bool operator==(const AbstractSyntaxTree &other) const override; }; @@ -159,6 +212,9 @@ struct ArrayAccess : public AbstractSyntaxTree { Node identifier; AbstractSyntaxTree *index; ArrayAccess(Node identifier, AbstractSyntaxTree *index); + ~ArrayAccess() override { + delete index; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -173,6 +229,9 @@ struct ImportStatement : public AbstractSyntaxTree { struct ExportStatement : public AbstractSyntaxTree { AbstractSyntaxTree *stm; explicit ExportStatement(AbstractSyntaxTree *stm); + ~ExportStatement() override { + delete stm; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; @@ -182,6 +241,11 @@ struct TernaryExpression : public AbstractSyntaxTree { AbstractSyntaxTree *thenCase; AbstractSyntaxTree *elseCase; TernaryExpression(AbstractSyntaxTree *condition, AbstractSyntaxTree *thenCase, AbstractSyntaxTree *elseCase); + ~TernaryExpression() override { + delete condition; + delete thenCase; + delete elseCase; + } bool operator==(const AbstractSyntaxTree &other) const override; void compile(Program &program, Segment &segment) const override; }; diff --git a/src/ast.cpp b/src/ast.cpp index 32cba40..749c28d 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -721,6 +721,7 @@ void compile(Program &program, const char *input) { } for (auto &node: ast) { node->compile(program, program.segments[0]); + delete node; } program.segments.front().instructions.push_back( Instruction{ diff --git a/tests/parser_tests.cpp b/tests/parser_tests.cpp index 369f2fe..db51151 100644 --- a/tests/parser_tests.cpp +++ b/tests/parser_tests.cpp @@ -4,6 +4,11 @@ #include "ast.h" #include "parser.h" +static void deleteAST(std::vector &ast) { + for (auto &node : ast) + delete node; +} + TEST(ParserTests, SimpleBinaryExpression) { const char *input = "1 + 2;"; auto expectedResult = std::vector{ @@ -17,6 +22,8 @@ TEST(ParserTests, SimpleBinaryExpression) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, CompoundBinaryExpression) { @@ -35,6 +42,8 @@ TEST(ParserTests, CompoundBinaryExpression) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, ParseStrings) { @@ -49,6 +58,8 @@ TEST(ParserTests, ParseStrings) { ASSERT_EQ(expectedResult.size(), actualResult.size()); ASSERT_EQ(*expectedResult[0], *actualResult[0]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, MultipleExpressions) { @@ -72,6 +83,8 @@ TEST(ParserTests, MultipleExpressions) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, OperationsPriority) { @@ -90,6 +103,8 @@ TEST(ParserTests, OperationsPriority) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, Declaration) { @@ -104,6 +119,8 @@ TEST(ParserTests, Declaration) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, DeclarationWithInitialization) { @@ -119,6 +136,8 @@ TEST(ParserTests, DeclarationWithInitialization) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, DeclarationWithAutoTypeDeduction) { @@ -133,6 +152,8 @@ TEST(ParserTests, DeclarationWithAutoTypeDeduction) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, VariableAssignment) { @@ -148,6 +169,8 @@ TEST(ParserTests, VariableAssignment) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, FunctionDeclaration) { @@ -169,6 +192,8 @@ TEST(ParserTests, FunctionDeclaration) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, FunctionDeclarationWithBody) { @@ -200,6 +225,8 @@ TEST(ParserTests, FunctionDeclarationWithBody) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, FunctionDeclarationWithBodyWithMultipleExpressions) { @@ -236,6 +263,8 @@ TEST(ParserTests, FunctionDeclarationWithBodyWithMultipleExpressions) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, FunctionDeclarationWithAutoTypeDeduction) { @@ -268,6 +297,8 @@ TEST(ParserTests, FunctionDeclarationWithAutoTypeDeduction) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, FunctionCall) { @@ -282,6 +313,8 @@ TEST(ParserTests, FunctionCall) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, FunctionCallWithArgument) { @@ -298,6 +331,8 @@ TEST(ParserTests, FunctionCallWithArgument) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, FunctionCallWithMultipleArguments) { @@ -315,6 +350,8 @@ TEST(ParserTests, FunctionCallWithMultipleArguments) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, IfStatement) { @@ -333,6 +370,8 @@ TEST(ParserTests, IfStatement) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, IfElseStatement) { @@ -356,6 +395,8 @@ TEST(ParserTests, IfElseStatement) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, WhileStatement) { @@ -374,6 +415,8 @@ TEST(ParserTests, WhileStatement) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, ForLoop) { @@ -403,6 +446,8 @@ TEST(ParserTests, ForLoop) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, UnaryExpression) { @@ -433,6 +478,8 @@ TEST(ParserTests, UnaryExpression) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, List) { @@ -449,6 +496,8 @@ TEST(ParserTests, List) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, ArrayDeclaration) { @@ -466,6 +515,8 @@ TEST(ParserTests, ArrayDeclaration) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, ArrayAccess) { @@ -480,6 +531,8 @@ TEST(ParserTests, ArrayAccess) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, NestedScopes) { @@ -534,6 +587,8 @@ TEST(ParserTests, NestedScopes) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, ImportStatement) { @@ -546,6 +601,8 @@ TEST(ParserTests, ImportStatement) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, ExportStatement) { @@ -567,6 +624,8 @@ TEST(ParserTests, ExportStatement) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); } TEST(ParserTests, TurnaryExpressions) { @@ -586,4 +645,6 @@ TEST(ParserTests, TurnaryExpressions) { ASSERT_EQ(expectedResult.size(), actualResult.size()); for (int i = 0; i < expectedResult.size(); i++) ASSERT_EQ(*expectedResult[i], *actualResult[i]); + deleteAST(actualResult); + deleteAST(expectedResult); }