fix(lexer): memory leaks

This commit is contained in:
matteo 2026-01-17 11:38:23 +01:00 committed by william.valenduc
parent 2c1f210d37
commit acbcb860a4

View file

@ -36,16 +36,16 @@ static void update_last_token(struct token *tok)
/* @brief: updates the current position in the stream. /* @brief: updates the current position in the stream.
* [stream] += [i] * [stream] += [i]
* Also saves the last token sent (so we can free it afterwards). * Also frees the last sent token, and sets it to current_token.
* Also sets the current token to NULL. * Current token is then set to NULL.
* This function is called by token_pop(). * This function is called by token_pop().
*/ */
static void save_state(char *stream, ssize_t i, struct token *tok) static void save_state(char *stream, ssize_t i)
{ {
remaining_chars -= i; remaining_chars -= i;
end_last_token = stream + i; end_last_token = stream + i;
update_last_token(tok); update_last_token(current_token);
update_current_token(NULL); update_current_token(NULL);
} }
@ -70,62 +70,62 @@ static void set_token_spechar(struct token *tok, char *begin, ssize_t size)
return; return;
switch (begin[0]) switch (begin[0])
{ {
case EOF: case EOF:
tok->type = TOKEN_EOF; tok->type = TOKEN_EOF;
break; break;
case ';': case ';':
tok->type = TOKEN_SEMICOLON; tok->type = TOKEN_SEMICOLON;
break; break;
case '\n': case '\n':
tok->type = TOKEN_NEWLINE; tok->type = TOKEN_NEWLINE;
break; break;
case '\'': case '\'':
tok->type = TOKEN_QUOTE; tok->type = TOKEN_QUOTE;
break; break;
case '"': case '"':
tok->type = TOKEN_DOUBLE_QUOTE; tok->type = TOKEN_DOUBLE_QUOTE;
break; break;
case '`': case '`':
tok->type = TOKEN_GRAVE; tok->type = TOKEN_GRAVE;
break; break;
case '#': case '#':
tok->type = TOKEN_COMMENT; tok->type = TOKEN_COMMENT;
break; break;
case '|': case '|':
tok->type = TOKEN_PIPE; tok->type = TOKEN_PIPE;
break; break;
case '&': case '&':
tok->type = TOKEN_AMPERSAND; tok->type = TOKEN_AMPERSAND;
break; break;
case '\\': case '\\':
tok->type = TOKEN_BACKSLASH; tok->type = TOKEN_BACKSLASH;
break; break;
case '$': case '$':
tok->type = TOKEN_DOLLAR; tok->type = TOKEN_DOLLAR;
break; break;
case '(': case '(':
tok->type = TOKEN_LEFT_PAREN; tok->type = TOKEN_LEFT_PAREN;
break; break;
case ')': case ')':
tok->type = TOKEN_RIGHT_PAREN; tok->type = TOKEN_RIGHT_PAREN;
break; break;
case '{': case '{':
tok->type = TOKEN_LEFT_BRACKET; tok->type = TOKEN_LEFT_BRACKET;
break; break;
case '}': case '}':
tok->type = TOKEN_RIGHT_BRACKET; tok->type = TOKEN_RIGHT_BRACKET;
break; break;
case '<': case '<':
tok->type = TOKEN_LESS; tok->type = TOKEN_LESS;
break; break;
case '>': case '>':
tok->type = TOKEN_GREATER; tok->type = TOKEN_GREATER;
break; break;
case '*': case '*':
tok->type = TOKEN_STAR; tok->type = TOKEN_STAR;
break; break;
default: default:
break; break;
} }
} }
@ -157,6 +157,10 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
tok->type = TOKEN_ELIF; tok->type = TOKEN_ELIF;
} }
// no keywords found.
if (tok->type == TOKEN_NULL)
return;
tok->data = calloc(size + 1, sizeof(char)); tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL) if (tok->data == NULL)
return; return;
@ -255,9 +259,11 @@ struct token *peek_token(void)
struct token *pop_token(void) struct token *pop_token(void)
{ {
if (last_token != NULL && last_token->type == TOKEN_EOF) if (current_token != NULL && current_token->type == TOKEN_EOF)
{ {
// we reached end of input, frees all the token still allocated.
free_token(&last_token); free_token(&last_token);
free_token(&current_token);
return NULL; return NULL;
} }
char *stream = stream_init(); char *stream = stream_init();
@ -279,8 +285,13 @@ struct token *pop_token(void)
i++; i++;
} }
struct token *tok = new_token(stream, i); // just in case peek() was not called before poping.
save_state(stream, i, tok); // (this should never happen)
if (current_token == NULL)
{
current_token = new_token(stream, i);
}
save_state(stream, i);
return tok; return last_token;
} }