This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prod_parse.c
90 lines (82 loc) · 2.53 KB
/
prod_parse.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prod_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgautier <mgautier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/11 14:47:13 by mgautier #+# #+# */
/* Updated: 2017/11/20 16:51:20 by mgautier ### ########.fr */
/* */
/* ************************************************************************** */
#include "prods_defs.h"
#include "sym_interface.h"
#include "libft.h"
#include <stdlib.h>
#include <stdarg.h>
static t_symbol *find_sym(
t_fifo const *sym_already_parsed,
t_fifo *sym_pending,
char const *sym_name)
{
t_symbol *sym_to_add;
sym_to_add = f_fifo_every_valid_va(
sym_already_parsed, FALSE, same_sym_parsed, sym_name);
if (sym_to_add == NULL)
{
sym_to_add = f_fifo_every_valid_va(sym_pending,
FALSE, same_sym_parsed, sym_name);
if (sym_to_add == NULL)
{
sym_to_add = create_symbol(sym_name);
if (sym_to_add != NULL)
f_fifo_add(sym_pending, sym_to_add);
else
destroy_symbol(&sym_to_add);
}
}
return (sym_to_add);
}
static t_bool is_valid(char const *prod_str)
{
size_t index;
index = 0;
while (prod_str[index] != '\0')
{
if (!(is_among(" \n\t", prod_str[index])
|| is_unix_constants(prod_str[index])
|| ft_isdigit(prod_str[index])))
break ;
index++;
}
return (prod_str[index] == '\0');
}
static void error_funct(t_symbol **sym, t_prod **prod)
{
destroy_symbol(sym);
destroy_prod(prod);
}
t_prod *parse_prod(
char const *one_str_prod,
t_fifo const *sym_parsed,
t_fifo *sym_pending)
{
char **each_sym_name;
size_t index;
t_symbol *sym_added;
t_prod *new_prod;
index = 0;
if (!is_valid(one_str_prod))
return (NULL);
each_sym_name = strsplit_with_str(one_str_prod, " \t\n");
new_prod = create_prod();
while (each_sym_name[index] != NULL)
{
sym_added = find_sym(sym_parsed, sym_pending, each_sym_name[index]);
if (sym_added == NULL || !append_to_prod(new_prod, sym_added))
error_funct(&sym_added, &new_prod);
index++;
}
ft_free_string_array(&each_sym_name);
return (new_prod);
}