refactor: god struct lexer and error message

This commit is contained in:
Quinten Mennen 2025-02-05 15:52:30 +01:00
parent 1722fff4c7
commit b909668a1a

View File

@ -12,6 +12,7 @@
#include "minishell.h" #include "minishell.h"
#include "prompt.h" #include "prompt.h"
#include <stdio.h>
void print_banner(void) void print_banner(void)
{ {
@ -26,6 +27,11 @@ void print_list(void *content)
{ {
t_token *token; t_token *token;
token = (t_token *)content; token = (t_token *)content;
if (token->type == T_ERROR)
{
printf("An error occurred near position %i\n", token->position);
}
else
ft_printf("%s\n", token->value); ft_printf("%s\n", token->value);
} }
@ -33,7 +39,6 @@ int main(int argc, char **argv, char **envp)
{ {
t_enviroment *enviroment; t_enviroment *enviroment;
char *line; char *line;
t_lexer *lexer;
t_list *list; t_list *list;
t_minishell *minishell; t_minishell *minishell;
@ -53,8 +58,8 @@ int main(int argc, char **argv, char **envp)
line = ft_prompt(minishell); line = ft_prompt(minishell);
if (line != NULL) if (line != NULL)
add_history(line); add_history(line);
lexer = ft_lexer_new(line); minishell->lexer = ft_lexer_new(line);
list = ft_parse_input(lexer); list = ft_parse_input(minishell->lexer);
ft_lstiter(list, print_list); ft_lstiter(list, print_list);
free(line); free(line);
if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "clear\0", 6) == 0) if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "clear\0", 6) == 0)
@ -64,10 +69,10 @@ int main(int argc, char **argv, char **envp)
else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0) else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0)
break; break;
ft_lstclear(&list, ft_clear_tokenlist); ft_lstclear(&list, ft_clear_tokenlist);
ft_lexer_free(lexer); ft_lexer_free(minishell->lexer);
} }
ft_lstclear(&list, ft_clear_tokenlist); ft_lstclear(&list, ft_clear_tokenlist);
ft_lexer_free(lexer); ft_lexer_free(minishell->lexer);
// print_enviroment(enviroment); // print_enviroment(enviroment);
free_enviroment(enviroment); free_enviroment(enviroment);
free(minishell); free(minishell);