fix: includes with relative path and memory leaks

This commit is contained in:
Gu://em_ 2026-01-20 19:25:55 +01:00
parent f38e828c0a
commit 603def1597

View file

@ -8,13 +8,13 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "lexer/lexer.h" #include "../lexer/lexer.h"
#include "utils/ast/ast.h" #include "../utils/ast/ast.h"
// === Static functions // === Static functions
/* Returns true if c is a command terminator, false otherwise /* Returns true if c is a command terminator, false otherwise
*/ */
static bool isterminator(struct token *token) static bool isterminator(struct token *token)
{ {
if (token == NULL) if (token == NULL)
@ -22,12 +22,12 @@ static bool isterminator(struct token *token)
switch (token->type) switch (token->type)
{ {
case TOKEN_NEWLINE: case TOKEN_NEWLINE:
case TOKEN_SEMICOLON: case TOKEN_SEMICOLON:
case TOKEN_EOF: case TOKEN_EOF:
return true; return true;
default: default:
return false; return false;
} }
} }
@ -70,7 +70,7 @@ struct ast *parse_list(void)
current_node = parse_and_or(); current_node = parse_and_or();
if (current_node == NULL) if (current_node == NULL)
return NULL; return NULL;
list_append(result_list, current_node); result_list = list_append(result_list, current_node);
// Following and_or commands // Following and_or commands
token = PEEK_TOKEN(); token = PEEK_TOKEN();
@ -82,11 +82,11 @@ struct ast *parse_list(void)
current_node = parse_and_or(); current_node = parse_and_or();
if (current_node == NULL) if (current_node == NULL)
{ {
//TODO free list // TODO free list
// There must be a function for that // There must be a function for that
return NULL; return NULL;
} }
list_append(result_list, current_node); result_list = list_append(result_list, current_node);
token = PEEK_TOKEN(); token = PEEK_TOKEN();
} }
} }
@ -135,17 +135,37 @@ struct ast *parse_simple_command(void)
while (token->type == TOKEN_WORD) while (token->type == TOKEN_WORD)
{ {
token = POP_TOKEN(); token = pop_token();
char* word = strdup(token->data); if (token == NULL)
{
// TODO free
return NULL;
}
char *word = strdup(token->data);
if (word == NULL)
{
// TODO free
puts("Internal error: Couldn't copy token content (is memory full "
"?)");
return NULL;
}
command_elements = list_append(command_elements, word); command_elements = list_append(command_elements, word);
token = PEEK_TOKEN(); token = peek_token();
if (token == NULL)
{
// TODO free
return NULL;
}
} }
struct ast *result = ast_create_command(command_elements); struct ast *result = ast_create_command(command_elements);
if (result == NULL)
{
// TODO free
}
return result; return result;
} }
// TODO check compliance with the grammar
struct ast *parse_shell_command(void) struct ast *parse_shell_command(void)
{ {
return parse_if_rule(); return parse_if_rule();
@ -158,7 +178,7 @@ struct ast *parse_if_rule(void)
if (token->type != TOKEN_IF) if (token->type != TOKEN_IF)
{ {
puts("Internal error: expected a if rule but token has different " puts("Internal error: expected a if rule but token has different "
"type"); "type");
return NULL; return NULL;
} }
@ -267,7 +287,6 @@ struct ast *parse_compound_list(void)
token = POP_TOKEN(); token = POP_TOKEN();
} }
struct ast *result = ast_create_list(result_list); struct ast *result = ast_create_list(result_list);
return result; return result;
} }