aboutsummaryrefslogtreecommitdiff
path: root/src/ast.c
blob: 7c3fb8f646d05ffeb59f11dcd2f576ff5a379f24 (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
#include "ast.h"

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->data.cmd, (t_cmd*)data, sizeof(t_cmd));
	else if (tag == TAG_LINE)
		ft_memcpy(&ast->data.line, (t_line*)data, sizeof(t_line));
	return (ast);
}

static void		cmd_destroy(t_cmd *cmd)
{
	ft_split_destroy(cmd->argv);
	free(cmd->in);
	free(cmd->out);
}

static void		line_destroy(t_line *line)
{
	ast_destroy(line->left);
	ast_destroy(line->right);
}

void			ast_destroy(t_ast *ast)
{
	if (ast == NULL)
		return ;
	if (ast->tag == TAG_CMD)
		cmd_destroy(&ast->data.cmd);
	else if (ast->tag == TAG_LINE)
		line_destroy(&ast->data.line);
	free(ast);
}