diff --git a/parser/include/parser/passes.h b/parser/include/parser/passes.h index 49e330fe..2f669316 100644 --- a/parser/include/parser/passes.h +++ b/parser/include/parser/passes.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/02 18:43:41 by maiboyer #+# #+# */ -/* Updated: 2024/10/12 15:38:12 by maiboyer ### ########.fr */ +/* Updated: 2024/10/12 16:34:22 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,4 +52,42 @@ t_error ts_fold_expension(t_vec_token input, t_vec_token *output); t_error ts_fold_redir(t_vec_token input, t_vec_token *output); t_error ts_remove_whitespace(t_vec_token input, t_vec_token *output); +struct s_global_passes +{ + struct s_ts_pass_def passes[13]; +}; + +struct s_dq_passes +{ + struct s_ts_pass_def passes[3]; +}; + +static inline struct s_global_passes _get_global_passes(void) +{ + return ((struct s_global_passes){.passes = {\ + {ts_double_string_pass, "double string parser"}, \ + {ts_fold_expension, "fold expansion"}, \ + {ts_fold_no_quote, "fold no quote"}, \ + {ts_fold_whitespace, "fold whitespace"}, \ + {ts_double_amp, "double amp => and"}, \ + {ts_double_pipe, "double pipe => or"}, \ + {ts_double_lcarret, "double lcarret => dlcarret"}, \ + {ts_double_rcarret, "double rcarrer => drcarret"}, \ + {ts_fold_into_word, "fold into words"}, \ + {ts_fold_redir, "fold redir+argument"}, \ + {ts_remove_whitespace, "rm extra whitespace"}, \ + {ts_fold_cmd, "fold into cmd"}, \ + {ts_verify_tokens, "verify only tokens"}, \ + }}); +} + +static inline struct s_dq_passes _get_dq_passes(void) +{ + return ((struct s_dq_passes){.passes = {\ + {ts_fold_expension, "fold expansion"}, \ + {ts_paren_to_noquote, "parenthesis to noquote"}, \ + {ts_fold_no_quote, "fold no quote"}, \ + }}); +} + #endif /* PASSES_H */ diff --git a/parser/src/passes.c b/parser/src/passes.c index 4a5eca3d..5542cba4 100644 --- a/parser/src/passes.c +++ b/parser/src/passes.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/02 18:41:16 by maiboyer #+# #+# */ -/* Updated: 2024/10/11 21:33:34 by maiboyer ### ########.fr */ +/* Updated: 2024/10/12 16:34:50 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,66 +32,44 @@ /// command into a single token stopping at the correct ending: /// semicolon, pipe, or, and -// here is the signature easily accessible: -// -// t_error pass_name(t_vec_token input, t_vec_token *output); - // bellow is some function to apply all the different passes ! -static const struct s_ts_pass_def g_ts_passes[] = {\ - {ts_double_string_pass, "double string parser"}, \ - {ts_fold_expension, "fold expansion"}, \ - {ts_fold_no_quote, "fold no quote"}, \ - {ts_fold_whitespace, "fold whitespace"}, \ - {ts_double_amp, "double amp => and"}, \ - {ts_double_pipe, "double pipe => or"}, \ - {ts_double_lcarret, "double lcarret => dlcarret"}, \ - {ts_double_rcarret, "double rcarrer => drcarret"}, \ - {ts_fold_into_word, "fold into words"}, \ - {ts_fold_redir, "fold redir+argument"}, \ - {ts_remove_whitespace, "rm extra whitespace"}, \ - {ts_fold_cmd, "fold into cmd"}, \ - {ts_verify_tokens, "verify only tokens"}, \ -}; - t_error ts_apply_passes(t_vec_token ts, t_vec_token *out) { - t_usize i; - t_vec_token next; + t_usize i; + t_vec_token next; + struct s_global_passes passes; i = 0; - while (i < sizeof(g_ts_passes) / sizeof(g_ts_passes[0])) + passes = _get_global_passes(); + while (i < sizeof(passes) / sizeof(passes.passes[0])) { - if (g_ts_passes[i].fn == NULL) + if (passes.passes[i].fn == NULL) return (vec_token_free(ts), ERROR); - if ((g_ts_passes[i].fn)(ts, &next)) + if ((passes.passes[i].fn)(ts, &next)) return (me_eprintf("failed on %s token pass\n", \ - g_ts_passes[i].name), ERROR); + passes.passes[i].name), ERROR); ts = next; i++; } return (*out = ts, NO_ERROR); } -static const struct s_ts_pass_def g_ts_dq_passes[] = {\ - {ts_fold_expension, "fold expansion"}, \ - {ts_paren_to_noquote, "parenthesis to noquote"}, \ - {ts_fold_no_quote, "fold no quote"}, -}; - t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out) { - t_usize i; - t_vec_token next; + t_usize i; + t_vec_token next; + struct s_dq_passes passes; i = 0; - while (i < sizeof(g_ts_dq_passes) / sizeof(g_ts_dq_passes[0])) + passes = _get_dq_passes(); + while (i < sizeof(passes) / sizeof(passes.passes[0])) { - if (g_ts_dq_passes[i].fn == NULL) + if (passes.passes[i].fn == NULL) return (vec_token_free(ts), ERROR); - if ((g_ts_dq_passes[i].fn)(ts, &next)) + if ((passes.passes[i].fn)(ts, &next)) return (me_eprintf(\ - "failed on '%s' dq token pass\n", g_ts_dq_passes[i].name), ERROR); + "failed on '%s' dq token pass\n", passes.passes[i].name), ERROR); ts = next; i++; }