diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index ed931e1..e04e170 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -2,7 +2,22 @@ #include -static struct ast *ast_create(enum ast_type type, void *data) +void ast_free(struct ast *node) +{ + 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)); + + free(node); +} + +struct ast *ast_create(enum ast_type type, void *data) { struct ast *node = malloc(sizeof(struct ast)); if (!node) diff --git a/src/utils/ast/ast.h b/src/utils/ast/ast.h index 0924ab8..64ee195 100644 --- a/src/utils/ast/ast.h +++ b/src/utils/ast/ast.h @@ -27,4 +27,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_H */ diff --git a/src/utils/ast/ast_command.c b/src/utils/ast/ast_command.c index 4e4af66..a1eafcb 100644 --- a/src/utils/ast/ast_command.c +++ b/src/utils/ast/ast_command.c @@ -4,6 +4,8 @@ #include #include +#include "utils/lists/lists.h" + struct ast *ast_create_cmd(struct list *cmd) { struct ast_cmd *cmd_data = malloc(sizeof(struct ast_cmd)); @@ -27,3 +29,9 @@ bool ast_is_cmd(struct ast *node) assert(node != NULL); return node->type == AST_CMD; } + +void ast_free_cmd(struct ast_cmd *cmd_data) +{ + list_deep_destroy(cmd_data->cmd); + free(cmd_data); +} diff --git a/src/utils/ast/ast_command.h b/src/utils/ast/ast_command.h index d95ad4c..9a623a8 100644 --- a/src/utils/ast/ast_command.h +++ b/src/utils/ast/ast_command.h @@ -3,7 +3,7 @@ #include -#include "../lists/lists.h" +#include "utils/lists/lists.h" #include "utils/ast/ast.h" struct ast_cmd @@ -27,4 +27,9 @@ struct ast_cmd *ast_get_cmd(struct ast *node); */ struct ast *ast_create_cmd(struct list *cmd); +/* + * @brief: frees the given ast_cmd. + */ +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 746fd9d..50d4c5e 100644 --- a/src/utils/ast/ast_if.c +++ b/src/utils/ast/ast_if.c @@ -30,3 +30,15 @@ bool ast_is_if(struct ast *node) assert(node != NULL); return node->type == AST_IF; } + +void ast_free_if(struct ast_if *if_data) +{ + if (if_data == NULL) + return; + + ast_free(if_data->condition); + ast_free(if_data->then_clause); + ast_free(if_data->else_clause); + + free(if_data); +} diff --git a/src/utils/ast/ast_if.h b/src/utils/ast/ast_if.h index d470a92..69d97ec 100644 --- a/src/utils/ast/ast_if.h +++ b/src/utils/ast/ast_if.h @@ -28,5 +28,9 @@ 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. + */ +void ast_free_if(struct ast_if *if_data) #endif /* ! AST_IF_H */ diff --git a/src/utils/ast/ast_list.h b/src/utils/ast/ast_list.h index b733c54..af7be28 100644 --- a/src/utils/ast/ast_list.h +++ b/src/utils/ast/ast_list.h @@ -26,4 +26,6 @@ struct ast_list struct list *children; // A list of ASTs (ast*) }; +void ast_free_list(struct ast_list *ast_list); + #endif /* ! AST_LIST_H */ diff --git a/src/utils/ast/ast_void.c b/src/utils/ast/ast_void.c index 3a2ae7b..1396121 100644 --- a/src/utils/ast/ast_void.c +++ b/src/utils/ast/ast_void.c @@ -1 +1,16 @@ #include "utils/ast/ast_void.h" + +#include +#include +#include + +bool ast_is_void(struct ast *node) +{ + assert(node != NULL); + return node->type == AST_VOID; +} + +struct ast *ast_create_void(void) +{ + return ast_create(AST_VOID, NULL); +} diff --git a/src/utils/ast/ast_void.h b/src/utils/ast/ast_void.h index 62996fb..c3fb123 100644 --- a/src/utils/ast/ast_void.h +++ b/src/utils/ast/ast_void.h @@ -3,6 +3,7 @@ #include +#include "utils/lists/lists.h" #include "utils/ast/ast.h" /** @@ -10,16 +11,10 @@ */ bool ast_is_void(struct ast *node); -/** - * Retrieves the if statement data from the given AST node. - * Assumes that the node is of type AST_VOID. - */ -struct ast_void *ast_get_void(struct ast *node); - /** * Creates a new AST node representing NOTHING * WARNING: data will be a NULL pointer */ -struct ast *ast_create_void(struct list *ast_list); +struct ast *ast_create_void(void); #endif /* ! AST_VOID_H */