aboutsummaryrefslogtreecommitdiff
path: root/src/parse/parse.c
blob: b9bb15641a62a4b25eae280e8b89fe96ae2c5e5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: nahaddac <nahaddac@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/06/17 18:09:04 by nahaddac          #+#    #+#             */
/*   Updated: 2020/06/17 18:36:54 by nahaddac         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

/*
** \file   parse.c
** \brief  Parser
*/

#include "parser.h"
#include "lexer.h"
// stdio.h est deja include dans minishell.h temporairement
// (comme ca on doit le retirer a un seul endroit a la fin)
// oui je sais maintenant tu peux effacer se petit commantaire ;)



t_ast					*cmd_push_ast(t_ftlst *rest)
{
	enum e_token_tag 	tag;
	t_ast 				*ast;

	ast = NULL;
	while (rest != NULL)
	{
		tag = ((t_token *)rest->data)->tag;
		if (parse_cmd_str_true_false(tag))
		{
			ast = push_cmd(ast, rest);
		}
		else if (parse_redir_true_false(tag))
		{
			while(rest != NULL)
			{
				ast = push_redir(ast, rest);
				if (tag & TAG_IS_STR && tag & TAG_STICK)
					rest = rest->next;
				else if (tag & TAG_IS_REDIR)
					rest = rest->next;
				else
					break;
				tag = ((t_token *)rest->data)->tag;
			}
		}
		rest = rest->next;
	}
	return ast;
}

t_ret					*parse(t_ftlst *input)
{
	t_ret 				*ret;
	t_ret 				*first;
	t_ast 				*new_ast;

	if(!(ret = malloc(sizeof(t_ret) * 1)))
		return(NULL);
	ret->rest = input;
	ret->ast = NULL;
	ret->unexpected = NULL;
	first = ret;

	if(!(new_ast = malloc(sizeof(t_ast) * 1)))
		return(0);
	new_ast = cmd_push_ast(ret->rest);
	printf("%s\n",((t_token *)ret->rest->data)->content);
	if (ret->ast)
	{
		while(ret->ast->cmd_argv != NULL)
	    {
	 	 	printf("[%s]\n", ((t_token *)ret->ast->cmd_argv->data)->content);
	      	ret->ast->cmd_argv = ret->ast->cmd_argv->next;
	    }
		while(ret->ast->redirs != NULL)
		{
		 	printf("[%s]\n", ((t_token *)ret->ast->redirs->data)->content);
			ret->ast->redirs = ret->ast->redirs->next;
		}
	}
	ast_destroy(ret->ast);
	ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy);
	return first;
}