42sh/src/utils/ast/ast_word.c

50 lines
925 B
C
Raw Normal View History

2026-01-27 18:00:59 +01:00
#define _POSIX_C_SOURCE 200809L
2026-01-24 15:34:10 +01:00
#include "ast_word.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
struct ast *ast_create_word(char *word)
{
struct ast_word *ast_node = malloc(sizeof(struct ast_word));
if (ast_node == NULL)
return NULL;
ast_node->type = AST_WORD;
ast_node->word = strdup(word);
struct ast *res = ast_create(AST_WORD, ast_node);
if (res == NULL)
{
2026-01-27 19:56:33 +01:00
free(ast_node->word);
2026-01-24 15:34:10 +01:00
free(ast_node);
return NULL;
}
return res;
}
struct ast_word *ast_get_word(struct ast *node)
{
if (node == NULL || node->type != AST_WORD)
return NULL;
return node->data;
}
bool ast_is_word(struct ast *node)
{
return node && node->type == AST_WORD;
}
void ast_free_word(struct ast_word *ast_node)
{
if (ast_node == NULL)
return;
2026-01-30 21:27:23 +01:00
if (ast_node->word != NULL)
free(ast_node->word);
2026-01-24 15:34:10 +01:00
free(ast_node);
}