From b23c6ebcbae7f246619d757a6c66ec7c4d70d971 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 14 Jan 2018 01:42:58 -0800 Subject: [PATCH] Migrate run_block_statement to tnode_t --- src/parse_execution.cpp | 52 ++++++++++++++--------------------------- src/parse_execution.h | 2 +- src/parse_grammar.h | 6 +++-- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index 4f0b94a26..307d0e8f4 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -392,42 +392,24 @@ parse_execution_result_t parse_execution_context_t::run_function_statement( } parse_execution_result_t parse_execution_context_t::run_block_statement( - const parse_node_t &statement) { - assert(statement.type == symbol_block_statement); - - const parse_node_t &block_header = - *get_child(statement, 0, symbol_block_header); // block header - const parse_node_t &header = - *get_child(block_header, 0); // specific header type (e.g. for loop) - const parse_node_t &contents = *get_child(statement, 1, symbol_job_list); // block contents + tnode_t statement) { + tnode_t bheader = statement.child<0>(); + tnode_t contents = statement.child<1>(); parse_execution_result_t ret = parse_execution_success; - switch (header.type) { - case symbol_for_header: { - ret = run_for_statement(header, contents); - break; - } - case symbol_while_header: { - ret = run_while_statement(header, contents); - break; - } - case symbol_function_header: { - const parse_node_t &function_end = *get_child( - statement, 2, symbol_end_command); // the 'end' associated with the block - ret = run_function_statement(header, function_end); - break; - } - case symbol_begin_header: { - ret = run_begin_statement(header, contents); - break; - } - default: { - debug(0, L"Unexpected block header: %ls\n", header.describe().c_str()); - PARSER_DIE(); - break; - } + if (auto header = bheader.try_get_child()) { + ret = run_for_statement(header, contents); + } else if (auto header = bheader.try_get_child()) { + ret = run_while_statement(header, contents); + } else if (auto header = bheader.try_get_child()) { + tnode_t func_end = statement.child<2>(); + ret = run_function_statement(header, func_end); + } else if (auto header = bheader.try_get_child()) { + ret = run_begin_statement(header, contents); + } else { + debug(0, L"Unexpected block header: %ls\n", bheader.node()->describe().c_str()); + PARSER_DIE(); } - return ret; } @@ -1233,7 +1215,7 @@ parse_execution_result_t parse_execution_context_t::run_1_job(const parse_node_t assert(specific_statement_type_is_redirectable_block(specific_statement)); switch (specific_statement.type) { case symbol_block_statement: { - result = this->run_block_statement(specific_statement); + result = this->run_block_statement({&tree(), &specific_statement}); break; } case symbol_if_statement: { @@ -1394,7 +1376,7 @@ parse_execution_result_t parse_execution_context_t::eval_node_at_offset( break; } case symbol_block_statement: { - status = this->run_block_statement(node); + status = this->run_block_statement({&tree(), &node}); break; } case symbol_if_statement: { diff --git a/src/parse_execution.h b/src/parse_execution.h index f9ee352ff..be6beff8a 100644 --- a/src/parse_execution.h +++ b/src/parse_execution.h @@ -94,7 +94,7 @@ class parse_execution_context_t { const parse_node_t &statement_node); // These encapsulate the actual logic of various (block) statements. - parse_execution_result_t run_block_statement(const parse_node_t &statement); + parse_execution_result_t run_block_statement(tnode_t statement); parse_execution_result_t run_for_statement(const parse_node_t &header, const parse_node_t &contents); parse_execution_result_t run_if_statement(const parse_node_t &statement); diff --git a/src/parse_grammar.h b/src/parse_grammar.h index 80640ac5a..cd82a5ecf 100644 --- a/src/parse_grammar.h +++ b/src/parse_grammar.h @@ -248,7 +248,8 @@ DEF(case_item) produces_sequence, argument_list, tok }; DEF(block_statement) -produces_sequence{}; +produces_sequence{ + BODY(block_statement)}; DEF_ALT(block_header) { using forh = single; @@ -271,7 +272,8 @@ DEF(begin_header) produces_single>{BODY(begin_heade // Functions take arguments, and require at least one (the name). No redirections allowed. DEF(function_header) -produces_sequence, argument, argument_list, tok_end>{}; +produces_sequence, argument, argument_list, tok_end>{ + BODY(function_header)}; // A boolean statement is AND or OR or NOT DEF_ALT(boolean_statement) {