feat: redirection rules

This commit is contained in:
Gu://em_ 2026-01-24 15:34:10 +01:00
parent 18c1da6bdf
commit 32f56beb6b
8 changed files with 218 additions and 25 deletions

View file

@ -3,9 +3,9 @@
#include <stdio.h>
#include <string.h>
#include "../lexer/lexer.h"
#include "../utils/lists/lists.h"
#include "grammar.h"
#include "grammar_advanced.h"
// === Static functions
@ -124,46 +124,86 @@ struct ast *parse_command(struct lexer_context *ctx)
struct ast *parse_simple_command(struct lexer_context *ctx)
{
struct list *command_elements = NULL;
struct token *token = PEEK_TOKEN();
// WORD
struct token *token = POP_TOKEN();
if (token->type != TOKEN_WORD)
{
puts("Expected a command but got a different token type");
return NULL;
}
char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
token = PEEK_TOKEN();
// Eventual elements
while (token->type == TOKEN_WORD)
{
token = pop_token(ctx);
if (token == NULL)
// Get element
struct ast *element = parse_element(ctx);
if (element == NULL)
{
// TODO free
list_deep_destroy(command_elements);
return NULL;
}
char *word = strdup(token->data);
if (word == NULL)
// Get element type
if (ast_is_word(element))
{
// TODO free
puts("Internal error: Couldn't copy token content (is memory full "
"?)");
struct ast_word *element_word = ast_get_word(element);
command_elements =
list_append(command_elements, element_word->word);
}
else if (ast_is_redir(element))
{
// TODO
puts("NOT IMPLEMENTED");
return NULL;
}
command_elements = list_append(command_elements, word);
token = peek_token(ctx);
if (token == NULL)
else
{
// TODO free
puts("Internal error: unexpected return value from parse_element "
"in parse_simple_command");
list_deep_destroy(command_elements);
return NULL;
}
// Forward
POP_TOKEN();
token = PEEK_TOKEN();
}
// Result
struct ast *result = ast_create_command(command_elements);
if (result == NULL)
{
// TODO free
list_deep_destroy(command_elements);
return NULL;
}
return result;
}
struct ast *parse_element(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_WORD)
{
// TODO
puts("NOT IMPLEMENTED");
return NULL;
}
else if (token->type == TOKEN_IONUMBER || token->type == TOKEN_REDIRECTION)
{
return parse_redirection(ctx);
}
else
{
puts("Syntax error: unexpected token at parse_element");
return NULL;
}
}
struct ast *parse_shell_command(struct lexer_context *ctx)
{
return parse_if_rule(ctx);