jack a raison: les enums ca marche..

This commit is contained in:
ariard@student.42.fr 2017-02-10 01:43:20 +01:00
parent 87b7d65d71
commit 1358c367e6
20 changed files with 197 additions and 70 deletions

BIN
42sh/includes/.parser.h.swn Normal file

Binary file not shown.

View file

@ -6,20 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/02/09 20:20:23 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:24:00 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LEXER_H
# define LEXER_H
struct s_nest
{
long long do_group;
};
typedef struct s_nest t_nest;
# include "minishell.h"
typedef long long t_type;
@ -43,6 +36,8 @@ typedef long long t_type;
*
*/
/*
# define TK_LESS (1 << 0) //transparent
# define TK_GREAT (1 << 1) //transparent
# define TK_DLESS (1 << 2)
@ -73,6 +68,9 @@ typedef long long t_type;
# define TK_ELSE (1 << 27)
# define TK_UNTIL (1 << 28)
*/
# define TK_COMMAND (1 << 10)
# define TK_WORD (TK_N_WORD | TK_Q_WORD | TK_DQ_WORD)
# define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)
# define TK_NON_FREEABLE (TK_PAREN_OPEN | TK_PAREN_CLOSE | TK_BQUOTE)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/02/06 23:47:53 by ariard ### ########.fr */
/* Updated: 2017/02/09 22:31:21 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -69,7 +69,6 @@ struct s_data
t_exec exec;
t_jobc jobc;
t_script script;
t_nest nest;
};
extern t_stof g_builtins[];

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/09 20:28:03 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:39:33 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,98 @@
*
*/
enum e_sym
{
EMPTY = 1,
TK_LESS,
TK_GREAT,
TK_DLESS,
TK_DGREAT,
TK_LESSAND,
TK_GREATAND,
TK_SEMI,
TK_PIPE,
TK_AND_IF,
TK_OR_IF,
TK_AMP,
TK_PAREN_OPEN,
TK_PAREN_CLOSE,
TK_BQUOTE,
TK_SUBSHELL,
TK_NEWLINE,
TK_WHILE,
TK_DO,
TK_DONE,
TK_IF,
TK_THEN,
TK_FI,
TK_ELIF,
TK_ELSE,
TK_UNTIL,
TK_N_WORD,
TK_Q_WORD,
TK_DQ_WORD,
PROGRAM = 100,
COMPLETE_COMMANDS,
LIST,
AND_OR,
PIPELINE,
PIPE_SEQUENCE,
COMMAND,
COMPOUND_COMMAND,
SUBSHELL,
COMPOUND_LIST,
TERM,
FOR_CLAUSE,
NAME,
IN,
WORDLIST,
CASE_CLAUSE,
CASE_LIST_NS,
CASE_ITEM_NS,
PATTERN,
IF_CLAUSE,
ELSE_PART,
WHILE_CLAUSE,
UNTIL_CLAUSE,
FUNCTION_DEFINITION,
FUNCTION_BODY,
FNAME,
BRACE_GROUP,
DO_GROUP,
SIMPLE_COMMAND,
CMD_NAME,
CMD_WORD,
CMD_PREFIX,
CMD_SUFFIX,
REDIRECT_LIST,
IO_REDIRECT,
IO_FILE,
FILENAME,
IO_HERE,
HERE_END,
NEWLINE_LIST,
LINEBREAK,
SEPARATOR_OP,
SEPARATOR,
SEQUENTIAL_SEP,
ALL = TK_LESS | TK_GREAT | TK_DLESS | TK_DGREAT | TK_LESSAND | TK_GREATAND\
| TK_SEMI | TK_PIPE | TK_AND_IF | TK_OR_IF | TK_AMP | TK_PAREN_OPEN\
| TK_PAREN_CLOSE | TK_BQUOTE | TK_SUBSHELL | TK_NEWLINE | TK_WHILE | TK_DO\
| TK_DONE | TK_IF | TK_THEN | TK_FI | TK_ELIF | TK_ELSE | TK_UNTIL | TK_N_WORD\
| TK_Q_WORD | TK_DQ_WORD | PROGRAM | COMPLETE_COMMANDS | LIST\
| AND_OR | PIPELINE | PIPE_SEQUENCE | COMMAND | COMPOUND_COMMAND | SUBSHELL\
| COMPOUND_LIST | TERM | FOR_CLAUSE | NAME | IN | WORDLIST | CASE_CLAUSE\
| CASE_LIST_NS | CASE_ITEM_NS | PATTERN | IF_CLAUSE | ELSE_PART\
| WHILE_CLAUSE | UNTIL_CLAUSE | FUNCTION_DEFINITION | FUNCTION_BODY | FNAME\
| BRACE_GROUP | DO_GROUP | SIMPLE_COMMAND | CMD_NAME | CMD_WORD | CMD_PREFIX\
| CMD_SUFFIX | REDIRECT_LIST | IO_REDIRECT | IO_FILE | FILENAME | IO_HERE\
| HERE_END | NEWLINE_LIST | LINEBREAK | SEPARATOR_OP | SEPARATOR | SEQUENTIAL_SEP
};
typedef unsigned long long int t_sym;
/*
typedef unsigned long long int t_sym;
#define PROGRAM (1UL << 0)
@ -69,13 +161,34 @@ typedef unsigned long long int t_sym;
#define SEPARATOR (1UL << 44)
#define SEQUENTIAL_SEP (1UL << 45)
#define SYM_DLESS (1UL << 46)
#define SYM_DGREAT (1UL << 47)
#define SYM_GREATAND (1UL << 48)
#define SYM_GREAT (1UL << 49)
#define SYM_LESSAND (1UL << 50)
#define SYM_LESS (1UL << 51)
#define SYM_SEMI (1UL << 52)
#define SYM_LESS (1UL << 49)
#define SYM_GREAT (1UL << 50)
#define ALL_SYM !0
//#define ALL_SYM -1UL
*/
struct s_prodmatch
{
t_type token;
t_sym stack;
t_sym new_sym;
};
typedef struct s_prodmatch t_prodmatch;
extern t_prodmatch g_prodmatch[];
struct s_stackmatch
{
t_sym under;
t_sym top;
};
typedef struct s_stackmatch t_stackmatch;
extern t_stackmatch g_stackmatch[];
int ft_parse(t_btree **ast, t_list **token);
int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst);

View file

@ -1 +1 @@
ls ;
ls > file1 > file2

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/02 15:30:59 by jhalford #+# #+# */
/* Updated: 2017/02/09 15:36:59 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:24:32 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,8 +21,8 @@ int ft_lexer(t_list **alst, char **command)
ret = 1;
else if (ft_tokenize(alst, *command, DEFAULT))
ret = 1;
else if (ft_post_tokenize(alst, command))
ret = 1;
// else if (ft_post_tokenize(alst, command))
// ret = 1;
ft_strdel(command);
return (ret);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */
/* Updated: 2017/02/09 19:54:13 by ariard ### ########.fr */
/* Updated: 2017/02/09 21:55:06 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:07:05 by ariard #+# #+# */
/* Updated: 2017/02/06 20:37:28 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:13:35 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 23:29:49 by ariard #+# #+# */
/* Updated: 2017/01/31 23:59:37 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:15:01 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,6 +26,6 @@ int lexer_then(t_list **alst, char *str)
}
token = (*alst)->content;
token->type = TK_THEN;
state = LIST;
state = TK_WORD;
return (ft_tokenize(&(*alst)->next, str + 4, state));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/02/03 14:34:02 by ariard ### ########.fr */
/* Updated: 2017/02/09 22:31:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,8 +23,6 @@ int data_init(void)
data->env = ft_sstrdup(environ);
data->opts = SHELL_OPTS_JOBC;
data->nest.do_group = 0;
data->exec.process.path = NULL;
data->exec.process.av = NULL;
data->exec.process.fdin = STDIN;

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -6,20 +6,32 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 16:26:30 by ariard #+# #+# */
/* Updated: 2017/02/09 20:14:46 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:42:21 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int eval_sym(t_sym stack, t_sym new_sym)
t_stackmatch g_stackmatch[] =
{
DG("eval head %s && eval sym %s", read_state(stack), read_state(new_sym));
if (stack == 0 && new_sym == CMD_NAME)
return (0);
if (stack == SYM_GREAT && new_sym == CMD_NAME)
return (0);
if (stack == CMD_NAME && new_sym == SYM_GREAT)
return (0);
{EMPTY, CMD_NAME},
{TK_GREAT, CMD_NAME},
{CMD_NAME, TK_GREAT},
{0, 0},
};
int eval_sym(t_sym stack, t_sym new_sym)
{
int i;
DG("eval head %s && sym %s", read_state(stack), read_state(new_sym));
i = 0;
while (g_stackmatch[i].top)
{
if (stack == g_stackmatch[i].under
&& new_sym == g_stackmatch[i].top)
return (0);
i++;
}
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 14:30:22 by ariard #+# #+# */
/* Updated: 2017/02/09 20:32:25 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:40:26 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,23 +22,24 @@ int ft_parse(t_btree **ast, t_list **token)
state = UNDEFINED;
new_sym = ft_memalloc(sizeof(t_sym));
stack = ft_memalloc(sizeof(t_sym) * 1000);
push_stack(stack, 0);
push_stack(stack, EMPTY);
while (*token)
{
produce_sym(*stack, new_sym, token);
DG("new sym %s", read_state(*new_sym));
if (eval_sym(*stack, *new_sym))
state = ERROR;
else
{
aggregate_sym(&stack, new_sym);
// aggregate_sym(&stack, new_sym);
//superflous sym
push_stack(++stack, *new_sym);
}
if (*stack == PROGRAM)
state = PROGRAM;
state = SUCCESS;
if (state == ERROR)
return (error_syntax(token));
if (state == PROGRAM)
if (state == SUCCESS)
ft_putstr("success");
// build_tree(token, ast);
ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/02/09 15:24:15 by ariard ### ########.fr */
/* Updated: 2017/02/09 22:06:41 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/02/03 16:35:32 by ariard ### ########.fr */
/* Updated: 2017/02/09 21:22:16 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,35 +6,33 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/02/09 20:28:25 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:42:12 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
t_prodmatch g_prodmatch[] =
{
{TK_N_WORD, EMPTY, CMD_NAME},
{TK_GREAT, ALL, TK_GREAT},
{TK_N_WORD, ALL, CMD_NAME},
{0, 0, 0},
};
int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst)
{
t_token *token;
int i;
(void)stack;
token = (*lst)->content;
if (token->type == TK_N_WORD)
*new_sym = CMD_NAME;
else if (token->type == TK_NEWLINE)
*new_sym = NEWLINE_LIST;
else if (token->type == TK_DLESS)
*new_sym = SYM_DLESS;
else if (token->type == TK_DGREAT)
*new_sym = SYM_DGREAT;
else if (token->type == TK_GREATAND)
*new_sym = SYM_GREATAND;
else if (token->type == TK_GREAT)
*new_sym = SYM_GREAT;
else if (token->type == TK_LESSAND)
*new_sym = SYM_LESSAND;
else if (token->type == TK_LESS)
*new_sym = SYM_LESS;
else if (token->type == TK_SEMI)
*new_sym = SYM_SEMI;
i = 0;
while (g_prodmatch[i].new_sym)
{
if (token->type == g_prodmatch[i].token
&& stack & g_prodmatch[i].stack)
*new_sym = g_prodmatch[i].new_sym;
i++;
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 15:32:10 by ariard #+# #+# */
/* Updated: 2017/02/09 19:41:44 by ariard ### ########.fr */
/* Updated: 2017/02/10 01:41:47 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@ char *read_state(t_sym current)
{
if (current == CMD_NAME)
return ("CMD_NAME");
if (current == NEWLINE_LIST)
/* if (current == NEWLINE_LIST)
return ("NEWLINE_LIST");
if (current == SYM_DLESS)
return ("DLESS");
@ -24,9 +24,11 @@ char *read_state(t_sym current)
return ("DGREAT");
if (current == SYM_GREATAND)
return ("GREATAND");
if (current == SYM_GREAT)
*/ if (current == TK_GREAT)
return ("GREAT");
if (current == SYM_LESSAND)
if (current == TK_LESS)
return ("LESS");
/* if (current == SYM_LESSAND)
return ("LESSAND");
if (current == SYM_LESS)
return ("LESS");
@ -34,9 +36,13 @@ char *read_state(t_sym current)
return ("SIMPLE_COMMAND");
if (current == PROGRAM)
return ("PROGRAM");
*/ if (current == EMPTY)
return ("EMPTY");
if (current != 0)
return ("NON-DEFINED");
return (NULL);
if (current == 0)
return ("NULL");
return ("NULL");
}
int ft_read_stack(t_sym *stack)

2
42sh/test.sh Normal file
View file

@ -0,0 +1,2 @@
while [ 1 ] ; do
ls