diff --git a/42sh/Makefile b/42sh/Makefile index d55a28b1..a1eb2f94 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -170,10 +170,13 @@ main/shell_script.c\ main/sig_handler.c\ parser/parse.c\ parser/ft_parse.c\ -parser/produce_prim_sym.c\ +parser/produce_sym.c\ parser/eval_sym.c\ +parser/aggregate_sym.c\ +parser/pop_stack.c\ parser/push_stack.c\ parser/superflous_sym.c\ +parser/error_syntax.c\ parser/get_instruction.c\ parser/get_sub_instruction.c\ parser/parse_dgreat.c\ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index a6b1c776..f55f3b82 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/02/09 17:06:08 by ariard ### ########.fr */ +/* Updated: 2017/02/09 20:20:23 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index fe6b869c..3c53dfb8 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/09 18:04:35 by ariard ### ########.fr */ +/* Updated: 2017/02/09 20:28:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,27 +75,27 @@ typedef unsigned long long int t_sym; #define SYM_GREAT (1UL << 49) #define SYM_LESSAND (1UL << 50) #define SYM_LESS (1UL << 51) - +#define SYM_SEMI (1UL << 52) int ft_parse(t_btree **ast, t_list **token); -int produce_sym(t_sym *new_sym, t_list **lst); +int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst); int eval_sym(t_sym stack, t_sym new_sym); -int aggregate_sym(t_sym stack, t_sym *new_sym); +int aggregate_sym(t_sym **stack, t_sym *new_sym); int superflous_sym(t_sym stack, t_sym new_sym); -int push_stack(t_sym *stack, t_sym *new_sym); +int push_stack(t_sym *stack, t_sym new_sym); +int pop_stack(t_sym **stack, int k); + +int error_syntax(t_list **token); int ft_read_stack(t_sym *stack); char *read_state(t_sym current); -enum e_parstate -{ - UNDEFINED, - ERROR, - SUCCESS, -}; +#define UNDEFINED (1 << 0) +#define ERROR (1 << 1) +#define SUCCESS (1 << 2) -typedef enum e_parstate t_parstate; +typedef int t_parstate; /* * Build AST diff --git a/42sh/sample/stack.sh b/42sh/sample/stack.sh index 35665a6c..1900a1fe 100644 --- a/42sh/sample/stack.sh +++ b/42sh/sample/stack.sh @@ -1 +1 @@ -ls > file1 +ls ; diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 2498ad2d..43d9fb29 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2017/02/09 15:37:13 by ariard ### ########.fr */ +/* Updated: 2017/02/09 19:54:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index dbc44be1..13a6d1dc 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/02/03 19:53:49 by ariard ### ########.fr */ +/* Updated: 2017/02/09 19:26:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index 57eb7fe1..0c33c5b3 100644 --- a/42sh/src/main/shell_script.c +++ b/42sh/src/main/shell_script.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ -/* Updated: 2017/02/09 16:45:16 by ariard ### ########.fr */ +/* Updated: 2017/02/09 20:30:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 1afd8229..05632520 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:39:18 by ariard #+# #+# */ -/* Updated: 2017/02/09 18:10:13 by ariard ### ########.fr */ +/* Updated: 2017/02/09 20:19:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,20 +14,15 @@ //descente recursive pour multi aggregation -int aggregate_sym(t_sym stack, t_sym *new_sym) +int aggregate_sym(t_sym **stack, t_sym *new_sym) { - int k; - - if (stack == 0 && *new_sym == CMD_NAME) + DG("aggregate head %s && sym %s", read_state(**stack), read_state(*new_sym)); + if (**stack == 0 && *new_sym == CMD_NAME) { - new_sym = SIMPLE_COMMAND; - k = aggregate_sym(stack, new_sym); - return (k); + *new_sym = SIMPLE_COMMAND; + aggregate_sym(stack, new_sym); } - if (stack == SIMPLE_COMMAND && *new_sym == PROGRAM) - { - new_sym = PROGRAM - } - return (0); - + if (**stack == 0 && *new_sym == SIMPLE_COMMAND) + *new_sym = PROGRAM; + return (0); } diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c new file mode 100644 index 00000000..bfbeb805 --- /dev/null +++ b/42sh/src/parser/error_syntax.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error_syntax.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ +/* Updated: 2017/02/09 20:31:48 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int error_syntax(t_list **lst) +{ + (void)lst; + ft_putstr_fd("syntax error near unexepected token ';'", 2); + return (1); +} diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 0ca80845..b78cda57 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 16:26:30 by ariard #+# #+# */ -/* Updated: 2017/02/09 17:50:23 by ariard ### ########.fr */ +/* Updated: 2017/02/09 20:14:46 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ int eval_sym(t_sym stack, t_sym new_sym) { -// DG("eval head %s && eval sym %s", read_state(stack), read_state(new_sym)); + DG("eval head %s && eval sym %s", read_state(stack), read_state(new_sym)); if (stack == 0 && new_sym == CMD_NAME) return (0); if (stack == SYM_GREAT && new_sym == CMD_NAME) diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index b56a2d19..f4edb39a 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 14:30:22 by ariard #+# #+# */ -/* Updated: 2017/02/09 18:03:53 by ariard ### ########.fr */ +/* Updated: 2017/02/09 20:32:25 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,40 +17,32 @@ int ft_parse(t_btree **ast, t_list **token) t_sym *new_sym; t_sym *stack; t_parstate state; - int k; (void)ast; state = UNDEFINED; new_sym = ft_memalloc(sizeof(t_sym)); stack = ft_memalloc(sizeof(t_sym) * 1000); - *stack = 0; - k = 0; + push_stack(stack, 0); while (*token) { - produce_sym(new_sym, token); + produce_sym(*stack, new_sym, token); if (eval_sym(*stack, *new_sym)) state = ERROR; - DG("head state : %s", read_state(*stack)); else { - k = aggregate_sym(*stack, new_sym) - if (k) - { - while (k--) - pop_stack(stack--); - push_stack(++stack, new_sym); - } - else if (!superflous_sym(new_sym, stack)) - push_stack(++stack, new_sym); + aggregate_sym(&stack, new_sym); + //superflous sym + push_stack(++stack, *new_sym); } -/* if (head_stack == PROGRAM) + if (*stack == PROGRAM) state = PROGRAM; if (state == ERROR) - return (error_syntax(token)); + return (error_syntax(token)); if (state == PROGRAM) - return (0); - build_tree(token, ast); -*/ } + ft_putstr("success"); +// build_tree(token, ast); + ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free); + } ft_read_stack(stack); return (0); } diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index 27b56c01..aa644e27 100644 --- a/42sh/src/parser/parse_separator.c +++ b/42sh/src/parser/parse_separator.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */ -/* Updated: 2017/02/06 17:52:31 by ariard ### ########.fr */ +/* Updated: 2017/02/09 19:27:22 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/pop_stack.c b/42sh/src/parser/pop_stack.c new file mode 100644 index 00000000..29e7045f --- /dev/null +++ b/42sh/src/parser/pop_stack.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pop_stack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/09 19:12:44 by ariard #+# #+# */ +/* Updated: 2017/02/09 20:02:05 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int pop_stack(t_sym **stack, int k) +{ + while (k--) + *stack-- = 0; + return (0); +} diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 8e9e4988..b3a9cead 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,16 +6,17 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/02/09 17:58:45 by ariard ### ########.fr */ +/* Updated: 2017/02/09 20:28:25 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int produce_sym(t_sym *new_sym, t_list **lst) +int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst) { t_token *token; + (void)stack; token = (*lst)->content; if (token->type == TK_N_WORD) *new_sym = CMD_NAME; @@ -33,5 +34,7 @@ int produce_sym(t_sym *new_sym, t_list **lst) *new_sym = SYM_LESSAND; else if (token->type == TK_LESS) *new_sym = SYM_LESS; + else if (token->type == TK_SEMI) + *new_sym = SYM_SEMI; return (0); } diff --git a/42sh/src/parser/push_stack.c b/42sh/src/parser/push_stack.c index 07605faf..a3cd4af9 100644 --- a/42sh/src/parser/push_stack.c +++ b/42sh/src/parser/push_stack.c @@ -6,14 +6,14 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 16:48:30 by ariard #+# #+# */ -/* Updated: 2017/02/09 17:55:44 by ariard ### ########.fr */ +/* Updated: 2017/02/09 20:00:35 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int push_stack(t_sym *stack, t_sym *new_sym) +int push_stack(t_sym *stack, t_sym new_sym) { - *stack = *new_sym; + *stack = new_sym; return (0); } diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index d8f8ee2e..4523b906 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/02/09 17:33:30 by ariard ### ########.fr */ +/* Updated: 2017/02/09 19:41:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,12 @@ char *read_state(t_sym current) return ("LESSAND"); if (current == SYM_LESS) return ("LESS"); + if (current == SIMPLE_COMMAND) + return ("SIMPLE_COMMAND"); + if (current == PROGRAM) + return ("PROGRAM"); + if (current != 0) + return ("NON-DEFINED"); return (NULL); } diff --git a/42sh/src/parser/superflous_sym.c b/42sh/src/parser/superflous_sym.c index 5c4e10a6..ac7790d2 100644 --- a/42sh/src/parser/superflous_sym.c +++ b/42sh/src/parser/superflous_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:47:33 by ariard #+# #+# */ -/* Updated: 2017/02/09 17:56:25 by ariard ### ########.fr */ +/* Updated: 2017/02/09 19:25:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ int superflous_sym(t_sym stack, t_sym new_sym) { - if (stack == new_sym); + if (stack == new_sym) return (1); return (0); }