Skip to content

Commit

Permalink
feat(repl): added reset command to the repl, closes #278
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFola committed May 11, 2024
1 parent 4b4e84e commit 5eb985b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- basic ArkScript code formatter, available through the CLI: `arkscript -f|--format`
- comments are now tracked in the AST and attached to the nearest node below them
- `VM::forceReloadPlugins`, to be used by the REPL to force reload the plugins and be sure that their symbols are all define
- added `help` and `save` commands to the REPL
- added `help`, `save` (save history to disk), `history` (print history), `reset` (reset vm and code) commands to the REPL
- REPL can now show when a code block isn't terminated (prompt changes from `>` to `:`)
- more controls available inside the REPL

Expand Down
8 changes: 6 additions & 2 deletions include/CLI/REPL/Repl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ namespace Ark

private:
replxx::Replxx m_repl;
int m_old_ip;
std::vector<std::filesystem::path> m_lib_env;
unsigned m_line_count;
std::string m_code;
bool m_running;

int m_old_ip;
std::vector<std::filesystem::path> m_lib_env;
State m_state;
VM m_vm;
bool m_has_init_vm;

/**
* @brief Configure replxx
*/
Expand Down
42 changes: 24 additions & 18 deletions src/arkscript/REPL/Repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,53 @@ namespace Ark
using namespace internal;

Repl::Repl(const std::vector<std::filesystem::path>& lib_env) :
m_old_ip(0), m_lib_env(lib_env), m_line_count(1), m_running(true)
m_line_count(1), m_running(true),
m_old_ip(0), m_lib_env(lib_env),
m_state(m_lib_env), m_vm(m_state), m_has_init_vm(false)
{}

int Repl::run()
{
Ark::State state(m_lib_env);
Ark::VM vm(state);
state.setDebug(0);
bool init = false;

fmt::print("ArkScript REPL -- Version {} [LICENSE: Mozilla Public License 2.0]\nType \"quit\" to quit. Try \"help\" for more informations\n", ARK_FULL_VERSION);
fmt::print("ArkScript REPL -- Version {} [LICENSE: Mozilla Public License 2.0]\nType \"quit\" to quit. Try \"help\" for more information\n", ARK_FULL_VERSION);
cuiSetup();

while (m_running)
{
auto maybe_block = getCodeBlock();

// save a valid ip if execution failed
m_old_ip = vm.m_execution_contexts[0]->ip;
m_old_ip = m_vm.m_execution_contexts[0]->ip;
if (maybe_block.has_value() && !maybe_block.value().empty())
{
std::string new_code = m_code + maybe_block.value();
if (state.doString(new_code))
if (m_state.doString(new_code))
{
// for only one vm init
if (!init)
if (!m_has_init_vm)
{
vm.init();
init = true;
m_vm.init();
m_has_init_vm = true;
}
else
vm.forceReloadPlugins();
m_vm.forceReloadPlugins();

if (vm.safeRun(*vm.m_execution_contexts[0]) == 0)
if (m_vm.safeRun(*m_vm.m_execution_contexts[0]) == 0)
{
// save good code
m_code = new_code;
// place ip to end of bytecode instruction (HALT)
vm.m_execution_contexts[0]->ip -= 4;
m_vm.m_execution_contexts[0]->ip -= 4;
}
else
{
// reset ip if execution failed
vm.m_execution_contexts[0]->ip = m_old_ip;
m_vm.m_execution_contexts[0]->ip = m_old_ip;
}

state.reset();
m_state.reset();
}
else
std::cout << "Couldn't run code\n";
std::cout << "\nCouldn't run code\n";
}
}

Expand Down Expand Up @@ -129,6 +126,7 @@ namespace Ark
std::cout << " quit -- quit the REPL\n";
std::cout << " save -- save the history to disk\n";
std::cout << " history -- print saved code\n";
std::cout << " reset -- reset the VM state\n";

return std::nullopt;
}
Expand All @@ -148,6 +146,14 @@ namespace Ark

return std::nullopt;
}
else if (line == "reset")
{
m_state.reset();
m_has_init_vm = false;
m_code.clear();

return std::nullopt;
}

return line;
}
Expand Down

0 comments on commit 5eb985b

Please sign in to comment.