include + parser state
This commit is contained in:
parent
71e8d28263
commit
d05cec4962
27 changed files with 147 additions and 995 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
|
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2016/08/29 21:32:58 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/error_syntax.c\
|
||||||
parser/eval_sym.c\
|
parser/eval_sym.c\
|
||||||
parser/ft_parse.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/pop_stack.c\
|
||||||
parser/produce_sym.c\
|
parser/produce_sym.c\
|
||||||
parser/push_stack.c\
|
parser/push_stack.c\
|
||||||
parser/read_stack.c\
|
parser/read_stack.c\
|
||||||
parser/tree_wrapper.c
|
parser/tree_wrapper.c\
|
||||||
|
parser/parser_init.c
|
||||||
|
|
||||||
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||||
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||||
|
|
|
||||||
37
42sh/STDEBUG
Normal file
37
42sh/STDEBUG
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
[7m[33m ft_parse.c [1m[34m42 [0mnew sym CMD_NAME[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym CMD_NAME[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym CMD_SUPERIOR[0m
|
||||||
|
[7m[33m build_tree.c [1m[34m60 [0mfunc TK : 'TK_WORD' TK : 'TK_WORD'[0m
|
||||||
|
[7m[33m add_cmd.c [1m[34m21 [0madd cmd[0m
|
||||||
|
[7m[33m ft_parse.c [1m[34m42 [0mnew sym CMD_SUFFIX[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head CMD_SUPERIOR && sym CMD_SUFFIX[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym SIMPLE_COMMAND[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym COMMAND[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym PIPE_SEMI_SEQUENCE[0m
|
||||||
|
[7m[33m build_tree.c [1m[34m60 [0mfunc TK : 'TK_WORD' TK : 'TK_WORD'[0m
|
||||||
|
[7m[33m add_cmd.c [1m[34m21 [0madd cmd[0m
|
||||||
|
[7m[33m add_loop.c [1m[34m23 [0mTEST LOOP[0m
|
||||||
|
[7m[33m add_cmd.c [1m[34m21 [0madd cmd[0m
|
||||||
|
TK_W
|
||||||
|
+----+
|
||||||
|
TK_W
|
||||||
|
[7m[33m ft_parse.c [1m[34m42 [0mnew sym LINEBREAK[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head PIPE_SEMI_SEQUENCE && sym LINEBREAK[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym PIPE_SEQUENCE[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym PIPELINE[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym AND_OR[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym LIST[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym COMPLETE_COMMAND[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head LINEBREAK && sym COMPLETE_COMMANDS[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head NULL && sym PROGRAM[0m
|
||||||
|
TK_W
|
||||||
|
+----+
|
||||||
|
TK_W
|
||||||
|
[7m[33m ft_parse.c [1m[34m42 [0mnew sym LINEBREAK[0m
|
||||||
|
[7m[33m eval_sym.c [1m[34m637[0meval head PROGRAM && sym LINEBREAK[0m
|
||||||
|
TK_W
|
||||||
|
+----+
|
||||||
|
TK_W
|
||||||
|
TK_W
|
||||||
|
+----+
|
||||||
|
TK_W
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/01 12:15:54 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
|
enum e_sym
|
||||||
{
|
{
|
||||||
LINEBREAK = 1,
|
LINEBREAK = 1,
|
||||||
|
|
@ -125,11 +132,14 @@ enum e_sym
|
||||||
|
|
||||||
# define TK_REDIR(x) (TK_LESS <= x && x <= TK_GREATAND)
|
# define TK_REDIR(x) (TK_LESS <= x && x <= TK_GREATAND)
|
||||||
|
|
||||||
typedef int t_sym;
|
|
||||||
|
|
||||||
# define MATCH_STACK(x, y) (x == y || y == ALL)
|
# 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
|
struct s_aggrematch
|
||||||
{
|
{
|
||||||
|
|
@ -139,8 +149,6 @@ struct s_aggrematch
|
||||||
int erase_sym;
|
int erase_sym;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct s_aggrematch t_aggrematch;
|
|
||||||
|
|
||||||
extern t_aggrematch g_aggrematch[];
|
extern t_aggrematch g_aggrematch[];
|
||||||
|
|
||||||
struct s_prodmatch
|
struct s_prodmatch
|
||||||
|
|
@ -150,8 +158,6 @@ struct s_prodmatch
|
||||||
t_sym new_sym;
|
t_sym new_sym;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct s_prodmatch t_prodmatch;
|
|
||||||
|
|
||||||
extern t_prodmatch g_prodmatch[];
|
extern t_prodmatch g_prodmatch[];
|
||||||
|
|
||||||
struct s_stackmatch
|
struct s_stackmatch
|
||||||
|
|
@ -160,8 +166,6 @@ struct s_stackmatch
|
||||||
t_sym under;
|
t_sym under;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct s_stackmatch t_stackmatch;
|
|
||||||
|
|
||||||
extern t_stackmatch g_stackmatch[];
|
extern t_stackmatch g_stackmatch[];
|
||||||
|
|
||||||
struct s_errormatch
|
struct s_errormatch
|
||||||
|
|
@ -170,13 +174,11 @@ struct s_errormatch
|
||||||
char *error;
|
char *error;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct s_errormatch t_errormatch;
|
|
||||||
|
|
||||||
extern t_errormatch g_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 produce_sym(t_sym stack, t_sym *new_sym, t_list **lst);
|
||||||
int eval_sym(t_sym stack, t_sym new_sym);
|
int eval_sym(t_sym stack, t_sym new_sym);
|
||||||
int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state);
|
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);
|
int ft_read_stack(t_sym *stack);
|
||||||
char *read_state(t_sym current);
|
char *read_state(t_sym current);
|
||||||
|
|
||||||
#define UNDEFINED (1 << 0)
|
|
||||||
#define ERROR (1 << 1)
|
|
||||||
#define SUCCESS (1 << 2)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Build AST - rewriting
|
* Build AST - rewriting
|
||||||
|
|
@ -204,8 +203,6 @@ struct s_treematch
|
||||||
int (*add)(t_btree **ast, t_list **lst);
|
int (*add)(t_btree **ast, t_list **lst);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct s_treematch t_treematch;
|
|
||||||
|
|
||||||
extern t_treematch g_treematch[];
|
extern t_treematch g_treematch[];
|
||||||
|
|
||||||
int build_tree(t_btree **ast, t_list **lst);
|
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
|
union u_word
|
||||||
{
|
{
|
||||||
char *word;
|
char *word;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/13 17:11:48 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -27,12 +27,12 @@ typedef struct s_lexer t_lexer;
|
||||||
typedef enum e_lexstate t_lexstate;
|
typedef enum e_lexstate t_lexstate;
|
||||||
typedef struct s_token t_token;
|
typedef struct s_token t_token;
|
||||||
|
|
||||||
typedef struct s_parser t_parser;
|
|
||||||
typedef struct s_ld t_ld;
|
typedef struct s_ld t_ld;
|
||||||
typedef struct s_astnode t_astnode;
|
typedef struct s_astnode t_astnode;
|
||||||
typedef struct s_redir t_redir;
|
typedef struct s_redir t_redir;
|
||||||
typedef union u_astdata t_astdata;
|
typedef union u_astdata t_astdata;
|
||||||
typedef union u_word t_word;
|
typedef union u_word t_word;
|
||||||
|
typedef long long t_type;
|
||||||
|
|
||||||
typedef struct s_job t_job;
|
typedef struct s_job t_job;
|
||||||
typedef struct s_execmap t_execmap;
|
typedef struct s_execmap t_execmap;
|
||||||
|
|
@ -40,6 +40,15 @@ typedef struct s_redirmap t_redirmap;
|
||||||
typedef struct s_process t_process;
|
typedef struct s_process t_process;
|
||||||
typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]);
|
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();
|
t_data *data_singleton();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,15 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* lexer_init.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2017/02/21 16:14:08 by ariard #+# #+# */
|
||||||
|
/* Updated: 2017/02/21 16:19:34 by ariard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
|
|
||||||
void lexer_init(t_lexer *lexer)
|
void lexer_init(t_lexer *lexer)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/06 18:40:58 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,14 +17,19 @@ int handle_instruction(int fd)
|
||||||
t_list *token;
|
t_list *token;
|
||||||
t_list *ltoken;
|
t_list *ltoken;
|
||||||
t_lexer lexer;
|
t_lexer lexer;
|
||||||
|
t_parser parser;
|
||||||
t_btree *ast;
|
t_btree *ast;
|
||||||
char *str;
|
char *str;
|
||||||
|
|
||||||
lexer_init(&lexer);
|
lexer_init(&lexer);
|
||||||
|
parser_init(&parser);
|
||||||
token = NULL;
|
token = NULL;
|
||||||
ast = NULL;
|
ast = NULL;
|
||||||
do {
|
while (1)
|
||||||
|
{
|
||||||
str = readline(fd, stack_to_prompt(lexer.stack));
|
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);
|
ft_strappend(&lexer.str, str);
|
||||||
if (get_lexer_stack(lexer) == BACKSLASH)
|
if (get_lexer_stack(lexer) == BACKSLASH)
|
||||||
pop(&lexer.stack);
|
pop(&lexer.stack);
|
||||||
|
|
@ -34,17 +39,23 @@ int handle_instruction(int fd)
|
||||||
if (lexer_lex(token ? <oken : &token, &lexer))
|
if (lexer_lex(token ? <oken : &token, &lexer))
|
||||||
return (1);
|
return (1);
|
||||||
//token_print(token);
|
//token_print(token);
|
||||||
} while (get_lexer_stack(lexer));
|
if (get_lexer_stack(lexer))
|
||||||
|
return (1);
|
||||||
if (!token)
|
if (!token)
|
||||||
return (0);
|
return (0);
|
||||||
ft_add_str_in_history(lexer.str);
|
ft_add_str_in_history(lexer.str);
|
||||||
if (ft_parse(&ast, &token))
|
if (ft_parse(&ast, &token, &parser))
|
||||||
return (1);
|
return (1);
|
||||||
|
if (parser.state == SUCCESS)
|
||||||
|
break;
|
||||||
|
if (parser.state == ERROR)
|
||||||
|
return (error_syntax(&token));
|
||||||
|
}
|
||||||
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_strdel(&lexer.str);
|
ft_strdel(&lexer.str);
|
||||||
return (0);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_input_fd()
|
int get_input_fd()
|
||||||
|
|
@ -69,7 +80,7 @@ int main(int ac, char **av)
|
||||||
|
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
shell_init(ac, av);
|
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();
|
fd = get_input_fd();
|
||||||
while (handle_instruction(fd))
|
while (handle_instruction(fd))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/11 14:04:48 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;
|
int i;
|
||||||
|
|
||||||
i = 1;
|
i = 1;
|
||||||
if (isatty(STDIN))
|
if (isatty(STDIN) && !av[1])
|
||||||
data_singleton()->opts |= SH_INTERACTIVE;
|
data_singleton()->opts |= SH_INTERACTIVE;
|
||||||
while (i < ac && av[i][0] == '-')
|
while (i < ac && av[i][0] == '-')
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
|
/* 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);
|
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)
|
while (*token)
|
||||||
{
|
{
|
||||||
produce_sym(*stack, new_sym, token);
|
produce_sym(*parser->stack, parser->new_sym, token);
|
||||||
DG("new sym %s", read_state(*new_sym));
|
DG("new sym %s", read_state(*parser->new_sym));
|
||||||
if (eval_sym(*stack, *new_sym))
|
if (eval_sym(*parser->stack, *parser->new_sym))
|
||||||
state = ERROR;
|
parser->state = ERROR;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
aggregate_sym(&stack, new_sym, &state);
|
aggregate_sym(&parser->stack, parser->new_sym, &parser->state);
|
||||||
push_stack(++stack, *new_sym);
|
push_stack(++parser->stack, *parser->new_sym);
|
||||||
}
|
}
|
||||||
ft_putstr("\n");
|
ft_putstr("\n");
|
||||||
ft_read_stack(stack);
|
if (*parser->stack == PROGRAM)
|
||||||
if (*stack == PROGRAM)
|
parser->state = SUCCESS;
|
||||||
state = SUCCESS;
|
|
||||||
if (state == ERROR)
|
|
||||||
return (error_syntax(token));
|
|
||||||
if (state == SUCCESS)
|
|
||||||
ft_putstr("success");
|
|
||||||
build_tree(ast, token);
|
build_tree(ast, token);
|
||||||
btree_print(STDBUG, *ast, &ft_putast);
|
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);
|
insert_linebreak(token);
|
||||||
else
|
else
|
||||||
ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free);
|
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);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,111 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* get_instruction.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* get_instruction2.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_dgreat.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_dless.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_do.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_done.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_elif.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_elif.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_great.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_greatand.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_if.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_less.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_lessand.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_redir.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_separator.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_subshell.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,132 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_while.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_word.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,25 +1,21 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* parse.c :+: :+: :+: */
|
/* parser_init.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/02/04 16:52:51 by ariard #+# #+# */
|
/* Created: 2017/02/21 16:14:04 by ariard #+# #+# */
|
||||||
/* Updated: 2017/02/14 17:48:58 by ariard ### ########.fr */
|
/* Updated: 2017/02/21 16:38:13 by ariard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
|
||||||
int parse(t_btree **ast, t_list **token)
|
void parser_init(t_parser *parser)
|
||||||
{
|
{
|
||||||
t_list *start;
|
parser->state = UNDEFINED;
|
||||||
|
parser->new_sym = ft_memalloc(sizeof(t_sym));
|
||||||
start = *token;
|
parser->stack = ft_memalloc(sizeof(t_sym) * 1000);
|
||||||
// if (get_instruction(token))
|
push_stack(parser->stack, LINEBREAK);
|
||||||
// return (1);
|
|
||||||
if (ft_parse(ast, &start))
|
|
||||||
return (1);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue