From: Lukas Jiriste Date: Thu, 20 Jun 2024 11:19:52 +0000 (+0200) Subject: Align the ft_parse style with that of f_arr X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=517363e683fda7c30772ea6f09ecb41e1351b0fc;p=Libft.git Align the ft_parse style with that of f_arr --- diff --git a/Makefile b/Makefile index b62cdd1..7c87000 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ INCLUDE := $(addprefix -I, $(INCDIR)) SRCDIR := ft_gen ft_math ft_str ft_mem ft_io ft_check ft_conv ft_lst ft_arr ft_parse SRCparse:= ft_parse.c \ - ft_print_parsing_table.c \ + ft_parsing_table_print.c \ SRCgen := ft_swap.c \ diff --git a/ft_parse/ft_parse.c b/ft_parse/ft_parse.c index e22015d..ef2d18b 100644 --- a/ft_parse/ft_parse.c +++ b/ft_parse/ft_parse.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/20 20:51:36 by ljiriste #+# #+# */ -/* Updated: 2024/06/20 10:08:13 by ljiriste ### ########.fr */ +/* Updated: 2024/06/20 13:15:32 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -109,7 +109,7 @@ int is_valid_rule(t_grammar_rule *rule) return (1); } -static int load_rules(t_vec *rules, const char *rules_filename) +static t_ft_stat load_rules(t_vec *rules, const char *rules_filename) { int fd; char *line; @@ -117,7 +117,7 @@ static int load_rules(t_vec *rules, const char *rules_filename) fd = open(rules_filename, O_RDONLY); if (fd < 0) - return (1); + return (file_error); line = get_next_line(fd); while (line) { @@ -125,13 +125,13 @@ static int load_rules(t_vec *rules, const char *rules_filename) if (!is_valid_rule(&rule) || ft_vec_append(rules, &rule) != success) { ft_vec_free(rules, free_rule); - return (2); + return (non_specific_failure); } free(line); line = get_next_line(fd); } close(fd); - return (0); + return (success); } static size_t get_lookahead_size(t_vec *tokens) @@ -262,45 +262,53 @@ static t_vec parse_header(const char *header) return (tokens); } -static void ft_init_parsing_table(t_parsing_table *table) +t_ft_stat ft_parsing_table_init(t_parsing_table *table) { - ft_vec_init(&table->rules, sizeof(t_grammar_rule)); - ft_vec_init(&table->states, sizeof(t_parser_state)); - ft_vec_init(&table->tokens, sizeof(t_token)); - return ; + t_ft_stat res; + + res = ft_vec_init(&table->rules, sizeof(t_grammar_rule)); + if (res != success) + return (res); + res = ft_vec_init(&table->states, sizeof(t_parser_state)); + if (res != success) + return (res); + res = ft_vec_init(&table->tokens, sizeof(t_token)); + return (res); +} + +int is_consistent(__attribute__((unused)) t_parsing_table *table) +{ + return (1); } -t_parsing_table ft_load_parsing_table(const char *filename, +t_ft_stat ft_parsing_table_load(t_parsing_table *table, + const char *filename, const char *rules_filename) { int fd; char *line; - t_parsing_table table; - ft_init_parsing_table(&table); - load_rules(&table.rules, rules_filename); + load_rules(&table->rules, rules_filename); fd = open(filename, O_RDONLY); if (fd < 0) - return (table); + return (file_error); line = get_next_line(fd); - table.tokens = parse_header(line); + table->tokens = parse_header(line); free(line); line = get_next_line(fd); while (line) { - if (add_line(&table.states, line, get_lookahead_size(&table.tokens))) - { - ft_free_parsing_table(&table); - return (table); - } + add_line(&table->states, line, get_lookahead_size(&table->tokens)); free(line); line = get_next_line(fd); } close(fd); - return (table); + if (is_consistent(table)) + return (success); + return (non_specific_failure); } -void ft_free_parsing_table(t_parsing_table *table) +void ft_parsing_table_free(t_parsing_table *table) { ft_vec_free(&table->rules, free_rule); ft_vec_free(&table->states, free_state); @@ -309,7 +317,7 @@ void ft_free_parsing_table(t_parsing_table *table) } /* -t_parse_tree *ft_parse(t_vec tokens, t_parsing_table *parsing_table) +t_parse_tree *ft_parsing_table_parse(t_vec tokens, t *parsing_table) { } */ diff --git a/ft_parse/ft_print_parsing_table.c b/ft_parse/ft_parsing_table_print.c similarity index 93% rename from ft_parse/ft_print_parsing_table.c rename to ft_parse/ft_parsing_table_print.c index a13915e..141130c 100644 --- a/ft_parse/ft_print_parsing_table.c +++ b/ft_parse/ft_parsing_table_print.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_print_parsing_table.c :+: :+: :+: */ +/* ft_parsing_table_print.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/16 07:19:50 by ljiriste #+# #+# */ -/* Updated: 2024/06/16 18:20:37 by ljiriste ### ########.fr */ +/* Updated: 2024/06/20 12:51:30 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,7 +66,7 @@ static void print_state(t_parser_state *state, return ; } -void ft_print_parsing_table(t_parsing_table *table, +void ft_parsing_table_print(t_parsing_table *table, unsigned int column_width) { size_t i; diff --git a/inc/ft_arr.h b/inc/ft_arr.h index f4389db..81ccbb4 100644 --- a/inc/ft_arr.h +++ b/inc/ft_arr.h @@ -6,27 +6,21 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/09 13:58:15 by ljiriste #+# #+# */ -/* Updated: 2024/04/05 10:05:22 by ljiriste ### ########.fr */ +/* Updated: 2024/06/20 12:43:56 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_ARR_H # define FT_ARR_H +# include "ft_stat.h" # include # ifndef V_DEFAULT_CAPACITY # define V_DEFAULT_CAPACITY 8 # endif -typedef enum e_arr_stat -{ - success, - alloc_fail, - invalid_size, - invalid_input, - non_specific_failure, -} t_arr_stat; +typedef t_ft_stat t_arr_stat; // It should be possible to remove el_size with the use of macros? typedef struct s_vec diff --git a/inc/ft_parse.h b/inc/ft_parse.h index 4de4635..d3f1c28 100644 --- a/inc/ft_parse.h +++ b/inc/ft_parse.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/27 21:21:54 by ljiriste #+# #+# */ -/* Updated: 2024/06/20 10:44:37 by ljiriste ### ########.fr */ +/* Updated: 2024/06/20 13:17:43 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,10 +77,12 @@ typedef struct s_parsing_table t_vec tokens; // t_vec of tokens } t_parsing_table; -t_parsing_table ft_load_parsing_table(const char *filename, - const char *rules_filename); -void ft_print_parsing_table(t_parsing_table *table, - unsigned int column_width); -void ft_free_parsing_table(t_parsing_table *table); +t_ft_stat ft_parsing_table_init(t_parsing_table *table); +t_ft_stat ft_parsing_table_load(t_parsing_table *table, + const char *filename, + const char *rules_filename); +void ft_parsing_table_print(t_parsing_table *table, + unsigned int column_width); +void ft_parsing_table_free(t_parsing_table *table); #endif // FT_PARSE_H diff --git a/inc/ft_stat.h b/inc/ft_stat.h new file mode 100644 index 0000000..c98c247 --- /dev/null +++ b/inc/ft_stat.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stat.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 12:40:28 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 13:08:35 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STAT_H +# define FT_STAT_H + +typedef enum e_ft_stat +{ + success, + alloc_fail, + invalid_size, + invalid_input, + file_error, + non_specific_failure, +} t_ft_stat; + +#endif //FT_STAT_H diff --git a/inc/libft.h b/inc/libft.h index 595e71b..179b729 100644 --- a/inc/libft.h +++ b/inc/libft.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ -/* Updated: 2024/06/15 09:05:22 by ljiriste ### ########.fr */ +/* Updated: 2024/06/20 12:45:07 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,4 +24,6 @@ # include "ft_arr.h" # include "ft_parse.h" +# include "ft_stat.h" + #endif