blob: f566832a8f10a846b4c303b342a964145fd555a2 (
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
|
/**
** \file ast.c
** \brief AST manipulation
*/
#include "ms_parse.h"
/**
** \brief Create a new AST node with default values
** \param tag Tag of the node
** \return The allocated node
*/
t_ast *ms_ast_new(t_tag tag)
{
t_ast *ast;
if ((ast = (t_ast*)malloc(sizeof(t_ast))) == NULL)
return (NULL);
ast->tag = tag;
ast->content = NULL;
ast->children_num = 0;
ast->children = NULL;
return (ast);
}
/**
** \brief Destroy an allocated AST
** \warning Assumes that `content`, `children` and the node itself have been malloc'd
** \param ast AST to destroy
*/
void ms_ast_destroy(t_ast *ast)
{
int i;
if (ast == NULL)
return ;
i = -1;
while (++i < ast->children_num)
ms_ast_destroy(ast->children[i]);
free(ast->children);
free(ast->content);
free(ast);
}
/**
** \brief Iterate over an AST node's childs
** \param f Function applied to each child, take `arg` has his first argument.
** \param arg Pointer that will be passed to `f`, to keep information between iterations
*/
void ms_ast_iter(
t_ast *ast,
void (*f)(void *f_arg, t_ast *children),
void *arg)
{
int i;
i = -1;
while (++i < ast->children_num)
f(arg, ast->children[i]);
}
|