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

162 lines
4.1 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
2026-01-21 19:57:44 +01:00
#include "ast.h"
#include <stdbool.h>
2026-01-22 16:46:33 +01:00
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
2026-01-22 16:46:33 +01:00
void ast_free(struct ast **node)
{
2026-01-23 17:33:15 +01:00
if (node == NULL || *node == NULL)
{
perror(
"WARNING: Internal error: failed to free AST node (NULL argument)");
return;
}
switch ((*node)->type)
{
case AST_IF:
2026-01-22 16:46:33 +01:00
ast_free_if(ast_get_if(*node));
break;
case AST_CMD:
2026-01-22 16:46:33 +01:00
ast_free_command(ast_get_command(*node));
break;
case AST_LIST:
2026-01-22 16:46:33 +01:00
ast_free_list(ast_get_list(*node));
break;
case AST_AND_OR:
2026-01-22 16:46:33 +01:00
ast_free_and_or(ast_get_and_or(*node));
break;
case AST_REDIR:
2026-01-22 16:46:33 +01:00
ast_free_redir(ast_get_redir(*node));
break;
case AST_PIPE:
ast_free_pipe(ast_get_pipe(*node));
break;
case AST_WORD:
ast_free_word(ast_get_word(*node));
break;
2026-01-29 20:29:02 +01:00
case AST_ASSIGNMENT:
ast_free_assignment(ast_get_assignment(*node));
break;
case AST_VOID:
case AST_END:
break;
default:
perror("WARNING: Internal error: failed to free an AST node (Unknown "
2026-01-27 19:56:33 +01:00
"type)");
return;
}
free(*node);
*node = NULL;
2026-01-22 16:46:33 +01:00
}
struct ast *ast_create(enum ast_type type, void *data)
{
struct ast *node = malloc(sizeof(struct ast));
if (!node)
return NULL;
node->type = type;
node->data = data;
return node;
}
// TODO handle new types (AST_WORD, AST_PIPE, etc.)
static void ast_print_dot_recursive(struct ast *node, FILE *out)
{
if (!node)
return;
switch (node->type)
{
2026-01-22 16:46:33 +01:00
case AST_LIST: {
struct ast_list *ast_list = ast_get_list(node);
fprintf(out, " node%p [label=\"LIST\"];\n", (void *)node);
struct list *elt = ast_list->children;
while (elt != NULL)
{
struct ast *child = (struct ast *)elt->data;
fprintf(out, " node%p -> node%p;\n", (void *)node, (void *)child);
ast_print_dot_recursive(child, out);
elt = elt->next;
}
break;
}
2026-01-21 19:57:44 +01:00
case AST_IF: {
struct ast_if *if_data = ast_get_if(node);
fprintf(out, " node%p [label=\"IF\"];\n", (void *)node);
if (if_data->condition)
{
fprintf(out, " node%p -> node%p;\n", (void *)node,
(void *)if_data->condition);
2026-01-21 19:57:44 +01:00
fprintf(out,
" node%p [fillcolor=\"lightyellow\", style=\"filled\"];\n",
(void *)if_data->condition);
ast_print_dot_recursive(if_data->condition, out);
if (if_data->then_clause)
{
fprintf(out, " node%p -> node%p [label=\"true\"];\n",
2026-01-21 19:57:44 +01:00
(void *)if_data->condition,
(void *)if_data->then_clause);
ast_print_dot_recursive(if_data->then_clause, out);
}
if (if_data->else_clause)
{
fprintf(out, " node%p -> node%p [label=\"false\"];\n",
2026-01-21 19:57:44 +01:00
(void *)if_data->condition,
(void *)if_data->else_clause);
ast_print_dot_recursive(if_data->else_clause, out);
}
}
break;
}
2026-01-21 19:57:44 +01:00
case AST_CMD: {
2026-01-22 16:46:33 +01:00
struct ast_command *command_data = ast_get_command(node);
fprintf(out, " node%p [label=\"", (void *)node);
2026-01-22 16:46:33 +01:00
struct list *l = command_data->command;
while (l)
{
fprintf(out, "%s", (char *)l->data);
if (l->next)
fprintf(out, " ");
l = l->next;
}
fprintf(out, "\"];\n");
break;
}
case AST_END:
fprintf(out, " node%p [label=\"END\"];\n", (void *)node);
break;
default:
break;
}
}
void ast_print_dot(struct ast *ast)
{
if (!ast)
return;
FILE *dot_pipe = popen("dot -Tsvg -o ast.svg", "w");
if (!dot_pipe)
{
return;
}
fprintf(dot_pipe, "digraph AST {\n");
ast_print_dot_recursive(ast, dot_pipe);
fprintf(dot_pipe, "}\n");
pclose(dot_pipe);
}