Implemented some ast handlings...

ast lists, and_or, redirection and builtins
This commit is contained in:
Jean Herail 2026-01-17 17:33:22 +01:00 committed by william.valenduc
parent cf7825aaf0
commit e9b6d39760
10 changed files with 359 additions and 14 deletions

View file

@ -13,6 +13,10 @@ void ast_free(struct ast **node)
ast_free_command(ast_get_command(*node));
else if (ast_is_list(*node))
ast_free_list(ast_get_list(*node));
else if (ast_is_and_or(*node))
ast_free_and_or(ast_get_and_or(*node));
else if (ast_is_redir(*node))
ast_free_redir(ast_get_redir(*node));
free(*node);
*node = NULL;

View file

@ -4,6 +4,8 @@
#include "utils/ast/ast_base.h"
#include "utils/ast/ast_command.h"
#include "utils/ast/ast_if.h"
#include "utils/ast/ast_and_or.h"
#include "utils/ast/ast_redir.h"
#include "utils/ast/ast_list.h"
#include "utils/ast/ast_end.h"
#include "utils/ast/ast_void.h"

View file

@ -0,0 +1,35 @@
#include "utils/ast/ast_and_or.h"
#include <stdlib.h>
bool ast_is_and_or(struct ast *node)
{
return node != NULL && node->type == AST_AND_OR;
}
struct ast_and_or *ast_get_and_or(struct ast *node)
{
if (ast_is_and_or(node))
return (struct ast_and_or *)node->data;
return NULL;
}
struct ast *ast_create_and_or(struct ast *left, struct ast *right, enum ast_and_or_type type)
{
struct ast_and_or *and_or = malloc(sizeof(struct ast_and_or));
if (!and_or)
return NULL;
and_or->left = left;
and_or->right = right;
and_or->type = type;
return ast_create(AST_AND_OR, and_or);
}
void ast_free_and_or(struct ast_and_or *and_or)
{
if (!and_or)
return;
ast_free(&and_or->left);
ast_free(&and_or->right);
free(and_or);
}

View file

@ -0,0 +1,23 @@
#ifndef AST_AND_OR_H
#define AST_AND_OR_H
#include <stdbool.h>
#include "utils/ast/ast_base.h"
enum ast_and_or_type {
AST_AND_OR_TYPE_AND,
AST_AND_OR_TYPE_OR
};
struct ast_and_or {
struct ast *left;
struct ast *right;
enum ast_and_or_type type;
};
bool ast_is_and_or(struct ast *node);
struct ast_and_or *ast_get_and_or(struct ast *node);
struct ast *ast_create_and_or(struct ast *left, struct ast *right, enum ast_and_or_type type);
void ast_free_and_or(struct ast_and_or *and_or);
#endif /* ! AST_AND_OR_H */

View file

@ -8,6 +8,8 @@ enum ast_type
AST_END,
AST_LIST,
AST_IF,
AST_AND_OR,
AST_REDIR,
AST_VOID,
AST_CMD
};
@ -20,7 +22,9 @@ struct ast
* Data associated with this AST node. It can be one of the following:
* - NULL (AST_END)
* - struct ast_if* (AST_IF)
* - struct ast_cmd* (AST_CMD)
* - struct ast_command* (AST_CMD)
* - struct ast_and_or* (AST_AND_OR)
* - struct ast_redir* (AST_REDIR)
*/
void *data;
};

View file

@ -29,7 +29,7 @@ void ast_free_list(struct ast_list *ast_list)
if (ast_list == NULL)
return;
list_deep_destroy(ast_list->children);
ast_list_deep_destroy(ast_list->children);
free(ast_list);
}
@ -41,7 +41,8 @@ void ast_list_deep_destroy(struct list *l)
{
next_elt = elt->next;
ast_free(elt->data);
struct ast *node = (struct ast *)elt->data;
ast_free(&node);
free(elt);
elt = next_elt;
}

36
src/utils/ast/ast_redir.c Normal file
View file

@ -0,0 +1,36 @@
#include "utils/ast/ast_redir.h"
#include <stdlib.h>
bool ast_is_redir(struct ast *node)
{
return node != NULL && node->type == AST_REDIR;
}
struct ast_redir *ast_get_redir(struct ast *node)
{
if (ast_is_redir(node))
return (struct ast_redir *)node->data;
return NULL;
}
struct ast *ast_create_redir(struct ast *child, char *filename, int io_number, enum ast_redir_type type)
{
struct ast_redir *redir = malloc(sizeof(struct ast_redir));
if (!redir)
return NULL;
redir->child = child;
redir->filename = filename; // Takes ownership? Usually yes in simple ASTs, or dup. Let's assume pointer copy for now, but user must manage memory.
redir->io_number = io_number;
redir->type = type;
return ast_create(AST_REDIR, redir);
}
void ast_free_redir(struct ast_redir *redir)
{
if (!redir)
return;
ast_free(&redir->child);
free(redir->filename);
free(redir);
}

29
src/utils/ast/ast_redir.h Normal file
View file

@ -0,0 +1,29 @@
#ifndef AST_REDIR_H
#define AST_REDIR_H
#include <stdbool.h>
#include "utils/ast/ast_base.h"
enum ast_redir_type {
AST_REDIR_TYPE_LESS, // <
AST_REDIR_TYPE_GREAT, // >
AST_REDIR_TYPE_DLESS, // <<
AST_REDIR_TYPE_DGREAT, // >>
AST_REDIR_TYPE_LESSAND, // <&
AST_REDIR_TYPE_GREATAND, // >&
AST_REDIR_TYPE_CLOBBER // >|
};
struct ast_redir {
struct ast *child;
char *filename;
int io_number; // The FD being redirected (default -1 if not specified, implies 0 or 1 based on type)
enum ast_redir_type type;
};
bool ast_is_redir(struct ast *node);
struct ast_redir *ast_get_redir(struct ast *node);
struct ast *ast_create_redir(struct ast *child, char *filename, int io_number, enum ast_redir_type type);
void ast_free_redir(struct ast_redir *redir);
#endif /* ! AST_REDIR_H */