From b4002940ce1c1c4648b24f6bf942fc4e4ebab098 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Thu, 27 Aug 2020 18:46:42 +0200 Subject: Updated lexer to use t_tok_lst instead of t_ftlst and t_token --- src/lexer/lexer.c | 71 +++++++++++++++++++++------------------------ src/lexer/tok_lst.c | 26 ++++++++++++++++- src/lexer/token.c | 83 ----------------------------------------------------- src/lexer/trim.c | 56 ++++++++++++++++-------------------- 4 files changed, 83 insertions(+), 153 deletions(-) delete mode 100644 src/lexer/token.c (limited to 'src') diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index f820ebb..e6aeb80 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */ -/* Updated: 2020/08/27 09:56:46 by charles ### ########.fr */ +/* Updated: 2020/08/27 17:34:53 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -99,83 +99,78 @@ int check_input_out(char *input) return(0); } -enum e_tok token_check_stick(t_token *lst_token) +enum e_tok token_check_stick(t_tok_lst *tok) { int i; - i = ft_strlen(lst_token->content); + i = ft_strlen(tok->content); if (i > 0) - if (lst_token->content[i - 1] == ' ') - return(lst_token->tag); - return(lst_token->tag | TAG_STICK); + if (tok->content[i - 1] == ' ') + return(tok->tag); + return(tok->tag | TAG_STICK); } -enum e_tok token_str_or_quote(t_token *lst_token) +enum e_tok token_str_or_quote(t_tok_lst *tok) { int i; i = 0; - while(lst_token->content[i] != '\0') + while(tok->content[i] != '\0') { - if(lst_token->content[i] == '\'') + if(tok->content[i] == '\'') { - lst_token->tag = TAG_STR_SINGLE; - return(token_check_stick(lst_token)); + tok->tag = TAG_STR_SINGLE; + return(token_check_stick(tok)); } - if(lst_token->content[i] == '"') + if(tok->content[i] == '"') { - lst_token->tag = TAG_STR_DOUBLE; - return(token_check_stick(lst_token)); + tok->tag = TAG_STR_DOUBLE; + return(token_check_stick(tok)); } else { - lst_token->tag = TAG_STR; - return(token_check_stick(lst_token)); + tok->tag = TAG_STR; + return(token_check_stick(tok)); } i++; } return(0); } -t_token *push_token_enum(t_token *lst_token) +void push_token_enum(t_tok_lst *tok) { - enum e_tok tk; + enum e_tok tag; - tk = ret_token(lst_token->content, 0); - if (tk == 0) - lst_token->tag = token_str_or_quote(lst_token); + tag = ret_token(tok->content, 0); + if (tag == 0) + tok->tag = token_str_or_quote(tok); else - lst_token->tag = tk; - return (lst_token); + tok->tag = tag; } -static t_ftlst *create_token_list(char *input, t_ftlst **lst) +static t_tok_lst *create_token_list(char *input, t_tok_lst **lst) { - t_token *lst_token; - t_ftlst *new; - int i; - int j; + t_tok_lst *tok; + size_t i; + size_t j; i = 0; - while (i < (int)ft_strlen(input)) + while (i < ft_strlen(input)) { j = 0; j += check_input(&input[i]); - lst_token = token_new_until(0, input + i, j); - lst_token = push_token_enum(lst_token); - if (lst_token->content[0] != ' ') - { - new = ft_lstnew(lst_token); - ft_lstpush_back(lst, new); - } + tok = tok_lst_new_until(0, input + i, j); + push_token_enum(tok); + if (tok->content[0] != ' ') + tok_lst_push_back(lst, tok); i += j; } return (*lst); } -t_ftlst *lexer(char *input) +t_tok_lst *lexer(char *input) { - t_ftlst *lst; + t_tok_lst *lst; if (!input) return (NULL); diff --git a/src/lexer/tok_lst.c b/src/lexer/tok_lst.c index 125e19c..8d29bb5 100644 --- a/src/lexer/tok_lst.c +++ b/src/lexer/tok_lst.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/08/27 09:32:58 by charles #+# #+# */ -/* Updated: 2020/08/27 09:37:57 by charles ### ########.fr */ +/* Updated: 2020/08/27 18:40:05 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,3 +33,27 @@ t_tok_lst *tok_lst_new_until(enum e_tok tag, char *content, size_t n) ret->tag = tag; return (ret); } + +void tok_lst_push_back(t_tok_lst **tokens, t_tok_lst *pushed) +{ + ft_lstpush_back((t_ftlst**)tokens, (t_ftlst*)pushed); +} + +t_tok_lst *tok_lst_push_front(t_tok_lst **tokens, t_tok_lst *pushed) +{ + if (pushed == NULL) + return (NULL); + ft_lstpush_front((t_ftlst**)tokens, (t_ftlst*)pushed); + return (*tokens); +} + +void *tok_lst_destroy(t_tok_lst **tokens, void (*del)(void*)) +{ + ft_lstdestroy((t_ftlst**)tokens, del); + return (NULL); +} + +t_tok_lst *tok_lst_last(t_tok_lst *tokens) +{ + return ((t_tok_lst*)ft_lstlast((t_ftlst*)tokens)); +} diff --git a/src/lexer/token.c b/src/lexer/token.c deleted file mode 100644 index 9904351..0000000 --- a/src/lexer/token.c +++ /dev/null @@ -1,83 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* token.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/06/09 13:38:08 by charles #+# #+# */ -/* Updated: 2020/08/19 13:40:55 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "lexer.h" - -t_token *token_new(enum e_tok tag, char *content) -{ - size_t len; - - len = 0; - if (content != NULL) - len = ft_strlen(content); - return (token_new_until(tag, content, len)); -} - -t_token *token_new_until(enum e_tok tag, char *content, int n) -{ - t_token *token; - - if ((token = (t_token*)malloc(sizeof(t_token))) == NULL) - return (NULL); - if (content == NULL) - token->content = NULL; - else if ((token->content = ft_strndup(content, n)) == NULL) - { - free(token); - return (NULL); - } - token->tag = tag; - return token; -} - -void token_destroy(t_token *token) -{ - free(token->content); - free(token); -} - -void token_destroy_lst(t_ftlst *tokens) -{ - ft_lstdestroy(&tokens, (void (*)(void*))token_destroy); -} - -void token_destroy_lst2(t_ftlst *tokens1, t_ftlst *tokens2) -{ - ft_lstdestroy(&tokens1, (void (*)(void*))token_destroy); - ft_lstdestroy(&tokens2, (void (*)(void*))token_destroy); -} - -enum e_tok token_tag(t_ftlst *token_lst) -{ - return (((t_token*)token_lst->data)->tag); -} - -void token_set_tag(t_ftlst *token_lst, enum e_tok tag) -{ - ((t_token*)token_lst->data)->tag = tag; -} - -char *token_content(t_ftlst *token_lst) -{ - return (((t_token*)token_lst->data)->content); -} - -void token_set_content(t_ftlst *token_lst, char *content) -{ - ((t_token*)token_lst->data)->content = content; -} - -void token_set_contentf(t_ftlst *token_lst, char *content) -{ - free(token_content(token_lst)); - token_set_content(token_lst, content); -} diff --git a/src/lexer/trim.c b/src/lexer/trim.c index 80c44bf..2cce38c 100644 --- a/src/lexer/trim.c +++ b/src/lexer/trim.c @@ -6,35 +6,32 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:36 by nahaddac #+# #+# */ -/* Updated: 2020/08/27 10:00:09 by charles ### ########.fr */ +/* Updated: 2020/08/27 17:30:11 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "lexer.h" -char *del_space(t_token *tk) +char *del_space(t_tok_lst *tok) { - int i; - char *s; + int i; i = 0; - while(tk->content[i] != '\0') + while (tok->content[i] != '\0') { - if(tk->content[i] == '\\') - return tk->content; - if(tk->content[i] == ' ') - break; + if (tok->content[i] == '\\') + return (tok->content); + if (tok->content[i] == ' ') + break ; i++; } - s = ft_strsubf(tk->content, 0, i); - return(s); + return (ft_strsubf(tok->content, 0, i)); } char *del_quote(char *str) { - int i; - char *s; + int i; i = 0; while(str[i++] != '\0') @@ -47,34 +44,31 @@ char *del_quote(char *str) if(str[i] != '\'' && str[i] != '"') return str; - s = ft_strsubf(str, 1, i - 1); - return (s); + return (ft_strsubf(str, 1, i - 1)); } -t_ftlst *lexer_trim_out(t_ftlst *lst) +t_tok_lst *lexer_trim_out(t_tok_lst *tokens) { - t_ftlst *first; - t_token *tk; + t_tok_lst *first; - first = lst; - while(lst != NULL) + first = tokens; + while (tokens != NULL) { - tk = lst->data; - if (tk->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE)) + if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE)) { - tk->content = del_quote(tk->content); - if(lst->next == NULL) - if (tk->tag & TAG_STICK) - tk->tag -= TAG_STICK; + tokens->content = del_quote(tokens->content); + if (tokens->next == NULL) + if (tokens->tag & TAG_STICK) + tokens->tag -= TAG_STICK; } else { - tk->content = del_space(tk); - if(lst->next == NULL) - if (tk->tag & TAG_STICK) - tk->tag -= TAG_STICK; + tokens->content = del_space(tokens); + if (tokens->next == NULL) + if (tokens->tag & TAG_STICK) + tokens->tag -= TAG_STICK; } - lst = lst->next; + tokens = tokens->next; } return (first); } -- cgit