lexing if ok

This commit is contained in:
ariard@student.42.fr 2017-02-01 00:00:27 +01:00
parent b0dececf1f
commit f33d2be526
18 changed files with 137 additions and 18 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/01/31 22:38:56 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:20:42 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/01/31 22:14:11 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:52:50 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,7 +47,10 @@ typedef long long t_type;
# define TK_WHILE (1 << 20) # define TK_WHILE (1 << 20)
# define TK_DO (1 << 21) # define TK_DO (1 << 21)
# define TK_DONE (1 << 22) # define TK_DONE (1 << 22)
# define TK_LIST (1 << 23) # define TK_IF (1 << 23)
# define TK_THEN (1 << 24)
# define TK_FI (1 << 25)
# define TK_LIST (1 << 26)
# define TK_WORD (TK_N_WORD | TK_Q_WORD | TK_DQ_WORD) # define TK_WORD (TK_N_WORD | TK_Q_WORD | TK_DQ_WORD)
# define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) # define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)
@ -73,6 +76,9 @@ enum e_lexstate
WHILE, WHILE,
DO, DO,
DONE, DONE,
IF,
THEN,
FI,
LIST, LIST,
COMMENT, COMMENT,
}; };
@ -126,6 +132,9 @@ int lexer_special(t_list **alst, char *str);
int lexer_while(t_list **alst, char *str); int lexer_while(t_list **alst, char *str);
int lexer_do(t_list **alst, char *str); int lexer_do(t_list **alst, char *str);
int lexer_done(t_list **alst, char *str); int lexer_done(t_list **alst, char *str);
int lexer_if(t_list **alst, char *str);
int lexer_then(t_list **alst, char *str);
int lexer_fi(t_list **alst, char *str);
int lexer_list(t_list **alst, char *str); int lexer_list(t_list **alst, char *str);
int lexer_comment(t_list **alst, char *str); int lexer_comment(t_list **alst, char *str);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/01/31 21:59:25 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:57:46 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

6
42sh/sample/if.sh Normal file
View file

@ -0,0 +1,6 @@
echo "begin if"
if [ 1 ]
then
echo "hello world"
fi
echo "end if"

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ /* Created: 2017/01/30 17:33:53 by ariard #+# #+# */
/* Updated: 2017/01/31 22:52:11 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:20:33 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 22:39:20 by ariard #+# #+# */ /* Created: 2017/01/31 22:39:20 by ariard #+# #+# */
/* Updated: 2017/01/31 22:51:56 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:20:40 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */
/* Updated: 2017/01/31 22:52:22 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:58:41 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,6 +32,9 @@ int (*g_lexer[])(t_list **alst, char *str) =
&lexer_while, &lexer_while,
&lexer_do, &lexer_do,
&lexer_done, &lexer_done,
&lexer_if,
&lexer_then,
&lexer_fi,
&lexer_list, &lexer_list,
&lexer_comment, &lexer_comment,
}; };

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */
/* Updated: 2017/01/31 21:11:47 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:51:56 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,5 +20,11 @@ t_lexstate get_reserved_words(char *str)
return (DONE); return (DONE);
else if (ft_strncmp(str, "do" , 2) == 0 && ft_isalpha(*(str + 2)) == 0) else if (ft_strncmp(str, "do" , 2) == 0 && ft_isalpha(*(str + 2)) == 0)
return (DO); return (DO);
else if (ft_strncmp(str, "if", 2) == 0 && ft_isalpha(*(str + 2)) == 0)
return (IF);
else if (ft_strncmp(str, "then", 4) == 0 && ft_isalpha(*(str + 4)) == 0)
return (THEN);
else if (ft_strncmp(str, "fi", 2) == 0 && ft_isalpha(*(str + 2)) == 0)
return (FI);
return (0); return (0);
} }

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* lexer_do_group.c :+: :+: :+: */ /* lexer_do.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:48:48 by ariard #+# #+# */ /* Created: 2017/01/31 23:29:09 by ariard #+# #+# */
/* Updated: 2017/01/31 22:14:26 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:32:45 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,6 +34,5 @@ int lexer_do(t_list **alst, char *str)
token = (*alst)->content; token = (*alst)->content;
token->type = TK_DO; token->type = TK_DO;
state = LIST; state = LIST;
// data_singleton()->scope |= (token->type == TK_DO) ? IN_LIST : OUT_LIST;
return (ft_tokenize(&(*alst)->next, str + 2, state)); return (ft_tokenize(&(*alst)->next, str + 2, state));
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 20:49:09 by ariard #+# #+# */ /* Created: 2017/01/31 20:49:09 by ariard #+# #+# */
/* Updated: 2017/01/31 22:14:28 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:33:03 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,6 +34,5 @@ int lexer_done(t_list **alst, char *str)
token = (*alst)->content; token = (*alst)->content;
token->type = TK_DONE; token->type = TK_DONE;
state = DEFAULT; state = DEFAULT;
// data_singleton()->scope |= (token->type == TK_DO) ? IN_LIST : OUT_LIST;
return (ft_tokenize(&(*alst)->next, str + 4, state)); return (ft_tokenize(&(*alst)->next, str + 4, state));
} }

31
42sh/src/lexer/lexer_fi.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_fi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 23:35:08 by ariard #+# #+# */
/* Updated: 2017/01/31 23:59:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_fi(t_list **alst, char *str)
{
t_token *token;
t_lexstate state;
if (*alst)
return (lexer_fi(&(*alst)->next, str));
else
{
token = token_init();
*alst = ft_lstnew(token, sizeof(*token));
}
token = (*alst)->content;
token->type = TK_FI;
state = DEFAULT;
return (ft_tokenize(&(*alst)->next, str + 2, state));
}

28
42sh/src/lexer/lexer_if.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 23:25:47 by ariard #+# #+# */
/* Updated: 2017/01/31 23:28:52 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_if(t_list **alst, char *str)
{
t_token *token;
token = (*alst)->content;
if (ft_strncmp(str, "if", 2) == 0)
token->type = TK_IF;
else if (ft_isalnum(*str))
{
token_append(token, *str);
return (ft_tokenize(alst, str + 1, WORD));
}
return (ft_tokenize(&(*alst)->next, str + 3, LIST));
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:55:33 by ariard #+# #+# */ /* Created: 2017/01/26 00:55:33 by ariard #+# #+# */
/* Updated: 2017/01/31 22:14:23 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:58:50 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,6 +31,12 @@ int lexer_list(t_list **alst, char *str)
else if (ft_strncmp(str, "do", 2) == 0 && (ft_is_delim_list(*(str + 2)) else if (ft_strncmp(str, "do", 2) == 0 && (ft_is_delim_list(*(str + 2))
|| *(str + 2) == ' ')) || *(str + 2) == ' '))
return (ft_tokenize(alst, str, DO)); return (ft_tokenize(alst, str, DO));
else if (ft_strncmp(str, "then", 4) == 0 && (ft_is_delim_list(*(str + 4))
|| *(str + 4) == ' '))
return (ft_tokenize(alst, str, THEN));
else if (ft_strncmp(str, "fi", 2) == 0 && (ft_is_delim_list(*(str + 2))
|| *(str + 2) == ' '))
return (ft_tokenize(alst, str, FI));
} }
token_append(token, *str++); token_append(token, *str++);
} }

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_then.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 23:29:49 by ariard #+# #+# */
/* Updated: 2017/01/31 23:59:37 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_then(t_list **alst, char *str)
{
t_token *token;
t_lexstate state;
if (*alst)
return (lexer_then(&(*alst)->next, str));
else
{
token = token_init();
*alst = ft_lstnew(token, sizeof(*token));
}
token = (*alst)->content;
token->type = TK_THEN;
state = LIST;
return (ft_tokenize(&(*alst)->next, str + 4, state));
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/25 21:58:12 by ariard #+# #+# */ /* Created: 2017/01/25 21:58:12 by ariard #+# #+# */
/* Updated: 2017/01/26 18:09:46 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:28:54 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */
/* Updated: 2017/01/31 22:21:14 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:56:53 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,6 +31,7 @@ int shell_script()
return (1); return (1);
DG("after post_tokenize"); DG("after post_tokenize");
token_print(token); token_print(token);
return (0);
if (ft_parse(&list_ast, &ast, &token)) if (ft_parse(&list_ast, &ast, &token))
return (1); return (1);
tmp2 = list_ast; tmp2 = list_ast;

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/01/31 22:15:59 by ariard ### ########.fr */ /* Updated: 2017/01/31 23:20:37 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */