mirror of
https://github.com/Savapitech/42sh.git
synced 2026-01-18 16:57:28 +01:00
66 lines
1.6 KiB
C
66 lines
1.6 KiB
C
/*
|
|
** EPITECH PROJECT, 2025
|
|
** __
|
|
** File description:
|
|
** _
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#include "ast.h"
|
|
#include "u_mem.h"
|
|
|
|
void print_ast(ast_t *ast, ast_ctx_t *ctx, size_t depth)
|
|
{
|
|
for (size_t i = 0; i < depth; i++)
|
|
fprintf(stderr, " ");
|
|
if (depth)
|
|
fprintf(stderr, "- ");
|
|
fprintf(stderr, "(%.*s)\n", (int)ast->tok.sz, ast->tok.str);
|
|
if (ast->type == N_BIN) {
|
|
print_ast(ast->binary.left, ctx, depth + 1);
|
|
print_ast(ast->binary.right, ctx, depth + 1);
|
|
}
|
|
if (ast->type == N_CMD) {
|
|
for (size_t i = 1; i < ast->vector.sz; i++)
|
|
fprintf(stderr, "%*s - (%.*s)\n", (int)depth, "",
|
|
(int)ast->vector.tokens[i].sz, ast->vector.tokens[i].str);
|
|
}
|
|
if (ast->type == N_LST) {
|
|
for (size_t i = 0; i < ast->list.sz; i++)
|
|
print_ast(ast->list.nodes[i], ctx, depth + 1);
|
|
}
|
|
}
|
|
|
|
ast_t *create_node(ast_ctx_t *ctx)
|
|
{
|
|
ast_t *new_ast;
|
|
|
|
if (ctx->ast == NULL)
|
|
return NULL;
|
|
if (ctx->sz + 1 == ctx->cap) {
|
|
new_ast = u_realloc(ctx->ast, sizeof *ctx->ast * ctx->sz,
|
|
sizeof *ctx->ast * (ctx->cap << 1));
|
|
if (new_ast == NULL)
|
|
return NULL;
|
|
ctx->ast = new_ast;
|
|
ctx->cap <<= 1;
|
|
}
|
|
ctx->ast[ctx->sz] = (ast_t){ 0 };
|
|
ctx->sz++;
|
|
return ctx->ast + ctx->sz - 1;
|
|
}
|
|
|
|
void free_ast(ast_ctx_t *ctx)
|
|
{
|
|
for (size_t i = 0; i < ctx->sz; i++) {
|
|
if (ctx->first_node[i].type == N_LST)
|
|
free((void *)ctx->first_node[i].list.nodes);
|
|
if (ctx->first_node[i].type == N_CMD)
|
|
free(ctx->first_node[i].vector.tokens);
|
|
}
|
|
free(ctx->first_node);
|
|
}
|