add debug arg

This commit is contained in:
sfja 2024-12-13 20:17:22 +01:00
parent 753788b06d
commit ab853d0516
5 changed files with 30 additions and 10 deletions

View File

@ -3,16 +3,16 @@
#include "rpc_server.hpp" #include "rpc_server.hpp"
#include "vm.hpp" #include "vm.hpp"
#include "vm_provider.hpp" #include "vm_provider.hpp"
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstdlib>
#include <format> #include <format>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
bool print_stack_debug = true; int execute_file_and_exit(std::string filename, bool print_debug)
int execute_file_and_exit(std::string filename)
{ {
auto file = std::ifstream(); auto file = std::ifstream();
file.open(filename.c_str()); file.open(filename.c_str());
@ -36,7 +36,7 @@ int execute_file_and_exit(std::string filename)
{ {
.flame_graph = false, .flame_graph = false,
.code_coverage = false, .code_coverage = false,
.print_stack_debug = print_stack_debug, .print_debug = print_debug,
}); });
vm.run_until_done(); vm.run_until_done();
return 0; return 0;
@ -45,7 +45,27 @@ int execute_file_and_exit(std::string filename)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc >= 3 && std::string(argv[1]) == "run") { if (argc >= 3 && std::string(argv[1]) == "run") {
return execute_file_and_exit(argv[2]); auto filename = std::string();
bool print_debug = false;
for (int i = 2; i < argc; ++i) {
auto arg = std::string(argv[i]);
if (arg == "--print-debug") {
print_debug = true;
} else {
if (!filename.empty()) {
std::cerr << std::format("error: >1 files specified\n");
std::exit(1);
}
filename = arg;
}
}
if (filename.empty()) {
std::cerr << std::format("error: no file specified\n");
std::exit(1);
}
return execute_file_and_exit(argv[2], print_debug);
} }
auto state = sliger::rpc::vm_provider::VmProvider(); auto state = sliger::rpc::vm_provider::VmProvider();

View File

@ -30,7 +30,7 @@ void VM::run_n_instructions(size_t amount)
void VM::run_instruction() void VM::run_instruction()
{ {
if (this->opts.print_stack_debug) { if (this->opts.print_debug) {
// 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]), // maybe_op_to_string(this->program[this->pc]),
// stack_repr_string(8)); // stack_repr_string(8));
@ -281,7 +281,7 @@ void VM::run_instruction()
void VM::run_builtin(Builtin builtin_id) void VM::run_builtin(Builtin builtin_id)
{ {
if (this->opts.print_stack_debug) { if (this->opts.print_debug) {
std::cout << std::format("Running builtin {}\n", std::cout << std::format("Running builtin {}\n",
maybe_builtin_to_string(static_cast<uint32_t>(builtin_id))); maybe_builtin_to_string(static_cast<uint32_t>(builtin_id)));
} }

View File

@ -141,7 +141,7 @@ private:
struct VMOpts { struct VMOpts {
bool flame_graph; bool flame_graph;
bool code_coverage; bool code_coverage;
bool print_stack_debug; bool print_debug;
}; };
class VM { class VM {

View File

@ -9,7 +9,7 @@ auto VmProvider::load_and_run(std::vector<uint32_t> instructions) -> void
{ {
.flame_graph = true, .flame_graph = true,
.code_coverage = true, .code_coverage = true,
.print_stack_debug = false, .print_debug = false,
}); });
vm.run_until_done(); vm.run_until_done();
this->vm = vm; this->vm = vm;

View File

@ -11,5 +11,5 @@ deno run --allow-read --allow-write compiler/main.ts $1
echo Running out.slgbc... echo Running out.slgbc...
./runtime/build/sliger run out.slgbc ./runtime/build/sliger run out.slgbc ${@:2}