Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LIB] Refactor the data type of stack to t_lst #31

Merged
merged 13 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: lyeh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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
12 changes: 6 additions & 6 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
/* By: lyeh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

#ifndef UTILS_H
# 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);

Expand Down
40 changes: 15 additions & 25 deletions libraries/libft/build/libft.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: ldulling <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -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), \
Expand All @@ -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:
Expand Down Expand Up @@ -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), \
Expand Down
50 changes: 25 additions & 25 deletions libraries/libft/inc/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
Expand All @@ -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 *));
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
24 changes: 12 additions & 12 deletions libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_back_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

#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.
*/
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 ;
}
21 changes: 11 additions & 10 deletions libraries/libft/src/libft/lists/doubly_linked/ft_lstadd_front_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 ;
}
16 changes: 7 additions & 9 deletions libraries/libft/src/libft/lists/doubly_linked/ft_lstclear_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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 ;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_stkdelone.c :+: :+: :+: */
/* ft_lstdelone_d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 ;
}
Loading