diff --git a/include/parser.h b/include/parser.h index 4c74392f..8692f760 100644 --- a/include/parser.h +++ b/include/parser.h @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/08 19:57:56 by lyeh #+# #+# */ -/* Updated: 2023/12/18 22:03:01 by lyeh ### ########.fr */ +/* Updated: 2023/12/22 20:52:14 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,16 +16,16 @@ t_pt_node *get_next_pt_entry(int state, int token_type, int action); -bool push_state(t_stack **state_stack, int next_step); -bool push_token(t_stack **parse_stack, t_token *token); +bool push_state(t_list **state_stack, int next_step); +bool push_token(t_list **parse_stack, t_token *token); bool parse_shift(t_token *input_token, - t_stack **state_stack, t_stack **parse_stack, int next_step); -bool parse_reduce(t_stack **state_stack, - t_stack **parse_stack, t_pt_node *pt_entry); -bool parse_goto(t_stack **state_stack, int token_type); + t_list **state_stack, t_list **parse_stack, int next_step); +bool parse_reduce(t_list **state_stack, + t_list **parse_stack, t_pt_node *pt_entry); +bool parse_goto(t_list **state_stack, int token_type); -bool init_parse(t_stack **state_stack, t_stack **parse_stack); -void free_parse(t_stack **state_stack, t_stack **parse_stack); +bool init_parse(t_list **state_stack, t_list **parse_stack); +void free_parse(t_list **state_stack, t_list **parse_stack); bool ft_parse(t_list **token_list); #endif diff --git a/include/utils.h b/include/utils.h index da6768e3..1443383a 100644 --- a/include/utils.h +++ b/include/utils.h @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/17 13:38:17 by lyeh #+# #+# */ -/* Updated: 2023/12/19 21:13:55 by lyeh ### ########.fr */ +/* Updated: 2023/12/22 20:53:30 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,20 +14,20 @@ # define UTILS_H # include "defines.h" -bool drop_num_stack(t_stack **stack, int num, void (*del)(void *)); +bool drop_num_stack(t_list **stack, int num, void (*del)(void *)); t_token *init_token_node(int type, char *data); void free_token_node(void *content); char *ft_get_token_type_str(int type); -int get_state_from_stack(t_stack *node); -t_token *get_token_from_stack(t_stack *node); +int get_state_from_stack(t_list *node); +t_token *get_token_from_stack(t_list *node); int get_token_type_from_list(t_list *token_list); char *get_token_data_from_list(t_list *token_list); -void print_state_stack(t_stack *stack); -void print_parse_stack(t_stack *stack); +void print_state_stack(t_list *stack); +void print_parse_stack(t_list *stack); void print_token_list(t_list *token_list); diff --git a/libraries/libft/build/libft.mk b/libraries/libft/build/libft.mk index 4c747c34..d2580f2a 100644 --- a/libraries/libft/build/libft.mk +++ b/libraries/libft/build/libft.mk @@ -6,7 +6,7 @@ # By: ldulling +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/16 13:33:38 by ldulling #+# #+# # -# Updated: 2023/12/20 22:20:35 by ldulling ### ########.fr # +# Updated: 2023/12/23 20:58:20 by ldulling ### ########.fr # # # # **************************************************************************** # @@ -43,6 +43,17 @@ TMP += $(addprefix $(DIR)$(SUBDIR), \ ) # Lists: +# Doubly-linked: +SUBDIR := lists/doubly_linked/ +TMP += $(addprefix $(DIR)$(SUBDIR), \ + ft_lstadd_back_d.c \ + ft_lstadd_front_d.c \ + ft_lstclear_d.c \ + ft_lstdelone_d.c \ + ft_lstlast_d.c \ + ft_lstnew_d.c \ +) + # Singly-linked: SUBDIR := lists/singly_linked/ TMP += $(addprefix $(DIR)$(SUBDIR), \ @@ -55,20 +66,11 @@ TMP += $(addprefix $(DIR)$(SUBDIR), \ ft_lstlast.c \ ft_lstmap.c \ ft_lstnew.c \ - ft_lstpop.c \ - ft_lstpop_content.c \ + ft_lstpop_front.c \ + ft_lstpop_front_content.c \ ft_lstsize.c \ ft_lstsort_bubble.c \ -) - -# Doubly-linked: -SUBDIR := lists/doubly_linked/ -TMP += $(addprefix $(DIR)$(SUBDIR), \ - ft_lstadd_back_d.c \ - ft_lstadd_front_d.c \ - ft_lstclear_d.c \ - ft_lstlast_d.c \ - ft_lstnew_d.c \ + ft_lstswap_head.c \ ) # Memory: @@ -104,18 +106,6 @@ TMP += $(addprefix $(DIR)$(SUBDIR), \ ft_putstr_fd.c \ ) -# Stack: -SUBDIR := stack/ -TMP += $(addprefix $(DIR)$(SUBDIR), \ - ft_stkclear.c \ - ft_stkdelone.c \ - ft_stknew.c \ - ft_stkpeektop.c \ - ft_stkpop.c \ - ft_stkpush.c \ - ft_stksize.c \ -) - # Strings: SUBDIR := strings/ TMP += $(addprefix $(DIR)$(SUBDIR), \ diff --git a/libraries/libft/inc/libft.h b/libraries/libft/inc/libft.h index 6be87a02..142b6b69 100644 --- a/libraries/libft/inc/libft.h +++ b/libraries/libft/inc/libft.h @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:17:46 by ldulling #+# #+# */ -/* Updated: 2023/12/20 22:19:36 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 20:58:20 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,13 +34,9 @@ typedef struct s_list_d struct s_list_d *next; } t_list_d; -typedef struct s_stack -{ - void *content; - struct s_stack *next; -} t_stack; - +\ /* Chars */ + int ft_isalnum(int c); int ft_isalpha(int c); int ft_isascii(int c); @@ -50,7 +46,19 @@ int ft_isspace(int c); int ft_tolower(int c); int ft_toupper(int c); +\ +/* Lists doubly-linked */ + +void ft_lstadd_back_d(t_list_d **lst, t_list_d *new); +void ft_lstadd_front_d(t_list_d **lst, t_list_d *new); +void ft_lstclear_d(t_list_d **lst, void (*del)(void *)); +void ft_lstdelone_d(t_list_d *lst, void (*del)(void *)); +t_list_d *ft_lstlast_d(t_list_d *lst); +t_list_d *ft_lstnew_d(void *content); + +\ /* Lists singly-linked */ + void ft_lstadd_back(t_list **lst, t_list *new); void ft_lstadd_front(t_list **lst, t_list *new); void ft_lstclear(t_list **lst, void (*del)(void *)); @@ -60,19 +68,15 @@ void ft_lstiter(t_list *lst, void (*f)(void *)); t_list *ft_lstlast(t_list *lst); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); t_list *ft_lstnew(void *content); -t_list *ft_lstpop(t_list **lst); -void *ft_lstpop_content(t_list **lst); +t_list *ft_lstpop_front(t_list **lst); +void *ft_lstpop_front_content(t_list **lst); int ft_lstsize(t_list *lst); void ft_lstsort_bubble(t_list **lst, void *(*cmp)(void *, void *)); +void ft_lstswap_head(t_list **lst); -/* Lists doubly-linked */ -void ft_lstadd_back_d(t_list_d **lst, t_list_d *new); -void ft_lstadd_front_d(t_list_d **lst, t_list_d *new); -void ft_lstclear_d(t_list_d **lst, void (*del)(void *)); -t_list_d *ft_lstlast_d(t_list_d *lst); -t_list_d *ft_lstnew_d(void *content); - +\ /* Memory */ + void ft_bzero(void *s, size_t n); void *ft_calloc(size_t nmemb, size_t size); void ft_free_and_null(void **ptr); @@ -82,11 +86,14 @@ void *ft_memcpy(void *dest, const void *src, size_t n); void *ft_memmove(void *dest, const void *src, size_t n); void *ft_memset(void *s, int c, size_t n); +\ /* Numbers */ + double ft_atof(const char *nptr); int ft_atoi(const char *nptr); long ft_atol(const char *nptr); +\ /* Put */ void ft_putchar_fd(char c, int fd); void ft_putendl_fd(char *s, int fd); @@ -96,16 +103,9 @@ size_t ft_putnchar_fd(unsigned char c, size_t n, int fd); size_t ft_putnstr_fd(const char *s, size_t n, int fd); void ft_putstr_fd(char *s, int fd); -/* Stack */ -void ft_stkclear(t_stack **stk, void (*del)(void *)); -void ft_stkdelone(t_stack *stk, void (*del)(void *)); -t_stack *ft_stknew(void *content); -t_stack *ft_stkpeektop(t_stack *stk); -t_stack *ft_stkpop(t_stack **stk); -void ft_stkpush(t_stack **stk, t_stack *new); -int ft_stksize(t_stack *stk); - +\ /* Strings */ + char *ft_itoa(int n); char **ft_split(char const *s, char c); char **ft_split_at_index(char *str, size_t index); diff --git a/libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_back_d.c b/libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_back_d.c index 1af58b43..7754f81c 100644 --- a/libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_back_d.c +++ b/libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_back_d.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:12 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:08:49 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 14:05:01 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,23 @@ /** * Does not mark the new node as the end of the list by itself. + * Therefore, a whole list can be appended, not just one node. */ void ft_lstadd_back_d(t_list_d **lst, t_list_d *new) { t_list_d *cur; - if (lst != NULL && new != NULL) + if (lst == NULL || new == NULL) + return ; + if (*lst == NULL) + *lst = new; + else { - if (*lst == NULL) - *lst = new; - else - { - cur = *lst; - while (cur->next != NULL) - cur = cur->next; - cur->next = new; - new->prev = cur; - } + cur = *lst; + while (cur->next != NULL) + cur = cur->next; + cur->next = new; + new->prev = cur; } return ; } diff --git a/libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_front_d.c b/libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_front_d.c index d0b935f4..ed0148fd 100644 --- a/libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_front_d.c +++ b/libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_front_d.c @@ -6,24 +6,25 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:18 by ldulling #+# #+# */ -/* Updated: 2023/11/27 00:25:38 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 14:10:51 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +/** + * Can prepend a whole list, not just one node. + */ void ft_lstadd_front_d(t_list_d **lst, t_list_d *new) { - if (lst != NULL && new != NULL) + if (lst == NULL || new == NULL) + return ; + if (*lst == NULL) + *lst = new; + else { - if (*lst == NULL) - *lst = new; - else - { - new->next = *lst; - (*lst)->prev = new; - *lst = new; - } + ft_lstadd_back_d(&new, *lst); + *lst = new; } return ; } diff --git a/libraries/libft/src/libft/lists/doubly_linked/ft_lstclear_d.c b/libraries/libft/src/libft/lists/doubly_linked/ft_lstclear_d.c index 9eab53ac..0de4fd04 100644 --- a/libraries/libft/src/libft/lists/doubly_linked/ft_lstclear_d.c +++ b/libraries/libft/src/libft/lists/doubly_linked/ft_lstclear_d.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:22 by ldulling #+# #+# */ -/* Updated: 2023/11/27 00:31:29 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 14:25:30 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,15 +16,13 @@ void ft_lstclear_d(t_list_d **lst, void (*del)(void *)) { t_list_d *cur; - if (lst != NULL && del != NULL) + if (lst == NULL || del == NULL) + return ; + while (*lst != NULL) { - while (*lst != NULL) - { - cur = *lst; - *lst = (*lst)->next; - (*del)(cur->content); - free(cur); - } + cur = *lst; + *lst = (*lst)->next; + ft_lstdelone_d(cur, del); } return ; } diff --git a/libraries/libft/src/libft/stack/ft_stkdelone.c b/libraries/libft/src/libft/lists/doubly_linked/ft_lstdelone_d.c similarity index 61% rename from libraries/libft/src/libft/stack/ft_stkdelone.c rename to libraries/libft/src/libft/lists/doubly_linked/ft_lstdelone_d.c index 19b8c760..56c03ff4 100644 --- a/libraries/libft/src/libft/stack/ft_stkdelone.c +++ b/libraries/libft/src/libft/lists/doubly_linked/ft_lstdelone_d.c @@ -1,24 +1,23 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_stkdelone.c :+: :+: :+: */ +/* ft_lstdelone_d.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ +/* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/17 19:41:12 by lyeh #+# #+# */ -/* Updated: 2023/12/17 22:13:23 by lyeh ### ########.fr */ +/* Created: 2023/09/24 16:04:26 by ldulling #+# #+# */ +/* Updated: 2023/12/23 14:16:57 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_stkdelone(t_stack *stk, void (*del)(void *)) +void ft_lstdelone_d(t_list_d *lst, void (*del)(void *)) { - if (stk != NULL) - { - if (del != NULL && stk->content != NULL) - (*del)(stk->content); - free(stk); - } + if (lst == NULL) + return ; + if (del != NULL && lst->content != NULL) + (*del)(lst->content); + free(lst); return ; } diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstadd_back.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstadd_back.c index eede9486..78691c45 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstadd_back.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstadd_back.c @@ -6,31 +6,30 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:12 by ldulling #+# #+# */ -/* Updated: 2023/11/13 21:33:20 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 14:29:36 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + /** * Does not mark the new node as the end of the list by itself. + * Therefore, a whole list can be appended, not just one node. */ - -#include "libft.h" - void ft_lstadd_back(t_list **lst, t_list *new) { t_list *cur; - if (lst != NULL && new != NULL) + if (lst == NULL || new == NULL) + return ; + if (*lst == NULL) + *lst = new; + else { - if (*lst == NULL) - *lst = new; - else - { - cur = *lst; - while (cur->next != NULL) - cur = cur->next; - cur->next = new; - } + cur = *lst; + while (cur->next != NULL) + cur = cur->next; + cur->next = new; } return ; } diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstadd_front.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstadd_front.c index 90af169a..96b8e227 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstadd_front.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstadd_front.c @@ -6,23 +6,25 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:18 by ldulling #+# #+# */ -/* Updated: 2023/09/24 16:21:35 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 11:32:51 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +/** + * Can prepend a whole list, not just one node. + */ void ft_lstadd_front(t_list **lst, t_list *new) { - if (lst != NULL && new != NULL) + if (lst == NULL || new == NULL) + return ; + if (*lst == NULL) + *lst = new; + else { - if (*lst == NULL) - *lst = new; - else - { - new->next = *lst; - *lst = new; - } + ft_lstadd_back(&new, *lst); + *lst = new; } return ; } diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstclear.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstclear.c index a9aa317d..fb4d9172 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstclear.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstclear.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:22 by ldulling #+# #+# */ -/* Updated: 2023/11/27 00:16:44 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 11:17:18 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,15 +16,13 @@ void ft_lstclear(t_list **lst, void (*del)(void *)) { t_list *cur; - if (lst != NULL && del != NULL) + if (lst == NULL) + return ; + while (*lst != NULL) { - while (*lst != NULL) - { - cur = *lst; - *lst = (*lst)->next; - (*del)(cur->content); - free(cur); - } + cur = *lst; + *lst = (*lst)->next; + ft_lstdelone(cur, del); } return ; } diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstdelone.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstdelone.c index c1d285d3..84e8a2bc 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstdelone.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstdelone.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:26 by ldulling #+# #+# */ -/* Updated: 2023/09/26 12:35:15 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 11:16:35 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,10 @@ void ft_lstdelone(t_list *lst, void (*del)(void *)) { - if (lst != NULL && del != NULL) - { + if (lst == NULL) + return ; + if (del != NULL && lst->content != NULL) (*del)(lst->content); - free(lst); - } + free(lst); return ; } diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstinsert_after.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstinsert_after.c index d64a0018..68237886 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstinsert_after.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstinsert_after.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:12 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:09:44 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 11:39:56 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,22 +26,17 @@ */ void ft_lstinsert_after(t_list **lst, t_list *new) { - t_list *last_new; - t_list *old; + t_list *old_next; - if (lst != NULL && new != NULL) + if (lst == NULL && new == NULL) + return ; + if (*lst == NULL) + *lst = new; + else { - if (*lst == NULL) - *lst = new; - else - { - old = (*lst)->next; - (*lst)->next = new; - last_new = new; - while (last_new->next != NULL) - last_new = last_new->next; - last_new->next = old; - } + old_next = (*lst)->next; + (*lst)->next = new; + ft_lstlast(new)->next = old_next; } return ; } diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstiter.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstiter.c index 5bec81f6..2b82ba08 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstiter.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstiter.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:29 by ldulling #+# #+# */ -/* Updated: 2023/09/24 16:21:57 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 11:41:51 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,13 @@ void ft_lstiter(t_list *lst, void (*f)(void *)) { t_list *cur; - if (lst != NULL && f != NULL) + if (lst == NULL || f == NULL) + return ; + cur = lst; + while (cur != NULL) { - cur = lst; - while (cur != NULL) - { - (*f)(cur->content); - cur = cur->next; - } + (*f)(cur->content); + cur = cur->next; } return ; } diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstmap.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstmap.c index c1d9de74..faf7cc48 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstmap.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstmap.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:35 by ldulling #+# #+# */ -/* Updated: 2023/11/27 00:18:44 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 11:43:14 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) t_list *new_node; void *new_content; - if (!lst || !f || !del) + if (lst == NULL || f == NULL || del == NULL) return (NULL); new_lst = NULL; new_lst_tail = NULL; diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstpop.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstpop_front.c similarity index 72% rename from libraries/libft/src/libft/lists/singly_linked/ft_lstpop.c rename to libraries/libft/src/libft/lists/singly_linked/ft_lstpop_front.c index 56d9d47a..dc2703fb 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstpop.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstpop_front.c @@ -1,33 +1,34 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstpop.c :+: :+: :+: */ +/* ft_lstpop_front.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:29 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:11:51 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 21:00:54 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" /** - * The ft_lstpop function removes the first node from a singly linked list and - * returns it - like popping a stack. + * The ft_lstpop_front function removes the first node from a singly linked list + * and returns it - like popping a stack. * * @param lst A double pointer to the first node of the list. * * @return The removed node, or NULL if the list was empty. * */ -t_list *ft_lstpop(t_list **lst) +t_list *ft_lstpop_front(t_list **lst) { - t_list *popped; + t_list *popped_node; if (lst == NULL || *lst == NULL) return (NULL); - popped = *lst; + popped_node = *lst; *lst = (*lst)->next; - return (popped); + popped_node->next = NULL; + return (popped_node); } diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstpop_content.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstpop_front_content.c similarity index 78% rename from libraries/libft/src/libft/lists/singly_linked/ft_lstpop_content.c rename to libraries/libft/src/libft/lists/singly_linked/ft_lstpop_front_content.c index 01d1e136..7c13cdb0 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstpop_content.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstpop_front_content.c @@ -1,27 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstpop_content.c :+: :+: :+: */ +/* ft_lstpop_front_content.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:29 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:11:45 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 21:00:58 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" /** - * The ft_lstpop_content function removes and frees the first node from a singly - * linked list and returns its content - like popping a stack. + * The ft_lstpop_front_content function removes and frees the first node from a + * singly linked list and returns its content - like popping a stack. * * @param lst A double pointer to the first node of the list. * * @return The content of the removed node, or NULL if the list was empty. * */ -void *ft_lstpop_content(t_list **lst) +void *ft_lstpop_front_content(t_list **lst) { void *popped_content; t_list *popped_node; diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstsort_bubble.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstsort_bubble.c index 32078e26..94b8daba 100644 --- a/libraries/libft/src/libft/lists/singly_linked/ft_lstsort_bubble.c +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstsort_bubble.c @@ -6,42 +6,30 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/19 21:16:52 by ldulling #+# #+# */ -/* Updated: 2023/11/27 10:36:43 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 11:55:40 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static void swap_head(t_list **lst); static bool bubble_sort(t_list **lst, void *(*cmp)(void *, void *)); void ft_lstsort_bubble(t_list **lst, void *(*cmp)(void *, void *)) { bool is_sorted; - if (lst != NULL && *lst != NULL && (*lst)->next != NULL && cmp != NULL) + if (lst == NULL || *lst == NULL || (*lst)->next == NULL || cmp == NULL) + return ; + is_sorted = false; + while (is_sorted != true) { - is_sorted = false; - while (is_sorted != true) - { - if (!(*cmp)((*lst)->content, (*lst)->next->content)) - swap_head(lst); - is_sorted = bubble_sort(lst, cmp); - } + if (!(*cmp)((*lst)->content, (*lst)->next->content)) + ft_lstswap_head(lst); + is_sorted = bubble_sort(lst, cmp); } return ; } -static void swap_head(t_list **lst) -{ - t_list *tmp; - - tmp = (*lst)->next; - (*lst)->next = tmp->next; - tmp->next = *lst; - *lst = tmp; -} - static bool bubble_sort(t_list **lst, void *(*cmp)(void *, void *)) { t_list *cur; diff --git a/libraries/libft/src/libft/lists/singly_linked/ft_lstswap_head.c b/libraries/libft/src/libft/lists/singly_linked/ft_lstswap_head.c new file mode 100644 index 00000000..0a7e41ca --- /dev/null +++ b/libraries/libft/src/libft/lists/singly_linked/ft_lstswap_head.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstswap_head.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ldulling +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/23 11:52:20 by ldulling #+# #+# */ +/* Updated: 2023/12/23 12:08:21 by ldulling ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * The ft_lstswap_head function swaps the first two nodes of a singly linked + * list. + * + * @param lst A double pointer to the first node of the list. + * + * @return No return value. + * + * @note The function does nothing if the list is empty or contains only + * one node. + * + */ +void ft_lstswap_head(t_list **lst) +{ + t_list *new_head; + + if (lst == NULL || *lst == NULL || (*lst)->next == NULL) + return ; + new_head = (*lst)->next; + (*lst)->next = new_head->next; + new_head->next = *lst; + *lst = new_head; +} diff --git a/libraries/libft/src/libft/memory/ft_memcpy.c b/libraries/libft/src/libft/memory/ft_memcpy.c index 2f123e85..74a7248e 100644 --- a/libraries/libft/src/libft/memory/ft_memcpy.c +++ b/libraries/libft/src/libft/memory/ft_memcpy.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:08:20 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:26:15 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 12:17:36 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,11 +20,13 @@ * @param dest The memory area to copy to. * @param src The memory area to copy from. * @param n The number of bytes to copy. - * Note: Calling ft_memcpy with dest or src equal to NULL with n - * not 0 will cause a segmentation fault. * * @return Returns a pointer to dest. * + * @note Calling ft_memcpy with dest or src equal to NULL with n not 0 + * will cause a segmentation fault (mirrors the behavior of the + * original memcpy). + * */ void *ft_memcpy(void *dest, const void *src, size_t n) { diff --git a/libraries/libft/src/libft/memory/ft_memmove.c b/libraries/libft/src/libft/memory/ft_memmove.c index e4f3a270..fa74ad4d 100644 --- a/libraries/libft/src/libft/memory/ft_memmove.c +++ b/libraries/libft/src/libft/memory/ft_memmove.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:08:23 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:26:52 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 12:17:54 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,8 +24,10 @@ * @param n The number of bytes to copy. * * @return Returns a pointer to dest. - * Note: Calling ft_memcpy with dest or src equal to NULL with n - * not 0 will cause a segmentation fault. + * + * @note Calling ft_memcpy with dest or src equal to NULL with n not 0 + * will cause a segmentation fault (mirrors the behavior of the + * original memmove). * */ void *ft_memmove(void *dest, const void *src, size_t n) diff --git a/libraries/libft/src/libft/stack/ft_stkclear.c b/libraries/libft/src/libft/stack/ft_stkclear.c deleted file mode 100644 index 127be87e..00000000 --- a/libraries/libft/src/libft/stack/ft_stkclear.c +++ /dev/null @@ -1,30 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_stkclear.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/11 21:03:00 by lyeh #+# #+# */ -/* Updated: 2023/12/21 14:23:00 by lyeh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_stkclear(t_stack **stk, void (*del)(void *)) -{ - t_stack *cur; - - if (stk == NULL) - return ; - while (*stk != NULL) - { - cur = *stk; - *stk = (*stk)->next; - if (*del) - (*del)(cur->content); - free(cur); - } - return ; -} diff --git a/libraries/libft/src/libft/stack/ft_stknew.c b/libraries/libft/src/libft/stack/ft_stknew.c deleted file mode 100644 index 97474814..00000000 --- a/libraries/libft/src/libft/stack/ft_stknew.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_stknew.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/11 20:59:19 by lyeh #+# #+# */ -/* Updated: 2023/12/11 21:02:21 by lyeh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -t_stack *ft_stknew(void *content) -{ - t_stack *new_node; - - new_node = malloc(sizeof(t_stack)); - if (new_node == NULL) - return (NULL); - new_node->content = content; - new_node->next = NULL; - return (new_node); -} diff --git a/libraries/libft/src/libft/stack/ft_stkpop.c b/libraries/libft/src/libft/stack/ft_stkpop.c deleted file mode 100644 index 4784b128..00000000 --- a/libraries/libft/src/libft/stack/ft_stkpop.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_stkpop.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/11 21:07:28 by lyeh #+# #+# */ -/* Updated: 2023/12/11 21:08:22 by lyeh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -t_stack *ft_stkpop(t_stack **stk) -{ - t_stack *top; - - if (stk != NULL && *stk != NULL) - { - top = *stk; - *stk = (*stk)->next; - top->next = NULL; - return (top); - } - return (NULL); -} diff --git a/libraries/libft/src/libft/stack/ft_stkpush.c b/libraries/libft/src/libft/stack/ft_stkpush.c deleted file mode 100644 index 5293bee5..00000000 --- a/libraries/libft/src/libft/stack/ft_stkpush.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_stkpush.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/11 21:05:44 by lyeh #+# #+# */ -/* Updated: 2023/12/11 21:07:11 by lyeh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_stkpush(t_stack **stk, t_stack *new) -{ - if (stk != NULL && new != NULL) - { - if (*stk == NULL) - *stk = new; - else - { - new->next = *stk; - *stk = new; - } - } - return ; -} diff --git a/libraries/libft/src/libft/stack/ft_stksize.c b/libraries/libft/src/libft/stack/ft_stksize.c deleted file mode 100644 index dc2b2164..00000000 --- a/libraries/libft/src/libft/stack/ft_stksize.c +++ /dev/null @@ -1,30 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_stksize.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/11 21:04:09 by lyeh #+# #+# */ -/* Updated: 2023/12/11 21:05:09 by lyeh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -int ft_stksize(t_stack *stk) -{ - int n; - t_stack *cur; - - if (stk == NULL) - return (0); - n = 0; - cur = stk; - while (cur != NULL) - { - cur = cur->next; - n++; - } - return (n); -} diff --git a/libraries/libft/src/libft/strings/ft_strchr.c b/libraries/libft/src/libft/strings/ft_strchr.c index 55315cfb..76f0c813 100644 --- a/libraries/libft/src/libft/strings/ft_strchr.c +++ b/libraries/libft/src/libft/strings/ft_strchr.c @@ -6,12 +6,26 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:13:12 by ldulling #+# #+# */ -/* Updated: 2023/09/24 16:14:51 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 16:59:08 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +/** + * The ft_strchr function locates the first occurrence of c (converted to a + * char) in the string pointed to by s. + * + * @param s The string in which to search for the character. + * @param c The character to search for. + * + * @return A pointer to the located character, or NULL if the character does + * not appear in the string. + * + * @note Calling ft_strchr with s equal to NULL will cause a segmentation + * fault (mirrors the behavior of the original strchr). + * + */ char *ft_strchr(const char *s, int c) { char casted_c; diff --git a/libraries/libft/src/libft/strings/ft_strcmp.c b/libraries/libft/src/libft/strings/ft_strcmp.c index c15ff97e..283d03f9 100644 --- a/libraries/libft/src/libft/strings/ft_strcmp.c +++ b/libraries/libft/src/libft/strings/ft_strcmp.c @@ -6,12 +6,29 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:16:22 by ldulling #+# #+# */ -/* Updated: 2023/11/12 11:18:04 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 16:51:07 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +/** + * The ft_strcmp function compares the string pointed to by s1 with the string + * pointed to by s2. + * + * @param s1 A pointer to the first string. + * @param s2 A pointer to the second string. + * + * @return 0 if the strings are identical. + * A positive integer if the first character that does not match + * has a greater value in s1 than in s2. + * A negative integer if the first character that does not match + * has a lower value in s1 than in s2. + * + * @note Calling ft_strcmp with s1 or s2 equal to NULL will cause a + * segmentation fault (mirrors the behavior of the original + * strcmp). + */ int ft_strcmp(const char *s1, const char *s2) { const unsigned char *casted_ptr_s1; diff --git a/libraries/libft/src/libft/strings/ft_strdup.c b/libraries/libft/src/libft/strings/ft_strdup.c index ef8bded8..14d9f171 100644 --- a/libraries/libft/src/libft/strings/ft_strdup.c +++ b/libraries/libft/src/libft/strings/ft_strdup.c @@ -6,12 +6,25 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:14:59 by ldulling #+# #+# */ -/* Updated: 2023/09/24 16:15:00 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 16:28:07 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +/** + * The ft_strdup function duplicates the string pointed to by s. + * + * @param s A pointer to the string to be duplicated. + * + * @return A pointer to the newly allocated string, or NULL if the + * allocation failed. + * The returned string will have to be freed later. + * + * @note Calling ft_strdup with s equal to NULL will cause a segmentation + * fault (mirrors the behavior of the original strdup). + * + */ char *ft_strdup(const char *s) { char *dup; diff --git a/libraries/libft/src/libft/strings/ft_strlcat.c b/libraries/libft/src/libft/strings/ft_strlcat.c index 7ab0088e..b4518244 100644 --- a/libraries/libft/src/libft/strings/ft_strlcat.c +++ b/libraries/libft/src/libft/strings/ft_strlcat.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:15:16 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:30:03 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 12:21:31 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,11 +20,7 @@ * longer than size. * * @param dst The string to be appended to. - * Note: Calling ft_strlcat with dst equal to NULL with a size - * not 0 will cause a segmentation fault. * @param src The string to append to dst. - * Note: Calling ft_strlcat with src equal to NULL will cause a - * segmentation fault. * @param size The total size of dst, including the space for the * NUL-terminator. * @@ -33,6 +29,10 @@ * If the return value is >= size, the output string has been * truncated. * + * @note Calling ft_strlcat with dst equal to NULL with a size not 0, + * or with src equal to NULL will cause a segmentation fault + * (mirrors the behavior of the original strlcat). + * */ size_t ft_strlcat(char *dst, const char *src, size_t size) { diff --git a/libraries/libft/src/libft/strings/ft_strlcpy.c b/libraries/libft/src/libft/strings/ft_strlcpy.c index cc19e3bb..4c4b0ab9 100644 --- a/libraries/libft/src/libft/strings/ft_strlcpy.c +++ b/libraries/libft/src/libft/strings/ft_strlcpy.c @@ -6,14 +6,31 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:15:57 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:18:18 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 17:15:07 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" /** - * @param size Has to account for NUL-terminator. + * The ft_strlcpy function copies the NUL-terminated string from src to dst. + * It will copy at most size - 1 characters. + * It will then NUL-terminate, unless size is 0 or the src string is longer than + * size. + * + * @param dst The string to copy to. + * @param src The string to copy from. + * @param size The total size of dst, including the space for the + * NUL-terminator. + * + * @return The total length of src. + * If the return value is >= size, the output string has been + * truncated. + * + * @note Calling ft_strlcpy with dst equal to NULL with a size not 0, + * or with src equal to NULL will cause a segmentation fault + * (mirrors the behavior of the original strlcpy). + * */ size_t ft_strlcpy(char *dst, const char *src, size_t size) { diff --git a/libraries/libft/src/libft/strings/ft_strlen.c b/libraries/libft/src/libft/strings/ft_strlen.c index 97180cb1..ffa8eeb8 100644 --- a/libraries/libft/src/libft/strings/ft_strlen.c +++ b/libraries/libft/src/libft/strings/ft_strlen.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:16:06 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:30:43 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 12:18:50 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,12 @@ * excluding the terminating null byte ('\0'). * * @param s The string to be measured. - * Note: Calling ft_strlen with s equal to NULL will cause a - * segmentation fault. * * @return The length of the string s. * + * @note Calling ft_strlen with s equal to NULL will cause a segmentation + * fault (mirrors the behavior of the original strlen). + * */ size_t ft_strlen(const char *s) { diff --git a/libraries/libft/src/libft/strings/ft_strmatches_any.c b/libraries/libft/src/libft/strings/ft_strmatches_any.c index c015db55..27a51bd3 100644 --- a/libraries/libft/src/libft/strings/ft_strmatches_any.c +++ b/libraries/libft/src/libft/strings/ft_strmatches_any.c @@ -6,40 +6,64 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/13 19:50:45 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:31:32 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 18:12:21 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +static int search_null_str(va_list ap, int n); + /** * The ft_strmatches_any function checks if the first string matches any of the * n other strings provided as arguments. * - * @param str The string to compare. - * @param n The number of strings to compare against. + * @param str The string to compare against. + * If str is NULL, NULL will be searched. + * @param n The number of strings to compare. * @param ... Variable number of strings to compare with str. * - * @return If a match is found, it returns the position of the matching - * string (1-indexed). - * If no match is found, it returns 0. + * @return If a match is found, the position of the matching string + * (1-indexed). + * If no match is found, 0. * */ int ft_strmatches_any(const char *str, int n, ...) { - va_list ap; - int i; + va_list ap; + int i; + const char *cmp; va_start(ap, n); - i = 0; - while (i++ < n) + if (str == NULL) + i = search_null_str(ap, n); + else { - if (ft_strcmp(str, va_arg(ap, const char *)) == 0) + i = 0; + while (i < n) { - va_end(ap); - return (i); + cmp = va_arg(ap, const char *); + if (cmp != NULL && ft_strcmp(str, cmp) == 0) + break ; + i++; } } va_end(ap); - return (0); + if (i == n) + return (0); + return (i + 1); +} + +static int search_null_str(va_list ap, int n) +{ + int i; + + i = 0; + while (i < n) + { + if (va_arg(ap, const char *) == NULL) + break ; + i++; + } + return (i); } diff --git a/libraries/libft/src/libft/strings/ft_strnstr.c b/libraries/libft/src/libft/strings/ft_strnstr.c index b241dfdd..02b5ff4a 100644 --- a/libraries/libft/src/libft/strings/ft_strnstr.c +++ b/libraries/libft/src/libft/strings/ft_strnstr.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:16:27 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:32:08 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 12:17:17 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,6 @@ * searched. Characters that appear after a '\0' character are not searched. * * @param big The string to be scanned. - * Note: Calling ft_strnstr with big equal to NULL with a size - * not 0 will cause a segmentation fault. * @param little The string to be searched within big. * @param len The number of characters to be scanned in big. * @@ -28,6 +26,10 @@ * to the first character of the first occurrence of little is * returned. * + * @note Calling ft_strnstr with big equal to NULL with a size not 0 + * will cause a segmentation fault (mirrors the behavior of the + * original strnstr). + * */ char *ft_strnstr(const char *big, const char *little, size_t len) { diff --git a/libraries/libft/src/libft/strings/ft_strtok.c b/libraries/libft/src/libft/strings/ft_strtok.c index 49e6b32d..c8d8d71a 100644 --- a/libraries/libft/src/libft/strings/ft_strtok.c +++ b/libraries/libft/src/libft/strings/ft_strtok.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/24 21:48:31 by ldulling #+# #+# */ -/* Updated: 2023/12/20 19:33:22 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 15:34:45 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,6 @@ * @param str The string to be tokenized. On the first call to ft_strtok, * this should be the string you want to tokenize. On subsequent * calls, it should be NULL. - * Note: Calling ft_strtok the first time with str equal to NULL - * will cause a segmentation fault. * @param delim The delimiter string. Each character in this string is * considered a valid delimiter. * @@ -28,6 +26,10 @@ * string. * If there are no more tokens, it returns NULL. * + * @note Calling ft_strtok the first time with str equal to NULL will + * cause a segmentation fault (mirrors the behavior of the + * original strtok). + * */ char *ft_strtok(char *str, const char *delim) { diff --git a/source/frontend/lexer/create_token_list.c b/source/frontend/lexer/create_token_list.c index 400cd139..139d8cc4 100644 --- a/source/frontend/lexer/create_token_list.c +++ b/source/frontend/lexer/create_token_list.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/20 11:54:32 by ldulling #+# #+# */ -/* Updated: 2023/12/21 20:09:21 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 20:58:20 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ bool create_token_list(t_list **token_list, t_list **token_data_list) if (!new_nodes || !separate_operators(new_nodes, 0)) break ; ft_lstadd_back(token_list, new_nodes); - free(ft_lstpop(token_data_list)); + free(ft_lstpop_front(token_data_list)); } if (*token_data_list) return (ft_lstclear(token_data_list, free), free_token_node(token), \ diff --git a/libraries/libft/src/libft/stack/ft_stkpeektop.c b/source/frontend/parser/ast.c similarity index 69% rename from libraries/libft/src/libft/stack/ft_stkpeektop.c rename to source/frontend/parser/ast.c index ba6ed896..1e99ad90 100644 --- a/libraries/libft/src/libft/stack/ft_stkpeektop.c +++ b/source/frontend/parser/ast.c @@ -1,20 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_stkpeektop.c :+: :+: :+: */ +/* ast.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/13 20:29:13 by lyeh #+# #+# */ -/* Updated: 2023/12/17 13:58:59 by lyeh ### ########.fr */ +/* Created: 2023/12/20 21:37:46 by lyeh #+# #+# */ +/* Updated: 2023/12/20 21:37:47 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" - -t_stack *ft_stkpeektop(t_stack *stk) -{ - if (stk == NULL) - return (NULL); - return (stk); -} diff --git a/source/frontend/parser/parse.c b/source/frontend/parser/parse.c index d9581b29..4ebe4413 100644 --- a/source/frontend/parser/parse.c +++ b/source/frontend/parser/parse.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* parse.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ +/* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 17:28:20 by lyeh #+# #+# */ -/* Updated: 2023/12/21 14:20:46 by lyeh ### ########.fr */ +/* Updated: 2023/12/23 20:58:20 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,20 +15,19 @@ #include "utils.h" bool parse_step(t_pt_node *pt_entry, - t_list **token_list, t_stack **state_stack, t_stack **parse_stack) + t_list **token_list, t_list **state_stack, t_list **parse_stack) { bool ret; ret = false; if (pt_entry && pt_entry->action == A_SHIFT) - ret = parse_shift(ft_lstpop_content(token_list), + ret = parse_shift(ft_lstpop_front_content(token_list), state_stack, parse_stack, pt_entry->next_state); else if (pt_entry && pt_entry->action == A_REDUCE) { if (parse_reduce(state_stack, parse_stack, pt_entry) && \ - parse_goto(state_stack, - get_token_from_stack(ft_stkpeektop(*parse_stack))->type)) + parse_goto(state_stack, get_token_from_stack(*parse_stack)->type)) ret = true; } else @@ -37,19 +36,18 @@ bool parse_step(t_pt_node *pt_entry, } // TODO: Need to verify if the return value always be one of the operators -char *get_error_token_data(t_list *token_list, t_stack *parse_stack) +char *get_error_token_data(t_list *token_list, t_list *parse_stack) { char *error_token_data; if (token_list) error_token_data = get_token_data_from_list(token_list); else - error_token_data = get_token_from_stack( - ft_stkpeektop(parse_stack))->data; + error_token_data = get_token_from_stack(parse_stack)->data; return (error_token_data); } -void report_syntax_error(t_list *token_list, t_stack *parse_stack) +void report_syntax_error(t_list *token_list, t_list *parse_stack) { ft_dprintf(2, "%s: syntax error near unexpected token `%s'\n", PROGRAM_NAME, @@ -57,7 +55,7 @@ void report_syntax_error(t_list *token_list, t_stack *parse_stack) } bool parse( - t_list **token_list, t_stack **state_stack, t_stack **parse_stack) + t_list **token_list, t_list **state_stack, t_list **parse_stack) { bool ret; t_pt_node *pt_entry; @@ -67,7 +65,7 @@ bool parse( { print_token_list(*token_list); pt_entry = get_next_pt_entry( - get_state_from_stack(ft_stkpeektop(*state_stack)), + get_state_from_stack(*state_stack), get_token_type_from_list(*token_list), A_SHIFT | A_REDUCE | A_ACCEPT); if (pt_entry && pt_entry->action == A_ACCEPT) @@ -86,8 +84,8 @@ bool parse( bool ft_parse(t_list **token_list) { - t_stack *state_stack; - t_stack *parse_stack; + t_list *state_stack; + t_list *parse_stack; if (!init_parse(&state_stack, &parse_stack)) return (false); diff --git a/source/frontend/parser/parser_operation.c b/source/frontend/parser/parser_operation.c index 067fad84..8887b847 100644 --- a/source/frontend/parser/parser_operation.c +++ b/source/frontend/parser/parser_operation.c @@ -3,45 +3,45 @@ /* ::: :::::::: */ /* parser_operation.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ +/* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 19:52:51 by lyeh #+# #+# */ -/* Updated: 2023/12/20 21:05:04 by lyeh ### ########.fr */ +/* Updated: 2023/12/23 18:46:46 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" #include "utils.h" -bool push_state(t_stack **state_stack, int next_step) +bool push_state(t_list **state_stack, int next_step) { - t_stack *node; - int *tmp; + t_list *node; + int *state; - tmp = malloc(sizeof(int)); - if (!tmp) + state = malloc(sizeof(int)); + if (!state) return (false); - *tmp = next_step; - node = ft_stknew((void *)tmp); + *state = next_step; + node = ft_lstnew(state); if (!node) - return (free(tmp), false); - ft_stkpush(state_stack, node); + return (free(state), false); + ft_lstadd_front(state_stack, node); return (true); } -bool push_token(t_stack **parse_stack, t_token *token) +bool push_token(t_list **parse_stack, t_token *token) { - t_stack *node; + t_list *node; - node = ft_stknew(token); + node = ft_lstnew(token); if (!node) return (false); - ft_stkpush(parse_stack, node); + ft_lstadd_front(parse_stack, node); return (true); } bool parse_shift(t_token *input_token, - t_stack **state_stack, t_stack **parse_stack, int next_step) + t_list **state_stack, t_list **parse_stack, int next_step) { if (!push_token(parse_stack, input_token)) return (free_token_node(input_token), false); @@ -49,14 +49,14 @@ bool parse_shift(t_token *input_token, return (false); printf("After shift:\n"); printf("state_stack: "); - print_state_stack(ft_stkpeektop(*state_stack)); + print_state_stack(*state_stack); printf("parse_stack: "); - print_parse_stack(ft_stkpeektop(*parse_stack)); + print_parse_stack(*parse_stack); return (true); } bool parse_reduce( - t_stack **state_stack, t_stack **parse_stack, t_pt_node *pt_entry) + t_list **state_stack, t_list **parse_stack, t_pt_node *pt_entry) { t_token *reduction_node; @@ -70,18 +70,18 @@ bool parse_reduce( return (free_token_node(reduction_node), false); printf("reduction_node->type: %d\n", reduction_node->type); printf("state_stack: "); - print_state_stack(ft_stkpeektop(*state_stack)); + print_state_stack(*state_stack); printf("parse_stack: "); - print_parse_stack(ft_stkpeektop(*parse_stack)); + print_parse_stack(*parse_stack); return (true); } -bool parse_goto(t_stack **state_stack, int token_type) +bool parse_goto(t_list **state_stack, int token_type) { t_pt_node *pt_entry; pt_entry = get_next_pt_entry( - get_state_from_stack(ft_stkpeektop(*state_stack)), + get_state_from_stack(*state_stack), token_type, A_GOTO); if (!pt_entry) @@ -90,6 +90,6 @@ bool parse_goto(t_stack **state_stack, int token_type) return (free(pt_entry), false); printf("After goto:\n"); printf("state_stack: "); - print_state_stack(ft_stkpeektop(*state_stack)); + print_state_stack(*state_stack); return (free(pt_entry), true); } diff --git a/source/frontend/parser/parser_utils.c b/source/frontend/parser/parser_utils.c index 7518d85f..10106246 100644 --- a/source/frontend/parser/parser_utils.c +++ b/source/frontend/parser/parser_utils.c @@ -6,14 +6,14 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 22:05:05 by lyeh #+# #+# */ -/* Updated: 2023/12/21 14:24:52 by ldulling ### ########.fr */ +/* Updated: 2023/12/23 15:41:56 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" #include "utils.h" -bool init_parse(t_stack **state_stack, t_stack **parse_stack) +bool init_parse(t_list **state_stack, t_list **parse_stack) { *state_stack = NULL; *parse_stack = NULL; @@ -22,8 +22,8 @@ bool init_parse(t_stack **state_stack, t_stack **parse_stack) return (true); } -void free_parse(t_stack **state_stack, t_stack **parse_stack) +void free_parse(t_list **state_stack, t_list **parse_stack) { - ft_stkclear(state_stack, free); - ft_stkclear(parse_stack, free_token_node); + ft_lstclear(state_stack, free); + ft_lstclear(parse_stack, free_token_node); } diff --git a/source/utils/stack_utils.c b/source/utils/stack_utils.c index 3f0b39bb..c6c113a8 100644 --- a/source/utils/stack_utils.c +++ b/source/utils/stack_utils.c @@ -3,45 +3,45 @@ /* ::: :::::::: */ /* stack_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ +/* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 21:55:34 by lyeh #+# #+# */ -/* Updated: 2023/12/18 21:46:44 by lyeh ### ########.fr */ +/* Updated: 2023/12/23 20:58:20 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "defines.h" -bool drop_num_stack(t_stack **stack, int num, void (*del)(void *)) +bool drop_num_stack(t_list **stack, int num, void (*del)(void *)) { int i; - t_stack *node; + t_list *node; i = 0; while (i < num) { - node = ft_stkpop(stack); + node = ft_lstpop_front(stack); if (!node) return (false); - ft_stkdelone(node, del); + ft_lstdelone(node, del); i++; } return (true); } -int get_state_from_stack(t_stack *node) +int get_state_from_stack(t_list *node) { return (*((int *)node->content)); } -t_token *get_token_from_stack(t_stack *node) +t_token *get_token_from_stack(t_list *node) { return ((t_token *)node->content); } -void print_state_stack(t_stack *stack) +void print_state_stack(t_list *stack) { - t_stack *node; + t_list *node; node = stack; while (node) @@ -52,7 +52,7 @@ void print_state_stack(t_stack *stack) printf("(NULL)\n"); } -void print_parse_stack(t_stack *node) +void print_parse_stack(t_list *node) { while (node) {