feat(parser): helper static function

This commit is contained in:
matteo 2026-01-28 19:16:48 +01:00
parent 967a78e1a4
commit 050a1909f0

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 * @return: NULL
*/ */
static void *err_simple_command(struct list *command_elements, 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); 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) struct ast *parse_if_rule(struct lexer_context *ctx)
{ {
// If keyword // If keyword
@ -311,38 +322,34 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
token = POP_TOKEN(); token = POP_TOKEN();
if (token->type != TOKEN_THEN) if (token->type != TOKEN_THEN)
{ {
ast_free(&condition_content);
perror("Expected the 'then' keyword but token has different type"); perror("Expected the 'then' keyword but token has different type");
return NULL; return err_if_rule(&condition_content, NULL, NULL);
} }
// Then content // Then content
struct ast *then_content = parse_compound_list(ctx); struct ast *then_content = parse_compound_list(ctx);
if (then_content == NULL) if (then_content == NULL)
{ {
ast_free(&condition_content); return err_if_rule(&condition_content, &then_content, NULL);
ast_free(&then_content);
return NULL;
} }
struct ast *else_content = NULL;
// Eventual else/elif clause(s) // Eventual else/elif clause(s)
struct ast *else_content = parse_else_clause(ctx); if (is_first(*token, RULE_ELSE_CLAUSE))
if (else_content == NULL)
{ {
ast_free(&condition_content); else_content = parse_else_clause(ctx);
ast_free(&then_content); if (else_content == NULL)
return NULL; {
return err_if_rule(&condition_content, &then_content, NULL);
}
} }
// Fi keyword // Fi keyword
token = POP_TOKEN(); token = POP_TOKEN();
if (token->type != TOKEN_FI) 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"); perror("Expected the 'fi' keyword but token has different type");
return NULL; return err_if_rule(&condition_content, &then_content, &else_content);
} }
// Result // Result
@ -350,11 +357,8 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
ast_create_if(condition_content, then_content, else_content); ast_create_if(condition_content, then_content, else_content);
if (result == NULL) 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)"); 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; return result;