Skip to content

Commit

Permalink
Merge pull request #34 from mjp41/0-extra-action
Browse files Browse the repository at this point in the history
UI: Make interactive mode more interactive
  • Loading branch information
xFrednet authored Oct 25, 2024
2 parents fd7ec03 + 473b7ca commit be9a1b0
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/lang/interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ namespace verona::interpreter
}
};

void start(trieste::Node main_body, bool interactive)
void start(trieste::Node main_body, int step_counter)
{
rt::ui::MermaidUI ui(interactive);
rt::ui::MermaidUI ui(step_counter);

size_t initial = rt::pre_run(&ui);

Expand Down
15 changes: 11 additions & 4 deletions src/lang/lang.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "interpreter.h"
#include "trieste/driver.h"

#include <limits>
#include <optional>

using namespace trieste;
Expand All @@ -21,17 +22,23 @@ std::pair<PassDef, std::shared_ptr<std::optional<Node>>> extract_bytecode_pass()

namespace verona::interpreter
{
void start(trieste::Node main_body, bool interactive);
void start(trieste::Node main_body, int step_counter);
}

struct CLIOptions : trieste::Options
{
bool iterative = false;
int step_counter = std::numeric_limits<int>::max();

void configure(CLI::App& app)
{
app.add_flag(
"-i,--interactive", iterative, "Run the interpreter iteratively");
"-i,--interactive",
[&](auto) { step_counter = 0; },
"Run the interpreter iteratively");
app.add_option(
"-s,--step",
step_counter,
"Step n instructions before entering interactive mode");
}
};

Expand All @@ -48,7 +55,7 @@ int load_trieste(int argc, char** argv)

if (build_res == 0 && result->has_value())
{
verona::interpreter::start(result->value(), options.iterative);
verona::interpreter::start(result->value(), options.step_counter);
}
return build_res;
}
8 changes: 8 additions & 0 deletions src/rt/core/builtin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ namespace rt::core

return std::nullopt;
});

add_builtin("breakpoint", [mermaid](auto, auto, auto args) {
assert(args == 0);

mermaid->break_next();

return std::nullopt;
});
}

void ctor_builtins()
Expand Down
21 changes: 19 additions & 2 deletions src/rt/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ namespace rt::ui
friend class MermaidDiagram;
friend void core::mermaid_builtins(ui::UI* ui);

bool interactive;
/// @brief Indicates if this is the first break and the help message should
/// be printed.
bool first_break = true;
/// @brief Indicates how many steps should be taken until entering
/// interactive mode again.
int steps;
std::string path;
std::ofstream out;

Expand All @@ -45,10 +50,22 @@ namespace rt::ui
std::set<objects::DynObject*> always_hide;

public:
MermaidUI(bool interactive);
MermaidUI(int step_counter);

void output(std::vector<objects::DynObject*>& roots, std::string message);

void next_action();

void break_next()
{
steps = 0;
}

bool should_break()
{
return steps == 0;
}

bool is_mermaid()
{
return true;
Expand Down
77 changes: 65 additions & 12 deletions src/rt/ui/mermaid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../ui.h"

#include <fstream>
#include <limits>
#include <map>
#include <string>
#include <vector>
Expand Down Expand Up @@ -177,21 +178,22 @@ namespace rt::ui
}
};

MermaidUI::MermaidUI(bool interactive_) : interactive(interactive_)
MermaidUI::MermaidUI(int step_counter) : steps(step_counter)
{
path = "mermaid.md";

if (!interactive)
{
// Will be opened by the output function.
out.open(path);
}
}

void MermaidUI::output(
std::vector<rt::objects::DynObject*>& roots, std::string message)
{
if (interactive)
// Reset the file if this is a breakpoint
if (should_break() && out.is_open())
{
out.close();
}

// Open the file if it's not open
if (!out.is_open())
{
out.open(path);
}
Expand All @@ -203,11 +205,62 @@ namespace rt::ui
MermaidDiagram diag(this);
diag.draw(roots);

if (interactive)
if (should_break())
{
out.close();
std::cout << "Press a key!" << std::endl;
getchar();
out.flush();
next_action();
}
else
{
steps -= 1;
}
}

void print_help()
{
std::cout << "Commands:" << std::endl;
std::cout << "- s <n>: Run n step (default n = 0) [Default]" << std::endl;
std::cout << "- r : Runs until the next break point" << std::endl;
std::cout << "- h : Prints this message " << std::endl;
}

void MermaidUI::next_action()
{
if (first_break)
{
print_help();
first_break = false;
}

while (true)
{
std::cout << "> ";
std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);
std::string command;
iss >> command;

if (command == "s" || line.empty())
{
int n = 0;
steps = (iss >> n) ? n : 0;

return;
}
else if (command == "r")
{
steps = std::numeric_limits<int>::max();
return;
}
else if (command == "h")
{
print_help();
}
else
{
std::cerr << "Unknown command. Type 'h' for help." << std::endl;
}
}
}
} // namespace rt::ui

0 comments on commit be9a1b0

Please sign in to comment.