From 5eed5fa65f7a965c7af6d77602693ac2d66922b0 Mon Sep 17 00:00:00 2001 From: matteo Date: Sat, 31 Jan 2026 19:25:42 +0100 Subject: [PATCH] feat: ast_subshell --- src/utils/Makefile.am | 1 + src/utils/ast/ast.h | 1 + src/utils/ast/ast_base.h | 3 ++- src/utils/ast/ast_subshell.c | 31 +++++++++++++++++++++++++++++++ src/utils/ast/ast_subshell.h | 19 +++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/utils/ast/ast_subshell.c create mode 100644 src/utils/ast/ast_subshell.h diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index 17454d2..ddb862c 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -21,6 +21,7 @@ libutils_a_SOURCES = \ args/args.c \ vars/vars.c \ ast/ast_assignment.c \ + ast/ast_subshell.c \ ast/ast_function.c libutils_a_CPPFLAGS = -I$(top_srcdir)/src diff --git a/src/utils/ast/ast.h b/src/utils/ast/ast.h index 12abee5..2eac62f 100644 --- a/src/utils/ast/ast.h +++ b/src/utils/ast/ast.h @@ -15,5 +15,6 @@ #include "ast_redir.h" #include "ast_void.h" #include "ast_word.h" +#include "ast_subshell.h" #endif /* ! AST_H */ diff --git a/src/utils/ast/ast_base.h b/src/utils/ast/ast_base.h index e1c7b07..de7dcfa 100644 --- a/src/utils/ast/ast_base.h +++ b/src/utils/ast/ast_base.h @@ -18,7 +18,8 @@ enum ast_type AST_NEG, AST_LOOP, AST_ASSIGNMENT, - AST_FUNCTION + AST_FUNCTION, + AST_SUBSHELL }; struct ast diff --git a/src/utils/ast/ast_subshell.c b/src/utils/ast/ast_subshell.c new file mode 100644 index 0000000..36efd6f --- /dev/null +++ b/src/utils/ast/ast_subshell.c @@ -0,0 +1,31 @@ +#include "ast_subshell.h" + +bool ast_is_subshell(struct ast *node) +{ + return node != NULL && node->type == AST_SUBSHELL; +} + +struct ast_subshell *ast_get_subshell(struct ast *node) +{ + if (ast_is_subshell(node)) + return (struct ast_subshell *)node->data; + return NULL; +} + +struct ast *ast_create_subshell(struct ast *child) +{ + struct ast_subshell *subshell = calloc(1, sizeof(struct ast_subshell)); + if (subshell == NULL) + return NULL; + subshell->child = child; + + return ast_create(AST_SUBSHELL, subshell); +} + +void ast_free_subshell(struct ast_subshell *subshell) +{ + if (!subshell) + return; + ast_free(&subshell->child); + free(subshell); +} diff --git a/src/utils/ast/ast_subshell.h b/src/utils/ast/ast_subshell.h new file mode 100644 index 0000000..a4648ef --- /dev/null +++ b/src/utils/ast/ast_subshell.h @@ -0,0 +1,19 @@ +#ifndef AST_SUBSHELL_H +#define AST_SUBSHELL_H + +#include "ast_base.h" + +struct ast_subshell +{ + struct ast *child; +}; + +bool ast_is_subshell(struct ast *node); + +struct ast_subshell *ast_get_subshell(struct ast *node); + +struct ast *ast_create_subshell(struct ast *child); + +void ast_free_subshell(struct ast_subshell *subshell); + +#endif /* ! AST_SUBSHELL_H */ \ No newline at end of file