#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 */