From e83a22b16b71fd0d966bf79f6093e5106c5964a1 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Wed, 22 Mar 2017 17:28:32 +0100 Subject: [PATCH] builtin, lexer et parser .h a la nomre --- 42sh/includes/builtin.h | 4 ++-- 42sh/includes/lexer.h | 10 +++++----- 42sh/includes/minishell.h | 3 ++- 42sh/includes/parser.h | 26 +++++++++++--------------- 42sh/includes/types.h | 4 +++- 42sh/src/main/main.c | 4 ++-- 42sh/src/parser/aggregate_sym.c | 10 +++++++++- 42sh/src/parser/eval_sym.c | 5 ++++- 42sh/src/parser/produce_sym.c | 2 +- 9 files changed, 39 insertions(+), 29 deletions(-) diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 094bc086..96fb1253 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */ -/* Updated: 2017/03/20 23:22:19 by ariard ### ########.fr */ +/* Updated: 2017/03/22 17:28:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ # define BT_ENV_LI (1 << 0) # define BT_ENV_LU (1 << 1) -struct s_env_data +struct s_env_data { t_flag flag; char **av_data; diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 13622138..f156068a 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/03/20 16:44:48 by ariard ### ########.fr */ +/* Updated: 2017/03/22 17:25:58 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ # include "libft.h" # include "types.h" -enum e_lexstate +enum e_lexstate { DEFAULT, PAREN, @@ -37,7 +37,7 @@ enum e_lexstate END, }; -struct s_token +struct s_token { t_type type; char *data; @@ -46,7 +46,7 @@ struct s_token int size; }; -struct s_lexer +struct s_lexer { char *str; int pos; @@ -55,7 +55,7 @@ struct s_lexer t_list *heredoc_stack; }; -struct s_rvwords +struct s_rvwords { char *word; int type; diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 26b20751..2be1b684 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,12 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/21 18:09:39 by ariard ### ########.fr */ +/* Updated: 2017/03/22 17:26:34 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H + # define SHELL_NAME "minishell" # include diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 1a25f778..d3a0cbb7 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -1,11 +1,12 @@ /* ************************************************************************** */ /* */ +/* ::: :::::::: */ /* parser.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:18:14 by ariard ### ########.fr */ +/* Created: 2017/03/22 17:22:51 by ariard #+# #+# */ +/* Updated: 2017/03/22 17:25:11 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,10 +36,10 @@ struct s_parser struct s_aggrematch { - t_sym top; - t_sym under; - t_sym new_sym; - int erase_sym; + t_sym top; + t_sym under; + t_sym new_sym; + int erase_sym; }; struct s_prodmatch @@ -64,7 +65,7 @@ int do_parser_routine(t_list **token, t_btree **ast); void parser_init(t_parser *parser); void parser_destroy(t_parser *parser); int stack_init(t_parser *parser); -int redir_init(t_type type, t_redir *redir); +int redir_init(t_type type, t_redir *redir); int ft_parse(t_btree **ast, t_list **token, t_parser *parser); int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst); @@ -82,15 +83,10 @@ int error_eof(void); int ft_read_stack(t_sym *stack); char *read_state(t_sym current); -/* - * Build AST - rewriting - * -*/ - struct s_treematch { t_type type; - int (*add)(t_btree **ast, t_list **lst); + int (*add)(t_btree **ast, t_list **lst); }; int build_tree(t_btree **ast, t_list **lst); @@ -145,7 +141,7 @@ int superflous_token(t_btree **ast, t_list **list); void sym_free(void *data, size_t size); void tree_func_free(void *data, size_t content_size); -struct s_distrostree +struct s_distrostree { int (*test)(t_btree **ast, t_list **lst); int (*add)(t_btree **ast, t_list **lst); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 726270b3..5ca2429f 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/20 10:38:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/22 16:53:22 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -198,6 +198,8 @@ enum e_sym OPEN_FUNC, CLOSE_FUNC, CLOSE_LIST, + SEMI_SUBSHELL, + SEMI_BRACE, REDIR, CMD, HEREDOCDATA, diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index d0ac9720..ad444135 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/22 15:22:59 by ariard ### ########.fr */ +/* Updated: 2017/03/22 17:00:12 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ static int handle_instruction(t_list **token, t_btree **ast) return (ret); if (do_lexer_routine(token, stream) > 0) continue ; - /* token_print(*token); */ + token_print(*token); if ((ret = do_parser_routine(token, ast)) == 1 && SH_NO_INTERACTIVE(data->opts)) return (ret); diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 9dc025f4..5e8450bc 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 15:58:38 by ariard #+# #+# */ -/* Updated: 2017/03/18 19:18:21 by ariard ### ########.fr */ +/* Updated: 2017/03/22 16:54:58 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,7 @@ t_aggrematch g_aggrematch[] = {TK_DONE, COMPOUND_LIST, DO_GROUP, TK_DO}, {TK_ESAC, TK_IN, CASE_CLAUSE, TK_CASE}, {TK_ESAC, CASE_LIST_NS, CASE_CLAUSE, TK_CASE}, + {TK_PAREN_CLOSE, SEMI_SUBSHELL, SUBSHELL, TK_PAREN_OPEN}, {TK_PAREN_CLOSE, COMPOUND_LIST, SUBSHELL, TK_PAREN_OPEN}, {TK_PAREN_CLOSE, CMD_SUPERIOR, SUBSHELL, TK_PAREN_OPEN}, {TK_PAREN_CLOSE, PIPE_SEMI_SEQUENCE, SUBSHELL, TK_PAREN_OPEN}, @@ -220,7 +221,9 @@ t_aggrematch g_aggrematch[] = {COMPOUND_LIST, CASE_LIST_NS, CASE_LIST_NS, CASE_LIST_NS}, {CLOSE_LIST, PATTERN, CASE_LIST_NS, PATTERN_CASE}, {CLOSE_LIST, FUNC_NAME, FUNCTION_DEFINITION, FUNC_NAME}, + {SUBSHELL, TK_PAREN_OPEN, SEMI_SUBSHELL, 0}, {SUBSHELL, ALL, COMPOUND_COMMAND, 0}, + {BRACE_CLAUSE, TK_LBRACE, SEMI_BRACE, 0}, {BRACE_CLAUSE, ALL, COMPOUND_COMMAND, 0}, {COMPOUND_COMMAND, FUNC_NAME, COMMAND, FUNC_NAME}, {AND_OR_MINOR, PIPE_SEMI_SEQUENCE, AND_OR_MAJOR, PIPE_SEMI_SEQUENCE}, @@ -379,15 +382,20 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) return (1); i = -1; head = (*stack)->content; + DG("aggregate head %s && sym %s", + read_state(*head), read_state(*new_sym)); while (g_aggrematch[++i].top) if (*new_sym == g_aggrematch[i].top && MATCH_STACK(*head, g_aggrematch[i].under)) { + + 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); head = (*stack)->content; + DG("stack after pop: %s", read_state(*head)); } if (eval_sym(stack, *new_sym) && !(*state = ERROR)) return (1); diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 458e17c9..82db6598 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:11:21 by ariard #+# #+# */ -/* Updated: 2017/03/22 16:11:11 by ariard ### ########.fr */ +/* Updated: 2017/03/22 16:56:05 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -578,6 +578,7 @@ t_stackmatch g_stackmatch[] = {TK_PAREN_CLOSE, COMPOUND_LIST}, {TK_PAREN_CLOSE, FUNC_NAME}, {TK_PAREN_CLOSE, OPEN_FUNC}, + {TK_PAREN_CLOSE, SEMI_SUBSHELL}, {TK_RBRACE, TK_SEMI}, {TK_RBRACE, END_COMMAND}, {TK_RBRACE, SEPARATOR_OP}, @@ -1047,6 +1048,7 @@ t_stackmatch g_stackmatch[] = {SUBSHELL, COMPLETE_CONDITION}, {SUBSHELL, CONDITION}, {SUBSHELL, AND_OR_MAJOR}, + {SEMI_SUBSHELL, TK_PAREN_OPEN}, {COMPOUND_COMMAND, LINEBREAK}, {COMPOUND_COMMAND, TK_PAREN_OPEN}, {COMPOUND_COMMAND, TK_LBRACE}, @@ -1271,6 +1273,7 @@ int eval_sym(t_list **stack, t_sym new_sym) return (1); head = (*stack)->content; i = 0; + DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); while (g_stackmatch[i].top) { if (new_sym == g_stackmatch[i].top && *head == g_stackmatch[i].under) diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 9027522a..090b39cd 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/03/22 16:10:09 by ariard ### ########.fr */ +/* Updated: 2017/03/22 16:34:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */