feat(parser): helper static function

This commit is contained in:
matteo 2026-01-28 19:16:48 +01:00
parent 0db50e28de
commit c40e5c2d0f

View file

@ -169,7 +169,7 @@ struct ast *parse_command(struct lexer_context *ctx)
}
}
/* @brief: frees command_elements and redirections lists
/* @brief: frees command_elements and redirections lists (helper func)
* @return: NULL
*/
static void *err_simple_command(struct list *command_elements,
@ -293,6 +293,17 @@ struct ast *parse_shell_command(struct lexer_context *ctx)
return parse_if_rule(ctx);
}
/* @brief: frees all the arguments. (helper func)
* @return: NULL.
*/
static void *err_if_rule(struct ast **cond, struct ast **then_clause, struct ast **else_clause)
{
ast_free(cond);
ast_free(then_clause);
ast_free(else_clause);
return NULL;
}
struct ast *parse_if_rule(struct lexer_context *ctx)
{
// If keyword
@ -311,38 +322,34 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
ast_free(&condition_content);
perror("Expected the 'then' keyword but token has different type");
return NULL;
return err_if_rule(&condition_content, NULL, NULL);
}
// Then content
struct ast *then_content = parse_compound_list(ctx);
if (then_content == NULL)
{
ast_free(&condition_content);
ast_free(&then_content);
return NULL;
return err_if_rule(&condition_content, &then_content, NULL);
}
struct ast *else_content = NULL;
// Eventual else/elif clause(s)
struct ast *else_content = parse_else_clause(ctx);
if (else_content == NULL)
if (is_first(*token, RULE_ELSE_CLAUSE))
{
ast_free(&condition_content);
ast_free(&then_content);
return NULL;
else_content = parse_else_clause(ctx);
if (else_content == NULL)
{
return err_if_rule(&condition_content, &then_content, NULL);
}
}
// Fi keyword
token = POP_TOKEN();
if (token->type != TOKEN_FI)
{
ast_free(&condition_content);
ast_free(&then_content);
ast_free(&else_content);
perror("Expected the 'fi' keyword but token has different type");
return NULL;
return err_if_rule(&condition_content, &then_content, &else_content);
}
// Result
@ -350,11 +357,8 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
ast_create_if(condition_content, then_content, else_content);
if (result == NULL)
{
ast_free(&condition_content);
ast_free(&then_content);
ast_free(&else_content);
perror("Internal error: could not create a new AST (AST_IF)");
return NULL;
return err_if_rule(&condition_content, &then_content, &else_content);
}
return result;