diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index 600ee40..9fd1be1 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -3,6 +3,10 @@ lib_LIBRARIES = libutils.a libutils_a_SOURCES = \ string_utils/string_utils.c \ ast/ast.c \ + ast/ast_if.c \ + ast/ast_command.c \ + ast/ast_list.c \ + ast/ast_void.c \ lists/lists.c \ args/args.c diff --git a/src/utils/ast/ast.h b/src/utils/ast/ast.h index 64ee195..79eeff3 100644 --- a/src/utils/ast/ast.h +++ b/src/utils/ast/ast.h @@ -1,32 +1,12 @@ #ifndef AST_H #define AST_H +#include "utils/ast/ast_base.h" #include "utils/ast/ast_command.h" #include "utils/ast/ast_if.h" #include "utils/ast/ast_list.h" #include "utils/ast/ast_void.h" -enum ast_type -{ - AST_END, - AST_LIST, - AST_IF, - AST_CMD -}; - -struct ast -{ - enum ast_type type; - - /** - * 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) - */ - void *data; -}; - /* @brief: returns an ast* with corresponding data and type. * * @note: this function should only be called by ast_create_[TYPE] functions. @@ -36,6 +16,6 @@ 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) +void ast_free(struct ast *node); #endif /* ! AST_H */ diff --git a/src/utils/ast/ast_base.h b/src/utils/ast/ast_base.h new file mode 100644 index 0000000..aa20b76 --- /dev/null +++ b/src/utils/ast/ast_base.h @@ -0,0 +1,25 @@ +#ifndef AST_BASE_H +#define AST_BASE_H + +enum ast_type +{ + AST_END, + AST_LIST, + AST_IF, + AST_CMD +}; + +struct ast +{ + enum ast_type type; + + /** + * 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) + */ + void *data; +}; + +#endif /* ! AST_BASE_H */ diff --git a/src/utils/ast/ast_command.c b/src/utils/ast/ast_command.c index a1eafcb..9eb2dd7 100644 --- a/src/utils/ast/ast_command.c +++ b/src/utils/ast/ast_command.c @@ -30,8 +30,9 @@ bool ast_is_cmd(struct ast *node) return node->type == AST_CMD; } -void ast_free_cmd(struct ast_cmd *cmd_data) +void ast_free_cmd(struct ast_cmd **cmd_data) { - list_deep_destroy(cmd_data->cmd); - free(cmd_data); + list_deep_destroy((*cmd_data)->cmd); + free(*cmd_data); + *cmd_data = NULL; } diff --git a/src/utils/ast/ast_command.h b/src/utils/ast/ast_command.h index 9a623a8..f7d22a1 100644 --- a/src/utils/ast/ast_command.h +++ b/src/utils/ast/ast_command.h @@ -4,7 +4,7 @@ #include #include "utils/lists/lists.h" -#include "utils/ast/ast.h" +#include "utils/ast/ast_base.h" struct ast_cmd { @@ -28,8 +28,8 @@ struct ast_cmd *ast_get_cmd(struct ast *node); struct ast *ast_create_cmd(struct list *cmd); /* - * @brief: frees the given ast_cmd. + * @brief: frees the given ast_cmd and sets the pointer to NULL. */ -void ast_free_cmd(struct ast_cmd *cmd_data) +void ast_free_cmd(struct ast_cmd **cmd_data); #endif /* ! AST_COMMAND_H */ diff --git a/src/utils/ast/ast_if.c b/src/utils/ast/ast_if.c index 50d4c5e..242d5fd 100644 --- a/src/utils/ast/ast_if.c +++ b/src/utils/ast/ast_if.c @@ -31,14 +31,15 @@ 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 (if_data == NULL || *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); + free(*if_data); + *if_data = NULL; } diff --git a/src/utils/ast/ast_if.h b/src/utils/ast/ast_if.h index 69d97ec..2036eb7 100644 --- a/src/utils/ast/ast_if.h +++ b/src/utils/ast/ast_if.h @@ -3,7 +3,7 @@ #include -#include "utils/ast/ast.h" +#include "utils/ast/ast_base.h" struct ast_if { @@ -29,8 +29,8 @@ struct ast_if *ast_get_if(struct ast *node); struct ast *ast_create_if(struct ast *condition, struct ast *then_clause, struct ast *else_clause); /* - * @brief: frees the given ast_if. + * @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 */ diff --git a/src/utils/ast/ast_list.c b/src/utils/ast/ast_list.c index de178ee..fae1f1a 100644 --- a/src/utils/ast/ast_list.c +++ b/src/utils/ast/ast_list.c @@ -1,16 +1,33 @@ #include "utils/ast/ast.h" -struct ast *ast_create_list(struct list *ast_list); +struct ast *ast_create_list(struct list *list) +{ + struct ast_list *ast_list = malloc(sizeof(struct ast_list)); + if (ast_list == NULL) + return NULL; -struct ast_list *ast_get_list(struct ast *node); + ast_list->list = list; + + return ast_create(AST_LIST, ast_list); +} + +struct ast_list *ast_get_list(struct ast *node) +{ + assert(node != NULL); + return (struct ast_list*)node->data; +} bool ast_is_list(struct ast *node) - -void ast_free_list(struct ast_list *ast_list) { - if (ast_list == NULL) + return node->type == AST_LIST; +} + +void ast_free_list(struct ast_list **ast_list) +{ + if (*ast_list == NULL || ast_list == NULL) return; - list_deep_destroy(ast_list->children); - free(ast_list); + list_deep_destroy((*ast_list)->children); + free(*ast_list); + *ast_list = NULL; } diff --git a/src/utils/ast/ast_list.h b/src/utils/ast/ast_list.h index af7be28..6f1cbd9 100644 --- a/src/utils/ast/ast_list.h +++ b/src/utils/ast/ast_list.h @@ -3,7 +3,14 @@ #include -#include "utils/ast/ast.h" +#include "utils/ast/ast_base.h" +#include "utils/lists/lists.h" + +struct ast_list +{ + struct list *children; // A list of ASTs (ast*) +}; + /** * Creates a new AST node representing a list of ASTs @@ -21,11 +28,10 @@ struct ast_list *ast_get_list(struct ast *node); */ bool ast_is_list(struct ast *node); -struct ast_list -{ - struct list *children; // A list of ASTs (ast*) -}; - +/* @brief: frees the given ast list. + * + * @warning: should only be called by ast_free() function. + */ void ast_free_list(struct ast_list *ast_list); #endif /* ! AST_LIST_H */ diff --git a/src/utils/ast/ast_void.h b/src/utils/ast/ast_void.h index c3fb123..866a8d8 100644 --- a/src/utils/ast/ast_void.h +++ b/src/utils/ast/ast_void.h @@ -4,7 +4,7 @@ #include #include "utils/lists/lists.h" -#include "utils/ast/ast.h" +#include "utils/ast/ast_base.h" /** * Checks if the given AST node is of type AST_VOID.