refactor(ast): one file per ast_type -- FINISHED
This commit is contained in:
parent
38f5a35885
commit
30df2993ee
10 changed files with 85 additions and 51 deletions
|
|
@ -3,6 +3,10 @@ lib_LIBRARIES = libutils.a
|
||||||
libutils_a_SOURCES = \
|
libutils_a_SOURCES = \
|
||||||
string_utils/string_utils.c \
|
string_utils/string_utils.c \
|
||||||
ast/ast.c \
|
ast/ast.c \
|
||||||
|
ast/ast_if.c \
|
||||||
|
ast/ast_command.c \
|
||||||
|
ast/ast_list.c \
|
||||||
|
ast/ast_void.c \
|
||||||
lists/lists.c \
|
lists/lists.c \
|
||||||
args/args.c
|
args/args.c
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,12 @@
|
||||||
#ifndef AST_H
|
#ifndef AST_H
|
||||||
#define AST_H
|
#define AST_H
|
||||||
|
|
||||||
|
#include "utils/ast/ast_base.h"
|
||||||
#include "utils/ast/ast_command.h"
|
#include "utils/ast/ast_command.h"
|
||||||
#include "utils/ast/ast_if.h"
|
#include "utils/ast/ast_if.h"
|
||||||
#include "utils/ast/ast_list.h"
|
#include "utils/ast/ast_list.h"
|
||||||
#include "utils/ast/ast_void.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.
|
/* @brief: returns an ast* with corresponding data and type.
|
||||||
*
|
*
|
||||||
* @note: this function should only be called by ast_create_[TYPE] functions.
|
* @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.
|
/* @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 */
|
#endif /* ! AST_H */
|
||||||
|
|
|
||||||
25
src/utils/ast/ast_base.h
Normal file
25
src/utils/ast/ast_base.h
Normal file
|
|
@ -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 */
|
||||||
|
|
@ -30,8 +30,9 @@ bool ast_is_cmd(struct ast *node)
|
||||||
return node->type == AST_CMD;
|
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);
|
list_deep_destroy((*cmd_data)->cmd);
|
||||||
free(cmd_data);
|
free(*cmd_data);
|
||||||
|
*cmd_data = NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "utils/lists/lists.h"
|
#include "utils/lists/lists.h"
|
||||||
#include "utils/ast/ast.h"
|
#include "utils/ast/ast_base.h"
|
||||||
|
|
||||||
struct ast_cmd
|
struct ast_cmd
|
||||||
{
|
{
|
||||||
|
|
@ -28,8 +28,8 @@ struct ast_cmd *ast_get_cmd(struct ast *node);
|
||||||
struct ast *ast_create_cmd(struct list *cmd);
|
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 */
|
#endif /* ! AST_COMMAND_H */
|
||||||
|
|
|
||||||
|
|
@ -31,14 +31,15 @@ bool ast_is_if(struct ast *node)
|
||||||
return node->type == AST_IF;
|
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;
|
return;
|
||||||
|
|
||||||
ast_free(if_data->condition);
|
ast_free((*if_data)->condition);
|
||||||
ast_free(if_data->then_clause);
|
ast_free((*if_data)->then_clause);
|
||||||
ast_free(if_data->else_clause);
|
ast_free((*if_data)->else_clause);
|
||||||
|
|
||||||
free(if_data);
|
free(*if_data);
|
||||||
|
*if_data = NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "utils/ast/ast.h"
|
#include "utils/ast/ast_base.h"
|
||||||
|
|
||||||
struct ast_if
|
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 *ast_create_if(struct ast *condition, struct ast *then_clause,
|
||||||
struct ast *else_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 */
|
#endif /* ! AST_IF_H */
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,33 @@
|
||||||
#include "utils/ast/ast.h"
|
#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)
|
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;
|
return;
|
||||||
|
|
||||||
list_deep_destroy(ast_list->children);
|
list_deep_destroy((*ast_list)->children);
|
||||||
free(ast_list);
|
free(*ast_list);
|
||||||
|
*ast_list = NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,14 @@
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#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
|
* 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);
|
bool ast_is_list(struct ast *node);
|
||||||
|
|
||||||
struct ast_list
|
/* @brief: frees the given ast list.
|
||||||
{
|
*
|
||||||
struct list *children; // A list of ASTs (ast*)
|
* @warning: should only be called by ast_free() function.
|
||||||
};
|
*/
|
||||||
|
|
||||||
void ast_free_list(struct ast_list *ast_list);
|
void ast_free_list(struct ast_list *ast_list);
|
||||||
|
|
||||||
#endif /* ! AST_LIST_H */
|
#endif /* ! AST_LIST_H */
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "utils/lists/lists.h"
|
#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.
|
* Checks if the given AST node is of type AST_VOID.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue