currently parsing if, to-do:syntax error, clean code& interactive shell

This commit is contained in:
ariard@student.42.fr 2017-02-06 00:05:32 +01:00
parent ffb0542c42
commit 9bb45abb3d
31 changed files with 266 additions and 151 deletions

View file

@ -48,7 +48,6 @@ exec/exec_default.c\
exec/exec_dgreat.c\ exec/exec_dgreat.c\
exec/exec_great.c\ exec/exec_great.c\
exec/exec_less.c\ exec/exec_less.c\
exec/exec_list.c\
exec/exec_or_if.c\ exec/exec_or_if.c\
exec/exec_pipe.c\ exec/exec_pipe.c\
exec/exec_semi.c\ exec/exec_semi.c\
@ -171,8 +170,8 @@ main/shell_script.c\
main/sig_handler.c\ main/sig_handler.c\
parser/parse.c\ parser/parse.c\
parser/ft_parse.c\ parser/ft_parse.c\
parser/get_instruction.c \ parser/get_instruction.c\
parser/get_instruction2.c \ parser/get_sub_instruction.c\
parser/parse_dgreat.c\ parser/parse_dgreat.c\
parser/parse_dless.c\ parser/parse_dless.c\
parser/parse_do.c\ parser/parse_do.c\
@ -181,10 +180,10 @@ parser/parse_great.c\
parser/parse_greatand.c\ parser/parse_greatand.c\
parser/parse_less.c\ parser/parse_less.c\
parser/parse_lessand.c\ parser/parse_lessand.c\
parser/parse_list.c\
parser/parse_separator.c\ parser/parse_separator.c\
parser/parse_subshell.c\ parser/parse_subshell.c\
parser/parse_while.c\ parser/parse_while.c\
parser/parse_if.c\
parser/parse_word.c parser/parse_word.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))

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/02/05 18:12:04 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:45:03 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,7 +50,6 @@ typedef long long t_type;
# define TK_IF (1 << 23) # define TK_IF (1 << 23)
# define TK_THEN (1 << 24) # define TK_THEN (1 << 24)
# define TK_FI (1 << 25) # 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)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/05 17:59:47 by ariard ### ########.fr */ /* Updated: 2017/02/05 23:44:43 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -63,7 +63,7 @@ int parse(t_btree **ast, t_list **token);
int ft_parse(t_btree **ast, t_list **token); int ft_parse(t_btree **ast, t_list **token);
int get_instruction(t_list **lst); int get_instruction(t_list **lst);
int get_instruction2(t_btree **ast, t_list **start, t_list **lst); int get_sub_instruction(t_btree **ast, t_list **start, t_list **lst);
int parse_newline(t_btree **ast, t_list **start, t_list **lst); int parse_newline(t_btree **ast, t_list **start, t_list **lst);
int parse_separator(t_btree **ast, t_list **start, t_list **lst); int parse_separator(t_btree **ast, t_list **start, t_list **lst);
@ -77,8 +77,11 @@ int parse_word(t_btree **ast, t_list **start, t_list **lst);
int parse_subshell(t_btree **ast, t_list **start, t_list **lst); int parse_subshell(t_btree **ast, t_list **start, t_list **lst);
int parse_newline(t_btree **ast, t_list **start, t_list **lst); int parse_newline(t_btree **ast, t_list **start, t_list **lst);
int parse_while(t_btree **ast, t_list **start, t_list **lst); int parse_while(t_btree **ast, t_list **start, t_list **lst);
int parse_if(t_btree **ast, t_list **start, t_list **lst);
int parse_do(t_btree **ast, t_list **start, t_list **lst); int parse_do(t_btree **ast, t_list **start, t_list **lst);
int parse_done(t_btree **ast, t_list **start, t_list **lst); int parse_done(t_btree **ast, t_list **start, t_list **lst);
int parse_list(t_btree **ast, t_list **start, t_list **lst); int parse_list(t_btree **ast, t_list **start, t_list **lst);
int delete_newline(t_list **start, t_list **lst);
#endif #endif

View file

@ -1,6 +1,11 @@
echo "begin if"
if [ 1 ] if [ 1 ]
then then
if [ 1 ]
then
echo "hello world" echo "hello world"
fi fi
echo "end if" if [ 1 ]
then
echo "salut monde"
fi
fi > file1

View file

@ -2,10 +2,27 @@ while ls
do do
while ls while ls
do do
pwd
done
while ls while ls
do do
cd while ls
do
while ls
do
while ls
do
echo hello
done done
done
done
done
done
ls
while ls
do
while ls
do
echo world
done
done
pwd
done done

16
42sh/sample/while2.sh Normal file
View file

@ -0,0 +1,16 @@
ONE=11
while [ $ONE -gt 1 ]
do
TWO=11
while [ $TWO -gt 1 ]
do
THREE=11
while [ $THREE -gt 1 ]
do
echo hello
((THREE--))
done
((TWO--))
done
((ONE--))
done

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */
/* Updated: 2017/01/30 21:11:38 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:10:08 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,8 +14,6 @@
int exec_ampersand(t_btree **ast) int exec_ampersand(t_btree **ast)
{ {
t_process *p;
if (SHELL_HAS_JOBC(data_singleton()->opts)) if (SHELL_HAS_JOBC(data_singleton()->opts))
data_singleton()->exec.job.attributes |= JOB_BG; data_singleton()->exec.job.attributes |= JOB_BG;
ft_exec(&(*ast)->left); ft_exec(&(*ast)->left);
@ -23,8 +21,6 @@ int exec_ampersand(t_btree **ast)
data_singleton()->exec.job.attributes &= ~JOB_BG; data_singleton()->exec.job.attributes &= ~JOB_BG;
ft_exec(&(*ast)->right); ft_exec(&(*ast)->right);
p = &data_singleton()->exec.process; // btree_delone(ast, &ast_free);
if (!(p->script & SCRIPT_LOOP))
btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */ /* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */
/* Updated: 2017/01/30 20:53:56 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:10:38 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
int exec_and_if(t_btree **ast) int exec_and_if(t_btree **ast)
{ {
t_data *data; t_data *data;
t_process *p;
data = data_singleton(); data = data_singleton();
if (data->exec.aol_status == NULL if (data->exec.aol_status == NULL
@ -34,8 +33,6 @@ int exec_and_if(t_btree **ast)
data->exec.aol_status = NULL; data->exec.aol_status = NULL;
data->exec.aol_search = 0; data->exec.aol_search = 0;
p = &data_singleton()->exec.process; // btree_delone(ast, &ast_free);
if (!(p->script & SCRIPT_LOOP))
btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */
/* Updated: 2017/02/03 14:40:45 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:09:37 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -66,6 +66,6 @@ int exec_command(t_btree **ast)
p->av = NULL; p->av = NULL;
p->pid = 0; p->pid = 0;
p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK);
btree_delone(ast, &ast_free); // btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 19:29:59 by ariard #+# #+# */ /* Created: 2017/01/31 19:29:59 by ariard #+# #+# */
/* Updated: 2017/02/03 15:31:39 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:18:50 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */ /* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */
/* Updated: 2017/01/30 20:55:31 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:10:53 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
int exec_dgreat(t_btree **ast) int exec_dgreat(t_btree **ast)
{ {
t_astnode *node; t_astnode *node;
t_process *p;
int fd; int fd;
node = (*ast)->item; node = (*ast)->item;
@ -24,8 +23,6 @@ int exec_dgreat(t_btree **ast)
ft_exec(&(*ast)->left); ft_exec(&(*ast)->left);
data_singleton()->exec.process.fdout = STDOUT; data_singleton()->exec.process.fdout = STDOUT;
p = &data_singleton()->exec.process; // btree_delone(ast, &ast_free);
if (!(p->script & SCRIPT_LOOP))
btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */
/* Updated: 2017/01/30 21:09:36 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:11:18 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
int exec_great(t_btree **ast) int exec_great(t_btree **ast)
{ {
t_astnode *node; t_astnode *node;
t_process *p;
int fd; int fd;
node = (*ast)->item; node = (*ast)->item;
@ -24,8 +23,6 @@ int exec_great(t_btree **ast)
ft_exec(&(*ast)->left); ft_exec(&(*ast)->left);
data_singleton()->exec.process.fdout = STDOUT; data_singleton()->exec.process.fdout = STDOUT;
p = &data_singleton()->exec.process; // btree_delone(ast, &ast_free);
if (!(p->script & SCRIPT_LOOP))
btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ /* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */
/* Updated: 2017/01/30 20:56:55 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:12:02 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
int exec_less(t_btree **ast) int exec_less(t_btree **ast)
{ {
t_astnode *node; t_astnode *node;
t_process *p;
int fd; int fd;
node = (*ast)->item; node = (*ast)->item;
@ -27,8 +26,6 @@ int exec_less(t_btree **ast)
data_singleton()->exec.process.fdin = STDIN; data_singleton()->exec.process.fdin = STDIN;
/* data->exec.process.command = NULL; */ /* data->exec.process.command = NULL; */
p = &data_singleton()->exec.process; // btree_delone(ast, &ast_free);
if (!(p->script & SCRIPT_LOOP))
btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 20:19:29 by ariard #+# #+# */
/* Updated: 2017/02/02 16:00:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_list(t_btree **ast)
{
(void)ast;
// btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */ /* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */
/* Updated: 2017/01/30 21:09:55 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:12:08 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
int exec_or_if(t_btree **ast) int exec_or_if(t_btree **ast)
{ {
t_data *data; t_data *data;
t_process *p;
data = data_singleton(); data = data_singleton();
if (data->exec.aol_status == NULL if (data->exec.aol_status == NULL
@ -34,8 +33,6 @@ int exec_or_if(t_btree **ast)
data->exec.aol_status = NULL; data->exec.aol_status = NULL;
data->exec.aol_search = 0; data->exec.aol_search = 0;
p = &data_singleton()->exec.process; // btree_delone(ast, &ast_free);
if (!(p->script & SCRIPT_LOOP))
btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */ /* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */
/* Updated: 2017/01/30 21:01:33 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:14:32 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -44,7 +44,6 @@ int exec_pipe(t_btree **ast)
close(fds[PIPE_READ]); close(fds[PIPE_READ]);
p->fdin = STDIN; p->fdin = STDIN;
if (!(p->script & SCRIPT_LOOP))
btree_delone(ast, &ast_free); btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */
/* Updated: 2017/01/31 19:31:59 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:19:21 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,13 +14,9 @@
int exec_semi(t_btree **ast) int exec_semi(t_btree **ast)
{ {
t_process *p;
ft_exec(&(*ast)->left); ft_exec(&(*ast)->left);
ft_exec(&(*ast)->right); ft_exec(&(*ast)->right);
p = &data_singleton()->exec.process; // btree_delone(ast, &ast_free);
if (!(p->script & SCRIPT_LOOP))
btree_delone(ast, &ast_free);
return (0); return (0);
} }

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/02/03 16:58:02 by ariard ### ########.fr */ /* Updated: 2017/02/05 22:38:03 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,23 +14,12 @@
int exec_while(t_btree **ast) int exec_while(t_btree **ast)
{ {
t_list **consequent_commands;
t_astnode *node;
int test; int test;
node = ((*ast)->right)->item;
DG("consequent command data '%s'", node->data.str);
consequent_commands = NULL;
test = 10; test = 10;
while (test--) while (test--)
{ ft_exec(&(*ast)->right);
loop_exec(*consequent_commands);
DG("end loop : '%d'", test);
}
// loop_del(*consequent_commands);
// btree_delone(ast, &ast_free); // btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2017/02/03 15:31:42 by ariard ### ########.fr */ /* Updated: 2017/02/06 00:04:12 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
t_execmap g_execmap[] = t_execmap g_execmap[] =
{ {
{TK_NEWLINE, &exec_default}, {TK_NEWLINE, &exec_semi},
{TK_AND_IF, &exec_and_if}, {TK_AND_IF, &exec_and_if},
{TK_OR_IF, &exec_or_if}, {TK_OR_IF, &exec_or_if},
{TK_SEMI, &exec_semi}, {TK_SEMI, &exec_semi},
@ -24,7 +24,7 @@ t_execmap g_execmap[] =
{TK_GREAT, &exec_great}, {TK_GREAT, &exec_great},
{TK_DGREAT, &exec_dgreat}, {TK_DGREAT, &exec_dgreat},
{TK_WHILE, &exec_while}, {TK_WHILE, &exec_while},
{TK_LIST, &exec_list}, {TK_IF, &exec_if},
{TK_COMMAND | TK_SUBSHELL, &exec_command}, {TK_COMMAND | TK_SUBSHELL, &exec_command},
{0, 0}, {0, 0},
}; };

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/02/04 17:15:59 by ariard ### ########.fr */ /* Updated: 2017/02/05 23:15:12 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,6 +33,12 @@ int get_reserved_words(t_list **alst)
token->type = TK_DONE; token->type = TK_DONE;
else if (ft_strncmp(token->data, "do" , 2) == 0) else if (ft_strncmp(token->data, "do" , 2) == 0)
token->type = TK_DO; token->type = TK_DO;
else if (ft_strncmp(token->data, "if", 2) == 0)
token->type = TK_IF;
else if (ft_strncmp(token->data, "then", 4) == 0)
token->type = TK_THEN;
else if(ft_strncmp(token->data, "fi", 4) == 0)
token->type = TK_FI;
} }
} }
previous_token = token; previous_token = token;

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2017/02/05 00:32:21 by ariard ### ########.fr */ /* Updated: 2017/02/05 23:50:29 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,14 +34,10 @@ char *ft_putast(void *nodein)
return (" COM "); return (" COM ");
else if (node->type == TK_WHILE) else if (node->type == TK_WHILE)
return (" WHILE "); return (" WHILE ");
else if (node->type == TK_DO) else if (node->type == TK_IF)
return ("DO"); return ("IF");
else if (node->type == TK_DONE)
return ("DONE");
else if (node->type == TK_NEWLINE) else if (node->type == TK_NEWLINE)
return ("NEW"); return ("NEW");
else if (node->type == TK_LIST)
return (" LIST ");
else if (node->type == TK_GREAT) else if (node->type == TK_GREAT)
return (" > "); return (" > ");
else if (node->type == TK_LESS) else if (node->type == TK_LESS)

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2016/12/05 13:17:50 by jhalford ### ########.fr */ /* Updated: 2017/02/05 22:48:44 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/02/05 21:12:36 by ariard ### ########.fr */ /* Updated: 2017/02/05 23:40:13 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,14 +26,14 @@ int shell_script()
DG("after post_tokenize"); DG("after post_tokenize");
token_print(token); token_print(token);
while (token) // while (token)
{ // {
if (parse(&ast, &token)) if (parse(&ast, &token))
return (1); return (1);
btree_print(STDBUG, ast, &ft_putast); btree_print(STDBUG, ast, &ft_putast);
// if (ft_exec(&ast)) // if (ft_exec(&ast))
// return (1); // return (1);
ast = NULL; ast = NULL;
} // }
return (0); return (0);
} }

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/02/05 21:10:03 by ariard ### ########.fr */ /* Updated: 2017/02/05 23:38:31 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,8 +14,7 @@
t_parser g_parser[] = t_parser g_parser[] =
{ {
{TK_WHILE | TK_NEWLINE, &get_instruction2}, {TK_WHILE | TK_IF | TK_NEWLINE | TK_SEMI, &get_sub_instruction},
{TK_SEMI, &parse_separator},
{TK_AND_IF | TK_OR_IF, &parse_separator}, {TK_AND_IF | TK_OR_IF, &parse_separator},
{TK_AMP, &parse_separator}, {TK_AMP, &parse_separator},
{TK_PIPE, &parse_separator}, {TK_PIPE, &parse_separator},

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 16:56:55 by ariard #+# #+# */ /* Created: 2017/02/03 16:56:55 by ariard #+# #+# */
/* Updated: 2017/02/05 20:52:30 by ariard ### ########.fr */ /* Updated: 2017/02/05 23:45:50 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@ static int get_simple_instruction(t_list **start, t_list **lst)
return (0); return (0);
} }
static int get_compound_instruction(t_list **start, t_list **lst) static int get_loop_instruction(t_list **start, t_list **lst)
{ {
t_list *temp; t_list *temp;
t_token *token; t_token *token;
@ -56,6 +56,39 @@ static int get_compound_instruction(t_list **start, t_list **lst)
return (0); return (0);
} }
static int get_condition_instruction(t_list **start, t_list **lst)
{
t_list *temp;
t_token *token;
int nest;
nest = 0;
while (((*lst) = (*lst)->next))
{
token = (*lst)->content;
if (token->type & TK_THEN)
nest++;
else if (token->type & TK_FI)
nest--;
if (nest == 0 && token->type & TK_FI)
break;
}
while (((*lst) = (*lst)->next))
{
token = (*lst)->content;
if (token->type & (TK_NEWLINE | TK_SEMI))
break;
}
if ((*lst))
{
temp = (*lst)->next;
(*lst)->next = NULL;
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
*lst = temp;
}
return (0);
}
int get_instruction(t_list **lst) int get_instruction(t_list **lst)
{ {
t_token *token; t_token *token;
@ -68,7 +101,9 @@ int get_instruction(t_list **lst)
if (token->type & TK_NEWLINE) if (token->type & TK_NEWLINE)
return (get_simple_instruction(&start, lst)); return (get_simple_instruction(&start, lst));
else if (token->type & TK_WHILE) else if (token->type & TK_WHILE)
return (get_compound_instruction(&start, lst)); return (get_loop_instruction(&start, lst));
else if (token->type & TK_IF)
return (get_condition_instruction(&start, lst));
(*lst) = (*lst)->next; (*lst) = (*lst)->next;
} }
return (0); return (0);

View file

@ -5,14 +5,14 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/04 18:20:53 by ariard #+# #+# */ /* Created: 2017/02/05 22:05:26 by ariard #+# #+# */
/* Updated: 2017/02/05 20:24:57 by ariard ### ########.fr */ /* Updated: 2017/02/05 23:15:02 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parser.h" #include "parser.h"
int get_instruction2(t_btree **ast, t_list **start, t_list **lst) int get_sub_instruction(t_btree **ast, t_list **start, t_list **lst)
{ {
t_token *token; t_token *token;
@ -21,5 +21,9 @@ int get_instruction2(t_btree **ast, t_list **start, t_list **lst)
return (parse_separator(ast, start, lst)); return (parse_separator(ast, start, lst));
else if (token->type == TK_WHILE) else if (token->type == TK_WHILE)
return (parse_while(ast, start, lst)); return (parse_while(ast, start, lst));
else if (token->type == TK_IF)
return (parse_if(ast, start, lst));
else if (token->type == TK_SEMI)
return (parse_separator(ast, start, lst));
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/04 16:52:51 by ariard #+# #+# */ /* Created: 2017/02/04 16:52:51 by ariard #+# #+# */
/* Updated: 2017/02/05 20:53:58 by ariard ### ########.fr */ /* Updated: 2017/02/05 23:46:00 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,6 @@ int parse(t_btree **ast, t_list **token)
{ {
t_list *start; t_list *start;
(void)ast;
start = *token; start = *token;
if (get_instruction(token)) if (get_instruction(token))
return (1); return (1);

116
42sh/src/parser/parse_if.c Normal file
View file

@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/05 23:15:23 by ariard #+# #+# */
/* Updated: 2017/02/05 23:57:18 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
static int parse_after_condition(t_btree **ast, t_list **start, t_list **lst)
{
t_list *temp;
t_list *del;
t_token *token;
int nest;
temp = (*lst);
nest = 0;
while ((temp = temp->next))
{
token = temp->content;
if (token->type & TK_THEN)
nest++;
else if (token->type & TK_FI)
nest--;
if (nest == 0 && (token->type & TK_FI))
break;
}
del = temp;
temp = temp->next;
del->next = NULL;
ft_lst_delif(start, del->content, &ft_addrcmp, &token_free);
if (temp)
ft_parse(ast, &temp);
return (0);
}
static int parse_head(t_btree **ast,
t_btree **new_ast, t_list **start, t_list **lst)
{
t_btree *father;
t_token *token;
t_list *del;
t_astnode *node;
t_astnode item;
father = *ast;
while (father->left)
father = father->left;
*new_ast = btree_create_node(&item, sizeof(item));
((t_astnode *)(*new_ast)->item)->data.token = NULL;
node = (*ast)->item;
if (node->type > 0)
father->left = *new_ast;
else
*new_ast = *ast;
token = (*lst)->content;
node = (*new_ast)->item;
node->type = token->type;
del = (*lst);
*lst = (*lst)->next;
ft_lst_delif(start, del->content, &ft_addrcmp, &token_free);
delete_newline(start, lst);
return (0);
}
static int parse_loop(t_btree **ast, t_list **start, t_list **lst)
{
t_token *token;
t_list *temp;
t_list *new_start;
int nest;
nest = 0;
while ((*lst)->next)
*lst = (*lst)->next;
new_start = *start;
ft_lst_reverse(start);
temp = *lst;
while ((*lst))
{
token = (*lst)->content;
if (token->type & TK_THEN)
nest++;
else if (token->type & TK_FI)
nest--;
if (nest == 1 && (token->type & TK_THEN))
break;
*lst = (*lst)->next;
}
ft_lst_reverse(&temp);
temp = (*lst)->next;
(*lst)->next = NULL;
ft_lst_delif(&new_start, (*lst)->content, &ft_addrcmp, &token_free);
delete_newline(start, &temp);
ft_parse(&(*ast)->right, &temp);
ft_parse(&(*ast)->left, &new_start);
return (0);
}
int parse_if(t_btree **ast, t_list **start, t_list **lst)
{
t_btree *new_ast;
parse_after_condition(ast, start, lst);
parse_head(ast, &new_ast, start, lst);
parse_loop(&new_ast, start, lst);
return (0);
}

View file

@ -1,26 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 16:34:21 by ariard #+# #+# */
/* Updated: 2017/02/03 16:51:39 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int parse_list(t_btree **ast, t_list **start, t_list **lst)
{
t_astnode *node;
t_token *token;
token = (*lst)->content;
node = (*ast)->item;
node->type = TK_LIST;
node->data.str = ft_strdup(token->data);
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */ /* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */
/* Updated: 2017/02/05 21:11:56 by ariard ### ########.fr */ /* Updated: 2017/02/05 21:54:28 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -12,7 +12,7 @@
#include "parser.h" #include "parser.h"
static int delete_newline(t_list **start, t_list **lst) int delete_newline(t_list **start, t_list **lst)
{ {
t_token *token; t_token *token;
t_list *del; t_list *del;
@ -63,6 +63,7 @@ static int parse_head(t_btree **ast,
{ {
t_btree *father; t_btree *father;
t_token *token; t_token *token;
t_list *del;
t_astnode *node; t_astnode *node;
t_astnode item; t_astnode item;
@ -80,8 +81,9 @@ static int parse_head(t_btree **ast,
token = (*lst)->content; token = (*lst)->content;
node = (*new_ast)->item; node = (*new_ast)->item;
node->type = token->type; node->type = token->type;
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); del = (*lst);
*lst = (*lst)->next; *lst = (*lst)->next;
ft_lst_delif(start, del->content, &ft_addrcmp, &token_free);
delete_newline(start, lst); delete_newline(start, lst);
return (0); return (0);
} }