fix: small bugs to make it compile

This commit is contained in:
Matteo Flebus 2026-01-27 18:00:59 +01:00
parent c48d86c8de
commit 13018e0a03
10 changed files with 33 additions and 4 deletions

View file

@ -32,7 +32,10 @@ static int main_loop(struct lexer_context *ctx, struct args_options *options,
{ {
int return_code = SUCCESS; int return_code = SUCCESS;
// init parser // init parser
int parser_init(); if (!parser_init())
{
perror("parser initialization failed.");
}
// Retrieve and build first AST // Retrieve and build first AST
struct ast *command_ast = get_ast(ctx); struct ast *command_ast = get_ast(ctx);

View file

@ -186,7 +186,10 @@ struct ast *parse_input(struct lexer_context *ctx)
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_EOF) if (token->type == TOKEN_EOF)
return ast_create_list(NULL); {
POP_TOKEN();
return ast_create_end();
}
if (token->type == TOKEN_NEWLINE) if (token->type == TOKEN_NEWLINE)
{ {

View file

@ -22,6 +22,9 @@ static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
struct ast *parse_redirection(struct lexer_context *ctx) struct ast *parse_redirection(struct lexer_context *ctx)
{ {
(void)ctx;
return NULL;
/*
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
int io_number = -1; int io_number = -1;
if (token->type == TOKEN_IONUMBER) if (token->type == TOKEN_IONUMBER)
@ -53,6 +56,7 @@ struct ast *parse_redirection(struct lexer_context *ctx)
POP_TOKEN(); POP_TOKEN();
return ast_create_redir(io_number, redir_type, target); return ast_create_redir(io_number, redir_type, target);
*/
} }
struct ast *parse_prefix(struct lexer_context *ctx) struct ast *parse_prefix(struct lexer_context *ctx)

View file

@ -25,6 +25,16 @@ bool parser_init(void)
return true; return true;
} }
void parser_close(void)
{
if (state != PARSER_STATE_READY)
{
perror("trying to close parser which was not opened");
}
state = PARSER_STATE_CLOSED;
// TODO close grammar
}
struct ast *get_ast(struct lexer_context *ctx) struct ast *get_ast(struct lexer_context *ctx)
{ {
if (ctx == NULL) if (ctx == NULL)

View file

@ -14,6 +14,9 @@ libutils_a_SOURCES = \
ast/ast_redir.c \ ast/ast_redir.c \
ast/ast_void.c \ ast/ast_void.c \
ast/ast_end.c \ ast/ast_end.c \
ast/ast_word.c \
ast/ast_neg.c \
ast/ast_pipe.c \
args/args.c \ args/args.c \
vars/vars.c vars/vars.c

View file

@ -11,6 +11,7 @@
#include "ast_void.h" #include "ast_void.h"
#include "ast_word.h" #include "ast_word.h"
#include "ast_pipe.h" #include "ast_pipe.h"
#include "ast_neg.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

@ -29,6 +29,7 @@ struct ast
* - struct ast_command* (AST_CMD) * - struct ast_command* (AST_CMD)
* - struct ast_and_or* (AST_AND_OR) * - struct ast_and_or* (AST_AND_OR)
* - struct ast_redir* (AST_REDIR) * - struct ast_redir* (AST_REDIR)
* - and a lot more now...
*/ */
void *data; void *data;
}; };

View file

@ -18,7 +18,7 @@ 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)
{ {
if (node == NULL || node->type == AST_CMD) if (node == NULL || node->type != AST_CMD)
return NULL; return NULL;
return (struct ast_command *)node->data; return (struct ast_command *)node->data;
} }

View file

@ -4,7 +4,7 @@
bool ast_is_neg(struct ast *node) bool ast_is_neg(struct ast *node)
{ {
return node != NULL && node->type == AST_REDIR; return node != NULL && node->type == AST_NEG;
} }
struct ast_neg *ast_get_neg(struct ast *node) struct ast_neg *ast_get_neg(struct ast *node)
@ -20,6 +20,8 @@ struct ast *ast_create_neg(bool negation, struct ast *child)
if (!node) if (!node)
return NULL; return NULL;
node->negation = negation;
node->child = child;
return ast_create(AST_NEG, node); return ast_create(AST_NEG, node);
} }

View file

@ -1,5 +1,7 @@
#define _POSIX_C_SOURCE 200809L
#include "ast_word.h" #include "ast_word.h"
#include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>