lexing while_clause, passage en buffer dynamique au readscript todo tomorrow

This commit is contained in:
ariard@student.42.fr 2017-01-26 01:00:03 +01:00
parent a011f94bcb
commit bcaa2fb943
23 changed files with 173 additions and 24 deletions

View file

@ -1 +0,0 @@
0

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */
/* Updated: 2017/01/19 20:54:43 by ariard ### ########.fr */
/* Updated: 2017/01/25 15:49:26 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/01/24 20:13:54 by ariard ### ########.fr */
/* Updated: 2017/01/26 00:57:09 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,9 +37,10 @@ typedef long long t_type;
# define TK_COMMAND (1 << 17)
# define TK_SUBSHELL (1 << 18)
# define TK_NEWLINE (1 << 19)
# define TK_LOOP (1 << 20)
# define TK_B_LOOP (1 << 21)
# define TK_E_LOOP (1 << 22)
# define TK_WHILE (1 << 20)
# define TK_DO (1 << 21)
# define TK_DONE (1 << 22)
# define TK_LIST (1 << 23)
# define TK_WORD (TK_N_WORD | TK_Q_WORD | TK_DQ_WORD)
# define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)
@ -62,6 +63,9 @@ enum e_lexstate
BACKSLASH,
VAR,
SPECIAL,
WHILE,
DO_GROUP,
LIST,
COMMENT,
};
@ -92,10 +96,11 @@ int reduce_bquotes(t_list **alst, char **str);
char *command_getoutput(char *command);
int ft_is_delim(char c);
int ft_is_delim_list(char c);
t_lexstate get_lexer_state(char *str);
t_lexstate get_reserved_words(char *str);
int lexer_default(t_list **alst, char *str);
int lexer_comment(t_list **alst, char *str);
int lexer_newline(t_list **alst, char *str);
int lexer_delim(t_list **alst, char *str);
int lexer_sep(t_list **alst, char *str);
@ -110,5 +115,9 @@ int lexer_dquote(t_list **alst, char *str);
int lexer_backslash(t_list **alst, char *str);
int lexer_var(t_list **alst, char *str);
int lexer_special(t_list **alst, char *str);
int lexer_while(t_list **alst, char *str);
int lexer_do_group(t_list **alst, char *str);
int lexer_list(t_list **alst, char *str);
int lexer_comment(t_list **alst, char *str);
#endif

View file

@ -1,2 +1,2 @@
ls #ls
pwd
ls | cat
pwd ; cd

View file

@ -1,6 +1,13 @@
#!/bin/bash
while [ 1 ]; do
echo "debut script"
do
while [ 1 ] ; [ 1 ]
do
sleep 1
echo "a"
done
echo "fin script"

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */
/* Updated: 2017/01/24 20:13:42 by ariard ### ########.fr */
/* Updated: 2017/01/26 00:56:23 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,6 +29,9 @@ int (*g_lexer[])(t_list **alst, char *str) =
&lexer_backslash,
&lexer_var,
&lexer_special,
&lexer_while,
&lexer_do_group,
&lexer_list,
&lexer_comment,
};
@ -37,6 +40,11 @@ int ft_is_delim(char c)
return (c == ' ' || c == '\t');
}
int ft_is_delim_list(char c)
{
return (c == ';' || c == '\n' || c == '&');
}
int ft_tokenize(t_list **alst, char *str, t_lexstate state)
{
t_token *token;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/10 13:45:46 by jhalford #+# #+# */
/* Updated: 2017/01/24 19:09:26 by ariard ### ########.fr */
/* Updated: 2017/01/26 00:31:25 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_reserved_words.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:07:05 by ariard #+# #+# */
/* Updated: 2017/01/26 00:51:26 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
t_lexstate get_reserved_words(char *str)
{
if (ft_strncmp(str, "while", 5) == 0)
return (WHILE);
else if (ft_strncmp(str, "done", 4) == 0
|| ft_strncmp(str, "do" , 2) == 0)
return (DO_GROUP);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */
/* Updated: 2017/01/24 19:22:58 by ariard ### ########.fr */
/* Updated: 2017/01/25 23:01:37 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:58:44 by jhalford #+# #+# */
/* Updated: 2017/01/24 19:48:31 by ariard ### ########.fr */
/* Updated: 2017/01/26 00:08:33 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_do_group.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:48:48 by ariard #+# #+# */
/* Updated: 2017/01/26 00:59:00 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_do_group(t_list **alst, char *str)
{
t_token *token;
t_lexstate state;
int type;
type = (str[0] == 'd' && str[1] == 'o' && str[2] != 'n' ?
2 : 4);
if (*alst)
{
if (ft_is_delim_list(*(str + type)) || *(str + type) == ' ')
return (lexer_do_group(&(*alst)->next, str));
return (ft_tokenize(alst, str + 1, LIST));
}
else
{
token = token_init();
*alst = ft_lstnew(token, sizeof(*token));
}
token = (*alst)->content;
token->type = (type == 2 ? TK_DO : TK_DONE);
state = (token->type == TK_DO) ? LIST : DEFAULT;
return (ft_tokenize(&(*alst)->next, str + 1, state));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:36:58 by jhalford #+# #+# */
/* Updated: 2017/01/19 22:04:11 by ariard ### ########.fr */
/* Updated: 2017/01/25 22:52:47 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:56:58 by jhalford #+# #+# */
/* Updated: 2017/01/24 19:02:45 by ariard ### ########.fr */
/* Updated: 2017/01/25 22:52:39 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:55:33 by ariard #+# #+# */
/* Updated: 2017/01/26 00:58:58 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_list(t_list **alst, char *str)
{
t_token *token;
token = (*alst)->content;
token->type = TK_LIST;
while (*str)
{
if (ft_is_delim_list(*str))
{
while (ft_is_delim(*str) || *str == '\n')
str++;
if (ft_strncmp(str, "done", 4) == 0
|| ft_strncmp(str, "do", 2) == 0)
return (ft_tokenize(alst, str, DO_GROUP));
}
token_append(token, *str++);
}
return (0);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_loop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/25 21:58:12 by ariard #+# #+# */
/* Updated: 2017/01/26 00:58:57 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_while(t_list **alst, char *str)
{
t_token *token;
token = (*alst)->content;
if (ft_strncmp(str, "while", 5) == 0)
token->type = TK_WHILE;
else if (ft_isalnum(*str))
{
token_append(token, *str);
return (ft_tokenize(alst, str + 1, WORD));
}
return (ft_tokenize(&(*alst)->next, str + 6, LIST));
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/23 23:19:46 by ariard #+# #+# */
/* Updated: 2017/01/24 20:13:45 by ariard ### ########.fr */
/* Updated: 2017/01/26 00:41:18 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */
/* Updated: 2017/01/23 22:54:33 by ariard ### ########.fr */
/* Updated: 2017/01/25 23:05:08 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */
/* Updated: 2017/01/24 01:25:42 by ariard ### ########.fr */
/* Updated: 2017/01/26 00:48:08 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 15:35:38 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:36:10 by jhalford ### ########.fr */
/* Updated: 2017/01/25 22:53:15 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */
/* Updated: 2017/01/24 18:50:42 by ariard ### ########.fr */
/* Updated: 2017/01/26 00:58:55 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,6 +25,8 @@ int lexer_word(t_list **alst, char *str)
return (ft_tokenize(&(*alst)->next, str, GREAT));
else if (*str == '<')
return (ft_tokenize(&(*alst)->next, str, LESS));
else if ((state = get_reserved_words(str)))
return (ft_tokenize(alst, str, state));
token_append(token, *str);
return (ft_tokenize(alst, str + 1, WORD));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/01/23 23:51:35 by ariard ### ########.fr */
/* Updated: 2017/01/25 16:25:43 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 23:06:34 by ariard #+# #+# */
/* Updated: 2017/01/24 20:14:52 by ariard ### ########.fr */
/* Updated: 2017/01/25 22:54:03 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,6 +39,7 @@ int shell_script()
DG("after post_tokenize");
token_print(head);
list_tmp = NULL;
return (0);
while (head)
{
if (ft_parse(&ast, &head))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */
/* Updated: 2017/01/24 19:42:12 by ariard ### ########.fr */
/* Updated: 2017/01/25 16:35:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */