From 12c8212c05ceb9618a916379ccbc8fd0e9946557 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Fri, 6 Dec 2024 10:54:33 -0300 Subject: [PATCH] feat(modularization): modules for freeTokens and getTokenTypeName --- include/frontend/freeTokens.h | 8 ++++++++ src/frontend/freeTokens.c | 22 ++++++++++++++++++++++ src/frontend/lexer/CMakeLists.txt | 2 ++ src/frontend/lexer/lexer.test.c | 22 +--------------------- src/frontend/parser/parser.test.c | 22 +--------------------- 5 files changed, 34 insertions(+), 42 deletions(-) create mode 100644 include/frontend/freeTokens.h create mode 100644 src/frontend/freeTokens.c diff --git a/include/frontend/freeTokens.h b/include/frontend/freeTokens.h new file mode 100644 index 0000000..2a93e6f --- /dev/null +++ b/include/frontend/freeTokens.h @@ -0,0 +1,8 @@ +#ifndef FREE_TOKENS_H +#define FREE_TOKENS_H + +#include "frontend/lexer/core.h" + +void freeTokens(Token *tokens, int tokenCount); + +#endif // FREE_TOKENS_H \ No newline at end of file diff --git a/src/frontend/freeTokens.c b/src/frontend/freeTokens.c new file mode 100644 index 0000000..9b5e868 --- /dev/null +++ b/src/frontend/freeTokens.c @@ -0,0 +1,22 @@ +#include +#include "frontend/lexer/core.h" + +/** + * @brief Frees the memory allocated for an array of tokens. + * + * This function frees both the `lexeme` and `message` strings of each token, as well as the + * memory allocated for the array of tokens itself. It helps prevent memory leaks after token + * processing is complete. + * + * @param tokens The array of tokens to be freed. + * @param tokenCount The number of tokens in the array. + */ +void freeTokens(Token *tokens, int tokenCount) { + if (tokens != NULL) { + for (int i = 0; i < tokenCount; i++) { + free(tokens[i].lexeme); + free(tokens[i].message); + } + free(tokens); + } +} \ No newline at end of file diff --git a/src/frontend/lexer/CMakeLists.txt b/src/frontend/lexer/CMakeLists.txt index 60873c1..3d3ae23 100644 --- a/src/frontend/lexer/CMakeLists.txt +++ b/src/frontend/lexer/CMakeLists.txt @@ -2,6 +2,8 @@ add_library(lexer lexer.c lexer_error.c + ../getTokenTypeName.c + ../freeTokens.c ) # Include the directory containing header files diff --git a/src/frontend/lexer/lexer.test.c b/src/frontend/lexer/lexer.test.c index 08b2329..4c206a5 100644 --- a/src/frontend/lexer/lexer.test.c +++ b/src/frontend/lexer/lexer.test.c @@ -4,27 +4,7 @@ #include "frontend/lexer/freeTokens.h" #include "frontend/getTokenTypeName.h" #include "utils.h" - -/** - * @brief Frees the memory allocated for an array of tokens. - * - * This function frees both the `lexeme` and `message` strings of each token, as well as the - * memory allocated for the array of tokens itself. It helps prevent memory leaks after token - * processing is complete. - * - * @param tokens The array of tokens to be freed. - * @param tokenCount The number of tokens in the array. - */ -void freeTokens(Token *tokens, int tokenCount) { - if (tokens != NULL) { - for (int i = 0; i < tokenCount; i++) { - free(tokens[i].lexeme); - free(tokens[i].message); - } - free(tokens); - } -} - +#include "frontend/freeTokens.h" /** * @brief The main entry point of the program for lexical analysis. diff --git a/src/frontend/parser/parser.test.c b/src/frontend/parser/parser.test.c index c8430d3..be00cc1 100644 --- a/src/frontend/parser/parser.test.c +++ b/src/frontend/parser/parser.test.c @@ -7,27 +7,7 @@ #include "frontend/parser/core.h" #include "frontend/parser/printer/print_ast.h" #include "frontend/getTokenTypeName.h" - - -/** - * @brief Frees the memory allocated for an array of tokens. - * - * This function frees both the `lexeme` and `message` strings of each token, as well as the - * memory allocated for the array of tokens itself. It helps prevent memory leaks after token - * processing is complete. - * - * @param tokens The array of tokens to be freed. - * @param tokenCount The number of tokens in the array. - */ -void freeTokens(Token *tokens, int tokenCount) { - if (tokens != NULL) { - for (int i = 0; i < tokenCount; i++) { - free(tokens[i].lexeme); - free(tokens[i].message); - } - free(tokens); - } -} +#include "frontend/freeTokens.h" /** * @brief The entry point of the program for lexical analysis and parsing.