From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- 42sh/src/ast/ast_accessors.h | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 42sh/src/ast/ast_accessors.h (limited to '42sh/src/ast/ast_accessors.h') 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 + +#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 */ -- cgit v1.2.3