update: added folding into cmd and changed fold_word name
This commit is contained in:
parent
7fb16c6a90
commit
5c452993d6
15 changed files with 112 additions and 62 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 18:41:16 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/06 16:50:33 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/10/07 16:46:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -26,15 +26,10 @@
|
|||
/// there is a few stuff we want to do, for example:
|
||||
/// - do somekind of arith expansion that will have any token between
|
||||
/// DOLLAR DLPAREN and the matching DRPAREN in it (yard algo)
|
||||
/// - do somekind of parenthesis token will have any token between
|
||||
/// LPAREN and the matching RPAREN in it
|
||||
///
|
||||
/// - do somekind of CMD token that will store every token that consitute
|
||||
/// command into a single token stopping at the correct ending:
|
||||
/// semicolon, pipe, or, and
|
||||
/// - do a smth that will take any <TOK (OR|AND) TOK> into a single token
|
||||
/// - do a smth that will take any TOK PIPE TOK into a single token and
|
||||
/// merge if any of the TOK is also a pipeline
|
||||
/// (merging may be done during the final ast building)
|
||||
|
||||
// here is the signature easily accessible:
|
||||
//
|
||||
|
|
@ -54,6 +49,7 @@ static const struct s_ts_pass_def g_ts_passes[] = {\
|
|||
{ts_double_lcarret, "double lcarret => dlcarret"}, \
|
||||
{ts_double_rcarret, "double rcarrer => drcarret"}, \
|
||||
{ts_fold_into_word, "fold into words"}, \
|
||||
{ts_fold_cmd, "fold into cmd"}, \
|
||||
// there should be an ts_fold_arith here
|
||||
{ts_split_paren, "split double parenthesis"}, \
|
||||
{ts_fold_redir, "fold redir+argument"}, \
|
||||
|
|
|
|||
61
parser/src/passes/fold_cmd.c
Normal file
61
parser/src/passes/fold_cmd.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* fold_cmd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/07 16:45:41 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/string.h"
|
||||
#include "parser/passes.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_token.h"
|
||||
#include "parser/token.h"
|
||||
|
||||
static bool _is_cmd_node(enum e_token ttype)
|
||||
{
|
||||
return (ttype == TOK_WHITESPACE || ttype == TOK_WORD);
|
||||
};
|
||||
|
||||
/// This is a sample pass
|
||||
///
|
||||
/// There is a few rules the rest of the tokenizer machinery assumes
|
||||
/// theses function follows:
|
||||
/// - the input vec WILL be freed when the function return, even in
|
||||
/// case of error
|
||||
/// - the output vector isn't populated if the function returns an error,
|
||||
/// thus it shouldn't be freed in case of error
|
||||
/// - the output tokens may not be direct copy of the input tokens,
|
||||
/// but need to be cloned (different allocations for stuff)
|
||||
|
||||
t_error ts_fold_cmd(t_vec_token input, t_vec_token *output)
|
||||
{
|
||||
t_vec_token out;
|
||||
t_usize i;
|
||||
t_usize j;
|
||||
t_token tmp;
|
||||
|
||||
i = 0;
|
||||
out = vec_token_new(input.len, token_free);
|
||||
while (i < input.len)
|
||||
{
|
||||
if (_is_cmd_node(input.buffer[i].type))
|
||||
{
|
||||
j = 0;
|
||||
tmp = token_new(TOK_CMD);
|
||||
while (i + j < input.len \
|
||||
&& _is_cmd_node(input.buffer[i + j].type))
|
||||
vec_token_push(&tmp.subtokens, token_clone(&input.buffer[i + j++]));
|
||||
vec_token_push(&out, tmp);
|
||||
i += j;
|
||||
}
|
||||
else
|
||||
vec_token_push(&out, token_clone(&input.buffer[i++]));
|
||||
}
|
||||
vec_token_free(input);
|
||||
return (*output = out, NO_ERROR);
|
||||
}
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* fold_double_quote.c :+: :+: :+: */
|
||||
/* fold_word.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/06 15:30:07 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/07 16:48:37 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -32,7 +32,8 @@ bool _type_extansion(enum e_token type)
|
|||
{
|
||||
if (type == TOK_DOLLAR)
|
||||
return (true);
|
||||
if (type == TOK_NQUOTE || type == TOK_DQUOTE || type == TOK_SQUOTE)
|
||||
if (type == TOK_NQUOTE || type == TOK_DQUOTE || type == TOK_SQUOTE \
|
||||
|| type == TOK_EXPENSION)
|
||||
return (true);
|
||||
return (false);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/06 13:32:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/06 13:32:39 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/07 16:47:20 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -62,5 +62,7 @@ t_str token_name(t_token *token)
|
|||
return ("DLPAREN");
|
||||
if (token->type == TOK_DRPAREN)
|
||||
return ("DRPAREN");
|
||||
if (token->type == TOK_CMD)
|
||||
return ("CMD");
|
||||
return (NULL);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue