Skip to content

Commit

Permalink
Merge pull request #458 from ArkScript-lang/refactor/repl
Browse files Browse the repository at this point in the history
refactor(repl): use modern C++ instead of C, tidy up the code and make it more maintainable
  • Loading branch information
SuperFola authored May 11, 2024
2 parents d827b15 + 5eb985b commit 8ff80c6
Show file tree
Hide file tree
Showing 10 changed files with 487 additions and 427 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
- added boost-ext/ut to write unit tests in C++
- 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`, `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

### Changed
- instructions are on 4 bytes: 1 byte for the instruction, 1 byte of padding, 2 bytes for an immediate argument
Expand Down
8 changes: 8 additions & 0 deletions include/Ark/VM/VM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ namespace Ark
*/
void deleteFuture(internal::Future* f);

/**
* @brief Used by the REPL to force reload all the plugins and their bound methods
*
* @return true on success
* @return false if one or more plugins couldn't be reloaded
*/
bool forceReloadPlugins();

friend class Value;
friend class internal::Closure;
friend class Repl;
Expand Down
173 changes: 0 additions & 173 deletions include/CLI/REPL/ConsoleStyle.hpp

This file was deleted.

44 changes: 29 additions & 15 deletions include/CLI/REPL/Repl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
* @file Repl.hpp
* @author Alexandre Plateau ([email protected])
* @brief ArkScript REPL - Read Eval Print Loop
* @version 0.2
* @version 1.0
* @date 2020-10-27
*
* @copyright Copyright (c) 2020-2021
* @copyright Copyright (c) 2020-2024
*
*/

#ifndef ARK_REPL_REPL_HPP
#define ARK_REPL_REPL_HPP

#include <iostream>
#include <filesystem>
#include <string>
#include <optional>

#include <Ark/Constants.hpp>
#include <Ark/Compiler/Compiler.hpp>
#include <Ark/VM/VM.hpp>
#include <Ark/VM/State.hpp>
#include <CLI/REPL/ConsoleStyle.hpp>

#include <replxx.hxx>

namespace Ark
{
Expand All @@ -29,9 +30,9 @@ namespace Ark
/**
* @brief Construct a new Repl object
*
* @param libenv search path for the std library
* @param lib_env search path for the std library
*/
explicit Repl(const std::vector<std::filesystem::path>& libenv);
explicit Repl(const std::vector<std::filesystem::path>& lib_env);

/**
* @brief Start the REPL
Expand All @@ -40,16 +41,29 @@ namespace Ark
int run();

private:
Replxx m_repl;
unsigned m_lines;
replxx::Replxx m_repl;
unsigned m_line_count;
std::string m_code;
bool m_running;

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

static inline void print_repl_header();
static int count_open_parentheses(const std::string& line);
static int count_open_braces(const std::string& line);
static void trim_whitespace(std::string& line);
void cui_setup();
/**
* @brief Configure replxx
*/
void cuiSetup();

/**
* @brief Get a line via replxx and handle commands
* @param continuation if the prompt needs to be modified because a code block isn't entirely closed, set to true
* @return
*/
std::optional<std::string> getLine(bool continuation);
std::optional<std::string> getCodeBlock();
};
}

Expand Down
Loading

0 comments on commit 8ff80c6

Please sign in to comment.