From 68d8a1fd1375f3d7964796d351cbb9fa0d379a53 Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Wed, 14 Jan 2026 20:53:57 +0100 Subject: [PATCH] refactor(ast): one file per ast_type -- UNSTABLE (2) --- src/utils/ast/ast.c | 53 ------------------------------------- src/utils/ast/ast_command.c | 29 ++++++++++++++++++++ src/utils/ast/ast_if.c | 32 ++++++++++++++++++++++ src/utils/ast/ast_void.c | 1 + 4 files changed, 62 insertions(+), 53 deletions(-) create mode 100644 src/utils/ast/ast_command.c create mode 100644 src/utils/ast/ast_if.c create mode 100644 src/utils/ast/ast_void.c diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index 40b42d7..ed931e1 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -1,34 +1,6 @@ #include "ast.h" -#include #include -#include - -bool ast_is_if(struct ast *node) -{ - assert(node != NULL); - return node->type == AST_IF; -} - -bool ast_is_cmd(struct ast *node) -{ - assert(node != NULL); - return node->type == AST_CMD; -} - -struct ast_if *ast_get_if(struct ast *node) -{ - assert(node != NULL); - assert(node->type == AST_IF); - return (struct ast_if *)node->data; -} - -struct ast_cmd *ast_get_cmd(struct ast *node) -{ - assert(node != NULL); - assert(node->type == AST_CMD); - return (struct ast_cmd *)node->data; -} static struct ast *ast_create(enum ast_type type, void *data) { @@ -41,28 +13,3 @@ static struct ast *ast_create(enum ast_type type, void *data) return node; } - -struct ast *ast_create_if(struct ast *condition, struct ast *then_clause, - struct ast *else_clause) -{ - struct ast_if *if_data = malloc(sizeof(struct ast_if)); - if (!if_data) - return NULL; - - if_data->condition = condition; - if_data->then_clause = then_clause; - if_data->else_clause = else_clause; - - return ast_create(AST_IF, if_data); -} - -struct ast *ast_create_cmd(struct list *cmd) -{ - struct ast_cmd *cmd_data = malloc(sizeof(struct ast_cmd)); - if (!cmd_data) - return NULL; - - cmd_data->cmd = cmd; - - return ast_create(AST_CMD, cmd_data); -} diff --git a/src/utils/ast/ast_command.c b/src/utils/ast/ast_command.c new file mode 100644 index 0000000..4e4af66 --- /dev/null +++ b/src/utils/ast/ast_command.c @@ -0,0 +1,29 @@ +#include "utils/ast/ast_command.h" + +#include +#include +#include + +struct ast *ast_create_cmd(struct list *cmd) +{ + struct ast_cmd *cmd_data = malloc(sizeof(struct ast_cmd)); + if (!cmd_data) + return NULL; + + cmd_data->cmd = cmd; + + return ast_create(AST_CMD, cmd_data); +} + +struct ast_cmd *ast_get_cmd(struct ast *node) +{ + assert(node != NULL); + assert(node->type == AST_CMD); + return (struct ast_cmd *)node->data; +} + +bool ast_is_cmd(struct ast *node) +{ + assert(node != NULL); + return node->type == AST_CMD; +} diff --git a/src/utils/ast/ast_if.c b/src/utils/ast/ast_if.c new file mode 100644 index 0000000..746fd9d --- /dev/null +++ b/src/utils/ast/ast_if.c @@ -0,0 +1,32 @@ +#include "utils/ast/ast_if.h" + +#include +#include +#include + +struct ast *ast_create_if(struct ast *condition, struct ast *then_clause, + struct ast *else_clause) +{ + struct ast_if *if_data = malloc(sizeof(struct ast_if)); + if (!if_data) + return NULL; + + if_data->condition = condition; + if_data->then_clause = then_clause; + if_data->else_clause = else_clause; + + return ast_create(AST_IF, if_data); +} + +struct ast_if *ast_get_if(struct ast *node) +{ + assert(node != NULL); + assert(node->type == AST_IF); + return (struct ast_if *)node->data; +} + +bool ast_is_if(struct ast *node) +{ + assert(node != NULL); + return node->type == AST_IF; +} diff --git a/src/utils/ast/ast_void.c b/src/utils/ast/ast_void.c new file mode 100644 index 0000000..3a2ae7b --- /dev/null +++ b/src/utils/ast/ast_void.c @@ -0,0 +1 @@ +#include "utils/ast/ast_void.h"