From 5402ec152ae9dd550ed4c6d39c715bb4eaa86f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Sun, 21 Jul 2024 17:49:03 +0200 Subject: [PATCH] Improved arithmetic comprehension --- Makefile | 4 ++-- ast/include/ast/ast.h | 8 +++++--- ast/include/ast/ast_forward_def.h | 3 ++- ast/include/ast/ast_raw_structs.h | 14 ++++++++++++- ast/src/from_node.c | 33 +++++++++++++++++++++++++++++-- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 05554ba0..4bd904a2 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# **************************************************************************** # +# **************************************************************************** #make # # # ::: :::::::: # # Makefile :+: :+: :+: # @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/07/03 20:37:35 by maiboyer ### ########.fr # +# Updated: 2024/07/17 17:05:25 by rparodi ### ########.fr # # # # **************************************************************************** # diff --git a/ast/include/ast/ast.h b/ast/include/ast/ast.h index 1ab1d1cf..26be44c4 100644 --- a/ast/include/ast/ast.h +++ b/ast/include/ast/ast.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/11 14:23:40 by maiboyer #+# #+# */ -/* Updated: 2024/07/21 16:28:16 by maiboyer ### ########.fr */ +/* Updated: 2024/07/21 17:42:37 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,8 @@ enum e_ast_node_kind { AST_ARITHMETIC_BINARY, - AST_ARITHMETIC_EXPANSION, + AST_ARITHMETIC_EXPANSION, //RAPH + AST_ARITHMETIC_LITTERAL, //RAPH AST_CASE, AST_CASE_ITEM, AST_COMMAND, @@ -50,7 +51,8 @@ enum e_ast_node_kind union u_ast_node_data { t_ast_arithmetic_binary arithmetic_binary; - t_ast_arithmetic_expansion arithmetic_expansion; + t_ast_arithmetic_literal arithmetic_literal; //RAPH + t_ast_arithmetic_expansion arithmetic_expansion; //RAPH t_ast_case case_; t_ast_case_item case_item; t_ast_command command; diff --git a/ast/include/ast/ast_forward_def.h b/ast/include/ast/ast_forward_def.h index c18ef3e1..e474504c 100644 --- a/ast/include/ast/ast_forward_def.h +++ b/ast/include/ast/ast_forward_def.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/11 14:24:24 by maiboyer #+# #+# */ -/* Updated: 2024/07/21 16:26:36 by maiboyer ### ########.fr */ +/* Updated: 2024/07/21 17:43:13 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ typedef union u_ast_node_data t_ast_node_data; typedef struct s_ast_node *t_ast_node; typedef struct s_ast_arithmetic_expansion t_ast_arithmetic_expansion; +typedef struct s_ast_arithmetic_literal t_ast_arithmetic_literal; typedef struct s_ast_arithmetic_binary t_ast_arithmetic_binary; typedef struct s_ast_case t_ast_case; typedef struct s_ast_case_item t_ast_case_item; diff --git a/ast/include/ast/ast_raw_structs.h b/ast/include/ast/ast_raw_structs.h index c3242f29..82c58414 100644 --- a/ast/include/ast/ast_raw_structs.h +++ b/ast/include/ast/ast_raw_structs.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 17:46:58 by maiboyer #+# #+# */ -/* Updated: 2024/07/21 16:26:17 by maiboyer ### ########.fr */ +/* Updated: 2024/07/21 17:46:08 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ enum e_ast_arithmetic_operator ARITH_MINUS, ARITH_DIVIDE, ARITH_MULT, + ARITH_MOD, }; /* @@ -424,6 +425,17 @@ struct s_ast_arithmetic_binary t_ast_node rhs; }; +/// NUMBER +/*struct s_ast_arithmetic_literal*/ +/*{*/ +/* t_i64 value;*/ +/*};*/ + +// RAPH +struct s_ast_arithmetic_literal +{ + t_ast_node value; +}; /// Command Substitution /// ```shell /// $(command) diff --git a/ast/src/from_node.c b/ast/src/from_node.c index ccd69229..a0f992d7 100644 --- a/ast/src/from_node.c +++ b/ast/src/from_node.c @@ -584,7 +584,8 @@ t_ast_arithmetic_operator _parse_operator(t_parse_node self) return (ARITH_MULT); if (symbol == anon_sym_SLASH) return (ARITH_DIVIDE); - // anon_sym_PERCENT + if (symbol == anon_sym_PERCENT) + return (ARITH_MOD); return (me_abort("invalid arithmetic operator"), 0); } @@ -622,10 +623,12 @@ t_error build_sym_word(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_expansion(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_command_substitution(t_parse_node self, t_const_str input, t_ast_node *out); +/* FUNCTION DONE*/ +t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str input, t_ast_node *out); + /* FUNCTION THAT ARE NOT DONE */ // TODO: This is your homework raph -t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_literal(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_postfix_expression(t_parse_node self, t_const_str input, t_ast_node *out); @@ -669,6 +672,32 @@ t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str in return (*out = ret, NO_ERROR); } +t_error build_sym_arithmetic_literal(t_parse_node self, t_const_str input, t_ast_node *out) +{ + t_usize i; + t_ast_node ret; + + if (out == NULL) + return (ERROR); + if (ts_node_symbol(self) != sym_arithmetic_literal) + return (ERROR); + i = 0; + ret = ast_alloc(AST_ARITHMETIC_LITTERAL); + while (i < ts_node_child_count(self)) + { + if (ts_node_field_id_for_child(self, i) == field_lhs) + if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_binary.lhs)) + return (ERROR); + if (ts_node_field_id_for_child(self, i) == field_op) + ret->data.arithmetic_binary.op = _parse_operator(ts_node_child(self, i)); + if (ts_node_field_id_for_child(self, i) == field_rhs) + if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_binary.rhs)) + return (ERROR); + i++; + } + return (*out = ret, NO_ERROR); +} + t_error build_sym_command_substitution(t_parse_node self, t_const_str input, t_ast_node *out) { t_ast_node ret;