From 75581c7ee0522300021b3bb79c4cdf63072ca254 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 8 Oct 2024 14:23:39 +0200 Subject: [PATCH] first birth of the yarn algo --- .clangd | 2 ++ flake.nix | 2 +- parser/src/yarn/yarn.c | 66 ++++++++++++++++++++++++++++++++++-------- sources/main.c | 12 ++++++-- 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/.clangd b/.clangd index 9e16af3b..33b4cf29 100644 --- a/.clangd +++ b/.clangd @@ -6,6 +6,8 @@ CompileFlags: # Tweak the parse settings - "-I/nix/store/wxxgsgjxbnkkyczgf8lkbfrsqiywm8bi-clang-17.0.6-lib/lib/clang/17/include/" - "-I/nix/store/3mmvgb08qy8n6n37mnprf77fnp4rssi9-glibc-2.38-27-dev/include/" - "-I/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clanglib/clang/17/include/" + - "-I/nix/store/8mh0vrd6ll02j61612wz28vvsww1ps1z-glibc-2.39-52/include/" + - "-I/nix/store/8vbzvmsm0iy931mdm4hk2vza7gsyc4gs-clang-17.0.6-lib/lib/clang/17/include/" - "-I/usr/include/" - "-I/home/maiboyer/Documents/ring-2/shcat/vendor/" - "-I/home/maiboyer/Documents/ring-2/shcat/stdme/include/" diff --git a/flake.nix b/flake.nix index b9fc8105..d17f3066 100644 --- a/flake.nix +++ b/flake.nix @@ -33,7 +33,7 @@ tree ] ++ ( - if system == "x86_64-linux" + if system == "x86_64-linux" || system == "aarch64-linux" then [valgrind valgrind.dev] else [] ); diff --git a/parser/src/yarn/yarn.c b/parser/src/yarn/yarn.c index 2c0b8e0a..7965ddbc 100644 --- a/parser/src/yarn/yarn.c +++ b/parser/src/yarn/yarn.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/07 18:04:13 by rparodi #+# #+# */ -/* Updated: 2024/10/07 18:14:22 by rparodi ### ########.fr */ +/* Updated: 2024/10/08 14:20:10 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,16 +15,58 @@ #include "me/vec/vec_token.h" #include "parser/token.h" -t_error yarn(t_vec_token *list, enum e_token type, t_vec_token *output) +int _get_precedance(t_token *token) { - size_t i; - - i = 0; - if (!list[0].buffer || !output->buffer) - return (ERROR); - while (list[i].buffer == NULL) - { - i++; - } - return (NO_ERROR); + if (!token) + return (-1); + else if (token->type == TOK_PIPE) + return (2); + else if (token->type == TOK_AND || token->type == TOK_OR) + return (1); + return (0); +} + +t_error yarn(t_vec_token *list, t_vec_token *output) +{ + t_token tmp; + t_token tmp2; + t_vec_token output_queue; + t_vec_token operator_stack; + + output_queue = vec_token_new(16, token_free);; + operator_stack = vec_token_new(16, token_free);; + while (!vec_token_pop_front(list, &tmp)) + { + if (tmp.type == TOK_CMD) + vec_token_push(&output_queue, tmp); + else if (tmp.type == TOK_OR || tmp.type == TOK_AND || tmp.type == TOK_PIPE) + { + while (vec_token_last(&operator_stack) != NULL && vec_token_last(&operator_stack)->type != TOK_LPAREN && _get_precedance(vec_token_last(&operator_stack)) > _get_precedance(&tmp)) + { + vec_token_pop(&operator_stack, &tmp2); + vec_token_push(&output_queue, tmp2); + } + vec_token_push(&operator_stack, tmp); + } + else if (tmp.type == TOK_LPAREN) + vec_token_push(&operator_stack, tmp); + else if (tmp.type == TOK_RPAREN) + { + while (vec_token_last(&operator_stack) != NULL && vec_token_last(&operator_stack)->type != TOK_LPAREN) + { + vec_token_pop(&operator_stack, &tmp2); + vec_token_push(&output_queue, tmp2); + } + if (!(vec_token_last(&operator_stack) != NULL && vec_token_last(&operator_stack)->type == TOK_LPAREN)) + return (ERROR); + vec_token_pop(&operator_stack, NULL); + } + } + while (!vec_token_pop(&operator_stack, &tmp)) + { + if (tmp.type == TOK_LPAREN) + return (ERROR); + vec_token_push(&output_queue, tmp); + } + return (*output = output_queue, NO_ERROR); } diff --git a/sources/main.c b/sources/main.c index 8003659b..27997628 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ -/* Updated: 2024/10/08 13:29:30 by maiboyer ### ########.fr */ +/* Updated: 2024/10/08 14:22:42 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -101,15 +101,21 @@ void print_node_data(t_node *t, t_usize depth) } */ +t_error yarn(t_vec_token *list, t_vec_token *output); + void parse_str(t_state *state) { - t_vec_token tokens; + t_vec_token tokens; + t_vec_token tok_yarn; + if (tokenize(state->str_input, &tokens)) return ; if (ts_apply_passes(tokens, &tokens)) return ; + if (yarn(&tokens, &tok_yarn)) + return ; printf("\n\nEND TOKENS\n"); - ts_print(&tokens); + ts_print(&tok_yarn); vec_token_free(tokens); }