From 93f734f5e92d49ae8f507b69da4cf015ea11a71a Mon Sep 17 00:00:00 2001 From: Maix0 Date: Wed, 29 May 2024 01:17:09 +0200 Subject: [PATCH] started to make ast functions --- ast/include/ast/ast.h | 5 +- ast/include/ast/forward.h | 68 +++++++----- ast/include/ast/from_node.h | 199 ++++++++++++++++++++++++++++++++++++ ast/src.list | 1 + ast/src/from_node.c | 106 +++++++++++++++++++ symbol_list | 110 -------------------- symbols_list | 60 +++++++++++ 7 files changed, 413 insertions(+), 136 deletions(-) create mode 100644 ast/include/ast/from_node.h create mode 100644 ast/src/from_node.c delete mode 100644 symbol_list create mode 100644 symbols_list diff --git a/ast/include/ast/ast.h b/ast/include/ast/ast.h index 1d94f51e..e7aae0d7 100644 --- a/ast/include/ast/ast.h +++ b/ast/include/ast/ast.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/25 19:30:30 by maiboyer #+# #+# */ -/* Updated: 2024/05/26 17:55:39 by maiboyer ### ########.fr */ +/* Updated: 2024/05/28 14:49:05 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "ast/forward.h" #include "me/types.h" +#include /// @brief Node types enumeration /// @details This enumeration is used to identify the type of a node @@ -520,7 +521,7 @@ struct s_command_backticks }; union u_ast_node { - t_ast_type ast_type; + t_ast_type type; t_and_list and_list; t_and_or_list and_or_list; t_arithmetic_expansion arithmetic_expansion; diff --git a/ast/include/ast/forward.h b/ast/include/ast/forward.h index 7d03f2df..7dddfebf 100644 --- a/ast/include/ast/forward.h +++ b/ast/include/ast/forward.h @@ -1,9 +1,27 @@ -typedef enum e_assignement_modifier t_assignement_modifier; -typedef enum e_ast_type t_ast_type; -typedef enum e_op_in t_op_in; -typedef enum e_op_pre t_op_pre; -typedef enum e_redirect_file_op t_redirect_file_op; -typedef enum e_redirect_heredoc_op t_redirect_heredoc_op; +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* forward.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 13:24:34 by maiboyer #+# #+# */ +/* Updated: 2024/05/28 13:51:40 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FORWARD_H +#define FORWARD_H + +typedef union u_ast_node t_ast_node; + +typedef enum e_assignement_modifier t_assignement_modifier; +typedef enum e_ast_type t_ast_type; +typedef enum e_op_in t_op_in; +typedef enum e_op_pre t_op_pre; +typedef enum e_redirect_file_op t_redirect_file_op; +typedef enum e_redirect_heredoc_op t_redirect_heredoc_op; + typedef struct s_and_list t_and_list; typedef struct s_and_or_list t_and_or_list; typedef struct s_arithmetic_expansion t_arithmetic_expansion; @@ -37,21 +55,23 @@ typedef struct s_simple_command t_simple_command; typedef struct s_subshell_command t_subshell_command; typedef struct s_until_command t_until_command; typedef struct s_while_command t_while_command; -typedef struct s_word t_word; -typedef union u_and_list_body t_and_list_body; -typedef union u_and_or_list_body t_and_or_list_body; -typedef union u_ast_node t_ast_node; -typedef union u_async_command_body t_async_command_body; -typedef union u_command_inner t_command_inner; -typedef union u_compound_command t_compound_command; -typedef union u_compound_list_body t_compound_list_body; -typedef union u_expension_or_string t_expension_or_string; -typedef union u_expension t_expension; -typedef union u_if_clauses t_if_clauses; -typedef union u_not_body t_not_body; -typedef union u_or_list_body t_or_list_body; -typedef union u_program_body t_program_body; -typedef union u_redirect_or_assign t_redirect_or_assign; -typedef union u_redirect_or_word t_redirect_or_word; -typedef union u_redirect t_redirect; -typedef union u_sequential_list_body t_sequential_list_body; +typedef struct s_word t_word; + +typedef union u_and_list_body t_and_list_body; +typedef union u_and_or_list_body t_and_or_list_body; +typedef union u_async_command_body t_async_command_body; +typedef union u_command_inner t_command_inner; +typedef union u_compound_command t_compound_command; +typedef union u_compound_list_body t_compound_list_body; +typedef union u_expension_or_string t_expension_or_string; +typedef union u_expension t_expension; +typedef union u_if_clauses t_if_clauses; +typedef union u_not_body t_not_body; +typedef union u_or_list_body t_or_list_body; +typedef union u_program_body t_program_body; +typedef union u_redirect_or_assign t_redirect_or_assign; +typedef union u_redirect_or_word t_redirect_or_word; +typedef union u_redirect t_redirect; +typedef union u_sequential_list_body t_sequential_list_body; + +#endif /* FORWARD_H */ diff --git a/ast/include/ast/from_node.h b/ast/include/ast/from_node.h new file mode 100644 index 00000000..5be64250 --- /dev/null +++ b/ast/include/ast/from_node.h @@ -0,0 +1,199 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* from_node.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 13:14:10 by maiboyer #+# #+# */ +/* Updated: 2024/05/29 00:54:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FROM_PROGRAM_C +#define FROM_PROGRAM_C + +#include "me/types.h" + +#include "app/node.h" +#include "ast/ast.h" + +t_ast_node *from_node(t_node *node); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_and_list *build_and_list(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_and_or_list *build_and_or_list(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_arithmetic_expansion *build_arithmetic_expansion(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_assignment_list *build_assignment_list(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_assignment *build_assignment(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_ast_string *build_ast_string(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_async_command *build_async_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_brace_command *build_brace_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_case_command *build_case_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_case_item *build_case_item(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_command_backticks *build_command_backticks(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_command_substitution *build_command_substitution(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_command *build_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_compound_list *build_compound_list(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_for_command *build_for_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_function_definition *build_function_definition(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_if_command *build_if_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_if_clause *build_if_clause(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_elif_clause *build_elif_clause(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_else_clause *build_else_clause(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_name *build_name(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_not *build_not(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_or_list *build_or_list(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_parameter_expansion *build_parameter_expansion(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_pipe_list *build_pipe_list(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_program *build_program(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_redirect_file *build_redirect_file(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_redirect_heredoc *build_redirect_heredoc(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_sequential_list *build_sequential_list(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_simple_command *build_simple_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_subshell_command *build_subshell_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_until_command *build_until_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_while_command *build_while_command(t_node *node, t_usize size); + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_word *build_word(t_node *node, t_usize size); + + +/// @param node a pointer to an array of `t_node` of size `size` +/// @param size the size of the nodes +/// @note can be null in case of error +t_ast_node *build_comment(t_node *node, t_usize size); + +#endif /* FROM_NODE_C */ diff --git a/ast/src.list b/ast/src.list index 761a1848..c233cda0 100644 --- a/ast/src.list +++ b/ast/src.list @@ -1 +1,2 @@ build_ast +from_node diff --git a/ast/src/from_node.c b/ast/src/from_node.c new file mode 100644 index 00000000..f32b93b5 --- /dev/null +++ b/ast/src/from_node.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* from_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 13:18:44 by maiboyer #+# #+# */ +/* Updated: 2024/05/29 00:51:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" + +#include "app/node.h" +#include "ast/from_node.h" +#include "gmr/symbols.h" +#include + +t_ast_node *build_comment(t_node *node, t_usize size) +{ + t_ast_node *ptr; + + (void)(node); + (void)(size); + + ptr = mem_alloc(sizeof(*ptr)); + ptr->type = TY_EMPTY; + return (ptr); +} + +t_ast_node *from_node(t_node *node) +{ + if (node == NULL) + return (NULL); + if (node->kind == sym_program) + return ((t_ast_node *)build_program(node, 1)); + + if (node->kind == sym_word) + return ((t_ast_node *)build_word(node, 1)); + if (node->kind == sym_string) + return ((t_ast_node *)build_word(node, 1)); + if (node->kind == sym_concatenation) + return ((t_ast_node *)build_word(node, 1)); + if (node->kind == sym_variable_name) + return ((t_ast_node *)build_word(node, 1)); + if (node->kind == sym_command_name) + return ((t_ast_node *)build_word(node, 1)); + if (node->kind == sym_translated_string) + return ((t_ast_node *)build_word(node, 1)); + + if (node->kind == sym_string_content) + return ((t_ast_node *)build_ast_string(node, 1)); + if (node->kind == sym_raw_string) + return ((t_ast_node *)build_ast_string(node, 1)); + if (node->kind == sym_ansi_c_string) + return ((t_ast_node *)build_ast_string(node, 1)); + + if (node->kind == sym_variable_assignments) + return (t_ast_node *)build_assignment_list(node, 1); + if (node->kind == sym_variable_assignment) + return (t_ast_node *)build_assignment_list(node, 1); + + if (node->kind == sym_command_substitution) + return (t_ast_node *)build_parameter_expansion(node, 1); + if (node->kind == sym_expansion) + return (t_ast_node *)build_parameter_expansion(node, 1); + if (node->kind == sym_simple_expansion) + return (t_ast_node *)build_parameter_expansion(node, 1); + + if (node->kind == sym_arithmetic_expansion) + return ((t_ast_node *)build_arithmetic_expansion(node, 1)); + + if (node->kind == sym_comment) + return ((t_ast_node *)build_comment(node, 1)); + + if (node->kind == sym_negated_command) + return ((t_ast_node *)build_not(node, 1)); + + if (node->kind == sym_redirected_statement) + return ((t_ast_node *)build_command(node, 1)); + if (node->kind == sym_command) + return ((t_ast_node *)build_command(node, 1)); + + if (node->kind == sym_file_redirect) + return ((t_ast_node *)build_redirect_file(node, 1)); + + if (node->kind == sym_heredoc_redirect) + return ((t_ast_node *)build_redirect_heredoc(node, 1)); + + if (node->kind == sym_list) + return ((t_ast_node *)build_and_or_list(node, 1)); + + if (node->kind == sym_pipeline) + return ((t_ast_node *)build_pipe_list(node, 1)); + + if (node->kind == sym_subshell) + return ((t_ast_node *)build_subshell_command(node, 1)); + + if (node->kind == sym_brace_expression) + return ((t_ast_node *)build_brace_command(node, 1)); + + printf("unknown node of kind '%s'\n", node->kind_str); + + return (NULL); +} diff --git a/symbol_list b/symbol_list deleted file mode 100644 index 40c9c6bc..00000000 --- a/symbol_list +++ /dev/null @@ -1,110 +0,0 @@ - sym_word = 1, - sym__special_character = 102, - sym_string_content = 104, - sym_raw_string = 105, - sym_ansi_c_string = 106, - sym_comment = 146, - sym__comment_word = 147, - sym_heredoc_start = 153, - sym_simple_heredoc_body = 154, - sym__heredoc_body_beginning = 155, - sym_heredoc_content = 156, - sym_heredoc_end = 157, - sym_file_descriptor = 158, - sym__empty_value = 159, - sym__concat = 160, - sym_variable_name = 161, - sym_test_operator = 162, - sym_regex = 163, - sym__regex_no_slash = 164, - sym__regex_no_space = 165, - sym__expansion_word = 166, - sym_extglob_pattern = 167, - sym__bare_dollar = 168, - sym__brace_start = 169, - sym__immediate_double_hash = 170, - sym__external_expansion_sym_hash = 171, - sym__external_expansion_sym_bang = 172, - sym__external_expansion_sym_equal = 173, - sym___error_recovery = 174, - sym_program = 175, - sym__statements = 176, - sym__statement_not_pipeline = 178, - sym_redirected_statement = 179, - sym_for_statement = 180, - sym_c_style_for_statement = 181, - sym__for_body = 182, - sym__c_expression = 183, - sym__c_expression_not_assignment = 184, - sym__c_variable_assignment = 185, - sym__c_unary_expression = 186, - sym__c_binary_expression = 187, - sym__c_postfix_expression = 188, - sym__c_parenthesized_expression = 189, - sym_while_statement = 190, - sym_do_group = 191, - sym_if_statement = 192, - sym_elif_clause = 193, - sym_else_clause = 194, - sym_case_statement = 195, - sym_case_item = 196, - sym_last_case_item = 197, - sym_function_definition = 198, - sym_compound_statement = 199, - sym_subshell = 200, - sym_pipeline = 201, - sym_list = 202, - sym_negated_command = 203, - sym_test_command = 204, - sym__test_command_binary_expression = 205, - sym_declaration_command = 206, - sym_unset_command = 207, - sym_command = 208, - sym_command_name = 209, - sym_variable_assignment = 210, - sym_variable_assignments = 211, - sym_subscript = 212, - sym_file_redirect = 213, - sym_heredoc_redirect = 214, - sym__heredoc_pipeline = 215, - sym__heredoc_expression = 216, - sym__heredoc_body = 218, - sym_heredoc_body = 219, - sym__simple_heredoc_body = 220, - sym_herestring_redirect = 221, - sym__expression = 222, - sym_binary_expression = 223, - sym_ternary_expression = 224, - sym_unary_expression = 225, - sym_postfix_expression = 226, - sym_parenthesized_expression = 227, - sym_arithmetic_expansion = 228, - sym_brace_expression = 229, - sym__arithmetic_expression = 230, - sym__arithmetic_literal = 231, - sym__arithmetic_binary_expression = 232, - sym__arithmetic_ternary_expression = 233, - sym__arithmetic_unary_expression = 234, - sym__arithmetic_postfix_expression = 235, - sym__arithmetic_parenthesized_expression = 236, - sym_concatenation = 237, - sym_string = 238, - sym_translated_string = 239, - sym_array = 240, - sym_number = 241, - sym_simple_expansion = 242, - sym_expansion = 243, - sym__expansion_body = 244, - sym__expansion_expression = 245, - sym__expansion_regex = 246, - sym__expansion_regex_replacement = 247, - sym__expansion_regex_removal = 248, - sym__expansion_max_length = 249, - sym__expansion_max_length_expression = 250, - sym__expansion_max_length_binary_expression = 251, - sym__expansion_operator = 252, - sym__concatenation_in_expansion = 253, - sym_command_substitution = 254, - sym_process_substitution = 255, - sym__extglob_blob = 256, - sym__c_terminator = 257, diff --git a/symbols_list b/symbols_list new file mode 100644 index 00000000..b6424be0 --- /dev/null +++ b/symbols_list @@ -0,0 +1,60 @@ + sym_ansi_c_string + sym_arithmetic_expansion + sym_brace_expression + sym_command + sym_command_name + sym_command_substitution + sym_comment + sym_concatenation + sym_expansion + sym_file_redirect + sym_heredoc_body + sym_heredoc_content + sym_heredoc_end + sym_heredoc_redirect + sym_heredoc_start + sym_list + sym_negated_command + sym_pipeline + sym_program + sym_raw_string + sym_redirected_statement + sym_simple_expansion + sym_string + sym_string_content + sym_subshell + sym_translated_string + sym_variable_assignment + sym_variable_assignments + sym_variable_name + sym_word +__sym_array +__sym_binary_expression +__sym_c_style_for_statement +__sym_do_group +__sym_file_descriptor +__sym_herestring_redirect +__sym_number +__sym_parenthesized_expression +__sym_postfix_expression +__sym_process_substitution +__sym_regex +__sym_simple_heredoc_body +__sym_subscript +__sym_ternary_expression +sym_case_item +sym_case_statement +sym_compound_statement +sym_declaration_command +sym_elif_clause +sym_else_clause +sym_extglob_pattern +sym_for_statement +sym_function_definition +sym_if_statement +sym_last_case_item +sym_test_command +sym_test_operator +sym_unary_expression +sym_unset_command +sym_while_statement