42sh/src/utils/ast/ast_list.c

52 lines
1,013 B
C

#include "ast_list.h"
#include <stdio.h>
struct ast *ast_create_list(struct list *list)
{
struct ast_list *ast_list = malloc(sizeof(struct ast_list));
if (ast_list == NULL)
{
perror("Error: could not allocate more memory");
return NULL;
}
ast_list->children = list;
struct ast *result = ast_create(AST_LIST, ast_list);
if (result == NULL)
free(ast_list);
return result;
}
struct ast_list *ast_get_list(struct ast *node)
{
if (node == NULL || node->type != AST_LIST)
return NULL;
return node->data;
}
void ast_free_list(struct ast_list *ast_list)
{
if (ast_list == NULL)
return;
ast_list_deep_destroy(ast_list->children);
free(ast_list);
}
void ast_list_deep_destroy(struct list *l)
{
struct list *elt = l;
struct list *next_elt;
while (elt != NULL)
{
next_elt = elt->next;
struct ast *node = elt->data;
ast_free(&node);
free(elt);
elt = next_elt;
}
}