diff --git a/parser/src/passes/fold_double_amp.c b/parser/src/passes/fold_double_amp.c new file mode 100644 index 00000000..6d37932f --- /dev/null +++ b/parser/src/passes/fold_double_amp.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fold_double_amp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */ +/* Updated: 2024/10/04 18:55:34 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_double_amp(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_AMP + && input.buffer[i + 1].type == TOK_AMP) + vec_token_push(&out, token_new(TOK_AND)); + else + vec_token_push(&out, token_clone(&input.buffer[i])); + i++; + } + vec_token_free(input); + return (*output = out, NO_ERROR); +} diff --git a/parser/src/passes/fold_double_lcarret.c b/parser/src/passes/fold_double_lcarret.c new file mode 100644 index 00000000..ee741037 --- /dev/null +++ b/parser/src/passes/fold_double_lcarret.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fold_double_lcarret.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */ +/* Updated: 2024/10/04 18:58:16 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_double_lcarret(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_LCARRET + && input.buffer[i + 1].type == TOK_LCARRET) + vec_token_push(&out, token_new(TOK_DLCARRET)); + else + vec_token_push(&out, token_clone(&input.buffer[i])); + i++; + } + vec_token_free(input); + return (*output = out, NO_ERROR); +} + +t_error ts_double_rcarret(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_RCARRET + && input.buffer[i + 1].type == TOK_RCARRET) + vec_token_push(&out, token_new(TOK_DRCARRET)); + else + vec_token_push(&out, token_clone(&input.buffer[i])); + i++; + } + vec_token_free(input); + return (*output = out, NO_ERROR); +}