Align the ft_parse style with that of f_arr
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 20 Jun 2024 11:19:52 +0000 (13:19 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 20 Jun 2024 11:19:52 +0000 (13:19 +0200)
Makefile
ft_parse/ft_parse.c
ft_parse/ft_parsing_table_print.c [moved from ft_parse/ft_print_parsing_table.c with 93% similarity]
inc/ft_arr.h
inc/ft_parse.h
inc/ft_stat.h [new file with mode: 0644]
inc/libft.h

index b62cdd13fcc05dfac888ef6b9b84dd35fe6f28c5..7c870002d45fdb9cb3f39a0d90d2ae01509ad576 100644 (file)
--- 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                                       \
 
index e22015d6b775aca0e3b629d528169a3f4dea66f3..ef2d18b76bf836eea805ed98dc78402f1326a377 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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)
 {
 }
 */
similarity index 93%
rename from ft_parse/ft_print_parsing_table.c
rename to ft_parse/ft_parsing_table_print.c
index a13915e6f4520f19eb7f5c3ea2dabae90cb2add6..141130c5535a0a55988c4b73adac598c73ac7c94 100644 (file)
@@ -1,12 +1,12 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   ft_print_parsing_table.c                           :+:      :+:    :+:   */
+/*   ft_parsing_table_print.c                           :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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;
index f4389dbb6b248ff4bdeb70a5d71414f509bed8ac..81ccbb45df4f68252bb75b7b751a052999ce541a 100644 (file)
@@ -6,27 +6,21 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stddef.h>
 
 # 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
index 4de4635d9b358745fee1d2e18a23de64f165331b..d3f1c28dcda60e40da0052a595da5fa7452f6060 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 (file)
index 0000000..c98c247
--- /dev/null
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_stat.h                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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
index 595e71b63811f22cf7581bf5590cc1a98e46e963..179b7295b25c26ebc2503dd4063e9c7bc2a25b05 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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