normed: parser/src/passes/
This commit is contained in:
parent
3d11b63428
commit
1ba7fc5f32
6 changed files with 29 additions and 157 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/08 14:44:55 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/12 15:51:43 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,10 +16,30 @@
|
|||
#include "me/vec/vec_token.h"
|
||||
#include "parser/token.h"
|
||||
|
||||
static bool _is_cmd_node(enum e_token ttype)
|
||||
static bool _is_cmd_node(enum e_token ttype)
|
||||
{
|
||||
return (ttype == TOK_WHITESPACE || ttype == TOK_WORD || ttype == TOK_REDIR);
|
||||
};
|
||||
}
|
||||
|
||||
static void _handle_cmd_node(\
|
||||
t_vec_token *input, t_usize *i, t_vec_token *out)
|
||||
{
|
||||
t_usize j;
|
||||
t_token tmp;
|
||||
|
||||
j = 0;
|
||||
tmp = token_new_meta(TOK_CMD);
|
||||
while (*i + j < input->len \
|
||||
&& _is_cmd_node(input->buffer[*i + j].type))
|
||||
{
|
||||
if (input->buffer[*i + j].type != TOK_WHITESPACE)
|
||||
vec_token_push(&tmp.subtokens, \
|
||||
token_clone(&input->buffer[*i + j]));
|
||||
j++;
|
||||
}
|
||||
vec_token_push(out, tmp);
|
||||
*i += j;
|
||||
}
|
||||
|
||||
/// This is a sample pass
|
||||
///
|
||||
|
|
@ -36,31 +56,15 @@ 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_meta(TOK_CMD);
|
||||
while (i + j < input.len \
|
||||
&& _is_cmd_node(input.buffer[i + j].type))
|
||||
{
|
||||
if (input.buffer[i + j].type != TOK_WHITESPACE)
|
||||
vec_token_push(&tmp.subtokens, token_clone(&input.buffer[i + j]));
|
||||
j++;
|
||||
}
|
||||
vec_token_push(&out, tmp);
|
||||
i += j;
|
||||
}
|
||||
_handle_cmd_node(&input, &i, &out);
|
||||
else
|
||||
{
|
||||
vec_token_push(&out, token_clone(&input.buffer[i++]));
|
||||
}
|
||||
}
|
||||
vec_token_free(input);
|
||||
return (*output = out, NO_ERROR);
|
||||
|
|
|
|||
|
|
@ -1,87 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* fold_double_paren.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/06 13:40:36 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 void _fold_parens_helper(t_vec_token *v, enum e_token ty, t_const_str s)
|
||||
{
|
||||
t_token tmp;
|
||||
|
||||
tmp = token_new(ty);
|
||||
string_push(&tmp.string, s);
|
||||
vec_token_push(v, tmp);
|
||||
}
|
||||
|
||||
/// 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_double_lparen(t_vec_token input, t_vec_token *output)
|
||||
{
|
||||
t_vec_token out;
|
||||
t_usize i;
|
||||
|
||||
i = 0;
|
||||
out = vec_token_new(input.len, token_free);
|
||||
while (i < input.len)
|
||||
{
|
||||
if (i + 1 >= input.len)
|
||||
vec_token_push(&out, token_clone(&input.buffer[i]));
|
||||
else if (input.buffer[i].type == TOK_LPAREN
|
||||
&& input.buffer[i + 1].type == TOK_LPAREN)
|
||||
{
|
||||
_fold_parens_helper(&out, TOK_DLPAREN, "((");
|
||||
i++;
|
||||
}
|
||||
else
|
||||
vec_token_push(&out, token_clone(&input.buffer[i]));
|
||||
i++;
|
||||
}
|
||||
vec_token_free(input);
|
||||
return (*output = out, NO_ERROR);
|
||||
}
|
||||
|
||||
t_error ts_double_rparen(t_vec_token input, t_vec_token *output)
|
||||
{
|
||||
t_vec_token out;
|
||||
t_usize i;
|
||||
|
||||
i = 0;
|
||||
out = vec_token_new(input.len, token_free);
|
||||
while (i < input.len)
|
||||
{
|
||||
if (i + 1 >= input.len)
|
||||
vec_token_push(&out, token_clone(&input.buffer[i]));
|
||||
else if (input.buffer[i].type == TOK_RPAREN
|
||||
&& input.buffer[i + 1].type == TOK_RPAREN)
|
||||
{
|
||||
_fold_parens_helper(&out, TOK_DRPAREN, "))");
|
||||
i++;
|
||||
}
|
||||
else
|
||||
vec_token_push(&out, token_clone(&input.buffer[i]));
|
||||
i++;
|
||||
}
|
||||
vec_token_free(input);
|
||||
return (*output = out, NO_ERROR);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/07 16:48:37 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/12 15:52:16 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -56,7 +56,7 @@ t_error ts_fold_into_word(t_vec_token input, t_vec_token *output)
|
|||
while (i + j < input.len \
|
||||
&& _type_extansion(input.buffer[i + j].type))
|
||||
vec_token_push(&tmp.subtokens, \
|
||||
token_clone(&input.buffer[i + j++]));
|
||||
token_clone(&input.buffer[i + j++]));
|
||||
vec_token_push(&out, tmp);
|
||||
i += j;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/08 15:18:58 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/12 15:52:35 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -26,9 +26,9 @@
|
|||
/// - the output tokens may not be direct copy of the input tokens,
|
||||
/// but need to be cloned (different allocations for stuff)
|
||||
|
||||
t_error ts_remove_whitespace(t_vec_token input, t_vec_token *output)
|
||||
t_error ts_remove_whitespace(t_vec_token input, t_vec_token *output)
|
||||
{
|
||||
t_vec_token out;
|
||||
t_vec_token out;
|
||||
t_usize i;
|
||||
|
||||
i = 0;
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* template_file.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/04 17:46:43 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser/passes.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_token.h"
|
||||
#include "parser/token.h"
|
||||
|
||||
/// 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_do_fuck_all(t_vec_token input, t_vec_token *output)
|
||||
{
|
||||
t_vec_token out;
|
||||
t_usize i;
|
||||
|
||||
i = 0;
|
||||
out = vec_token_new(input.len, token_free);
|
||||
while (i < input.len)
|
||||
{
|
||||
vec_token_push(&out, token_clone(&input.buffer[i]));
|
||||
i++;
|
||||
}
|
||||
vec_token_free(input);
|
||||
return (*output = out, NO_ERROR);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue