diff --git a/ast/ast.c b/ast/ast.c index 63be7df..be0ef11 100644 --- a/ast/ast.c +++ b/ast/ast.c @@ -2,7 +2,7 @@ t_ast *ast_new(char *name, t_type type) { - t_ast *node; + t_ast *node; if (!name) return (NULL); @@ -20,7 +20,7 @@ t_ast *ast_new(char *name, t_type type) t_ast *ast_enast(t_ast *head, t_ast *node) { - t_ast *cursor; + t_ast *cursor; if (!node) return (head); @@ -35,7 +35,7 @@ t_ast *ast_enast(t_ast *head, t_ast *node) t_ast *ast_deast(t_ast *head) { - t_ast *cpy; + t_ast *cpy; if (!head) return (NULL); @@ -55,7 +55,7 @@ t_ast *ast_deast(t_ast *head) int ast_len(t_ast *head) { - int i; + int i; i = 0; while (head) diff --git a/ast/get.c b/ast/get.c index dffd5eb..df90f65 100644 --- a/ast/get.c +++ b/ast/get.c @@ -1,5 +1,36 @@ #include "parse.h" +t_type get_type(char *str) +{ + if (ft_strlen(str) == 1 && *str == ';') + return (SEP); + if (ft_strlen(str) == 1 && *str == '|') + return (RED_PIPE); + if (ft_strlen(str) == 2) + { + if (IS_OP_AND(str)) + return (OP_OR); + if (IS_OP_AND(str)) + return (OP_AND); + } + return (CMD); +} + +int get_nbr_instructions(t_ast *lex) +{ + int i; + + if (!lex) + return (0); + i = 1; + while (lex) + { + if (lex->type == SEP) + i++; + lex = lex->next; + } + return (i); +} t_type get_type(char *str) { if (ft_strlen(str) == 1 && *str == ';') @@ -30,4 +61,4 @@ int get_nbr_instructions(t_ast *lex) lex = lex->next; } return (i); -} \ No newline at end of file +} diff --git a/ast/helper.c b/ast/helper.c index cd733e2..352d73a 100644 --- a/ast/helper.c +++ b/ast/helper.c @@ -1,34 +1,35 @@ #include "parse.h" -char *contain_term(char *str) +char *contain_term(char *str) { - char *dest; - int i; + char *dest; + int i; - i = 0; - if (str && *str && (*str == '|' || *str == ';' || (str[0] == '&' && str[1] && str[1] == '&'))) - { - if (!(dest = ft_memalloc(sizeof(char) * 3))) - return (NULL); - if (*str == ';' || *str == '|' || *str == '&') - dest[i++] = *str++; - if (*str == '|' || *str == '&') - dest[i++] = *str++; - dest[i] = '\0'; - return (dest); - } - return (NULL); + i = 0; + if (str && *str && (*str == '|' || *str == ';' || / + (str[0] == '&' && str[1] && str[1] == '&'))) + { + if (!(dest = ft_memalloc(sizeof(char) * 3))) + return (NULL); + if (*str == ';' || *str == '|' || *str == '&') + dest[i++] = *str++; + if (*str == '|' || *str == '&') + dest[i++] = *str++; + dest[i] = '\0'; + return (dest); + } + return (NULL); } -char *remove_start_space(char *str) +char *remove_start_space(char *str) { - char *dest; + char *dest; - if (!str) - return (NULL); - while (str && *str && *str == ' ') - str++; - if (!(dest = ft_memalloc(ft_strlen(str) + 1))) - return (NULL); - return (ft_strcpy(dest, str)); + if (!str) + return (NULL); + while (str && *str && *str == ' ') + str++; + if (!(dest = ft_memalloc(ft_strlen(str) + 1))) + return (NULL); + return (ft_strcpy(dest, str)); } diff --git a/ast/main.c b/ast/main.c index d0295cb..3644811 100644 --- a/ast/main.c +++ b/ast/main.c @@ -1,27 +1,28 @@ #include "parse.h" -int main(int argc, char **argv) +int main(int argc, char **argv) { - t_ast *lex; - t_ast **ast; + t_ast *lex; + t_ast **ast; - //lex = parse_lexer("echo 'hello \" world' ; ls -la rep | grep \"repertoire 1\" && sed -a -b fichier2"); - lex = parse_lexer("echo 'hello \" world' ; ls -la rep ;grep \"repertoire 1\" && sed -a -b fichier2 ; "); + //lex = parse_lexer("echo 'hello \" world' ; ls -la rep | grep \"repertoire 1\" && sed -a -b fichier2"); + lex = parse_lexer("echo 'hello \" world' ; ls -la rep ;grep \"repertoire 1\" && sed -a -b fichier2 ; "); - if (!lex) - { - print_error_lexer(); - return (1); - } - if (!(ast = (t_ast **)ft_memalloc(sizeof(t_ast *) * get_nbr_instructions(lex) + 1))) - { - print_error_ast(); - return (1); - } - if (!(ast = parse_ast(ast, lex))) - print_error_ast(); - print_trees(ast); - free_ast(lex); - free_trees(ast); - return (0); + if (!lex) + { + print_error_lexer(); + return (1); + } + if (!(ast = (t_ast **)ft_memalloc(sizeof(t_ast *) * / + get_nbr_instructions(lex) + 1))) + { + print_error_ast(); + return (1); + } + if (!(ast = parse_ast(ast, lex))) + print_error_ast(); + print_trees(ast); + free_ast(lex); + free_trees(ast); + return (0); } diff --git a/ast/parse.c b/ast/parse.c index 9bc6cda..b60b8f5 100644 --- a/ast/parse.c +++ b/ast/parse.c @@ -1,129 +1,133 @@ #include "parse.h" + /* - * Don't send space at the end of the string - */ -t_ast *parse_lexer(char *str) +** Don't send space at the end of the string +*/ + +t_ast *parse_lexer(char *str) { - t_ast *lex; - char *word; - char *term; - t_ast *node; + t_ast *lex; + char *word; + char *term; + t_ast *node; - lex = NULL; - word = NULL; - term = NULL; - while (str && *str) - { - if (*str == '\'' || *str == '"') - { - str += parse_quote(&word, str); - continue ; - } - if (*str && (*str == '|' || *str == '&' || *str == ';') && (term = contain_term(str))) - { - node = ast_new(remove_start_space(word), CMD); - if (node && !(lex = ast_enast(lex, node))) - return (NULL); - ft_strdel(&word); - word = NULL; - if (!(lex = ast_enast(lex, ast_new(term, get_type(term))))) - return (NULL); - str += ft_strlen(term); - ft_strdel(&term); - term = NULL; - continue ; - } - word = ft_str_append(word, *str++); - } - if (ft_strlen(word)) - { - node = ast_new(remove_start_space(word), CMD); - if (node && !(lex = ast_enast(lex, node))) - return (NULL); - ft_strdel(&word); - word = NULL; - } - return (validate_lexer(lex)); + lex = NULL; + word = NULL; + term = NULL; + while (str && *str) + { + if (*str == '\'' || *str == '"') + { + str += parse_quote(&word, str); + continue ; + } + if (*str && (*str == '|' || *str == '&' || *str == ';') && / + (term = contain_term(str))) + { + node = ast_new(remove_start_space(word), CMD); + if (node && !(lex = ast_enast(lex, node))) + return (NULL); + ft_strdel(&word); + word = NULL; + if (!(lex = ast_enast(lex, ast_new(term, get_type(term))))) + return (NULL); + str += ft_strlen(term); + ft_strdel(&term); + term = NULL; + continue ; + } + word = ft_str_append(word, *str++); + } + if (ft_strlen(word)) + { + node = ast_new(remove_start_space(word), CMD); + if (node && !(lex = ast_enast(lex, node))) + return (NULL); + ft_strdel(&word); + word = NULL; + } + return (validate_lexer(lex)); } -int parse_quote(char **word, char *str) +int parse_quote(char **word, char *str) { - int i; + int i; - i = 1; - if (*str == '\'' && str++) - { - if (str && *str && *str == '\'' && ++i) - return (i); - *word = ft_str_append(*word, '\''); - while (str && *str && ++i && *str != '\'') - *word = ft_str_append(*word, *str++); - if (str && *str == '\'' && ++i) - *word = ft_str_append(*word, '\''); - } - else if (*str == '"' && str++) - { - if (str && *str && *str == '"' && ++i) - return (i); - *word = ft_str_append(*word, '"'); - while (str && str[0] && str[0] == '"' && str[-1] != '\\' && ++i) - *word = ft_str_append(*word, *str++); - if (str && *str == '"' && ++i) - *word = ft_str_append(*word, '"'); - } - return (i); + i = 1; + if (*str == '\'' && str++) + { + if (str && *str && *str == '\'' && ++i) + return (i); + *word = ft_str_append(*word, '\''); + while (str && *str && ++i && *str != '\'') + *word = ft_str_append(*word, *str++); + if (str && *str == '\'' && ++i) + *word = ft_str_append(*word, '\''); + } + else if (*str == '"' && str++) + { + if (str && *str && *str == '"' && ++i) + return (i); + *word = ft_str_append(*word, '"'); + while (str && str[0] && str[0] == '"' && str[-1] != '\\' && ++i) + *word = ft_str_append(*word, *str++); + if (str && *str == '"' && ++i) + *word = ft_str_append(*word, '"'); + } + return (i); } -t_ast **parse_ast(t_ast **ast, t_ast *lex) +t_ast **parse_ast(t_ast **ast, t_ast *lex) { - int i; + int i; - i = 0; - ast[i] = NULL; - while (lex) - { - if (lex->type == SEP) - ast[++i] = NULL; - else if (!(ast[i] = ast_enast(ast[i], ast_new(lex->name, lex->type)))) - return (NULL); - lex = lex->next; - } - ast[++i] = 0; - i = 0; - while (ast[i]) - { - if (!(ast[i] = parse_tree(ast[i]))) - return (NULL); - i++; - } - return (ast); + i = 0; + ast[i] = NULL; + while (lex) + { + if (lex->type == SEP) + ast[++i] = NULL; + else if (!(ast[i] = ast_enast(ast[i], ast_new(lex->name, lex->type)))) + return (NULL); + lex = lex->next; + } + ast[++i] = 0; + i = 0; + while (ast[i]) + { + if (!(ast[i] = parse_tree(ast[i]))) + return (NULL); + i++; + } + return (ast); } -t_ast *parse_tree(t_ast *lex) +t_ast *parse_tree(t_ast *lex) { - t_ast *ast; - t_ast *op; - t_ast *right; - t_ast *cpy; + t_ast *ast; + t_ast *op; + t_ast *right; + t_ast *cpy; - if (!lex || lex->type != CMD || !(ast = ast_new(lex->name, lex->type))) - return (NULL); - cpy = lex; - lex = lex->next; - while (lex) - { - if (lex->type == CMD || !(op = ast_new(lex->name, lex->type))) - return (NULL); - lex = lex->next; - if (!lex || lex->type != CMD || !(right = ast_new(lex->name, lex->type))) - return (NULL); - op->next = ast; - op->right = right; - right->parent = op; - ast->parent = op; - ast = op; - lex = lex->next; - } - free_ast(cpy); - return (validate_ast(ast)); + if (!lex || lex->type != CMD || !(ast = ast_new(lex->name, lex->type))) + return (NULL); + cpy = lex; + lex = lex->next; + while (lex) + { + if (lex->type == CMD || !(op = ast_new(lex->name, lex->type))) + return (NULL); + lex = lex->next; + if (!lex || lex->type != CMD || / + !(right = ast_new(lex->name, lex->type))) + return (NULL); + op->next = ast; + op->right = right; + right->parent = op; + ast->parent = op; + ast = op; + lex = lex->next; + } + free_ast(cpy); + return (validate_ast(ast)); } diff --git a/ast/parse.h b/ast/parse.h index e563c09..562039f 100644 --- a/ast/parse.h +++ b/ast/parse.h @@ -5,7 +5,8 @@ # define IS_RED_NNEXT(x) (!ft_strcmp_withspace(x, ">>")) # define IS_RED_PREV(x) (!ft_strcmp_withspace(x, "<")) # define IS_RED_PIPE(x) (!ft_strcmp_withspace(x, "|")) -# define IS_RED(x) (IS_RED_NEXT(x) || IS_RED_NNEXT(x) || IS_RED_PREV(x) || IS_RED_PIPE(x)) +# define IS_RED(x) (IS_RED_NEXT(x) || IS_RED_NNEXT(x) || / + IS_RED_PREV(x) || IS_RED_PIPE(x)) # define IS_OP_AND(x) (!ft_strcmp_withspace(x, "&&")) # define IS_OP_OR(x) (!ft_strcmp_withspace(x, "||")) @@ -19,68 +20,69 @@ # include "../ft_printf/ft_printf_header.h" # include "../ft_ls/ft_ls.h" -// TODO to delete +/* TODO to delete this shit before is too late*/ # include +/* TODO to delete this shit before is too late*/ -typedef enum e_type +typedef enum e_type { - CMD, + CMD, - RED_NEXT, - RED_NNEXT, - RED_PREV, + RED_NEXT, + RED_NNEXT, + RED_PREV, - RED_PIPE, - OP_AND, - OP_OR, - SEP, - ROOT -} t_type; + RED_PIPE, + OP_AND, + OP_OR, + SEP, + ROOT +} t_type; typedef struct s_ast { - char *name; - t_type type; - struct s_ast *parent; - struct s_ast *next; - struct s_ast *right; + char *name; + struct s_ast *parent; + struct s_ast *next; + struct s_ast *right; + t_type type; } t_ast; -/** - * free.c - */ -void free_ast(t_ast *head); -void free_trees(t_ast **ast); - -/** - * get.c - */ -t_type get_type(char *str); -int get_nbr_instructions(t_ast *lex); - -/** - * helper.c - */ -char *contain_term(char *str); -char *remove_start_space(char *str); - -/** - * lexer.c - */ -t_ast *parse_lexer(char *str); -int parse_quote(char **word, char *str); -t_ast **parse_ast(t_ast **ast, t_ast *lex); -t_ast *parse_tree(t_ast *lex); +/* +** free.c +*/ +void free_ast(t_ast *head); +void free_trees(t_ast **ast); + +/* +** get.c +*/ +t_type get_type(char *str); +int get_nbr_instructions(t_ast *lex); + +/* +** helper.c +*/ +char *contain_term(char *str); +char *remove_start_space(char *str); + +/* +** lexer.c +*/ + t_ast **parse_ast(t_ast **ast, t_ast *lex); +t_ast *parse_lexer(char *str); +t_ast *parse_tree(t_ast *lex); +int parse_quote(char **word, char *str); /** * print.c */ -void print_ast(t_ast *node); -void print_trees(t_ast **ast); -void print_lexer_start_error(char *name); -void print_error_lexer(void); -void print_error_ast(void); +void print_ast(t_ast *node); +void print_trees(t_ast **ast); +void print_lexer_start_error(char *name); +void print_error_lexer(void); +void print_error_ast(void); /** * ast.c @@ -91,10 +93,10 @@ t_ast *ast_deast(t_ast *head); t_ast *ast_deast_front(t_ast *head); int ast_len(t_ast *head); -/** - * validate.c - */ -t_ast *validate_lexer(t_ast *lex); -t_ast *validate_ast(t_ast *ast); +/* +** validate.c +*/ +t_ast *validate_lexer(t_ast *lex); +t_ast *validate_ast(t_ast *ast); #endif diff --git a/ast/print.c b/ast/print.c index ea5fdcf..938b398 100644 --- a/ast/print.c +++ b/ast/print.c @@ -2,24 +2,45 @@ void print_ast(t_ast *node) { - while (node) - { - ft_putstr(node->name); - ft_putstr("["); - ft_putnbr(node->type); - ft_putstr("]"); - if (node->right) - { - ft_putstr("\t-> RIGHT["); - ft_putstr(node->right->name); - ft_putstr("]"); - ft_putstr("["); - ft_putnbr(node->right->type); - ft_putstr("]"); - } - ft_putstr("\n"); - node = node->next; - } + while (node) + { + ft_putstr(node->name); + ft_putstr("["); + ft_putnbr(node->type); + ft_putstr("]"); + if (node->right) + { + ft_putstr("\t-> RIGHT["); + ft_putstr(node->right->name); + ft_putstr("]"); + ft_putstr("["); + ft_putnbr(node->right->type); + ft_putstr("]"); + } + ft_putstr("\n"); + node = node->next; + } +} + +void print_trees(t_ast **ast) +{ + while (ast && *ast) + { + printf("#\n"); + print_ast(*ast); + printf("*****\n"); + ast++; + } +} + +void print_error_lexer(void) +{ + ft_putstr("Parse error lexer [CMD invalid]\n"); +} + +void print_error_ast(void) +{ + ft_putstr("Parse error ast\n"); } void print_trees(t_ast **ast) diff --git a/ast/validate.c b/ast/validate.c new file mode 100644 index 0000000..982aaee --- /dev/null +++ b/ast/validate.c @@ -0,0 +1,23 @@ +#include "parse.h" + +t_ast *validate_lexer(t_ast *lex) +{ + t_ast *cpy; + + cpy = lex; + while (lex) + { + if (!ft_strlen(lex->name) || lex->type != CMD) + return (NULL); + if ((lex = lex->next) && lex->type == CMD) + return (NULL); + lex = lex ? lex->next : lex; + } + return (cpy); +} + +// TODO implement method +t_ast *validate_ast(t_ast *ast) +{ + return (ast); +}