gen ast en cours

This commit is contained in:
ariard@student.42.fr 2017-02-15 20:58:41 +01:00
parent fc37e7999a
commit 185ed2ebd3
24 changed files with 189 additions and 86 deletions

View file

@ -176,6 +176,10 @@ parser/aggregate_sym.c\
parser/pop_stack.c\
parser/push_stack.c\
parser/error_syntax.c\
parser/build_tree.c\
parser/tree_wrapper.c\
parser/add_sep.c\
parser/add_cmd.c\
parser/get_instruction.c\
parser/get_sub_instruction.c\
parser/parse_dgreat.c\

View file

View file

View file

View file

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/02/13 19:41:57 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:46:49 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/02/09 22:31:21 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:22:59 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/14 19:09:38 by ariard ### ########.fr */
/* Updated: 2017/02/15 20:50:55 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -121,63 +121,6 @@ enum e_sym
typedef int t_sym;
/*
typedef unsigned long long int t_sym;
#define PROGRAM (1UL << 0)
#define COMPLETE_COMMANDS (1UL << 1)
#define COMPLETE_COMMAND (1UL << 2)
#define LIST (1UL << 3)
#define AND_OR (1UL << 4)
#define PIPELINE (1UL << 5)
#define PIPE_SEQUENCE (1UL << 6)
#define COMMAND (1UL << 7)
#define COMPOUND_COMMAND (1UL << 8)
#define SUBSHELL (1UL << 9)
#define COMPOUND_LIST (1UL << 10)
#define TERM (1UL << 11)
#define FOR_CLAUSE (1UL << 12)
#define NAME (1UL << 13)
#define IN (1UL << 14)
#define WORDLIST (1UL << 15)
#define CASE_CLAUSE (1UL << 16)
#define CASE_LIST_NS (1UL << 17)
#define CASE_LIST (1UL << 18)
#define CASE_ITEM_NS (1UL << 19)
#define PATTERN (1UL << 20)
#define IF_CLAUSE (1UL << 21)
#define ELSE_PART (1UL << 22)
#define WHILE_CLAUSE (1UL << 23)
#define UNTIL_CLAUSE (1UL << 24)
#define FUNCTION_DEFINITION (1UL << 25)
#define FUNCTION_BODY (1UL << 26)
#define FNAME (1UL << 27)
#define BRACE_GROUP (1UL << 28)
#define DO_GROUP (1UL << 29)
#define SIMPLE_COMMAND (1UL << 30)
#define CMD_NAME (1UL << 31)
#define CMD_WORD (1UL << 32)
#define CMD_PREFIX (1UL << 33)
#define CMD_SUFFIX (1UL << 34)
#define REDIRECT_LIST (1UL << 35)
#define IO_REDIRECT (1UL << 36)
#define IO_FILE (1UL << 37)
#define FILENAME (1UL << 38)
#define IO_HERE (1UL << 39)
#define HERE_END (1UL << 40)
#define NEWLINE_LIST (1UL << 41)
#define LINEBREAK (1UL << 42)
#define SEPARATOR_OP (1UL << 43)
#define SEPARATOR (1UL << 44)
#define SEQUENTIAL_SEP (1UL << 45)
#define SYM_LESS (1UL << 49)
#define SYM_GREAT (1UL << 50)
#define ALL_SYM !0
//#define ALL_SYM -1UL
*/
# define MATCH_STACK(x, y) (x == y || y == ALL)
typedef int t_parstate;
@ -232,6 +175,31 @@ char *read_state(t_sym current);
#define ERROR (1 << 1)
#define SUCCESS (1 << 2)
/*
* Build AST - rewriting
*
*/
struct s_treematch
{
t_type type;
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);
int add_sep(t_btree **ast, t_list **lst);
int add_cmd(t_btree **ast, t_list **lst);
int join_ast(t_btree **ast, t_btree **new_node);
int gen_node(t_btree **ast);
/*
* Build AST
*

View file

@ -1 +1 @@
ls | cat | cat > file1
ls | cat

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 23:06:34 by ariard #+# #+# */
/* Updated: 2017/02/14 18:29:49 by ariard ### ########.fr */
/* Updated: 2017/02/15 20:58:03 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,10 +31,10 @@ int shell_script()
token_print(token);
if (ft_parse(&ast, &token))
return (1);
// btree_print(STDBUG, ast, &ft_putast);
// if (ft_exec(&ast))
// return (1);
// ast = NULL;
btree_print(STDBUG, ast, &ft_putast);
if (ft_exec(&ast))
return (1);
ast = NULL;
script->size = 0;
get_script_content(script);
}

33
42sh/src/parser/add_cmd.c Normal file
View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* add_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/02/15 20:57:47 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int add_cmd(t_btree **ast, t_list **lst)
{
t_token *token;
t_astnode *node;
char **my_tab;
if (!*ast)
gen_node(ast);
else
return (add_cmd(&(*ast)->right, lst));
my_tab = NULL;
token = (*lst)->content;
node = (*ast)->item;
node->type = TK_COMMAND;
my_tab = ft_sstradd(my_tab, token->data);
my_tab = ft_sstradd(my_tab, (char *)token->esc);
ft_ld_pushback(&node->data.token, my_tab);
return (0);
}

28
42sh/src/parser/add_sep.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* add_sep.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 19:12:07 by ariard #+# #+# */
/* Updated: 2017/02/15 20:48:17 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int add_sep(t_btree **ast, t_list **lst)
{
t_token *token;
t_astnode *node;
t_btree *new_node;
new_node = NULL;
gen_node(&new_node);
join_ast(ast, &new_node);
token = (*lst)->content;
node = (new_node)->item;
node->type = token->type;
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:39:18 by ariard #+# #+# */
/* Updated: 2017/02/14 19:09:44 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:52:56 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -149,20 +149,20 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state)
int i;
i = 0;
DG("aggregate head %s && sym %s",
read_state(**stack), read_state(*new_sym));
// DG("aggregate head %s && sym %s",
// read_state(**stack), read_state(*new_sym));
while (g_aggrematch[i].top)
{
if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(**stack, g_aggrematch[i].under))
{
DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
*new_sym = g_aggrematch[i].new_sym;
if (g_aggrematch[i].erase_sym)
{
pop_stack(stack, g_aggrematch[i].erase_sym);
DG("stack after pop: %s", read_state(**stack));
// DG("stack after pop: %s", read_state(**stack));
}
if (eval_sym(**stack, *new_sym))
return ((*state = ERROR));

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* build_tree.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 18:32:59 by ariard #+# #+# */
/* Updated: 2017/02/15 20:51:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
t_treematch g_treematch[] =
{
{TK_N_WORD, &add_cmd},
{TK_PIPE, &add_sep},
{0, NULL},
};
int build_tree(t_btree **ast, t_list **lst)
{
int i;
t_token *token;
i = 0;
token = (*lst)->content;
while (g_treematch[i].type)
{
DG("func TK : '%s' TK : '%s'",
read_state(g_treematch[i].type) ,read_state(token->type));
if (g_treematch[i].type == token->type)
return (g_treematch[i].add(ast, lst));
i++;
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 16:26:30 by ariard #+# #+# */
/* Updated: 2017/02/14 19:09:40 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:52:12 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -427,7 +427,7 @@ 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));
// DG("eval head %s && sym %s", read_state(stack), read_state(new_sym));
i = 0;
while (g_stackmatch[i].top)
{

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 14:30:22 by ariard #+# #+# */
/* Updated: 2017/02/13 22:59:00 by ariard ### ########.fr */
/* Updated: 2017/02/15 20:40:28 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ int ft_parse(t_btree **ast, t_list **token)
while (*token)
{
produce_sym(*stack, new_sym, token);
DG("new sym %s", read_state(*new_sym));
// DG("new sym %s", read_state(*new_sym));
if (eval_sym(*stack, *new_sym))
state = ERROR;
else
@ -43,7 +43,7 @@ int ft_parse(t_btree **ast, t_list **token)
return (error_syntax(token));
if (state == SUCCESS)
ft_putstr("success");
// build_tree(token, ast);
build_tree(ast, token);
ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free);
}
return (0);

View file

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

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 16:28:41 by ariard #+# #+# */
/* Updated: 2017/02/03 16:46:45 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:08:49 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/04 23:44:59 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:30:06 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */
/* Updated: 2017/02/09 19:27:22 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:34:57 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/09 15:24:49 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:54:27 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 19:12:44 by ariard #+# #+# */
/* Updated: 2017/02/13 22:51:52 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:52:42 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@ int pop_stack(t_sym **stack, t_sym erase_sym)
{
t_sym *temp;
DG("pop until :%s", read_state(erase_sym));
// DG("pop until :%s", read_state(erase_sym));
temp = *stack;
while (*temp != erase_sym)
*temp-- = 0;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/02/14 18:26:07 by ariard ### ########.fr */
/* Updated: 2017/02/15 19:50:59 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -64,8 +64,8 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst)
int i;
token = (*lst)->content;
DG("produce stack : %s && token : %s", read_state(stack),
read_state(token->type));
// DG("produce stack : %s && token : %s", read_state(stack),
// read_state(token->type));
i = 0;
*new_sym = 0;
while (g_prodmatch[i].new_sym)

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tree_wrapper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 18:57:44 by ariard #+# #+# */
/* Updated: 2017/02/15 20:56:38 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int join_ast(t_btree **ast, t_btree **new_node)
{
(*new_node)->left = *ast;
*ast = *new_node;
return (0);
}
int gen_node(t_btree **ast)
{
t_astnode item;
if (!*ast)
{
*ast = btree_create_node(&item, sizeof(item));
((t_astnode *)(*ast)->item)->data.token = NULL;
}
return (0);
}