added &&,|| and & operators to lexer/parser

This commit is contained in:
Jack Halford 2016-11-30 22:09:55 +01:00
parent af6452358c
commit 07b8519239
18 changed files with 143 additions and 44 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2016/11/29 19:22:30 by jhalford ### ########.fr */
/* Updated: 2016/11/30 21:07:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,7 +30,10 @@ extern t_exec g_exec[];
int ft_exec(t_btree *ast, t_data *data);
int exec_semi(t_btree *ast, t_data *data);
int exec_or_if(t_btree *ast, t_data *data);
int exec_and_if(t_btree *ast, t_data *data);
int exec_pipe(t_btree *ast, t_data *data);
int exec_less(t_btree *ast, t_data *data);
int exec_great(t_btree *ast, t_data *data);
int exec_dgreat(t_btree *ast, t_data *data);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 15:31:42 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:36:29 by jhalford ### ########.fr */
/* Updated: 2016/11/30 21:07:42 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,16 +15,19 @@
# include "minishell.h"
# define TK_LESS 0x0001
# define TK_GREAT 0x0002
# define TK_DLESS 0x0004
# define TK_DGREAT 0x0008
# define TK_LESSAND 0x0010
# define TK_GREATAND 0x0020
# define TK_SEMI 0x0040
# define TK_PIPE 0x0080
# define TK_WORD 0x0100
# define TK_COMMAND 0x0200
# define TK_LESS 0x00000001
# define TK_GREAT 0x00000002
# define TK_DLESS 0x00000004
# define TK_DGREAT 0x00000008
# define TK_LESSAND 0x00000010
# define TK_GREATAND 0x00000020
# define TK_SEMI 0x00000040
# define TK_PIPE 0x00000080
# define TK_AND_IF 0x00000100
# define TK_OR_IF 0x00000200
# define TK_AMP 0x00000400
# define TK_WORD 0x00100000
# define TK_COMMAND 0x00200000
# define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2016/11/29 20:30:49 by jhalford ### ########.fr */
/* Updated: 2016/11/30 21:48:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_and_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */
/* Updated: 2016/11/30 21:06:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_and_if(t_btree *ast, t_data *data)
{
ft_exec(ast->left, data);
ft_exec(ast->right, data);
return (0);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_or_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */
/* Updated: 2016/11/30 21:45:53 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_or_if(t_btree *ast, t_data *data)
{
ft_exec(ast->left, data);
ft_exec(ast->right, data);
return (0);
}

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_semi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */
/* Updated: 2016/11/30 20:52:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_semi(t_btree *ast, t_data *data)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:16:13 by jhalford ### ########.fr */
/* Updated: 2016/11/30 20:52:07 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,8 @@
t_exec g_exec[] =
{
{TK_AND_IF, &exec_and_if},
{TK_OR_IF, &exec_or_if},
{TK_SEMI, &exec_semi},
{TK_PIPE, &exec_pipe},
{TK_LESS, &exec_less},

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */
/* Updated: 2016/11/28 16:46:16 by jhalford ### ########.fr */
/* Updated: 2016/11/30 16:57:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -44,7 +44,7 @@ int ft_tokenize(t_list **alst, char *str, t_lexstate state)
*alst = ft_lstnew(token, sizeof(*token));
if (ft_is_delim(*str))
state = DELIM;
if (*str == ';' || *str == '|')
if (*str == '&' || *str == ';' || *str == '|')
state = SEP;
else if (*str == '\'')
{

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_sep.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */
/* Updated: 2016/11/30 16:56:58 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_sep(t_list **alst, char *str)
@ -16,6 +28,13 @@ int lexer_sep(t_list **alst, char *str)
*alst = ft_lstnew(token, sizeof(*token));
}
token = (*alst)->content;
token->type = (*str == ';') ? TK_SEMI : TK_PIPE;
return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT));
if (str[0] == '&')
token->type = str[1] == '&' ? TK_AND_IF : TK_AMP;
if (str[0] == '|')
token->type = str[1] == '|' ? TK_OR_IF : TK_PIPE;
token->type = (*str == ';') ? TK_SEMI : token->type;
return (ft_tokenize(&(*alst)->next,
str + 1 +
(token->type & (TK_AND_IF | TK_OR_IF) ? 1 : 0),
DEFAULT));
}

View file

@ -1,6 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_cmp_type.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:08:55 by jhalford #+# #+# */
/* Updated: 2016/11/30 17:52:00 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int token_cmp_type(t_token *token, t_type *ref)
{
return (token->type - *ref);
return (!(token->type & *ref));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */
/* Updated: 2016/11/28 14:39:08 by jhalford ### ########.fr */
/* Updated: 2016/11/30 20:50:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@ void token_print(t_list *lst)
while (lst)
{
token = lst->content;
ft_dprintf(2, "%#06llx: '%s'\n", token->type, token->data);
ft_dprintf(2, "%#010llx: '%s' (at %p next at %p)\n", token->type, token->data, lst, lst->next);
lst = lst->next;
}
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2016/11/28 14:47:40 by jhalford ### ########.fr */
/* Updated: 2016/11/30 21:48:51 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,8 +17,14 @@ char *ft_putast(void *nodein)
t_astnode *node;
node = nodein;
if (node->type == TK_SEMI)
if (node->type == TK_AMP)
return (" & ");
else if (node->type == TK_SEMI)
return (" ; ");
else if (node->type == TK_AND_IF)
return (" && ");
else if (node->type == TK_OR_IF)
return (" || ");
else if (node->type == TK_PIPE)
return (" | ");
else if (node->type == TK_COMMAND)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */
/* Updated: 2016/11/29 20:08:54 by jhalford ### ########.fr */
/* Updated: 2016/11/30 21:48:53 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,21 +28,21 @@ int main(void)
if (ft_interactive_sh(&data))
return (1);
/* ft_dprintf(STDERR, "command='%s'\n", data.input); */
token = NULL;
if (ft_tokenize(&token, data.input, DEFAULT))
return (1);
if (!token)
continue ;
/* token_print(token); */
token_print(token);
ast = NULL;
if (ft_parse(&ast, &token))
return (1);
/* btree_print(STDERR, ast, &ft_putast); */
btree_print(STDERR, ast, &ft_putast);
/* ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); */
/* btree_apply_infix(ast, &ft_putast2); */
/* ft_lstdel(&token, &token_free); */
token = NULL;
if (ft_exec(ast, &data))
return (1);
/* if (ft_exec(ast, &data)) */
/* return (1); */
}
return (0);
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 11:30:08 by jhalford #+# #+# */
/* Updated: 2016/11/28 16:38:17 by jhalford ### ########.fr */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2016/11/30 21:05:53 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,8 @@
t_parser g_parser[] =
{
{TK_SEMI, &parse_separator},
{TK_SEMI | TK_AMP, &parse_separator},
{TK_AND_IF | TK_OR_IF, &parse_separator},
{TK_PIPE, &parse_separator},
{TK_LESS, &parse_less},
{TK_GREAT, &parse_great},
@ -44,14 +45,11 @@ int ft_parse(t_btree **ast, t_list **start)
{
if ((lst = ft_lst_find(*start, &g_parser[i].type, &token_cmp_type)))
{
/* ft_printf("found token:%#06llx\n", g_parser[i].type); */
item.type = g_parser[i].type;
if (g_parser[i].f)
(*g_parser[i].f)(ast, start, &lst);
return (0);
}
else
i++;
i++;
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2016/11/14 17:47:35 by jhalford ### ########.fr */
/* Updated: 2016/11/30 21:45:46 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,8 +28,10 @@ int parse_great(t_btree **ast, t_list **start, t_list **lst)
return (1);
node->u_data.redir.n = ft_atoi(tok->data);
node->u_data.redir.u_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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:12:39 by jhalford ### ########.fr */
/* Updated: 2016/11/30 18:03:37 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */
/* Updated: 2016/11/28 16:21:56 by jhalford ### ########.fr */
/* Updated: 2016/11/30 21:45:49 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,10 +19,11 @@ int parse_separator(t_btree **ast, t_list **start, t_list **lst)
token = (*lst)->content;
node = (*ast)->item;
node->type = token->type;
ft_parse(&(*ast)->right, &(*lst)->next);
node->type = token->type;
ft_lstdelone(lst, &token_free);
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
ft_parse(&(*ast)->left, start);
return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2016/11/14 17:48:50 by jhalford ### ########.fr */
/* Updated: 2016/11/30 21:19:16 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,6 +23,7 @@ int parse_word(t_btree **ast, t_list **start, t_list **lst)
node->type = TK_COMMAND;
node->u_data.sstr = ft_sstradd(node->u_data.sstr, token->data);
ft_parse(ast, &(*lst)->next);
ft_lstdelone(lst, &token_free);
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
return (0);
}