diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index 60aecb2..ebdadd6 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -11,8 +11,9 @@ void ast_free(struct ast **node) { if (node == NULL || *node == NULL) { - perror("[WARNING] Internal error: failed to free AST node (NULL " - "argument)"); + fprintf(stderr, + "[WARNING] Internal error: failed to free AST node (NULL " + "argument)\n"); return; } @@ -48,13 +49,17 @@ void ast_free(struct ast **node) case AST_LOOP: ast_free_loop(ast_get_loop(*node)); break; + case AST_FUNCTION: + ast_free_function(ast_get_function(*node)); + break; case AST_VOID: case AST_END: break; default: - perror("WARNING: Internal error: failed to free an AST node (Unknown " - "type)"); + fprintf(stderr, + "WARNING: Internal error: failed to free an AST node (Unknown " + "type)"); return; } diff --git a/src/utils/ast/ast_base.h b/src/utils/ast/ast_base.h index 71e4a38..e1c7b07 100644 --- a/src/utils/ast/ast_base.h +++ b/src/utils/ast/ast_base.h @@ -17,7 +17,8 @@ enum ast_type AST_PIPE, AST_NEG, AST_LOOP, - AST_ASSIGNMENT + AST_ASSIGNMENT, + AST_FUNCTION }; struct ast diff --git a/src/utils/ast/ast_function.c b/src/utils/ast/ast_function.c new file mode 100644 index 0000000..edd53e0 --- /dev/null +++ b/src/utils/ast/ast_function.c @@ -0,0 +1,40 @@ +#include "ast_function.h" + +#include +#include + +#include "ast_base.h" + +bool ast_is_function(struct ast *node) +{ + return node != NULL && node->type == AST_FUNCTION; +} + +struct ast_function *ast_get_function(struct ast *node) +{ + if (!ast_is_function(node)) + return NULL; + return (struct ast_function *)node->data; +} + +struct ast *ast_create_function(char *name, struct ast *value) +{ + struct ast_function *function_data = malloc(sizeof(struct ast_function)); + if (!function_data) + return NULL; + + function_data->name = strdup(name); + function_data->value = value; + + return ast_create(AST_FUNCTION, function_data); +} + +void ast_free_function(struct ast_function *function_data) +{ + if (function_data) + { + free(function_data->name); + ast_free(&function_data->value); + free(function_data); + } +} diff --git a/src/utils/ast/ast_function.h b/src/utils/ast/ast_function.h new file mode 100644 index 0000000..86e35cd --- /dev/null +++ b/src/utils/ast/ast_function.h @@ -0,0 +1,33 @@ +#ifndef AST_FUNCTION_H +#define AST_FUNCTION_H + +#include + +struct ast_function +{ + char *name; + struct ast *value; +}; + + +/** + * Checks if the given AST node is an ast_function + */ +bool ast_is_function(struct ast *node); + +/** + * Retrieves the function data from the given AST node. + * Assumes that the node is of type AST_function. + */ +struct ast_function *ast_get_function(struct ast *node); + +/** + * Creates a new AST node representing an AST_function + */ +struct ast *ast_create_function(char *name, struct ast *value); +/* + * @brief: frees the given ast_function and sets the pointer to NULL. + */ +void ast_free_function(struct ast_function *function_data); + +#endif /* AST_FUNCTION_H */