From dbb53d37abf840c52d9e095706d5be1e34431011 Mon Sep 17 00:00:00 2001 From: maix0 Date: Thu, 10 Oct 2024 16:22:06 +0200 Subject: [PATCH] update: added some comments and fixed a bit of code --- parser/src/yarn/ast_op.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/parser/src/yarn/ast_op.c b/parser/src/yarn/ast_op.c index 396636cd..a9303f54 100644 --- a/parser/src/yarn/ast_op.c +++ b/parser/src/yarn/ast_op.c @@ -6,15 +6,15 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */ -/* Updated: 2024/10/10 15:56:05 by rparodi ### ########.fr */ +/* Updated: 2024/10/10 16:21:44 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "ast/ast.h" #include "me/types.h" -#include "parser/token.h" #include "me/vec/vec_ast.h" #include "me/vec/vec_token.h" +#include "parser/token.h" /// en fonction de op, qui peut etre: TOK_AND TOK_PIPE TOK_OR /// choisir le bon ast_node a faire (t_ast_node->data.list + set operator ou t_asdt_node->data.pipeline) @@ -26,32 +26,42 @@ /// struct s_ast_list if (tok.type == TOK_AND || tok.type == TOK_OR) /// struct s_ast_pipeline if (tok.type == TOK_PIPE) /// `ast/include/ast/_raw_structs.h` +/// +/// +/// in the end we should change to using `t_error` and pushing the ast_node directly to output_queue in the function, +/// will change that later tho :) t_ast_node ast_from_op(t_token tok, t_vec_ast *output_queue) { - t_ast_node tmp; + t_ast_node ret; + t_ast_node tmp; + // this needs have a protection in case output_queue is smaller than 2 elements + // otherwise it is good :) + // you could also make it so TOK_AND and TOK_OR share the same code to win some lines if (tok.type == TOK_AND) { - tmp = ast_alloc(AST_LIST); - tmp->data.list.op = AST_LIST_AND; - vec_ast_pop(output_queue, &tmp->data.list.right); - vec_ast_pop(output_queue, &tmp->data.list.left); + ret = ast_alloc(AST_LIST); + ret->data.list.op = AST_LIST_AND; + vec_ast_pop(output_queue, &ret->data.list.right); + vec_ast_pop(output_queue, &ret->data.list.left); } else if (tok.type == TOK_OR) { - tmp = ast_alloc(AST_LIST); - tmp->data.list.op = AST_LIST_OR; - vec_ast_pop(output_queue, &tmp->data.list.right); - vec_ast_pop(output_queue, &tmp->data.list.left); + ret = ast_alloc(AST_LIST); + ret->data.list.op = AST_LIST_OR; + vec_ast_pop(output_queue, &ret->data.list.right); + vec_ast_pop(output_queue, &ret->data.list.left); } else if (tok.type == TOK_PIPE) { - tmp = ast_alloc(AST_PIPELINE); + // Here there is some kind of optimization that could be done in the future: if one node is already a AST_PIPELINE, just pus the other node into the right place in it and return the non created AST_PIPELINE node ! + ret = ast_alloc(AST_PIPELINE); vec_ast_pop(output_queue, &tmp); - vec_ast_push(&tmp.data.pipeline.statements, tmp); + vec_ast_push(&ret->data.pipeline.statements, tmp); vec_ast_pop(output_queue, &tmp); - vec_ast_push(&tmp.data.pipeline.statements, tmp); + vec_ast_push(&ret->data.pipeline.statements, tmp); } else me_abort("ast_from_op not the good token type gived !\n"); + return (ret); }