fix: local merge
This commit is contained in:
commit
adea32552d
4 changed files with 84 additions and 5 deletions
|
|
@ -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,12 +49,16 @@ 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 "
|
||||
fprintf(stderr,
|
||||
"WARNING: Internal error: failed to free an AST node (Unknown "
|
||||
"type)");
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ enum ast_type
|
|||
AST_PIPE,
|
||||
AST_NEG,
|
||||
AST_LOOP,
|
||||
AST_ASSIGNMENT
|
||||
AST_ASSIGNMENT,
|
||||
AST_FUNCTION
|
||||
};
|
||||
|
||||
struct ast
|
||||
|
|
|
|||
40
src/utils/ast/ast_function.c
Normal file
40
src/utils/ast/ast_function.c
Normal 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);
|
||||
}
|
||||
}
|
||||
33
src/utils/ast/ast_function.h
Normal file
33
src/utils/ast/ast_function.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef AST_FUNCTION_H
|
||||
#define AST_FUNCTION_H
|
||||
|
||||
#include <stdbool.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 */
|
||||
Loading…
Add table
Add a link
Reference in a new issue