diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /21sh/ll-expr/src/ast | |
Diffstat (limited to '21sh/ll-expr/src/ast')
| -rw-r--r-- | 21sh/ll-expr/src/ast/ast.c | 27 | ||||
| -rw-r--r-- | 21sh/ll-expr/src/ast/ast.h | 40 |
2 files changed, 67 insertions, 0 deletions
diff --git a/21sh/ll-expr/src/ast/ast.c b/21sh/ll-expr/src/ast/ast.c new file mode 100644 index 0000000..701d40e --- /dev/null +++ b/21sh/ll-expr/src/ast/ast.c @@ -0,0 +1,27 @@ +#include "ast.h" + +#include <err.h> +#include <stdlib.h> + +struct ast *ast_new(enum ast_type type) +{ + struct ast *new = calloc(1, sizeof(struct ast)); + if (!new) + return NULL; + new->type = type; + return new; +} + +void ast_free(struct ast *ast) +{ + if (ast == NULL) + return; + + ast_free(ast->left); + ast->left = NULL; + + ast_free(ast->right); + ast->right = NULL; + + free(ast); +} diff --git a/21sh/ll-expr/src/ast/ast.h b/21sh/ll-expr/src/ast/ast.h new file mode 100644 index 0000000..01e0064 --- /dev/null +++ b/21sh/ll-expr/src/ast/ast.h @@ -0,0 +1,40 @@ +#ifndef AST_H +#define AST_H + +#include <unistd.h> + +enum ast_type +{ + AST_PLUS, + AST_MINUS, + AST_MUL, + AST_DIV, + AST_NUMBER, + AST_NEG +}; + +/** + * This very simple AST structure should be sufficient for a simple AST. + * It is however, NOT GOOD ENOUGH for more complicated projects, such as a + * shell. Please read the project guide for some insights about other kinds of + * ASTs. + */ +struct ast +{ + enum ast_type type; // The kind of node we're dealing with + ssize_t value; // If the node is a number, it stores its value + struct ast *left; // The left branch if any, unary or binary + struct ast *right; // The right branch of the binary node +}; + +/** + ** \brief Allocates a new ast with the given type. + */ +struct ast *ast_new(enum ast_type type); + +/** + ** \brief Recursively frees the given ast. + */ +void ast_free(struct ast *ast); + +#endif /* !AST_H */ |
