blob: 4c66fa75cde18ac0a6adea6a07c494f22a03ed42 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:42 by charles #+# #+# */
/* Updated: 2020/05/04 12:00:20 by charles ### ########.fr */
/* */
/* ************************************************************************** */
/*
** \file ast.c
** \brief AST functions
*/
#include "ast.h"
/*
** \brief Create a new AST node according to `tag`
** \param tag Tag of node
** \param data Pointer to node data (t_cmd or t_line)
** which will be copied in ast::data union
** \return Created node or NULL on error
*/
t_ast *ast_new(t_ast_tag tag, void *data)
{
t_ast *ast;
if (data == NULL)
return (NULL);
if ((ast = (t_ast*)malloc(sizeof(t_ast))) == NULL)
return (NULL);
ft_bzero(ast, sizeof(t_ast));
ast->tag = tag;
if (tag == TAG_CMD)
ft_memcpy(&ast->cmd, (t_cmd*)data, sizeof(t_cmd));
else if (tag == TAG_LINE)
ft_memcpy(&ast->line, (t_line*)data, sizeof(t_line));
return (ast);
}
/*
** \brief Destroy an AST node and all his child nodes
** \param ast AST to destroy
*/
void ast_destroy(t_ast *ast)
{
if (ast == NULL)
return ;
if (ast->tag == TAG_CMD)
{
ft_split_destroy(ast->cmd.argv);
free(ast->cmd.in);
free(ast->cmd.out);
}
else if (ast->tag == TAG_LINE)
{
ast_destroy(ast->line.left);
ast_destroy(ast->line.right);
}
free(ast);
}
|