feat: toujours les mêmes qui font les pipes. Plus de assert dans ASTs (pour des raisons évidentes de stabilité du code) et nouveaux types (AST_PIPE et AST_NEG), + modifs random dans le parser

This commit is contained in:
Gu://em_ 2026-01-27 00:30:19 +01:00
parent 07e7d83c60
commit 96626d9850
27 changed files with 238 additions and 86 deletions

View file

@ -2,7 +2,6 @@
#define _POSIX_C_SOURCE 200809L
#include "ast.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
@ -11,18 +10,45 @@
void ast_free(struct ast **node)
{
if (node == NULL || *node == NULL)
{
puts(
"WARNING: Internal error: failed to free AST node (NULL argument)");
return;
// ast void does not need to be freed.
if (ast_is_if(*node))
}
switch ((*node)->type)
{
case AST_IF:
ast_free_if(ast_get_if(*node));
else if (ast_is_command(*node))
break;
case AST_CMD:
ast_free_command(ast_get_command(*node));
else if (ast_is_list(*node))
break;
case AST_LIST:
ast_free_list(ast_get_list(*node));
else if (ast_is_and_or(*node))
break;
case AST_AND_OR:
ast_free_and_or(ast_get_and_or(*node));
else if (ast_is_redir(*node))
break;
case AST_REDIR:
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;
case AST_VOID:
case AST_END:
break;
default:
puts("WARNING: Internal error: failed to free an AST node (Unknown "
"type)");
return;
}
free(*node);
*node = NULL;
@ -40,6 +66,7 @@ struct ast *ast_create(enum ast_type type, void *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)

View file

@ -10,6 +10,7 @@
#include "ast_redir.h"
#include "ast_void.h"
#include "ast_word.h"
#include "ast_pipe.h"
/**
* Prints the Graphviz DOT representation of the given AST to stdout.

View file

@ -1,8 +1,6 @@
#ifndef AST_AND_OR_H
#define AST_AND_OR_H
#include <stdbool.h>
#include "ast_base.h"
enum ast_and_or_type

View file

@ -2,6 +2,7 @@
#define AST_BASE_H
#include <stdlib.h>
#include <stdbool.h>
enum ast_type
{
@ -12,7 +13,9 @@ enum ast_type
AST_REDIR,
AST_VOID,
AST_CMD,
AST_WORD
AST_WORD,
AST_PIPE,
AST_NEG
};
struct ast

View file

@ -1,6 +1,5 @@
#include "ast_command.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
@ -19,15 +18,14 @@ struct ast *ast_create_command(struct list *command)
struct ast_command *ast_get_command(struct ast *node)
{
assert(node != NULL);
assert(node->type == AST_CMD);
if (node == NULL || node->type == AST_CMD)
return NULL;
return (struct ast_command *)node->data;
}
bool ast_is_command(struct ast *node)
{
assert(node != NULL);
return node->type == AST_CMD;
return node != NULL && node->type == AST_CMD;
}
void ast_free_command(struct ast_command *command_data)

View file

@ -1,8 +1,6 @@
#ifndef AST_COMMAND_H
#define AST_COMMAND_H
#include <stdbool.h>
#include "../lists/lists.h"
#include "ast_base.h"

View file

@ -1,13 +1,11 @@
#include "ast_end.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
bool ast_is_end(struct ast *node)
{
assert(node != NULL);
return node->type == AST_END;
return node != NULL && node->type == AST_END;
}
struct ast *ast_create_end(void)

View file

@ -1,8 +1,6 @@
#ifndef AST_END_H
#define AST_END_H
#include <stdbool.h>
#include "../lists/lists.h"
#include "ast_base.h"

View file

@ -1,6 +1,5 @@
#include "ast_if.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
@ -20,15 +19,14 @@ struct ast *ast_create_if(struct ast *condition, struct ast *then_clause,
struct ast_if *ast_get_if(struct ast *node)
{
assert(node != NULL);
assert(node->type == AST_IF);
return (struct ast_if *)node->data;
if (node == NULL || node->type == AST_IF)
return NULL;
return node->data;
}
bool ast_is_if(struct ast *node)
{
assert(node != NULL);
return node->type == AST_IF;
return node != NULL && node->type == AST_IF;
}
void ast_free_if(struct ast_if *if_data)

View file

@ -1,8 +1,6 @@
#ifndef AST_IF_H
#define AST_IF_H
#include <stdbool.h>
#include "ast_base.h"
struct ast_if

View file

@ -1,6 +1,4 @@
#include <assert.h>
#include "ast.h"
#include "ast_list.h"
struct ast *ast_create_list(struct list *list)
{
@ -15,13 +13,14 @@ struct ast *ast_create_list(struct list *list)
struct ast_list *ast_get_list(struct ast *node)
{
assert(node != NULL);
return (struct ast_list *)node->data;
if (node == NULL)
return NULL;
return node->data;
}
bool ast_is_list(struct ast *node)
{
return node->type == AST_LIST;
return node != NULL && node->type == AST_LIST;
}
void ast_free_list(struct ast_list *ast_list)

View file

@ -1,8 +1,6 @@
#ifndef AST_LIST_H
#define AST_LIST_H
#include <stdbool.h>
#include "../lists/lists.h"
#include "ast_base.h"

32
src/utils/ast/ast_neg.c Normal file
View file

@ -0,0 +1,32 @@
#include "ast_neg.h"
#include <stdlib.h>
bool ast_is_neg(struct ast *node)
{
return node != NULL && node->type == AST_REDIR;
}
struct ast_neg *ast_get_neg(struct ast *node)
{
if (ast_is_neg(node))
return node->data;
return NULL;
}
struct ast *ast_create_neg(bool negation, struct ast *child)
{
struct ast_neg *node = malloc(sizeof(struct ast_neg));
if (!node)
return NULL;
return ast_create(AST_NEG, node);
}
void ast_free_neg(struct ast_neg *node)
{
if (!node)
return;
ast_free(&node->child);
free(node);
}

17
src/utils/ast/ast_neg.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef AST_NEG_H
#define AST_NEG_H
#include "ast_base.h"
struct ast_neg
{
bool negation; // True negates the child's output
struct ast* child;
};
bool ast_is_neg(struct ast *node);
struct ast_neg *ast_get_neg(struct ast *node);
struct ast *ast_create_neg(bool negation, struct ast* child);
void ast_free_neg(struct ast_neg* node);
#endif /* ! AST_NEG_H */

35
src/utils/ast/ast_pipe.c Normal file
View file

@ -0,0 +1,35 @@
#include "ast_pipe.h"
#include <stdlib.h>
bool ast_is_pipe(struct ast *node)
{
return node != NULL && node->type == AST_REDIR;
}
struct ast_pipe *ast_get_pipe(struct ast *node)
{
if (ast_is_pipe(node))
return node->data;
return NULL;
}
struct ast *ast_create_pipe(struct ast *left, struct ast *right)
{
struct ast_pipe *node = malloc(sizeof(struct ast_pipe));
if (!node)
return NULL;
node->left = left;
node->right = right;
return ast_create(AST_PIPE, node);
}
void ast_free_pipe(struct ast_pipe *node)
{
if (!node)
return;
ast_free(&node->left);
ast_free(&node->right);
free(node);
}

18
src/utils/ast/ast_pipe.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef AST_PIPE_H
#define AST_PIPE_H
#include "ast_base.h"
struct ast_pipe
{
struct ast* left;
struct ast* right;
// Output of left will be redirected to right stdin
};
bool ast_is_pipe(struct ast *node);
struct ast_pipe *ast_get_pipe(struct ast *node);
struct ast *ast_create_pipe(struct ast* left, struct ast* right);
void ast_free_pipe(struct ast_pipe* node);
#endif /* ! AST_PIPE_H */

View file

@ -1,8 +1,6 @@
#ifndef AST_REDIR_H
#define AST_REDIR_H
#include <stdbool.h>
#include "ast_base.h"
enum ast_redir_type

View file

@ -1,13 +1,11 @@
#include "ast_void.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
bool ast_is_void(struct ast *node)
{
assert(node != NULL);
return node->type == AST_VOID;
return node != NULL && node->type == AST_VOID;
}
struct ast *ast_create_void(void)

View file

@ -1,8 +1,6 @@
#ifndef AST_VOID_H
#define AST_VOID_H
#include <stdbool.h>
#include "../lists/lists.h"
#include "ast_base.h"

View file

@ -1,8 +1,6 @@
#ifndef AST_WORD_H
#define AST_WORD_H
#include <stdbool.h>
#include "ast_base.h"
struct ast_word