if elif ok, else coming

This commit is contained in:
ariard@student.42.fr 2017-02-20 17:20:34 +01:00
parent 3d65d42708
commit 2e012b3e91
17 changed files with 81 additions and 43 deletions

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/20 14:57:52 by ariard ### ########.fr */
/* Updated: 2017/02/20 17:07:02 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -217,9 +217,11 @@ int add_file(t_btree **ast, t_list **lst);
int add_loop_cmd(t_btree **ast, t_list **lst);
int add_loop_sep(t_btree **ast, t_list **lst);
int add_condition_cmd(t_btree **ast, t_list **lst);
int add_condition_sep(t_btree **ast, t_list **lst);
int add_branch(t_btree **ast, t_list **lst);
int isloop(t_btree **ast);
int isdir(t_btree **ast);
int iscondition(t_btree **ast);
int iscondition(t_btree **ast, t_list **lst);
int join_ast(t_btree **ast, t_btree **new_node);
int gen_node(t_btree **ast);

View file

@ -1,4 +1,10 @@
if ls ;
if ls
then
pwd
elif ls
then
pwd
elif ls
then
pwd
fi

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:41:26 by ariard ### ########.fr */
/* Updated: 2017/02/20 16:42:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,9 +20,13 @@ char *ft_putast(void *nodein)
t_type type;
};
node = nodein;
if (node->type == TK_DO)
if (node->type == TK_THEN)
return ("THEN");
else if (node->type == TK_FI)
return ("FI");
else if (node->type == TK_DO)
return ("TK_DO");
if (node->type == TK_AMP)
else if (node->type == TK_AMP)
return (" & ");
else if (node->type == TK_N_WORD)
return (" TK_N_WORD");

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/02/19 18:46:04 by ariard ### ########.fr */
/* Updated: 2017/02/20 17:03:15 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,8 +25,10 @@ int add_cmd(t_btree **ast, t_list **lst)
return (add_file(ast, lst));
else if (isloop(ast))
return (add_loop_cmd(ast, lst));
else if (iscondition(ast))
else if (iscondition(ast, lst) == 1)
return (add_condition_cmd(ast, lst));
else if (iscondition(ast, lst) == 2)
return (add_branch(ast, lst));
else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN)
return (add_cmd(&(*ast)->right, lst));
my_tab = NULL;

View file

@ -6,29 +6,31 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 18:12:52 by ariard #+# #+# */
/* Updated: 2017/02/19 18:46:01 by ariard ### ########.fr */
/* Updated: 2017/02/20 17:17:34 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int iscondition(t_btree **ast)
int iscondition(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
node = NULL;
token = (*lst)->content;
if (*ast)
{
node = (*ast)->item;
if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && iscondition(&(*ast)->right) == 1)
return (1);
if ((node->type == TK_IF || node->type == TK_ELIF)
&& node->full == 1)
if ((node->type == TK_IF || iscondition(&(*ast)->right, lst))
&& token->type == TK_ELIF && node->full == 0)
return (2);
if ((node->type == TK_IF || node->type == TK_ELIF)
&& node->full == 0)
return (1);
if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && iscondition(&(*ast)->right, lst) == 1)
return (1);
}
return (0);
}
@ -40,17 +42,40 @@ int add_condition_cmd(t_btree **ast, t_list **lst)
token = (*lst)->content;
node = (*ast)->item;
if (token->type == TK_IF && node->type == TK_IF)
if (token->type == TK_IF && (node->type == TK_IF || node->type == TK_ELIF))
node->nest++;
if (token->type == TK_FI && node->type == TK_IF && node->nest > 0)
if (token->type == TK_FI && (node->type == TK_IF || node->type == TK_ELIF)
&& node->nest > 0)
node->nest--;
else if (token->type == TK_FI && node->type == TK_IF && node->nest == 0)
else if (token->type == TK_FI && (node->type == TK_IF || node->type == TK_ELIF))
return ((node->full = 1));
if (token->type == TK_THEN)
return (add_cmd(&(*ast)->right, lst));
else if (!(*ast)->right && iscondition(&(*ast)->left) != 2)
else if (!(*ast)->right)
return (add_cmd(&(*ast)->left, lst));
else
return (add_cmd(&(*ast)->right, lst));
return (0);
}
int add_condition_sep(t_btree **ast, t_list **lst)
{
if (!(*ast)->right)
return (add_sep(&(*ast)->left, lst));
else
return (add_sep(&(*ast)->right, lst));
return (0);
}
int add_branch(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_btree *new_node;
new_node = NULL;
gen_node(&new_node);
join_ast(ast, &new_node);
node = (new_node)->item;
node->type = TK_NEWLINE;
return (add_cmd(ast, lst));
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 22:17:14 by ariard #+# #+# */
/* Updated: 2017/02/19 18:38:25 by ariard ### ########.fr */
/* Updated: 2017/02/20 17:18:49 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 19:12:07 by ariard #+# #+# */
/* Updated: 2017/02/19 18:46:06 by ariard ### ########.fr */
/* Updated: 2017/02/20 17:07:16 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,9 +21,10 @@ int add_sep(t_btree **ast, t_list **lst)
DG("add sep");
if (isloop(ast) == 1)
return (add_loop_sep(ast, lst));
else if (iscondition(ast, lst) == 1)
return (add_condition_sep(ast, lst));
if (!*ast)
gen_node(ast);
node = (*ast)->item;
token = (*lst)->content;
// if (node->type != TK_DO)
// {
@ -33,6 +34,5 @@ int add_sep(t_btree **ast, t_list **lst)
node = (new_node)->item;
// }
node->type = token->type;
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:39:18 by ariard #+# #+# */
/* Updated: 2017/02/20 16:06:08 by ariard ### ########.fr */
/* Updated: 2017/02/20 16:10:21 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -232,20 +232,20 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state)
int i;
i = 0;
DG("aggregate head %s && sym %s",
read_state(**stack), read_state(*new_sym));
// DG("aggregate head %s && sym %s",
// read_state(**stack), read_state(*new_sym));
while (g_aggrematch[i].top)
{
if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(**stack, g_aggrematch[i].under))
{
DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
*new_sym = g_aggrematch[i].new_sym;
if (g_aggrematch[i].erase_sym)
{
pop_stack(stack, g_aggrematch[i].erase_sym);
DG("stack after pop: %s", read_state(**stack));
// DG("stack after pop: %s", read_state(**stack));
}
if (eval_sym(**stack, *new_sym))
return ((*state = ERROR));

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 18:32:59 by ariard #+# #+# */
/* Updated: 2017/02/19 18:40:15 by ariard ### ########.fr */
/* Updated: 2017/02/20 16:53:55 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,7 +35,7 @@ t_treematch g_treematch[] =
static int isseparator(int type, int cache)
{
if (type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO
|| cache == TK_NEWLINE))
|| cache == TK_NEWLINE || cache == TK_THEN))
return (0);
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 16:26:30 by ariard #+# #+# */
/* Updated: 2017/02/20 16:06:15 by ariard ### ########.fr */
/* Updated: 2017/02/20 16:10:06 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -600,7 +600,7 @@ int eval_sym(t_sym stack, t_sym new_sym)
{
int i;
DG("eval head %s && sym %s", read_state(stack), read_state(new_sym));
// DG("eval head %s && sym %s", read_state(stack), read_state(new_sym));
i = 0;
while (g_stackmatch[i].top)
{

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 14:30:22 by ariard #+# #+# */
/* Updated: 2017/02/19 18:41:29 by ariard ### ########.fr */
/* Updated: 2017/02/20 16:32:27 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,7 +33,6 @@ int ft_parse2(t_btree **ast, t_list **token)
t_sym *stack;
t_parstate state;
(void)ast;
state = UNDEFINED;
new_sym = ft_memalloc(sizeof(t_sym));
stack = ft_memalloc(sizeof(t_sym) * 1000);
@ -57,8 +56,8 @@ int ft_parse2(t_btree **ast, t_list **token)
return (error_syntax(token));
if (state == SUCCESS)
ft_putstr("success");
// build_tree(ast, token);
// btree_print(STDBUG, *ast, &ft_putast);
build_tree(ast, token);
btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(*stack) && !(*token)->next) || *stack == PROGRAM)
insert_linebreak(token);
else

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/02/20 15:54:45 by ariard ### ########.fr */
/* Updated: 2017/02/20 16:09:54 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -86,8 +86,8 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst)
int i;
token = (*lst)->content;
DG("produce stack : %s && token : %s", read_state(stack),
read_state(token->type));
// DG("produce stack : %s && token : %s", read_state(stack),
// read_state(token->type));
i = 0;
*new_sym = 0;
while (g_prodmatch[i].new_sym)
@ -95,7 +95,7 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst)
if (token->type == g_prodmatch[i].token
&& stack == g_prodmatch[i].stack)
{
DG("MATCH : %s", read_state(g_prodmatch[i].new_sym));
// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym));
*new_sym = g_prodmatch[i].new_sym;
}
i++;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 15:32:10 by ariard #+# #+# */
/* Updated: 2017/02/20 15:04:21 by ariard ### ########.fr */
/* Updated: 2017/02/20 16:09:16 by ariard ### ########.fr */
/* */
/* ************************************************************************** */