diff options
author | 2013-06-24 12:33:40 -0700 | |
---|---|---|
committer | 2013-06-24 12:33:40 -0700 | |
commit | 66af0c1a53b8108e6509a24921a068870120244c (patch) | |
tree | d83a2a61204a0754606c7efae59fa1bfc9046989 /builtin.cpp | |
parent | 70c6ca76cb72c7a863a24ba24be3a0eec26504e7 (diff) |
More work on the AST. block statements worked out a bit more.
Diffstat (limited to 'builtin.cpp')
-rw-r--r-- | builtin.cpp | 112 |
1 files changed, 109 insertions, 3 deletions
diff --git a/builtin.cpp b/builtin.cpp index fe09f4f6..d77d6361 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -3940,6 +3940,105 @@ static int builtin_history(parser_t &parser, wchar_t **argv) return STATUS_BUILTIN_ERROR; } +#pragma mark Simulator + +struct parse_execution_simulator_t : public parse_execution_visitor_t +{ + wcstring_list_t result; + + wcstring &back() + { + assert(! result.empty()); + return result.back(); + } + + void append_src(node_offset_t idx) + { + wcstring tmp; + context->get_source(idx, &tmp); + back().append(tmp); + } + + void append(const wchar_t *s) + { + back().append(s); + } + + bool enter_job_list(void) + { + return true; + } + + bool enter_job(void) + { + result.resize(result.size() + 1); + return true; + } + + void visit_statement(void) + { + } + + virtual void visit_boolean_statement(void) + { + } + + virtual void enter_if_header(const if_header_t &statement) + { + } + + virtual void exit_if_header(const if_header_t &statement) + { + append_format(back(), L"\nIF successful jump to %lu", (unsigned long)statement.body); + } + + void visit_basic_statement(const exec_basic_statement_t &statement) + { + wcstring &line = this->back(); + if (! line.empty()) + { + line.append(L" <pipe> "); + } + switch (statement.decoration) + { + case exec_basic_statement_t::decoration_builtin: + line.append(L"<builtin> "); + break; + + case exec_basic_statement_t::decoration_command: + line.append(L"<command> "); + break; + + default: + break; + } + + line.append(L"cmd:"); + this->append_src(statement.command_idx); + for (size_t i=0; i < statement.arguments().size(); i++) + { + const exec_argument_t &arg = statement.arguments().at(i); + append(L" "); + append(L"arg:"); + append_src(arg.parse_node_idx); + } + } + + void visit_function(const exec_function_header_t &function) { + wcstring &line = this->back(); + line.append(L"define function: "); + wcstring tmp; + context->get_source(function.name_idx, &tmp); + line.append(tmp); + } + + void exit_job_list(void) + { + } +}; + + + int builtin_parse(parser_t &parser, wchar_t **argv) { std::vector<char> txt; @@ -3969,10 +4068,17 @@ int builtin_parse(parser_t &parser, wchar_t **argv) else { parse_execution_context_t ctx(parse_tree, src); + parse_execution_simulator_t sim; + sim.context = &ctx; + while (ctx.visit_next_node(&sim)) + { + } stdout_buffer.append(L"Simulating execution:\n"); - wcstring simulation = ctx.simulate(); - stdout_buffer.append(simulation); - stdout_buffer.push_back(L'\n'); + for (size_t i=0; i < sim.result.size(); i++) + { + stdout_buffer.append(sim.result.at(i)); + stdout_buffer.push_back(L'\n'); + } } } return STATUS_BUILTIN_OK; |