fix: merging all the code
This commit is contained in:
parent
2e9e98d343
commit
1eecb1bd42
12 changed files with 86 additions and 61 deletions
|
|
@ -2,25 +2,26 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
void ast_free(struct ast *node)
|
||||
void ast_free(struct ast **node)
|
||||
{
|
||||
if (node == NULL)
|
||||
if (*node == NULL)
|
||||
return;
|
||||
// ast void does not need to be freed.
|
||||
if (ast_is_if(node))
|
||||
ast_free_if(ast_get_if(node));
|
||||
else if (ast_is_command(node))
|
||||
ast_free_command(ast_get_command(node));
|
||||
else if (ast_is_list(node))
|
||||
ast_free_list(ast_get_list(node));
|
||||
if (ast_is_if(*node))
|
||||
ast_free_if(ast_get_if(*node));
|
||||
else if (ast_is_command(*node))
|
||||
ast_free_command(ast_get_command(*node));
|
||||
else if (ast_is_list(*node))
|
||||
ast_free_list(ast_get_list(*node));
|
||||
|
||||
free(node);
|
||||
free(*node);
|
||||
*node = NULL;
|
||||
}
|
||||
|
||||
struct ast *ast_create(enum ast_type type, void *data)
|
||||
{
|
||||
struct ast *node = malloc(sizeof(struct ast));
|
||||
if (!node)
|
||||
if (node == NULL)
|
||||
return NULL;
|
||||
|
||||
node->type = type;
|
||||
|
|
|
|||
|
|
@ -7,15 +7,4 @@
|
|||
#include "utils/ast/ast_list.h"
|
||||
#include "utils/ast/ast_void.h"
|
||||
|
||||
/* @brief: returns an ast* with corresponding data and type.
|
||||
*
|
||||
* @note: this function should only be called by ast_create_[TYPE] functions.
|
||||
*/
|
||||
struct ast *ast_create(enum ast_type type, void *data);
|
||||
|
||||
/* @brief: frees the given ast. If ast is NULL, does nothing.
|
||||
*
|
||||
*/
|
||||
void ast_free(struct ast *node);
|
||||
|
||||
#endif /* ! AST_H */
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ enum ast_type
|
|||
AST_END,
|
||||
AST_LIST,
|
||||
AST_IF,
|
||||
AST_VOID,
|
||||
AST_CMD
|
||||
};
|
||||
|
||||
|
|
@ -24,4 +25,15 @@ struct ast
|
|||
void *data;
|
||||
};
|
||||
|
||||
/* @brief: returns an ast* with corresponding data and type.
|
||||
*
|
||||
* @note: this function should only be called by ast_create_[TYPE] functions.
|
||||
*/
|
||||
struct ast *ast_create(enum ast_type type, void *data);
|
||||
|
||||
/* @brief: frees the given ast. If ast is NULL, does nothing.
|
||||
*
|
||||
*/
|
||||
void ast_free(struct ast **node);
|
||||
|
||||
#endif /* ! AST_BASE_H */
|
||||
|
|
|
|||
|
|
@ -30,9 +30,10 @@ bool ast_is_command(struct ast *node)
|
|||
return node->type == AST_CMD;
|
||||
}
|
||||
|
||||
void ast_free_command(struct ast_command **command_data)
|
||||
void ast_free_command(struct ast_command *command_data)
|
||||
{
|
||||
list_deep_destroy((*command_data)->command);
|
||||
free(*command_data);
|
||||
*command_data = NULL;
|
||||
if (command_data == NULL)
|
||||
return;
|
||||
list_deep_destroy(command_data->command);
|
||||
free(command_data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,6 @@ struct ast *ast_create_command(struct list *command);
|
|||
/*
|
||||
* @brief: frees the given ast_command and sets the pointer to NULL.
|
||||
*/
|
||||
void ast_free_command(struct ast_command **command_data);
|
||||
void ast_free_command(struct ast_command *command_data);
|
||||
|
||||
#endif /* ! AST_COMMAND_H */
|
||||
|
|
|
|||
|
|
@ -31,15 +31,14 @@ bool ast_is_if(struct ast *node)
|
|||
return node->type == AST_IF;
|
||||
}
|
||||
|
||||
void ast_free_if(struct ast_if **if_data)
|
||||
void ast_free_if(struct ast_if *if_data)
|
||||
{
|
||||
if (if_data == NULL || *if_data == NULL)
|
||||
if (if_data == NULL)
|
||||
return;
|
||||
|
||||
ast_free((*if_data)->condition);
|
||||
ast_free((*if_data)->then_clause);
|
||||
ast_free((*if_data)->else_clause);
|
||||
ast_free(&if_data->condition);
|
||||
ast_free(&if_data->then_clause);
|
||||
ast_free(&if_data->else_clause);
|
||||
|
||||
free(*if_data);
|
||||
*if_data = NULL;
|
||||
free(if_data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,6 @@ struct ast *ast_create_if(struct ast *condition, struct ast *then_clause,
|
|||
/*
|
||||
* @brief: frees the given ast_if and sets the pointer to NULL.
|
||||
*/
|
||||
void ast_free_if(struct ast_if **if_data);
|
||||
void ast_free_if(struct ast_if *if_data);
|
||||
|
||||
#endif /* ! AST_IF_H */
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
#include "utils/ast/ast.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
struct ast *ast_create_list(struct list *list)
|
||||
{
|
||||
struct ast_list *ast_list = malloc(sizeof(struct ast_list));
|
||||
if (ast_list == NULL)
|
||||
return NULL;
|
||||
|
||||
ast_list->list = list;
|
||||
ast_list->children = list;
|
||||
|
||||
return ast_create(AST_LIST, ast_list);
|
||||
}
|
||||
|
|
@ -22,12 +24,25 @@ bool ast_is_list(struct ast *node)
|
|||
return node->type == AST_LIST;
|
||||
}
|
||||
|
||||
void ast_free_list(struct ast_list **ast_list)
|
||||
void ast_free_list(struct ast_list *ast_list)
|
||||
{
|
||||
if (*ast_list == NULL || ast_list == NULL)
|
||||
if (ast_list == NULL)
|
||||
return;
|
||||
|
||||
list_deep_destroy((*ast_list)->children);
|
||||
free(*ast_list);
|
||||
*ast_list = NULL;
|
||||
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;
|
||||
|
||||
ast_free(elt->data);
|
||||
free(elt);
|
||||
elt = next_elt;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,14 @@ struct ast_list
|
|||
struct list *children; // A list of ASTs (ast*)
|
||||
};
|
||||
|
||||
/*
|
||||
** Release the memory used by the AST list and its AST children
|
||||
** Does nothing if `list` is `NULL`.
|
||||
*
|
||||
* @warning: this function should NEVER be used on a list containing
|
||||
* anything else than ASTs.
|
||||
*/
|
||||
void ast_list_deep_destroy(struct list *l);
|
||||
|
||||
/**
|
||||
* Creates a new AST node representing a list of ASTs
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue