test program stack viz

This commit is contained in:
SimonFJ20 2024-11-20 15:07:39 +01:00
parent 2fbccd0bc8
commit 3d89031748
3 changed files with 11 additions and 5 deletions

View File

@ -171,5 +171,7 @@ int main()
.code_coverage = true, .code_coverage = true,
}); });
vm.run_until_done(); vm.run_until_done();
std::cout << std::format("done\n{}\n", vm.stack_repr_string()); std::cout << std::format("done\n{}\n", vm.stack_repr_string(4));
auto flame_graph = vm.flame_graph_json();
std::cout << std::format("flame graph: {}\n", flame_graph);
} }

View File

@ -86,7 +86,7 @@ void VM::run_n_instructions(size_t amount)
void VM::run_instruction() void VM::run_instruction()
{ {
std::cout << std::format(" {:>4}: {:<12}{}\n", this->pc, std::cout << std::format(" {:>4}: {:<12}{}\n", this->pc,
maybe_op_to_string(this->program[this->pc]), stack_repr_string()); maybe_op_to_string(this->program[this->pc]), stack_repr_string(4));
auto op = eat_op(); auto op = eat_op();
switch (op) { switch (op) {
case Op::Nop: case Op::Nop:

View File

@ -156,16 +156,20 @@ public:
return this->stack; return this->stack;
} }
inline auto stack_repr_string() const -> std::string inline auto stack_repr_string(size_t max_items) const -> std::string
{ {
auto result = std::string(); auto result = std::string();
result += ""; result += "";
const auto& stack = view_stack(); const auto& stack = view_stack();
for (size_t i = 0; i < stack.size(); ++i) { for (size_t i = 0; i < stack.size() and i < max_items; ++i) {
if (i != 0) { if (i != 0) {
result += " "; result += " ";
} }
result += stack[stack.size() - i - 1].to_repr_string(); result += std::format(
"{:<11}", stack[stack.size() - i - 1].to_repr_string());
}
if (stack.size() >= max_items) {
result += std::format(" ... + {}", stack.size() - max_items + 1);
} }
return result; return result;
} }