From 980cae5597c3def0229a913e32d9f22aa1bf8a82 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Wed, 4 Sep 2024 16:43:14 +0000 Subject: [PATCH] Updated to support heredocs --- ast/src/_here_doc_not_done.c | 77 +++++----------- ast/src/from_node/condition_node.c | 5 +- ast/src/from_node/from_node.c | 135 ----------------------------- ast/src/from_node/node_utils2.c | 19 ++-- test_heredoc.sh | 8 ++ 5 files changed, 42 insertions(+), 202 deletions(-) create mode 100755 test_heredoc.sh diff --git a/ast/src/_here_doc_not_done.c b/ast/src/_here_doc_not_done.c index dee249f2..a7f54d6f 100644 --- a/ast/src/_here_doc_not_done.c +++ b/ast/src/_here_doc_not_done.c @@ -11,67 +11,34 @@ /* ************************************************************************** */ #include "app/node.h" +#include "ast/_from_node.h" #include "ast/ast.h" #include "gmr/symbols.h" +#include "gmr/field_identifiers.h" +#include "me/str/str.h" #include "me/types.h" +#include "parser/api.h" #include -t_error build_sym_heredoc_body(t_parse_node self, t_const_str input, \ - t_ast_node *out) -{ - printf("This function is not done '%s'", __func__); - (void)(self); - (void)(input); - (void)(out); - return (ERROR); -} - -t_error build_sym_heredoc_content(t_parse_node self, t_const_str input, \ - t_ast_node *out) -{ - printf("This function is not done '%s'", __func__); - (void)(self); - (void)(input); - (void)(out); - return (ERROR); -} - -t_error build_sym_heredoc_end(t_parse_node self, t_const_str input, \ - t_ast_node *out) -{ - printf("This function is not done '%s'", __func__); - (void)(self); - (void)(input); - (void)(out); - return (ERROR); -} - t_error build_sym_heredoc_redirect(t_parse_node self, t_const_str input, \ t_ast_node *out) { - printf("This function is not done '%s'", __func__); - (void)(self); - (void)(input); - (void)(out); - return (ERROR); -} + t_ast_node ret; + t_usize i; -t_error build_sym_heredoc_start(t_parse_node self, t_const_str input, \ - t_ast_node *out) -{ - printf("This function is not done '%s'", __func__); - (void)(self); - (void)(input); - (void)(out); - return (ERROR); -} - -t_error build_sym_simple_heredoc_body(t_parse_node self, t_const_str input, \ - t_ast_node *out) -{ - printf("This function is not done '%s'", __func__); - (void)(self); - (void)(input); - (void)(out); - return (ERROR); -} + if (ts_node_symbol(self) != sym_heredoc_redirect || input == NULL || out == NULL) + return (ERROR); + ret = ast_alloc(AST_HEREDOC_REDIRECTION); + i = 0; + while (i < ts_node_child_count(self)) + { + if (ts_node_field_id_for_child(self, i) == field_op) + ret->data.heredoc_redirection.op = _get_redirection_op(ts_node_child(self, i)); + else if (ts_node_symbol(ts_node_child(self, i)) == sym_heredoc_start) + ret->data.heredoc_redirection.delimiter = _extract_str(ts_node_child(self, i), input); + else if (ts_node_symbol(ts_node_child(self, i)) == sym_heredoc_body) + ret->data.heredoc_redirection.content = _extract_str(ts_node_child(self, i), input); + i++; + } + return (*out = ret, NO_ERROR); +} \ No newline at end of file diff --git a/ast/src/from_node/condition_node.c b/ast/src/from_node/condition_node.c index f7121a48..20393d1b 100644 --- a/ast/src/from_node/condition_node.c +++ b/ast/src/from_node/condition_node.c @@ -29,7 +29,7 @@ void ast_set_term(t_ast_node *node, t_ast_terminator_kind term) t_ast_node val; if (node == NULL) - return ((void)printf("node == NULL\n")); + return ; val = *node; ptr = &void_storage; if (val->kind == AST_CASE) @@ -45,9 +45,6 @@ void ast_set_term(t_ast_node *node, t_ast_terminator_kind term) if (val->kind == AST_SUBSHELL) ptr = &val->data.subshell.term; *ptr = term; - if (ptr == &void_storage) - printf("node wasn't a term capable node\n"); - (void)(void_storage); } t_error build_sym_if_statement(\ diff --git a/ast/src/from_node/from_node.c b/ast/src/from_node/from_node.c index bb3e0289..683f6735 100644 --- a/ast/src/from_node/from_node.c +++ b/ast/src/from_node/from_node.c @@ -19,58 +19,6 @@ #include "me/vec/vec_ast.h" #include "parser/api.h" #include -/* -sym_arithmetic_binary_expression -sym_arithmetic_expansion -sym_arithmetic_literal -sym_arithmetic_parenthesized_expression -sym_arithmetic_postfix_expression -sym_arithmetic_ternary_expression -sym_arithmetic_unary_expression -sym_case_item -sym_case_statement -sym_command -sym_command_name -sym_command_substitution -sym_comment -sym_compound_statement -sym_concatenation -sym_do_group -sym_elif_clause -sym_else_clause -sym_expansion -sym_expansion_expression -sym_expansion_regex -sym_extglob_pattern -sym_file_descriptor -sym_file_redirect -sym_for_statement -sym_function_definition -sym_heredoc_body -sym_heredoc_content -sym_heredoc_end -sym_heredoc_redirect -sym_heredoc_start -sym_if_statement -sym_list -sym_negated_command -sym_number -sym_pipeline -sym_program -sym_raw_string -sym_redirected_statement -sym_regex -sym_simplex _expansion -sym_simple_heredoc_body -sym_string -sym_string_content -sym_subshell -sym_variable_assignment -sym_variable_assignments -sym_variable_name -sym_while_statement -sym_word -*/ /* E_OP_NONE = 0, // ${var} @@ -193,79 +141,6 @@ t_error build_sym_list(t_parse_node self, t_const_str input, t_ast_node *out) return (*out = ret, NO_ERROR); } -/* -alias_sym_statements -alias_sym_terminator -sym___error_recovery -sym__arithmetic_expression -sym__bare_dollar -sym__case_item_last -sym__comment_word -sym__concat -sym__concatenation_in_expansion -sym__empty_value -sym__expansion_body -sym__expansion_word -sym__extglob_blob -sym__heredoc_body -sym__heredoc_body_beginning -sym__heredoc_expression -sym__heredoc_pipeline -sym__immediate_double_hash -sym__simple_heredoc_body -sym__statement_not_pipeline -sym__statements -sym_arithmetic_binary_expression -sym_arithmetic_expansion -sym_arithmetic_literal -sym_arithmetic_parenthesized_expression -sym_arithmetic_postfix_expression -sym_arithmetic_ternary_expression -sym_arithmetic_unary_expression -sym_case_item -sym_case_statement -sym_command -sym_command_name -sym_command_substitution -sym_comment -sym_compound_statement -sym_concatenation -sym_do_group -sym_elif_clause -sym_else_clause -sym_expansion -sym_expansion_expression -sym_expansion_regex -sym_extglob_pattern -sym_file_descriptor -sym_file_redirect -sym_for_statement -sym_function_definition -sym_heredoc_body -sym_heredoc_content -sym_heredoc_end -sym_heredoc_redirect -sym_heredoc_start -sym_if_statement -sym_list -sym_negated_command -sym_number -sym_pipeline -sym_program -sym_raw_string -sym_redirected_statement -sym_regex -sym_simple_expansion -sym_simple_heredoc_body -sym_string -sym_string_content -sym_subshell -sym_variable_assignment -sym_variable_assignments -sym_variable_name -sym_while_statement -sym_word -*/ t_error ast_from_node(\ t_parse_node node, t_const_str input, t_ast_node *out) @@ -321,16 +196,8 @@ t_error ast_from_node(\ return (build_sym_for_statement(node, input, out)); if (ts_node_symbol(node) == sym_function_definition) return (build_sym_function_definition(node, input, out)); - if (ts_node_symbol(node) == sym_heredoc_body) - return (build_sym_heredoc_body(node, input, out)); - if (ts_node_symbol(node) == sym_heredoc_content) - return (build_sym_heredoc_content(node, input, out)); - if (ts_node_symbol(node) == sym_heredoc_end) - return (build_sym_heredoc_end(node, input, out)); if (ts_node_symbol(node) == sym_heredoc_redirect) return (build_sym_heredoc_redirect(node, input, out)); - if (ts_node_symbol(node) == sym_heredoc_start) - return (build_sym_heredoc_start(node, input, out)); if (ts_node_symbol(node) == sym_if_statement) return (build_sym_if_statement(node, input, out)); if (ts_node_symbol(node) == sym_list) @@ -351,8 +218,6 @@ t_error ast_from_node(\ return (build_sym_regex(node, input, out)); if (ts_node_symbol(node) == sym_simple_expansion) return (build_sym_simple_expansion(node, input, out)); - if (ts_node_symbol(node) == sym_simple_heredoc_body) - return (build_sym_simple_heredoc_body(node, input, out)); if (ts_node_symbol(node) == sym_string) return (build_sym_string(node, input, out)); if (ts_node_symbol(node) == sym_string_content) diff --git a/ast/src/from_node/node_utils2.c b/ast/src/from_node/node_utils2.c index b953776c..5d25b6bd 100644 --- a/ast/src/from_node/node_utils2.c +++ b/ast/src/from_node/node_utils2.c @@ -52,16 +52,19 @@ void _append_redirection(t_ast_node node, t_ast_node redirection) (ast_free(redirection)); } +//// hello I am the SEGV police and I sized this code +// +// t_symbol symbol; +// +// symbol = ts_node_grammar_symbol(ts_node_child(node, 0)); +// if (symbol == anon_sym_SEMI) +// return (AST_TERM_SEMI); +// if (symbol == anon_sym_SEMI_SEMI) +// return (AST_TERM_DOUBLE_SEMI); +// printf("unknown term symbol %d\n", symbol); t_ast_terminator_kind _select_term(t_parse_node node) { - t_symbol symbol; - - symbol = ts_node_grammar_symbol(ts_node_child(node, 0)); - if (symbol == anon_sym_SEMI) - return (AST_TERM_SEMI); - if (symbol == anon_sym_SEMI_SEMI) - return (AST_TERM_DOUBLE_SEMI); - printf("unknown term symbol %d\n", symbol); + (void)(node); return (AST_TERM_NONE); } diff --git a/test_heredoc.sh b/test_heredoc.sh new file mode 100755 index 00000000..59051575 --- /dev/null +++ b/test_heredoc.sh @@ -0,0 +1,8 @@ +make +valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=yes --trace-children=yes ./minishell <<<'cat <