diff options
Diffstat (limited to '42sh/src/ast/ast_accessors.h')
| -rw-r--r-- | 42sh/src/ast/ast_accessors.h | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/42sh/src/ast/ast_accessors.h b/42sh/src/ast/ast_accessors.h new file mode 100644 index 0000000..757bff5 --- /dev/null +++ b/42sh/src/ast/ast_accessors.h @@ -0,0 +1,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 */ |
