Merge branch 'ast' into dev

This commit is contained in:
matteo 2026-01-31 11:58:40 +01:00
commit a63632005c
4 changed files with 78 additions and 3 deletions

View file

@ -11,7 +11,7 @@ void ast_free(struct ast **node)
{ {
if (node == NULL || *node == NULL) if (node == NULL || *node == NULL)
{ {
perror( fprintf(stderr,
"WARNING: Internal error: failed to free AST node (NULL argument)"); "WARNING: Internal error: failed to free AST node (NULL argument)");
return; return;
} }
@ -48,12 +48,15 @@ void ast_free(struct ast **node)
case AST_LOOP: case AST_LOOP:
ast_free_loop(ast_get_loop(*node)); ast_free_loop(ast_get_loop(*node));
break; break;
case AST_FUNCTION:
ast_free_function(ast_get_function(*node));
break;
case AST_VOID: case AST_VOID:
case AST_END: case AST_END:
break; break;
default: default:
perror("WARNING: Internal error: failed to free an AST node (Unknown " fprintf(stderr, "WARNING: Internal error: failed to free an AST node (Unknown "
"type)"); "type)");
return; return;
} }

View file

@ -17,7 +17,8 @@ enum ast_type
AST_PIPE, AST_PIPE,
AST_NEG, AST_NEG,
AST_LOOP, AST_LOOP,
AST_ASSIGNMENT AST_ASSIGNMENT,
AST_FUNCTION
}; };
struct ast struct ast

View file

@ -0,0 +1,40 @@
#include "ast_function.h"
#include <stdlib.h>
#include <string.h>
#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);
}
}

View file

@ -0,0 +1,31 @@
#ifndef AST_FUNCTION_H
#define AST_FUNCTION_H
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 */