fix: merge local changes

This commit is contained in:
Gu://em_ 2026-01-30 20:58:15 +01:00
commit f721e6db72
19 changed files with 568 additions and 104 deletions

View file

@ -89,9 +89,21 @@ static bool init_firsts_map(void)
return true;
}
static void add_first_redir(void)
{
add_first(RULE_REDIRECTION, TOKEN_IONUMBER);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_DOUBLE_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_PIPE);
}
// === Functions
bool grammar_init(void)
int grammar_init(void)
{
// Initialize the firsts map
bool success = init_firsts_map();
@ -128,15 +140,9 @@ bool grammar_init(void)
add_firsts(RULE_CASE_CLAUSE, first(RULE_CASE_ITEM));
// Redirection
add_first(RULE_REDIRECTION, TOKEN_IONUMBER);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_DOUBLE_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_PIPE);
add_first_redir();
// %RIP Matteo 30/01/2026
// %RAX Guillem 30/01/2026 hehe
// Element
add_first(RULE_ELEMENT, TOKEN_WORD);
@ -153,6 +159,7 @@ bool grammar_init(void)
// Simple command
add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX));
add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD);
add_first(RULE_SIMPLE_COMMAND, TOKEN_EXPORT);
// Funcdec
add_first(RULE_FUNCDEC, TOKEN_WORD);

View file

@ -64,7 +64,7 @@ struct firsts_list
* @return PARSER_INIT_SUCCESS on success PARSER_INIT_ERROR on error
* @warning Do not use outside the parser
*/
bool grammar_init(void);
int grammar_init(void);
/*
* @brief Closes the grammar submodule

View file

@ -138,7 +138,7 @@ struct ast *parse_prefix(struct lexer_context *ctx)
if (token->type == TOKEN_ASSIGNMENT_WORD)
{
token = POP_TOKEN();
return ast_create_assignment(token->data);
return ast_create_assignment(token->data, false);
}
else if (is_first(*token, RULE_REDIRECTION))
return parse_redirection(ctx);

View file

@ -26,17 +26,6 @@ static enum ast_and_or_type and_or_tok_to_ast(enum token_type tok_type)
}
}
/* @brief: frees command_elements and redirections lists (helper func)
* @return: NULL
*/
static void *err_simple_command(struct list *command_elements,
struct list *redirections)
{
list_deep_destroy(command_elements);
list_deep_destroy(redirections);
return NULL;
}
/* @brief: frees all the arguments. (helper func)
* @return: NULL.
*/
@ -49,6 +38,46 @@ static void *err_if_rule(struct ast **cond, struct ast **then_clause,
return NULL;
}
/* @brief: frees command_elements and redirections lists (helper func)
* @return: NULL
*/
static void *err_s_com(struct list *command_elements, struct list *redirections,
struct list *assignments)
{
list_deep_destroy(command_elements);
list_deep_destroy(redirections);
list_deep_destroy(assignments);
return NULL;
}
/* @brief: used when export keyword is found, and expects an assignment after.
* @return: an ast_assignment with the field [global] set to true.
*/
static struct ast *parse_export(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
if (token->type != TOKEN_EXPORT)
{
fprintf(stderr, "expected the export keyword in parse_export");
return NULL;
}
// export
POP_TOKEN();
token = PEEK_TOKEN();
if (token->type != TOKEN_ASSIGNMENT_WORD)
{
fprintf(stderr, "in parser: export must be followed by 'x=y'");
return NULL;
}
// assignment
POP_TOKEN();
return ast_create_assignment(token->data, true);
}
// === Functions
struct ast *parse_list(struct lexer_context *ctx)
@ -224,7 +253,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
struct ast *prefix = parse_prefix(ctx);
if (prefix == NULL)
{
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
if (prefix->type == AST_ASSIGNMENT)
{
@ -240,66 +269,70 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
if (token->type != TOKEN_WORD)
{
if (!has_prefix)
if (!has_prefix && token->type != TOKEN_EXPORT)
{
perror("Expected a command but got a different token type");
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
if (token->type == TOKEN_EXPORT)
{
struct ast *assignment_export = parse_export(ctx);
if (assignment_export == NULL)
return err_s_com(command_elements, redirections, assignments);
assignments = list_append(assignments, assignment_export);
}
// else : only prefixes
}
else
else // TOKEN WORD
{
if (token->type == TOKEN_WORD)
char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
POP_TOKEN();
}
token = PEEK_TOKEN();
// Eventual elements
while (is_first(*token, RULE_ELEMENT))
{
// Get element
struct ast *element = parse_element(ctx);
if (element == NULL)
{
char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
POP_TOKEN();
token = PEEK_TOKEN();
return err_s_com(command_elements, redirections, assignments);
}
// Eventual elements
while (is_first(*token, RULE_ELEMENT))
// Get element type
if (ast_is_word(element))
{
// Get element
struct ast *element = parse_element(ctx);
if (element == NULL)
{
return err_simple_command(command_elements, redirections);
}
struct ast_word *element_word = ast_get_word(element);
// Get element type
if (ast_is_word(element))
{
struct ast_word *element_word = ast_get_word(element);
// TODO test this fix for the memory leaks
char *word = strdup(element_word->word);
ast_free(&element);
command_elements = list_append(command_elements, word);
// end of fix
}
else if (ast_is_redir(element))
{
// append redirections to the list of redirections
redirections = list_append(redirections, element);
}
else
{
perror("Internal error: unexpected return value from "
"parse_element in parse_simple_command");
return err_simple_command(command_elements, redirections);
}
// Forward
token = PEEK_TOKEN();
// TODO test this fix for the memory leaks
char *word = strdup(element_word->word);
ast_free(&element);
command_elements = list_append(command_elements, word);
// end of fix
}
else if (ast_is_redir(element))
{
// append redirections to the list of redirections
redirections = list_append(redirections, element);
}
else
{
perror("Internal error: unexpected return value from "
"parse_element in parse_simple_command");
return err_s_com(command_elements, redirections, assignments);
}
// Forward
token = PEEK_TOKEN();
}
struct ast *result =
ast_create_command(command_elements, redirections, assignments);
if (result == NULL)
{
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
return result;
}
@ -334,6 +367,10 @@ struct ast *parse_element(struct lexer_context *ctx)
{
return parse_redirection(ctx);
}
else if (token->type == TOKEN_EXPORT)
{
return parse_export(ctx);
}
else
{
perror("Syntax error: unexpected token at parse_element");
@ -531,12 +568,11 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
perror(
"Expected the 'then' keyword but got a different token type");
perror("Expected the 'then' keyword but got a different token "
"type");
return NULL;
}
// Then clause
struct ast *then_content = parse_compound_list(ctx);
if (then_content == NULL)
{

View file

@ -10,12 +10,12 @@ static enum parser_state state = PARSER_STATE_NOT_INITIALIZED;
// === Functions
bool parser_init(void)
int parser_init(void)
{
if (state == PARSER_STATE_READY)
{
perror("Internal error: tried to initialize the parser module twice.");
return NULL;
return false;
}
int success = grammar_init();
if (success == false)

View file

@ -18,7 +18,7 @@ enum parser_state
*
* @return Returns false on error and true on success
*/
bool parser_init(void);
int parser_init(void);
/* @brief Closes the parser module after use
*/