From 2c1f210d3711170cff4ae7bfc5f55b648018e222 Mon Sep 17 00:00:00 2001 From: matteo Date: Sat, 17 Jan 2026 11:06:50 +0100 Subject: [PATCH] fix(lexer): some memory leaks + removed useless [at_begining] variable --- src/lexer/lexer.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 1df0ec5..cbb2b1b 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -12,7 +12,6 @@ static char *end_last_token; static ssize_t remaining_chars; -static bool at_beginning = true; static struct token *last_token; static struct token *current_token; @@ -35,17 +34,19 @@ static void update_last_token(struct token *tok) last_token = tok; } -/* @brief: saves state for the next call to the the lexer. - * this function is called by token_pop(). - * +/* @brief: updates the current position in the stream. + * [stream] += [i] + * Also saves the last token sent (so we can free it afterwards). + * Also sets the current token to NULL. + * This function is called by token_pop(). */ static void save_state(char *stream, ssize_t i, struct token *tok) { remaining_chars -= i; end_last_token = stream + i; - at_beginning = false; update_last_token(tok); + update_current_token(NULL); } /* @return: true if a special character from the grammar was found, @@ -204,10 +205,9 @@ char *stream_init(void) { char *stream; - if (at_beginning) + if (last_token == NULL) // at the begining { remaining_chars = stream_read(&stream); - // at_beginning = true; } else { @@ -223,8 +223,8 @@ char *stream_init(void) struct token *peek_token(void) { - // EOF looping mode - if (current_token != NULL && current_token->type == TOKEN_EOF) + // we already created the upcoming token during the previous call to peek() + if (current_token != NULL) { return current_token; }