diff --git a/42sh/Makefile b/42sh/Makefile index f7785ee8..64335c15 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/02/20 20:02:23 by ariard ### ########.fr # +# Updated: 2017/02/21 16:38:00 by ariard ### ########.fr # # # # **************************************************************************** # @@ -235,30 +235,12 @@ parser/build_tree.c\ parser/error_syntax.c\ parser/eval_sym.c\ parser/ft_parse.c\ -parser/get_instruction.c\ -parser/get_sub_instruction.c\ -parser/parse.c\ -parser/parse_dgreat.c\ -parser/parse_dless.c\ -parser/parse_do.c\ -parser/parse_done.c\ -parser/parse_elif.c\ -parser/parse_else.c\ -parser/parse_great.c\ -parser/parse_greatand.c\ -parser/parse_if.c\ -parser/parse_less.c\ -parser/parse_lessand.c\ -parser/parse_redir.c\ -parser/parse_separator.c\ -parser/parse_subshell.c\ -parser/parse_while.c\ -parser/parse_word.c\ parser/pop_stack.c\ parser/produce_sym.c\ parser/push_stack.c\ parser/read_stack.c\ -parser/tree_wrapper.c +parser/tree_wrapper.c\ +parser/parser_init.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) diff --git a/42sh/STDEBUG b/42sh/STDEBUG new file mode 100644 index 00000000..e10d17e6 --- /dev/null +++ b/42sh/STDEBUG @@ -0,0 +1,37 @@ + ft_parse.c 42 new sym CMD_NAME + eval_sym.c 637eval head LINEBREAK && sym CMD_NAME + eval_sym.c 637eval head LINEBREAK && sym CMD_SUPERIOR + build_tree.c 60 func TK : 'TK_WORD' TK : 'TK_WORD' + add_cmd.c 21 add cmd + ft_parse.c 42 new sym CMD_SUFFIX + eval_sym.c 637eval head CMD_SUPERIOR && sym CMD_SUFFIX + eval_sym.c 637eval head LINEBREAK && sym SIMPLE_COMMAND + eval_sym.c 637eval head LINEBREAK && sym COMMAND + eval_sym.c 637eval head LINEBREAK && sym PIPE_SEMI_SEQUENCE + build_tree.c 60 func TK : 'TK_WORD' TK : 'TK_WORD' + add_cmd.c 21 add cmd + add_loop.c 23 TEST LOOP + add_cmd.c 21 add cmd + TK_W + +----+ + TK_W + ft_parse.c 42 new sym LINEBREAK + eval_sym.c 637eval head PIPE_SEMI_SEQUENCE && sym LINEBREAK + eval_sym.c 637eval head LINEBREAK && sym PIPE_SEQUENCE + eval_sym.c 637eval head LINEBREAK && sym PIPELINE + eval_sym.c 637eval head LINEBREAK && sym AND_OR + eval_sym.c 637eval head LINEBREAK && sym LIST + eval_sym.c 637eval head LINEBREAK && sym COMPLETE_COMMAND + eval_sym.c 637eval head LINEBREAK && sym COMPLETE_COMMANDS + eval_sym.c 637eval head NULL && sym PROGRAM + TK_W + +----+ + TK_W + ft_parse.c 42 new sym LINEBREAK + eval_sym.c 637eval head PROGRAM && sym LINEBREAK + TK_W + +----+ + TK_W + TK_W + +----+ + TK_W diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 761c3415..f11e925c 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/21 15:20:26 by ariard ### ########.fr */ +/* Updated: 2017/02/21 16:39:07 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,13 @@ * */ +enum e_parstate +{ + UNDEFINED, + SUCCESS, + ERROR, +}; + enum e_sym { LINEBREAK = 1, @@ -125,11 +132,14 @@ enum e_sym # define TK_REDIR(x) (TK_LESS <= x && x <= TK_GREATAND) -typedef int t_sym; - # define MATCH_STACK(x, y) (x == y || y == ALL) -typedef int t_parstate; +struct s_parser +{ + t_parstate state; + t_sym *stack; + t_sym *new_sym; +}; struct s_aggrematch { @@ -139,8 +149,6 @@ struct s_aggrematch int erase_sym; }; -typedef struct s_aggrematch t_aggrematch; - extern t_aggrematch g_aggrematch[]; struct s_prodmatch @@ -150,8 +158,6 @@ struct s_prodmatch t_sym new_sym; }; -typedef struct s_prodmatch t_prodmatch; - extern t_prodmatch g_prodmatch[]; struct s_stackmatch @@ -160,8 +166,6 @@ struct s_stackmatch t_sym under; }; -typedef struct s_stackmatch t_stackmatch; - extern t_stackmatch g_stackmatch[]; struct s_errormatch @@ -170,13 +174,11 @@ struct s_errormatch char *error; }; -typedef struct s_errormatch t_errormatch; - extern t_errormatch g_errormatch[]; -int ft_parse(t_btree **ast, t_list **token); +void parser_init(t_parser *parser); +int ft_parse(t_btree **ast, t_list **token, t_parser *parser); -int ft_parse(t_btree **ast, t_list **token); 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, t_parstate *state); @@ -189,9 +191,6 @@ int error_syntax(t_list **token); int ft_read_stack(t_sym *stack); char *read_state(t_sym current); -#define UNDEFINED (1 << 0) -#define ERROR (1 << 1) -#define SUCCESS (1 << 2) /* * Build AST - rewriting @@ -204,8 +203,6 @@ struct s_treematch int (*add)(t_btree **ast, t_list **lst); }; -typedef struct s_treematch t_treematch; - extern t_treematch g_treematch[]; int build_tree(t_btree **ast, t_list **lst); @@ -230,14 +227,6 @@ int gen_node(t_btree **ast); * */ -typedef struct s_parser t_parser; -typedef struct s_ld t_ld; -typedef struct s_astnode t_astnode; -typedef struct s_redir t_redir; -typedef union u_astdata t_astdata; -typedef union u_word t_word; -typedef long long t_type; - union u_word { char *word; diff --git a/42sh/includes/types.h b/42sh/includes/types.h index f2175ab8..f365bd63 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,40 +6,49 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:03:25 by ariard ### ########.fr */ +/* Updated: 2017/02/21 16:15:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef TYPES_H # define TYPES_H -typedef struct s_data t_data; +typedef struct s_data t_data; -typedef long long t_type; -typedef long long t_flag; -typedef struct s_line t_line; -typedef struct s_comp t_comp; -typedef struct s_exec t_exec; -typedef struct s_jobc t_jobc; -typedef enum e_mode t_mode; +typedef long long t_type; +typedef long long t_flag; +typedef struct s_line t_line; +typedef struct s_comp t_comp; +typedef struct s_exec t_exec; +typedef struct s_jobc t_jobc; +typedef enum e_mode t_mode; -typedef struct s_lexer t_lexer; -typedef enum e_lexstate t_lexstate; -typedef struct s_token t_token; +typedef struct s_lexer t_lexer; +typedef enum e_lexstate t_lexstate; +typedef struct s_token t_token; -typedef struct s_parser t_parser; -typedef struct s_ld t_ld; -typedef struct s_astnode t_astnode; -typedef struct s_redir t_redir; -typedef union u_astdata t_astdata; -typedef union u_word t_word; +typedef struct s_ld t_ld; +typedef struct s_astnode t_astnode; +typedef struct s_redir t_redir; +typedef union u_astdata t_astdata; +typedef union u_word t_word; +typedef long long t_type; -typedef struct s_job t_job; -typedef struct s_execmap t_execmap; -typedef struct s_redirmap t_redirmap; -typedef struct s_process t_process; +typedef struct s_job t_job; +typedef struct s_execmap t_execmap; +typedef struct s_redirmap t_redirmap; +typedef struct s_process t_process; typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]); +typedef int t_sym; +typedef struct s_parser t_parser; +typedef enum e_parstate t_parstate; +typedef struct s_aggrematch t_aggrematch; +typedef struct s_prodmatch t_prodmatch; +typedef struct s_stackmatch t_stackmatch; +typedef struct s_errormatch t_errormatch; +typedef struct s_treematch t_treematch; + t_data *data_singleton(); #endif diff --git a/42sh/src/lexer/lexer_init.c b/42sh/src/lexer/lexer_init.c index 230ee3f6..2c4a0c2f 100644 --- a/42sh/src/lexer/lexer_init.c +++ b/42sh/src/lexer/lexer_init.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/21 16:14:08 by ariard #+# #+# */ +/* Updated: 2017/02/21 16:19:34 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "lexer.h" void lexer_init(t_lexer *lexer) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index cf710d51..b8148e1f 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/02/21 14:29:13 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 16:40:17 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,17 +14,22 @@ int handle_instruction(int fd) { - t_list *token; - t_list *ltoken; - t_lexer lexer; - t_btree *ast; - char *str; + t_list *token; + t_list *ltoken; + t_lexer lexer; + t_parser parser; + t_btree *ast; + char *str; lexer_init(&lexer); + parser_init(&parser); token = NULL; ast = NULL; - do { + while (1) + { str = readline(fd, stack_to_prompt(lexer.stack)); +// if (parser.state == UNDEFINED && !str) +// return ((int)ft_putstr_fd("syntax error near unexpected EOF", 2)); ft_strappend(&lexer.str, str); if (get_lexer_stack(lexer) == BACKSLASH) pop(&lexer.stack); @@ -34,17 +39,23 @@ int handle_instruction(int fd) if (lexer_lex(token ? <oken : &token, &lexer)) return (1); //token_print(token); - } while (get_lexer_stack(lexer)); - if (!token) - return (0); - ft_add_str_in_history(lexer.str); - if (ft_parse(&ast, &token)) - return (1); + if (get_lexer_stack(lexer)) + return (1); + if (!token) + return (0); + ft_add_str_in_history(lexer.str); + if (ft_parse(&ast, &token, &parser)) + return (1); + if (parser.state == SUCCESS) + break; + if (parser.state == ERROR) + return (error_syntax(&token)); + } btree_print(STDBUG, ast, &ft_putast); if (ft_exec(&ast)) return (1); ft_strdel(&lexer.str); - return (0); + return (1); } int get_input_fd() @@ -69,7 +80,7 @@ int main(int ac, char **av) setlocale(LC_ALL, ""); shell_init(ac, av); -// DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", SH_HAS_JOBC(data->opts)?"ON":"OFF"); + // DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", SH_HAS_JOBC(data->opts)?"ON":"OFF"); fd = get_input_fd(); while (handle_instruction(fd)) { diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c index c5a08dab..215f2145 100644 --- a/42sh/src/main/shell_get_opts.c +++ b/42sh/src/main/shell_get_opts.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:00:13 by ariard ### ########.fr */ +/* Updated: 2017/02/21 15:37:22 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ void shell_get_opts(int ac, char **av) int i; i = 1; - if (isatty(STDIN)) + if (isatty(STDIN) && !av[1]) data_singleton()->opts |= SH_INTERACTIVE; while (i < ac && av[i][0] == '-') { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index dc8a97c3..a03801f6 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/02/20 22:39:39 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 16:40:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,46 +26,29 @@ static int end_instruction(t_sym sym) return (0); } -int ft_parse(t_btree **ast, t_list **token) +int ft_parse(t_btree **ast, t_list **token, t_parser *parser) { - t_sym *new_sym; - t_sym *stack; - t_parstate state; - - state = UNDEFINED; - new_sym = ft_memalloc(sizeof(t_sym)); - stack = ft_memalloc(sizeof(t_sym) * 1000); - push_stack(stack, LINEBREAK); while (*token) { - produce_sym(*stack, new_sym, token); - DG("new sym %s", read_state(*new_sym)); - if (eval_sym(*stack, *new_sym)) - state = ERROR; + produce_sym(*parser->stack, parser->new_sym, token); + DG("new sym %s", read_state(*parser->new_sym)); + if (eval_sym(*parser->stack, *parser->new_sym)) + parser->state = ERROR; else { - aggregate_sym(&stack, new_sym, &state); - push_stack(++stack, *new_sym); + aggregate_sym(&parser->stack, parser->new_sym, &parser->state); + push_stack(++parser->stack, *parser->new_sym); } ft_putstr("\n"); - ft_read_stack(stack); - if (*stack == PROGRAM) - state = SUCCESS; - if (state == ERROR) - return (error_syntax(token)); - if (state == SUCCESS) - ft_putstr("success"); + if (*parser->stack == PROGRAM) + parser->state = SUCCESS; build_tree(ast, token); btree_print(STDBUG, *ast, &ft_putast); - if ((end_instruction(*stack) && !(*token)->next) || *stack == PROGRAM) + if ((end_instruction(*parser->stack) && !(*token)->next) + || *parser->stack == PROGRAM) insert_linebreak(token); else ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free); } - if (state == UNDEFINED) - { - ft_putstr("syntax error: unexpected end of file\n"); - return (0); - } return (0); } diff --git a/42sh/src/parser/get_instruction.c b/42sh/src/parser/get_instruction.c deleted file mode 100644 index 09910fd3..00000000 --- a/42sh/src/parser/get_instruction.c +++ /dev/null @@ -1,111 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_instruction.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/03 16:56:55 by ariard #+# #+# */ -/* Updated: 2017/02/06 20:30:13 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -static int get_simple_instruction(t_list **start, t_list **lst) -{ - t_list *temp; - - temp = (*lst)->next; - (*lst)->next = NULL; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - (*lst) = temp; - return (0); -} - -static int get_loop_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_DO) - nest++; - else if (token->type & TK_DONE) - nest--; - if (nest == 0 && token->type & TK_DONE) - 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); -} - -static int get_condition_instruction(t_list **start, t_list **lst) -{ - t_list *temp; - t_token *token; - int nest; - - nest = 0; - while (*lst) - { - token = (*lst)->content; - if (token->type & TK_IF) - nest++; - else if (token->type & TK_FI) - nest--; - if (nest == 0 && token->type & TK_FI) - break; - *lst = (*lst)->next; - } - 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) -{ - t_token *token; - t_list *start; - - start = *lst; - while ((*lst)) - { - token = (*lst)->content; - if (token->type & TK_NEWLINE) - return (get_simple_instruction(&start, lst)); - else if (token->type & (TK_WHILE | TK_UNTIL)) - return (get_loop_instruction(&start, lst)); - else if (token->type & TK_IF) - return (get_condition_instruction(&start, lst)); - (*lst) = (*lst)->next; - } - return (0); -} diff --git a/42sh/src/parser/get_sub_instruction.c b/42sh/src/parser/get_sub_instruction.c deleted file mode 100644 index c5935ce3..00000000 --- a/42sh/src/parser/get_sub_instruction.c +++ /dev/null @@ -1,33 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_instruction2.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/05 22:05:26 by ariard #+# #+# */ -/* Updated: 2017/02/06 20:37:24 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int get_sub_instruction(t_btree **ast, t_list **start, t_list **lst) -{ - t_token *token; - - token = (*lst)->content; - if (token->type & TK_NEWLINE) - return (parse_separator(ast, start, lst)); - else if (token->type & (TK_WHILE | TK_UNTIL)) - return (parse_while(ast, start, lst)); - else if (token->type & TK_IF) - return (parse_if(ast, start, lst)); - else if (token->type & TK_ELIF) - return (parse_elif(ast, start, lst)); - else if (token->type & TK_ELSE) - return (parse_else(ast, start, lst)); - else if (token->type & TK_SEMI) - return (parse_separator(ast, start, lst)); - return (0); -} diff --git a/42sh/src/parser/parse_dgreat.c b/42sh/src/parser/parse_dgreat.c deleted file mode 100644 index d1f19571..00000000 --- a/42sh/src/parser/parse_dgreat.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_dgreat.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:02:01 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_dgreat(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *tok; - t_token *next_tok; - - node = (*ast)->item; - node->type = TK_DGREAT; - if (!(*lst)->next) - return (1); - tok = (*lst)->content; - next_tok = (*lst)->next->content; - if (!(next_tok->type & TK_WORD)) - return (1); - node->data.redir.n = ft_atoi(tok->data); - node->data.redir.word.word = ft_strdup(next_tok->data); - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->left, start); - return (0); -} diff --git a/42sh/src/parser/parse_dless.c b/42sh/src/parser/parse_dless.c deleted file mode 100644 index 77c671e9..00000000 --- a/42sh/src/parser/parse_dless.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_dless.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/03 13:31:40 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:02:10 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_dless(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *tok; - t_token *next_tok; - - node = (*ast)->item; - node->type = TK_DLESS; - if (!(*lst)->next) - return (1); - tok = (*lst)->content; - next_tok = (*lst)->next->content; - if (next_tok->type != TK_WORD) - return (1); - node->data.redir.word.word = ft_strdup(tok->data); - node->data.redir.n = 0; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->left, start); - return (0); -} diff --git a/42sh/src/parser/parse_do.c b/42sh/src/parser/parse_do.c deleted file mode 100644 index 1c0051c1..00000000 --- a/42sh/src/parser/parse_do.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_do.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 16:28:41 by ariard #+# #+# */ -/* Updated: 2017/02/15 19:08:49 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_do(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *token; - - token = (*lst)->content; - node = (*ast)->item; - node->type = TK_DO; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_parse(ast, start); - return (0); -} diff --git a/42sh/src/parser/parse_done.c b/42sh/src/parser/parse_done.c deleted file mode 100644 index 93301b12..00000000 --- a/42sh/src/parser/parse_done.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_done.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 16:36:28 by ariard #+# #+# */ -/* Updated: 2017/02/03 16:47:12 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_done(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *token; - - token = (*lst)->content; - node = (*ast)->item; - node->type = TK_DONE; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_parse(ast, start); - return (0); -} diff --git a/42sh/src/parser/parse_elif.c b/42sh/src/parser/parse_elif.c deleted file mode 100644 index 4867a3b9..00000000 --- a/42sh/src/parser/parse_elif.c +++ /dev/null @@ -1,79 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_elif.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/06 14:50:54 by ariard #+# #+# */ -/* Updated: 2017/02/06 19:22:58 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -static int parse_after_elif(t_btree **ast, t_list **lst) -{ - t_list *temp; - t_list *prev_prev; - t_list *prev; - t_token *token; - int nest; - - temp = (*lst); - nest = 0; - while ((temp = temp->next)) - { - token = temp->content; - if (nest == 0 && (token->type & (TK_ELIF | TK_ELSE))) - break; - else if (token->type & TK_IF) - nest++; - else if (token->type & TK_FI) - nest--; - prev_prev = prev; - prev = temp; - } - prev_prev->next = NULL; - if (prev) - ft_parse(ast, &prev); - return (0); -} - -static int parse_loop(t_btree **ast, t_list **start, t_list **lst) -{ - t_token *token; - t_list *temp; - int nest; - - nest = 0; - while ((*lst)) - { - token = (*lst)->content; - if (token->type & TK_THEN) - nest++; - else if (token->type & (TK_ELIF | TK_ELSE)) - nest--; - if (nest == 1 && (token->type & TK_THEN)) - break; - *lst = (*lst)->next; - } - temp = (*lst)->next; - (*lst)->next = NULL; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - delete_newline(start, &temp); - ft_parse(&(*ast)->right, &temp); - ft_parse(&(*ast)->left, start); - return (0); -} - -int parse_elif(t_btree **ast, t_list **start, t_list **lst) -{ - t_btree *new_ast; - - parse_after_elif(ast, lst); - parse_head(ast, &new_ast, start, lst); - *start = *lst; - parse_loop(&new_ast, start, lst); - return (0); -} diff --git a/42sh/src/parser/parse_else.c b/42sh/src/parser/parse_else.c deleted file mode 100644 index b343eb9b..00000000 --- a/42sh/src/parser/parse_else.c +++ /dev/null @@ -1,42 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_elif.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/06 14:50:54 by ariard #+# #+# */ -/* Updated: 2017/02/06 19:22:56 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -static int parse_after_else(t_btree **ast, t_list **lst) -{ - t_list *temp; - - temp = (*lst); - while ((temp = temp->next)); - if (temp) - ft_parse(ast, &temp); - return (0); -} - -static int parse_loop(t_btree **ast, t_list **start, t_list **lst) -{ - delete_newline(start, lst); - ft_parse(&(*ast)->right, lst); - return (0); -} - -int parse_else(t_btree **ast, t_list **start, t_list **lst) -{ - t_btree *new_ast; - - parse_after_else(ast, lst); - parse_head(ast, &new_ast, start, lst); - *start = *lst; - parse_loop(&new_ast, start, lst); - return (0); -} diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c deleted file mode 100644 index d4a77383..00000000 --- a/42sh/src/parser/parse_great.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_great.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:02:17 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_great(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *tok; - t_token *next_tok; - - node = (*ast)->item; - node->type = TK_GREAT; - if (!(*lst)->next) - return (1); - tok = (*lst)->content; - next_tok = (*lst)->next->content; - if (!(next_tok->type & TK_WORD)) - return (1); - node->data.redir.n = *tok->data == '>' ? 1 : ft_atoi(tok->data); - node->data.redir.word.word = ft_strdup(next_tok->data); - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->left, start); - return (0); -} diff --git a/42sh/src/parser/parse_greatand.c b/42sh/src/parser/parse_greatand.c deleted file mode 100644 index eb93503d..00000000 --- a/42sh/src/parser/parse_greatand.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_greatand.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:02:27 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_greatand(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *tok; - char *and; - - node = (*ast)->item; - node->type = TK_GREATAND; - tok = (*lst)->content; - and = ft_strchr(tok->data, '&'); - node->data.redir.n = *tok->data == '>' ? 1 : ft_atoi(tok->data); - node->data.redir.word.fd = ft_atoi(and + 1); - node->data.redir.close = - tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->left, start); - return (0); -} diff --git a/42sh/src/parser/parse_if.c b/42sh/src/parser/parse_if.c deleted file mode 100644 index f1e13014..00000000 --- a/42sh/src/parser/parse_if.c +++ /dev/null @@ -1,79 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_if.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/05 23:15:23 by ariard #+# #+# */ -/* Updated: 2017/02/06 19:20:47 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) - { - token = temp->content; - if (token->type & TK_IF) - nest++; - else if (token->type & TK_FI) - nest--; - if (nest == 0 && (token->type & TK_FI)) - break; - temp = temp->next; - } - 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_condition(t_btree **ast, - t_btree **new_ast, t_list **start, t_list **lst) -{ - t_btree *father; - t_token *token; - 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; - token->type = TK_ELIF; - ft_parse(&(*new_ast)->right, 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_condition(ast, &new_ast, start, lst); - return (0); -} diff --git a/42sh/src/parser/parse_less.c b/42sh/src/parser/parse_less.c deleted file mode 100644 index ea34770c..00000000 --- a/42sh/src/parser/parse_less.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_less.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:02:38 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_less(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *tok; - t_token *next_tok; - - node = (*ast)->item; - node->type = TK_LESS; - if (!(*lst)->next) - return (1); - tok = (*lst)->content; - next_tok = (*lst)->next->content; - if (!(next_tok->type & TK_WORD)) - return (1); - node->data.redir.n = *tok->data == '>' ? 0 : ft_atoi(tok->data); - node->data.redir.word.word = ft_strdup(next_tok->data); - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->left, start); - return (0); -} diff --git a/42sh/src/parser/parse_lessand.c b/42sh/src/parser/parse_lessand.c deleted file mode 100644 index 636421ba..00000000 --- a/42sh/src/parser/parse_lessand.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_lessand.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:02:46 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_lessand(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *tok; - char *and; - - node = (*ast)->item; - node->type = TK_LESSAND; - tok = (*lst)->content; - and = ft_strchr(tok->data, '&'); - node->data.redir.n = *tok->data == '<' ? 0 : ft_atoi(tok->data); - node->data.redir.word.fd = ft_atoi(and + 1); - node->data.redir.close = - tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->left, start); - return (0); -} diff --git a/42sh/src/parser/parse_redir.c b/42sh/src/parser/parse_redir.c deleted file mode 100644 index 4d209f62..00000000 --- a/42sh/src/parser/parse_redir.c +++ /dev/null @@ -1,34 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_redir.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/02 18:58:27 by jhalford #+# #+# */ -/* Updated: 2017/02/06 16:07:08 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_redir(t_btree **ast, t_list **start, t_list **lst) -{ - t_token *tok; - - tok = (*lst)->content; - if (tok->type == TK_LESS) - return (parse_less(ast, start, lst)); - else if (tok->type == TK_GREAT) - return (parse_great(ast, start, lst)); - else if (tok->type == TK_DLESS) - return (parse_dless(ast, start, lst)); - else if (tok->type == TK_DGREAT) - return (parse_dgreat(ast, start, lst)); - else if (tok->type == TK_LESSAND) - return (parse_lessand(ast, start, lst)); - else if (tok->type == TK_GREATAND) - return (parse_greatand(ast, start, lst)); - else - return (-42); -} diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c deleted file mode 100644 index ca1e0387..00000000 --- a/42sh/src/parser/parse_separator.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_separator.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */ -/* Updated: 2017/02/15 19:34:57 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_separator(t_btree **ast, t_list **start, t_list **lst) -{ - t_token *token; - t_astnode *node; - - token = (*lst)->content; - node = (*ast)->item; - node->type = token->type; - ft_parse(&(*ast)->right, &(*lst)->next); - (*lst)->next = NULL; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->left, start); - return (0); -} diff --git a/42sh/src/parser/parse_subshell.c b/42sh/src/parser/parse_subshell.c deleted file mode 100644 index 2177a919..00000000 --- a/42sh/src/parser/parse_subshell.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_subshell.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/11 16:52:44 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:02:58 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_subshell(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *token; - - (void)start; - token = (*lst)->content; - if ((*lst)->next && ((t_token*)(*lst)->next->content)->type & TK_WORD) - { - ft_dprintf(2, "{red}%s: parse error near ')'{eoc}\n", SHELL_NAME); - return (-1); - } - node = (*ast)->item; - node->type = TK_SUBSHELL; - node->data.sstr = malloc(4 * sizeof(char *)); - node->data.sstr[0] = ft_strdup(data_singleton()->argv[0]); - node->data.sstr[1] = ft_strdup("-c"); - node->data.sstr[2] = ft_strdup(token->data); - node->data.sstr[3] = NULL; - ft_parse(ast, &(*lst)->next); - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - return (0); -} diff --git a/42sh/src/parser/parse_while.c b/42sh/src/parser/parse_while.c deleted file mode 100644 index 6206e152..00000000 --- a/42sh/src/parser/parse_while.c +++ /dev/null @@ -1,132 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_while.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 16:03:28 by ariard #+# #+# */ -/* UpdatGed: 2017/02/05 18:28:11 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int delete_newline(t_list **start, t_list **lst) -{ - t_token *token; - t_list *del; - - while ((*lst)) - { - token = (*lst)->content; - del = *lst; - if (token->type & TK_NEWLINE) - ft_lst_delif(start, del->content, &ft_addrcmp, &token_free); - else - break; - *lst = (*lst)->next; - } - return (0); -} - -static int parse_after_loop(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_DO) - nest++; - else if (token->type & TK_DONE) - nest--; - if (nest == 0 && (token->type & TK_DONE)) - 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); -} - -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_DO) - nest++; - else if (token->type & TK_DONE) - nest--; - if (nest == 1 && (token->type & TK_DO)) - 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_while(t_btree **ast, t_list **start, t_list **lst) -{ - t_btree *new_ast; - - parse_after_loop(ast, start, lst); - parse_head(ast, &new_ast, start, lst); - parse_loop(&new_ast, start, lst); - return (0); -} diff --git a/42sh/src/parser/parse_word.c b/42sh/src/parser/parse_word.c deleted file mode 100644 index 95a79202..00000000 --- a/42sh/src/parser/parse_word.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_word.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:03:20 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_word(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *token; - char **the_tab; - - token = (*lst)->content; - node = (*ast)->item; - node->type = TK_COMMAND; - if ((the_tab = (char **)malloc(sizeof(char *) * 4))) - { - the_tab[0] = ft_strdup(token->data); - the_tab[1] = (char *)dup_char_esc(token->esc, token->size >> 3); - the_tab[2] = (char *)dup_char_esc(token->esc2, token->size >> 3); - the_tab[3] = NULL; - } - ft_ld_pushback(&node->data.token, the_tab); - ft_parse(ast, &(*lst)->next); - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - DG("parse word end"); - return (0); -} diff --git a/42sh/src/parser/parse.c b/42sh/src/parser/parser_init.c similarity index 64% rename from 42sh/src/parser/parse.c rename to 42sh/src/parser/parser_init.c index f21dacab..2869bea3 100644 --- a/42sh/src/parser/parse.c +++ b/42sh/src/parser/parser_init.c @@ -1,25 +1,21 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parse.c :+: :+: :+: */ +/* parser_init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/04 16:52:51 by ariard #+# #+# */ -/* Updated: 2017/02/14 17:48:58 by ariard ### ########.fr */ +/* Created: 2017/02/21 16:14:04 by ariard #+# #+# */ +/* Updated: 2017/02/21 16:38:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int parse(t_btree **ast, t_list **token) +void parser_init(t_parser *parser) { - t_list *start; - - start = *token; -// if (get_instruction(token)) -// return (1); - if (ft_parse(ast, &start)) - return (1); - return (0); + parser->state = UNDEFINED; + parser->new_sym = ft_memalloc(sizeof(t_sym)); + parser->stack = ft_memalloc(sizeof(t_sym) * 1000); + push_stack(parser->stack, LINEBREAK); }