trop fatigue pour debug les case nesting, fuck it, todo tomorrow

This commit is contained in:
AntoHesse 2017-02-23 01:55:09 +01:00
parent 03238b2042
commit 79bceb05a9
17 changed files with 163 additions and 33 deletions

View file

@ -229,6 +229,7 @@ parser/add_condition.c\
parser/add_file.c\ parser/add_file.c\
parser/add_loop.c\ parser/add_loop.c\
parser/add_sep.c\ parser/add_sep.c\
parser/add_case.c\
parser/aggregate_sym.c\ parser/aggregate_sym.c\
parser/build_tree.c\ parser/build_tree.c\
parser/error_syntax.c\ parser/error_syntax.c\

View file

@ -216,8 +216,12 @@ int add_loop_sep(t_btree **ast, t_list **lst);
int add_condition_cmd(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_condition_sep(t_btree **ast, t_list **lst);
int add_branch(t_btree **ast, t_list **lst); int add_branch(t_btree **ast, t_list **lst);
int add_case_cmd(t_btree **ast, t_list **lst);
int add_case_sep(t_btree **ast, t_list **lst);
int add_pattern(t_btree **ast, t_list **lst);
int isloop(t_btree **ast); int isloop(t_btree **ast);
int isdir(t_btree **ast); int isdir(t_btree **ast);
int iscase(t_btree **ast, t_list **lst);
int iscondition(t_btree **ast, t_list **lst); int iscondition(t_btree **ast, t_list **lst);
int join_ast(t_btree **ast, t_btree **new_node); int join_ast(t_btree **ast, t_btree **new_node);
@ -255,6 +259,7 @@ struct s_astnode
int nest; int nest;
int full; int full;
int pattern;
t_type type; t_type type;
t_astdata data; t_astdata data;
}; };

View file

@ -1,12 +1,7 @@
case $rental in case $rental in
("bus") case yolo in ("bus") case $rental in
("bonjour") echo hello ;; ("yolo") echo hello ;;
("hello") echo bonjour ;; ("bonjour") echo yolo ;;
esac esac ;;
case yala in ("van") echo yolo ;;
("bonjour") echo hello ;;
("hello") echo yolo ;;
esac ;;
("van") echo "For $rental rental is Rs.10 per k/m.";;
("jeep") echo "For $rental rental is Rs.5 per k/m.";;
esac esac

11
42sh/sample/for/for.sh Normal file
View file

@ -0,0 +1,11 @@
for i in ls
do
for i in echo
do
pwd
done
for i in cd
do
cat
done
done

View file

@ -1,8 +1,10 @@
if ls if ls
then then
if ls ls
then elif ls
ls | cat then
pwd ; ls ls
fi elif ls
then
ls
fi fi

View file

@ -16,7 +16,17 @@ char *ft_putast(void *nodein)
{ {
t_astnode *node; t_astnode *node;
node = nodein; node = nodein;
if (node->type == TK_THEN) if (node->type == TK_CASE)
return ("TK_CASE");
else if (node->type == TK_PAREN_OPEN)
return ("TK_OPE");
else if (node->type == TK_PAREN_CLOSE)
return ("TK_CLO");
else if (node->type == TK_IN)
return ("TK_IN");
else if (node->type ==TK_ESAC)
return ("TK_ESAC");
else if (node->type == TK_THEN)
return ("THEN"); return ("THEN");
else if (node->type == TK_FI) else if (node->type == TK_FI)
return ("FI"); return ("FI");

View file

@ -63,7 +63,7 @@ int handle_instruction(int fd)
token = NULL; token = NULL;
} }
DG("succesful parsing:"); DG("succesful parsing:");
// btree_print(STDBUG, ast, &ft_putast); btree_print(STDBUG, ast, &ft_putast);
/* if (ft_exec(&ast)) */ /* if (ft_exec(&ast)) */
/* return (1); */ /* return (1); */
ft_add_str_in_history(lexer.str); ft_add_str_in_history(lexer.str);

View file

@ -0,0 +1,81 @@
#include "parser.h"
int iscase(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_PAREN_OPEN && node->full == 0)
{
// DG("go right");
return (1);
}
if ((node->type == TK_CASE || node->type == TK_PAREN_OPEN
|| iscase(&(*ast)->right, lst) == 1) && token->type == TK_WORD
&& node->pattern == 0)
{
// DG("add pattern");
return (2);
}
if ((node->type == TK_CASE || iscase(&(*ast)->right, lst) == 4)
&& token->type == TK_PAREN_OPEN)
{
// DG("new branch");
return (3);
}
if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && iscase(&(*ast)->right, lst) == 1)
{
// DG(" go right");
return (1);
}
if (node->type == TK_PAREN_OPEN && node->nest == 0)
return (4);
}
return (0);
}
int add_case_cmd(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
token = (*lst)->content;
node = (*ast)->item;
DG("add case cmd");
if (token->type == TK_CASE && node->type == TK_PAREN_OPEN)
{
DG("nesting");
node->nest++;
}
if (token->type == TK_ESAC && node->type == TK_PAREN_OPEN && node->nest > 0)
{
DG("nesting less");
node->nest--;
}
else if (token->type == TK_DSEMI && node->type == TK_PAREN_OPEN)
return ((node->full = 1));
return (add_cmd(&(*ast)->right, lst));
}
int add_case_sep(t_btree **ast, t_list **lst)
{
return (add_sep(&(*ast)->right, lst));
}
int add_pattern(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
token = (*lst)->content;
node = (*ast)->item;
node->data.redir.word.word = ft_strdup(token->data);
node->pattern = 1;
return (0);
}

View file

@ -29,8 +29,24 @@ int add_cmd(t_btree **ast, t_list **lst)
return (add_condition_cmd(ast, lst)); return (add_condition_cmd(ast, lst));
else if (iscondition(ast, lst) == 2) else if (iscondition(ast, lst) == 2)
return (add_branch(ast, lst)); return (add_branch(ast, lst));
else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN) else if (iscase(ast, lst) == 1)
{
DG("go add cmd");
return (add_case_cmd(ast, lst));
}
else if (iscase(ast, lst) == 2)
return (add_pattern(ast, lst));
else if (iscase(ast, lst) == 3)
{
DG("add branc");
return (add_branch(ast, lst));
}
else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN
&& node->type != TK_PAREN_CLOSE && node->type != TK_ESAC)
{
DG("return cmd : %s", read_state(node->type));
return (add_cmd(&(*ast)->right, lst)); return (add_cmd(&(*ast)->right, lst));
}
my_tab = NULL; my_tab = NULL;
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;

View file

@ -20,7 +20,6 @@ int isloop(t_btree **ast)
if (*ast) if (*ast)
{ {
node = (*ast)->item; node = (*ast)->item;
DG("TEST LOOP");
if ((node->type == TK_NEWLINE || node->type == TK_SEMI if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && isloop(&(*ast)->right) == 1) || node->type == TK_AMP) && isloop(&(*ast)->right) == 1)
return (1); return (1);

View file

@ -23,6 +23,8 @@ int add_sep(t_btree **ast, t_list **lst)
return (add_loop_sep(ast, lst)); return (add_loop_sep(ast, lst));
else if (iscondition(ast, lst) == 1) else if (iscondition(ast, lst) == 1)
return (add_condition_sep(ast, lst)); return (add_condition_sep(ast, lst));
else if (iscase(ast, lst))
return (add_case_sep(ast, lst));
if (!*ast) if (!*ast)
gen_node(ast); gen_node(ast);
token = (*lst)->content; token = (*lst)->content;

View file

@ -279,20 +279,20 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state)
int i; int i;
i = 0; i = 0;
DG("aggregate head %s && sym %s", // DG("aggregate head %s && sym %s",
read_state(**stack), read_state(*new_sym)); // read_state(**stack), read_state(*new_sym));
while (g_aggrematch[i].top) while (g_aggrematch[i].top)
{ {
if (*new_sym == g_aggrematch[i].top if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(**stack, g_aggrematch[i].under)) && 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; *new_sym = g_aggrematch[i].new_sym;
if (g_aggrematch[i].erase_sym) if (g_aggrematch[i].erase_sym)
{ {
pop_stack(stack, 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)) if (eval_sym(**stack, *new_sym))
return ((*state = ERROR)); return ((*state = ERROR));

View file

@ -31,13 +31,17 @@ t_treematch g_treematch[] =
{TK_THEN, &add_cmd}, {TK_THEN, &add_cmd},
{TK_FI, &add_cmd}, {TK_FI, &add_cmd},
{TK_NEWLINE, &add_sep}, {TK_NEWLINE, &add_sep},
{TK_CASE, &add_cmd},
{TK_ESAC, &add_cmd},
{TK_PAREN_OPEN, &add_cmd},
{TK_PAREN_CLOSE, &add_cmd},
{0, NULL}, {0, NULL},
}; };
static int isseparator(int type, int cache) static int isseparator(int type, int cache)
{ {
if (type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO if (type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO
|| cache == TK_NEWLINE || cache == TK_THEN)) || cache == TK_NEWLINE || cache == TK_THEN || cache == TK_IN))
return (0); return (0);
return (1); return (1);
} }
@ -50,6 +54,8 @@ int build_tree(t_btree **ast, t_list **lst)
i = 0; i = 0;
token = (*lst)->content; token = (*lst)->content;
//check bug de cache
cache = token->type;
while (g_treematch[i].type) while (g_treematch[i].type)
{ {
if (g_treematch[i].type == token->type if (g_treematch[i].type == token->type
@ -57,8 +63,7 @@ int build_tree(t_btree **ast, t_list **lst)
{ {
DG("func TK : '%s' TK : '%s'", DG("func TK : '%s' TK : '%s'",
read_state(g_treematch[i].type) ,read_state(token->type)); read_state(g_treematch[i].type) ,read_state(token->type));
cache = token->type;
return (g_treematch[i].add(ast, lst)); return (g_treematch[i].add(ast, lst));
} }
i++; i++;

View file

@ -823,7 +823,7 @@ int eval_sym(t_sym stack, t_sym new_sym)
{ {
int i; 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; i = 0;
while (g_stackmatch[i].top) while (g_stackmatch[i].top)
{ {

View file

@ -43,14 +43,14 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
aggregate_sym(&parser->stack, parser->new_sym, &parser->state); aggregate_sym(&parser->stack, parser->new_sym, &parser->state);
push_stack(++parser->stack, *parser->new_sym); push_stack(++parser->stack, *parser->new_sym);
} }
ft_read_stack(parser->stack); // ft_read_stack(parser->stack);
DG("\n"); DG("\n");
if (*parser->stack == PROGRAM) if (*parser->stack == PROGRAM)
parser->state = SUCCESS; parser->state = SUCCESS;
else else
parser->state = UNDEFINED; parser->state = UNDEFINED;
// build_tree(ast, token); build_tree(ast, token);
// btree_print(STDBUG, *ast, &ft_putast); btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(*parser->stack) && !(*token)->next)) if ((end_instruction(*parser->stack) && !(*token)->next))
insert_linebreak(token); insert_linebreak(token);
else else

View file

@ -97,8 +97,8 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst)
int i; int i;
token = (*lst)->content; token = (*lst)->content;
DG("produce stack : %s && token : %s", read_state(stack), // DG("produce stack : %s && token : %s", read_state(stack),
read_state(token->type)); // read_state(token->type));
i = 0; i = 0;
*new_sym = 0; *new_sym = 0;
while (g_prodmatch[i].new_sym) while (g_prodmatch[i].new_sym)
@ -106,7 +106,7 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst)
if (token->type == g_prodmatch[i].token if (token->type == g_prodmatch[i].token
&& stack == g_prodmatch[i].stack) && 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; *new_sym = g_prodmatch[i].new_sym;
} }
i++; i++;

View file

@ -31,6 +31,9 @@ int gen_node(t_btree **ast)
((t_astnode *)(*ast)->item)->nest = 0; ((t_astnode *)(*ast)->item)->nest = 0;
((t_astnode *)(*ast)->item)->full = 0; ((t_astnode *)(*ast)->item)->full = 0;
((t_astnode *)(*ast)->item)->type = 0; ((t_astnode *)(*ast)->item)->type = 0;
((t_astnode *)(*ast)->item)->pattern = 0;
} }
return (0); return (0);
} }