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

@ -4,6 +4,12 @@
#include <sys/types.h> #include <sys/types.h>
#include "lexer_utils.h" #include "lexer_utils.h"
/*
* @brief returns true if token is a redir type, false otherwise
*/
bool is_token_redir(struct token *token);
/* /*
* @brief: returns the next (newly allocated) token without consuming it. * @brief: returns the next (newly allocated) token without consuming it.
* if end of input is reached, returns a token of type TOKEN_EOF. * if end of input is reached, returns a token of type TOKEN_EOF.

View file

@ -178,6 +178,25 @@ static bool is_end_of_line(char c)
return c == EOF || c == '\n'; return c == EOF || c == '\n';
} }
// === Functions
bool is_token_redir(struct token *token)
{
switch (token->type)
{
case TOKEN_REDIR_LEFT:
case TOKEN_REDIR_RIGHT:
case TOKEN_REDIR_LEFT_RIGHT:
case TOKEN_REDIR_DOUBLE_RIGHT:
case TOKEN_REDIR_LEFT_AMP:
case TOKEN_REDIR_RIGHT_AMP:
case TOKEN_REDIR_RIGHT_PIPE:
return true;
default:
return false;
}
}
bool is_special_char(char *stream, ssize_t i) bool is_special_char(char *stream, ssize_t i)
{ {
char c = stream[i]; char c = stream[i];

View file

@ -31,16 +31,13 @@ enum lexing_mode
enum token_type enum token_type
{ {
// Special characters // Blanks
TOKEN_NULL = 0, TOKEN_NULL = 0,
TOKEN_EOF, TOKEN_EOF,
TOKEN_WORD, TOKEN_WORD,
TOKEN_NEWLINE, TOKEN_NEWLINE,
// WARNING: quote and double quote should never be used inside a token. // SPecial characters
TOKEN_QUOTE,
TOKEN_DOUBLE_QUOTE,
TOKEN_GRAVE, TOKEN_GRAVE,
TOKEN_SEMICOLON, TOKEN_SEMICOLON,
TOKEN_COMMENT, TOKEN_COMMENT,
@ -51,8 +48,14 @@ enum token_type
TOKEN_RIGHT_PAREN, TOKEN_RIGHT_PAREN,
TOKEN_LEFT_BRACKET, TOKEN_LEFT_BRACKET,
TOKEN_RIGHT_BRACKET, TOKEN_RIGHT_BRACKET,
TOKEN_PIPE,
TOKEN_NEGATION, // TODO handle
// redirections // TODO merge into one and use the data field
// (Too difficult to handle in the parser because of firsts)
// TOKEN_REDIRECTION
//
// Redirections
TOKEN_REDIR_LEFT, TOKEN_REDIR_LEFT,
TOKEN_REDIR_RIGHT, TOKEN_REDIR_RIGHT,
TOKEN_REDIR_LEFT_RIGHT, TOKEN_REDIR_LEFT_RIGHT,
@ -62,7 +65,6 @@ enum token_type
TOKEN_REDIR_RIGHT_PIPE, TOKEN_REDIR_RIGHT_PIPE,
TOKEN_IONUMBER, TOKEN_IONUMBER,
TOKEN_PIPE,
// Keywords // Keywords
TOKEN_IF, TOKEN_IF,

View file

@ -4,8 +4,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "grammar_basic.h"
#include "../lexer/lexer.h" #include "../lexer/lexer.h"
#include "grammar_basic.h"
// === Static variables // === Static variables
@ -34,8 +34,8 @@ static bool add_first(enum rule rule, enum token_type token)
// Append // Append
item->list_length++; item->list_length++;
item->tokens = realloc( item->tokens = realloc(item->tokens,
item->tokens, (item->list_length) * sizeof(enum token_type)); (item->list_length) * sizeof(enum token_type));
} }
else else
{ {
@ -91,24 +91,6 @@ static void add_first_redir(enum rule rule)
// === Functions // === Functions
bool is_token_redir(struct token *token)
{
switch (token->type)
{
case TOKEN_REDIR_LEFT:
case TOKEN_REDIR_RIGHT:
case TOKEN_REDIR_LEFT_RIGHT:
case TOKEN_REDIR_DOUBLE_RIGHT:
case TOKEN_REDIR_LEFT_AMP:
case TOKEN_REDIR_RIGHT_AMP:
case TOKEN_REDIR_RIGHT_PIPE:
return true;
default:
return false;
}
}
bool grammar_init(void) bool grammar_init(void)
{ {
// Initialize the firsts map // Initialize the firsts map
@ -119,17 +101,21 @@ bool grammar_init(void)
// Populate the firsts map // Populate the firsts map
add_first(RULE_INPUT, TOKEN_WORD); add_first(RULE_INPUT, TOKEN_WORD);
add_first(RULE_INPUT, TOKEN_IF); add_first(RULE_INPUT, TOKEN_IF);
add_first(RULE_COMMAND, TOKEN_NEGATION);
add_first(RULE_INPUT, TOKEN_NEWLINE); add_first(RULE_INPUT, TOKEN_NEWLINE);
add_first(RULE_INPUT, TOKEN_EOF); add_first(RULE_INPUT, TOKEN_EOF);
add_first(RULE_LIST, TOKEN_WORD); add_first(RULE_LIST, TOKEN_WORD);
add_first(RULE_LIST, TOKEN_IF); add_first(RULE_LIST, TOKEN_IF);
add_first(RULE_COMMAND, TOKEN_NEGATION);
add_first(RULE_AND_OR, TOKEN_WORD); add_first(RULE_AND_OR, TOKEN_WORD);
add_first(RULE_AND_OR, TOKEN_IF); add_first(RULE_AND_OR, TOKEN_IF);
add_first(RULE_COMMAND, TOKEN_NEGATION);
add_first(RULE_PIPELINE, TOKEN_WORD); add_first(RULE_PIPELINE, TOKEN_WORD);
add_first(RULE_PIPELINE, TOKEN_IF); add_first(RULE_PIPELINE, TOKEN_IF);
add_first(RULE_COMMAND, TOKEN_NEGATION);
add_first(RULE_COMMAND, TOKEN_WORD); add_first(RULE_COMMAND, TOKEN_WORD);
add_first(RULE_COMMAND, TOKEN_IF); add_first(RULE_COMMAND, TOKEN_IF);

View file

@ -50,10 +50,6 @@ struct firsts_list {
// === Functions // === Functions
/* @brief: returns true if token has a type redirection (not pipe or IONUMBER).
*/
bool is_token_redir(struct token *token);
/* /*
* @brief Initializes the grammar submodule * @brief Initializes the grammar submodule
* @return PARSER_INIT_SUCCESS on success PARSER_INIT_ERROR on error * @return PARSER_INIT_SUCCESS on success PARSER_INIT_ERROR on error

View file

@ -1,13 +1,13 @@
#include <stdbool.h>
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include "grammar_basic.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "../utils/lists/lists.h" #include "../utils/lists/lists.h"
#include "grammar.h" #include "grammar.h"
#include "grammar_advanced.h" #include "grammar_advanced.h"
#include "grammar_basic.h"
// === Static functions // === Static functions
@ -108,7 +108,42 @@ struct ast *parse_and_or(struct lexer_context *ctx)
struct ast *parse_pipeline(struct lexer_context *ctx) struct ast *parse_pipeline(struct lexer_context *ctx)
{ {
return parse_command(ctx); bool negation = false;
struct token *token = PEEK_TOKEN();
// Eventual '!'
if (token->type == TOKEN_NEGATION)
{
negation = true;
POP_TOKEN();
token = PEEK_TOKEN();
}
// TODO handle negation (new AST type)
struct ast *left = parse_command(ctx);
token = PEEK_TOKEN();
while (token->type == TOKEN_PIPE)
{
POP_TOKEN();
// skip newlines
token = PEEK_TOKEN();
while (token->type == TOKEN_NEWLINE)
{
POP_TOKEN();
token = PEEK_TOKEN();
}
struct ast *right = parse_command(ctx);
// Create AST
left = ast_create_pipe(left, right);
token = PEEK_TOKEN();
}
return left;
} }
struct ast *parse_command(struct lexer_context *ctx) struct ast *parse_command(struct lexer_context *ctx)
@ -125,7 +160,7 @@ struct ast *parse_command(struct lexer_context *ctx)
} }
else else
{ {
puts("Syntax error: expected command"); puts("Syntax error: unexpected token");
return NULL; return NULL;
} }
} }
@ -324,7 +359,7 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
return NULL; return NULL;
result_list = list_append(result_list, current_cmd); result_list = list_append(result_list, current_cmd);
} }
token = PEEK_TOKEN(); token = PEEK_TOKEN();
} }

View file

@ -28,9 +28,9 @@ struct ast *parse_and_or(struct lexer_context *ctx);
/* /*
* @brief Only parses a command rule for the moment * @brief Only parses a command rule for the moment
* *
* @code pipeline = command ; * @code pipeline = ['!'] command { '|' {'\n'} command } ;
* *
* @first first(command) * @first '!', first(command)
*/ */
struct ast *parse_pipeline(struct lexer_context *ctx); struct ast *parse_pipeline(struct lexer_context *ctx);

View file

@ -2,7 +2,6 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include "ast.h" #include "ast.h"
#include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
@ -11,18 +10,45 @@
void ast_free(struct ast **node) void ast_free(struct ast **node)
{ {
if (node == NULL || *node == NULL) if (node == NULL || *node == NULL)
{
puts(
"WARNING: Internal error: failed to free AST node (NULL argument)");
return; 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)); ast_free_if(ast_get_if(*node));
else if (ast_is_command(*node)) break;
case AST_CMD:
ast_free_command(ast_get_command(*node)); ast_free_command(ast_get_command(*node));
else if (ast_is_list(*node)) break;
case AST_LIST:
ast_free_list(ast_get_list(*node)); 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)); 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)); 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); free(*node);
*node = NULL; *node = NULL;
@ -40,6 +66,7 @@ struct ast *ast_create(enum ast_type type, void *data)
return node; return node;
} }
// TODO handle new types (AST_WORD, AST_PIPE, etc.)
static void ast_print_dot_recursive(struct ast *node, FILE *out) static void ast_print_dot_recursive(struct ast *node, FILE *out)
{ {
if (!node) if (!node)

View file

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

View file

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

View file

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

View file

@ -1,6 +1,5 @@
#include "ast_command.h" #include "ast_command.h"
#include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.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) struct ast_command *ast_get_command(struct ast *node)
{ {
assert(node != NULL); if (node == NULL || node->type == AST_CMD)
assert(node->type == AST_CMD); return NULL;
return (struct ast_command *)node->data; return (struct ast_command *)node->data;
} }
bool ast_is_command(struct ast *node) bool ast_is_command(struct ast *node)
{ {
assert(node != NULL); return node != NULL && node->type == AST_CMD;
return node->type == AST_CMD;
} }
void ast_free_command(struct ast_command *command_data) void ast_free_command(struct ast_command *command_data)

View file

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

View file

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

View file

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

View file

@ -1,6 +1,5 @@
#include "ast_if.h" #include "ast_if.h"
#include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.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) struct ast_if *ast_get_if(struct ast *node)
{ {
assert(node != NULL); if (node == NULL || node->type == AST_IF)
assert(node->type == AST_IF); return NULL;
return (struct ast_if *)node->data; return node->data;
} }
bool ast_is_if(struct ast *node) bool ast_is_if(struct ast *node)
{ {
assert(node != NULL); return node != NULL && node->type == AST_IF;
return node->type == AST_IF;
} }
void ast_free_if(struct ast_if *if_data) void ast_free_if(struct ast_if *if_data)

View file

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

View file

@ -1,6 +1,4 @@
#include <assert.h> #include "ast_list.h"
#include "ast.h"
struct ast *ast_create_list(struct list *list) 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) struct ast_list *ast_get_list(struct ast *node)
{ {
assert(node != NULL); if (node == NULL)
return (struct ast_list *)node->data; return NULL;
return node->data;
} }
bool ast_is_list(struct ast *node) 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) void ast_free_list(struct ast_list *ast_list)

View file

@ -1,8 +1,6 @@
#ifndef AST_LIST_H #ifndef AST_LIST_H
#define AST_LIST_H #define AST_LIST_H
#include <stdbool.h>
#include "../lists/lists.h" #include "../lists/lists.h"
#include "ast_base.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 #ifndef AST_REDIR_H
#define AST_REDIR_H #define AST_REDIR_H
#include <stdbool.h>
#include "ast_base.h" #include "ast_base.h"
enum ast_redir_type enum ast_redir_type

View file

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

View file

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

View file

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