refactor(ast): one file per ast_type -- UNSTABLE (3)
This commit is contained in:
parent
68d8a1fd13
commit
e3ec484621
9 changed files with 76 additions and 9 deletions
|
|
@ -2,7 +2,22 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
#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 */
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -1 +1,16 @@
|
|||
#include "utils/ast/ast_void.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
#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 */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue