Split functions too large to comply with the Norm
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 26 Nov 2024 14:43:52 +0000 (15:43 +0100)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 26 Nov 2024 14:43:52 +0000 (15:43 +0100)
ft_parse/ft_parsing_table_generate.c

index 87931906f09671bde802938a7783dcf9021137ba..fc8c160ddc31368d147ed029a0f33510825f77fb 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 11:16:53 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/11/26 13:01:45 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/11/26 15:43:30 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -190,7 +190,7 @@ t_ft_stat   add_viable_items(t_vec *kernel,
        i = 0;
        while (i < candidate_items->size)
        {
-               item = ft_vec_caccess(candidate_items, i);
+               item = ft_vec_caccess(candidate_items, i++);
                if (is_viable_item(item, token))
                {
                        new_item = duplicate_item(item);
@@ -198,16 +198,12 @@ t_ft_stat add_viable_items(t_vec *kernel,
                                return (alloc_fail);
                        ++new_item->core.position;
                        res = ft_vec_append(kernel, new_item);
-                       if (res == success)
-                               free(new_item);
-                       else
-                       {
+                       if (res != success)
                                free_item(new_item);
-                               free(new_item);
+                       free(new_item);
+                       if (res != success)
                                return (res);
-                       }
                }
-               ++i;
        }
        return (success);
 }
@@ -266,25 +262,30 @@ int       is_terminal_token(const t_token *token, const t_vec *tokens)
 
 t_ft_stat      expand_lookahead(t_vec *lookahead, const t_marked_grammar_rule *rule, const t_vec *rules, const t_vec *tokens);
 
+t_ft_stat      insert_terminal(t_vec *lookahead, const t_token *token)
+{
+       t_ft_stat       res;
+       t_token         token_copy;
+
+       token_copy = ft_token_dup(token);
+       res = ft_vec_setinsert(lookahead, &token_copy, void_cmp_token_type);
+       if (res != success)
+               ft_free_token(&token_copy);
+       if (res == already_inside)
+               return (success);
+       return (res);
+}
+
 t_ft_stat      add_first(t_vec *lookahead, const t_token *token,
                                const t_vec *rules, const t_vec *tokens)
 {
        t_ft_stat                               res;
        size_t                                  i;
        t_marked_grammar_rule   rule;
-       t_token                                 token_copy;
 
        if (is_terminal_token(token, tokens)
                || !cmp_token_type(token, &g_empty_token))
-       {
-               token_copy = ft_token_dup(token);
-               res = ft_vec_setinsert(lookahead, &token_copy, void_cmp_token_type);
-               if (res != success)
-                       ft_free_token(&token_copy);
-               if (res == already_inside)
-                       return (success);
-               return (res);
-       }
+               return (insert_terminal(lookahead, token));
        append_token(lookahead, token);
        rule.position = 0;
        i = 1;
@@ -317,7 +318,8 @@ void        remove_token(t_vec *lookahead, const t_token *removed_token)
        }
 }
 
-t_ft_stat      expand_lookahead(t_vec *lookahead, const t_marked_grammar_rule *rule,
+t_ft_stat      expand_lookahead(
+                               t_vec *lookahead, const t_marked_grammar_rule *rule,
                                const t_vec *rules, const t_vec *tokens)
 {
        size_t                  i;
@@ -636,6 +638,21 @@ t_ft_stat  add_constituents(
        return (success);
 }
 
+t_ft_stat      add_tokens_of_rule(
+                               t_vec *tokens, const t_grammar_rule *rule, const t_vec *rules)
+{
+       t_ft_stat       res;
+
+       if (!ft_vec_contains(tokens, &rule->result, void_cmp_token_type))
+       {
+               res = append_token(tokens, &rule->result);
+               if (res != success)
+                       return (res);
+       }
+       res = add_constituents(tokens, &rule->constituents, rules);
+       return (res);
+}
+
 t_ft_stat      categorize_tokens(t_vec *tokens, const t_vec *rules)
 {
        t_ft_stat                               res;
@@ -649,16 +666,7 @@ t_ft_stat  categorize_tokens(t_vec *tokens, const t_vec *rules)
        while (i < rules->size)
        {
                rule = ft_vec_caccess(rules, i);
-               if (!ft_vec_contains(tokens, &rule->result, void_cmp_token_type))
-               {
-                       res = append_token(tokens, &rule->result);
-                       if (res != success)
-                       {
-                               ft_vec_free(tokens, ft_free_token);
-                               return (res);
-                       }
-               }
-               res = add_constituents(tokens, &rule->constituents, rules);
+               res = add_tokens_of_rule(tokens, rule, rules);
                if (res != success)
                {
                        ft_vec_free(tokens, ft_free_token);
@@ -868,7 +876,8 @@ void        convert_reduces(t_vec *lookahead, const t_generator_state *state,
        {
                item = ft_vec_caccess(&state->kernel, i);
                if (item->core.position == item->core.rule->constituents.size
-                       || !cmp_token_type(item->core.rule->constituents.vec, &g_empty_token))
+                       || !cmp_token_type(
+                               item->core.rule->constituents.vec, &g_empty_token))
                        add_reduce(lookahead, item, tokens, rules);
                ++i;
        }
@@ -877,7 +886,8 @@ void        convert_reduces(t_vec *lookahead, const t_generator_state *state,
        {
                item = ft_vec_caccess(&state->closure, i);
                if (item->core.position == item->core.rule->constituents.size
-                       || !cmp_token_type(item->core.rule->constituents.vec, &g_empty_token))
+                       || !cmp_token_type(
+                               item->core.rule->constituents.vec, &g_empty_token))
                        add_reduce(lookahead, item, tokens, rules);
                ++i;
        }