summaryrefslogtreecommitdiff
path: root/42sh/src/ast/ast_accessors.h
blob: 757bff5794fbd661a4dd38050de80d09edcb14a0 (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
#ifndef AST_ACCESSORS_H
#define AST_ACCESSORS_H

#include <stddef.h>

#include "ast/ast.h"

/**
 * @brief Returns the i-th child of `ast`.
 * The children are stored as follows:
 * `index` 0: left child, or expression in unary trees, or `condition` in `if`
 * blocks. `index` 1: right child, or `then` expression in `if` blocks. `index`
 * 2+: middle children, or `else` expression in `if` blocks (only `index` 2).
 * @param ast Any kind of AST.
 * @param index The index of the child to get.
 * @note All kinds of ASTs can be passed to this function.
 * All `index` can be passed to this function too.
 * However, if the given type does not have any child or if the `index` is too
 * big, `get_i` returns `NULL.`
 */
struct ast *get_i(struct ast *ast, size_t index);

/**
 * @brief Alias for `get_i(ast, 0)`.
 * @note See `get_i` for a full explanation.
 */
struct ast *get_left(struct ast *ast);

/**
 * @brief Alias for `get_i(ast, 1)`.
 * @note See `get_i` for a full explanation.
 */
struct ast *get_right(struct ast *ast);

/**
 * @brief Sets the `child` as the i-th child of `ast`.
 * The children are stored as follows:
 * `index` 0: left child, or expression in unary trees, or `condition` in `if`
 * blocks. `index` 1: right child, or `then` expression in `if` blocks. `index`
 * 2+: middle children, or `else` expression in `if` blocks (only `index` 2).
 * @param ast Any kind of AST.
 * @param child The child to be assigned in `ast`.
 * @param index The position of the child in the `ast`.
 * @note All kinds of ASTs can be passed to this function.
 * All `index` can be passed to this function too.
 * However, if the given type can not contain any child or if the `index` is too
 * big, `set_i` does nothing.
 */
void set_i(struct ast *ast, struct ast *child, size_t index);

/**
 * @brief Alias for `set_i(ast, child, 0)`.
 * @note See `set_i` for a full explanation.
 */
void set_left(struct ast *ast, struct ast *child);

/**
 * @brief Alias for `set_i(ast, child, 1)`.
 * @note See `set_i` for a full explanation.
 */
void set_right(struct ast *ast, struct ast *child);

#endif /* ! AST_ACCESSORS_H */