chore: split up function in multiple files
This commit is contained in:
parent
8f9c2e33e8
commit
a88a4656e3
2
Makefile
2
Makefile
@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a
|
||||
|
||||
OBJ_PATH = obj
|
||||
|
||||
VPATH = src:src/enviroment:src/prompt:src/tokenizer:src/utils
|
||||
VPATH = src:src/enviroment:src/prompt:src/lexer:src/token:src/utils
|
||||
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
|
||||
|
||||
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
|
||||
|
||||
32
src/lexer/lexer_new.c
Normal file
32
src/lexer/lexer_new.c
Normal file
@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lexer_new.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/05 19:03:01 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:08:41 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_lexer *ft_lexer_new(const char *input)
|
||||
{
|
||||
t_lexer *lexer;
|
||||
|
||||
lexer = malloc(sizeof(t_lexer));
|
||||
if (!lexer)
|
||||
{
|
||||
perror("failed assigning lexer memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
lexer->input = ft_strdup(input);
|
||||
lexer->pos = 0;
|
||||
lexer->n_pos = 1;
|
||||
lexer->current_char = '\0';
|
||||
if (ft_strlen(input) > 0)
|
||||
lexer->current_char = *input;
|
||||
return (lexer);
|
||||
}
|
||||
41
src/lexer/lexer_parse_input.c
Normal file
41
src/lexer/lexer_parse_input.c
Normal file
@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lexer_parse_input.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/05 19:09:20 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:09:26 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
/**
|
||||
* @brief Parses the input from the lexer and returns a list of tokens.
|
||||
*
|
||||
* This function continuously retrieves the next token from the lexer and adds
|
||||
* it to a linked list until an end-of-file (EOF) or error token is encountered.
|
||||
* The list of tokens is then returned.
|
||||
*
|
||||
* @param lexer A pointer to the lexer structure containing
|
||||
* the input to be parsed.
|
||||
* @return A linked list of tokens parsed from the input.
|
||||
*/
|
||||
t_list *ft_parse_input(t_lexer *lexer)
|
||||
{
|
||||
t_list *list;
|
||||
t_token *token;
|
||||
|
||||
list = NULL;
|
||||
while (TRUE)
|
||||
{
|
||||
token = ft_token_next(lexer);
|
||||
if (token->type == T_EOF)
|
||||
break ;
|
||||
ft_lstadd_back(&list, ft_lstnew(token));
|
||||
}
|
||||
ft_token_free(token);
|
||||
return (list);
|
||||
}
|
||||
26
src/lexer/lexer_read_char.c
Normal file
26
src/lexer/lexer_read_char.c
Normal file
@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lexer_readchar.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/05 19:04:53 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:05:06 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void ft_lexer_readchar(t_lexer *lexer)
|
||||
{
|
||||
if ((size_t)lexer->n_pos > ft_strlen(lexer->input))
|
||||
{
|
||||
lexer->current_char = '\0';
|
||||
return ;
|
||||
}
|
||||
lexer->current_char = lexer->input[lexer->n_pos];
|
||||
lexer->pos = lexer->n_pos;
|
||||
lexer->n_pos++;
|
||||
}
|
||||
|
||||
@ -1,48 +1,17 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lexer.c :+: :+: :+: */
|
||||
/* lexer_readword.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/04 18:04:07 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/04 20:53:26 by qmennen ### ########.fr */
|
||||
/* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:05:03 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_lexer *ft_lexer_new(const char *input)
|
||||
{
|
||||
t_lexer *lexer;
|
||||
|
||||
lexer = malloc(sizeof(t_lexer));
|
||||
if (!lexer)
|
||||
{
|
||||
perror("failed assigning lexer memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
lexer->input = ft_strdup(input);
|
||||
lexer->pos = 0;
|
||||
lexer->n_pos = 1;
|
||||
lexer->current_char = '\0';
|
||||
if (ft_strlen(input) > 0)
|
||||
lexer->current_char = *input;
|
||||
return (lexer);
|
||||
}
|
||||
|
||||
void ft_lexer_readchar(t_lexer *lexer)
|
||||
{
|
||||
if ((size_t)lexer->n_pos > ft_strlen(lexer->input))
|
||||
{
|
||||
lexer->current_char = '\0';
|
||||
return ;
|
||||
}
|
||||
lexer->current_char = lexer->input[lexer->n_pos];
|
||||
lexer->pos = lexer->n_pos;
|
||||
lexer->n_pos++;
|
||||
}
|
||||
|
||||
static char *ft_parse_quotes(t_lexer *lexer)
|
||||
{
|
||||
int start;
|
||||
@ -6,40 +6,12 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/04 20:57:25 by qmennen ### ########.fr */
|
||||
/* Updated: 2025/02/05 19:09:35 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
/**
|
||||
* @brief Parses the input from the lexer and returns a list of tokens.
|
||||
*
|
||||
* This function continuously retrieves the next token from the lexer and adds
|
||||
* it to a linked list until an end-of-file (EOF) or error token is encountered.
|
||||
* The list of tokens is then returned.
|
||||
*
|
||||
* @param lexer A pointer to the lexer structure containing
|
||||
* the input to be parsed.
|
||||
* @return A linked list of tokens parsed from the input.
|
||||
*/
|
||||
t_list *ft_parse_input(t_lexer *lexer)
|
||||
{
|
||||
t_list *list;
|
||||
t_token *token;
|
||||
|
||||
list = NULL;
|
||||
while (TRUE)
|
||||
{
|
||||
token = ft_token_next(lexer);
|
||||
if (token->type == T_EOF)
|
||||
break ;
|
||||
ft_lstadd_back(&list, ft_lstnew(token));
|
||||
}
|
||||
ft_token_free(token);
|
||||
return (list);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Retrieves the next token from the lexer.
|
||||
*
|
||||
32
src/token/token_new.c
Normal file
32
src/token/token_new.c
Normal file
@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* token_new.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/05 19:10:49 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:11:32 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_token *ft_token_new(t_token_type type, char *c, int pos)
|
||||
{
|
||||
t_token *token;
|
||||
|
||||
token = malloc(sizeof(t_token));
|
||||
if (!token)
|
||||
{
|
||||
perror("failed assigning token memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
token->type = type;
|
||||
token->position = pos;
|
||||
if (c)
|
||||
token->value = ft_strdup(c);
|
||||
else
|
||||
token->value = NULL;
|
||||
return (token);
|
||||
}
|
||||
@ -1,17 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tokens.c :+: :+: :+: */
|
||||
/* parse_token.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/04 18:02:56 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/04 20:53:10 by qmennen ### ########.fr */
|
||||
/* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:11:21 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
|
||||
t_token *ft_parse_token(t_lexer *lexer)
|
||||
{
|
||||
t_token *token;
|
||||
@ -37,22 +38,3 @@ t_token *ft_parse_token(t_lexer *lexer)
|
||||
ft_lexer_readchar(lexer);
|
||||
return (token);
|
||||
}
|
||||
|
||||
t_token *ft_token_new(t_token_type type, char *c, int pos)
|
||||
{
|
||||
t_token *token;
|
||||
|
||||
token = malloc(sizeof(t_token));
|
||||
if (!token)
|
||||
{
|
||||
perror("failed assigning token memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
token->type = type;
|
||||
token->position = pos;
|
||||
if (c)
|
||||
token->value = ft_strdup(c);
|
||||
else
|
||||
token->value = NULL;
|
||||
return (token);
|
||||
}
|
||||
20
src/utils/free_lexer.c
Normal file
20
src/utils/free_lexer.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free_lexer.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/05 19:07:01 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:07:18 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void ft_lexer_free(t_lexer *lexer)
|
||||
{
|
||||
if (lexer->input)
|
||||
free(lexer->input);
|
||||
free(lexer);
|
||||
}
|
||||
20
src/utils/free_token.c
Normal file
20
src/utils/free_token.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free_token.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/05 19:07:45 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:07:54 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void ft_token_free(t_token *token)
|
||||
{
|
||||
if (token->value)
|
||||
free(token->value);
|
||||
free(token);
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lexer_utils.c :+: :+: :+: */
|
||||
/* free_token_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/04 20:54:09 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/04 20:54:36 by qmennen ### ########.fr */
|
||||
/* Created: 2025/02/05 19:08:14 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/05 19:08:31 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,17 +19,3 @@ void ft_clear_tokenlist(void *content)
|
||||
token = (t_token *)content;
|
||||
ft_token_free(token);
|
||||
}
|
||||
|
||||
void ft_token_free(t_token *token)
|
||||
{
|
||||
if (token->value)
|
||||
free(token->value);
|
||||
free(token);
|
||||
}
|
||||
|
||||
void ft_lexer_free(t_lexer *lexer)
|
||||
{
|
||||
if (lexer->input)
|
||||
free(lexer->input);
|
||||
free(lexer);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user