Skip to content

Commit

Permalink
feat(modularization): modules for freeTokens and getTokenTypeName
Browse files Browse the repository at this point in the history
  • Loading branch information
wesuRage committed Dec 6, 2024
1 parent e9f3bee commit 12c8212
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 42 deletions.
8 changes: 8 additions & 0 deletions include/frontend/freeTokens.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions src/frontend/freeTokens.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdlib.h>
#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);
}
}
2 changes: 2 additions & 0 deletions src/frontend/lexer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
add_library(lexer
lexer.c
lexer_error.c
../getTokenTypeName.c
../freeTokens.c
)

# Include the directory containing header files
Expand Down
22 changes: 1 addition & 21 deletions src/frontend/lexer/lexer.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 1 addition & 21 deletions src/frontend/parser/parser.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 12c8212

Please sign in to comment.