Skip to content

Commit

Permalink
refactor(repl): move keyword and color per keyword collection outside…
Browse files Browse the repository at this point in the history
… the REPL class
  • Loading branch information
SuperFola committed Jan 27, 2025
1 parent 029de70 commit abdc173
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 32 deletions.
13 changes: 13 additions & 0 deletions include/CLI/REPL/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ namespace Ark::internal
*/
void trimWhitespace(std::string& line);

/**
* @brief Compute a list of all the language keywords and builtins
*
* @return std::vector<std::string>
*/
std::vector<std::string> getAllKeywords();

/**
* @brief Compute a list of pairs (word -> color) to be used for coloration by the REPL
* @return std::vector<std::pair<std::string, replxx::Replxx::Color>>
*/
std::vector<std::pair<std::string, replxx::Replxx::Color>> getColorPerKeyword();

replxx::Replxx::completions_t hookCompletion(const std::vector<std::string>& words, const std::string& context, int& length);

void hookColor(const std::vector<std::pair<std::string, replxx::Replxx::Color>>& words_colors, const std::string& context, replxx::Replxx::colors_t& colors);
Expand Down
34 changes: 2 additions & 32 deletions src/arkscript/REPL/Repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <ranges>

#include <Ark/Builtins/Builtins.hpp>
#include <Ark/Compiler/Common.hpp>

#include <CLI/REPL/Repl.hpp>
#include <CLI/REPL/Utils.hpp>
Expand All @@ -19,37 +18,8 @@ namespace Ark
m_old_ip(0), m_lib_env(lib_env),
m_state(m_lib_env), m_vm(m_state), m_has_init_vm(false)
{
m_keywords.reserve(keywords.size() + Language::listInstructions.size() + Language::operators.size() + Builtins::builtins.size() + 2);
for (auto keyword : keywords)
m_keywords.emplace_back(keyword);
for (auto inst : Language::listInstructions)
m_keywords.emplace_back(inst);
for (auto op : Language::operators)
m_keywords.emplace_back(op);
for (const auto& builtin : std::ranges::views::keys(Builtins::builtins))
m_keywords.push_back(builtin);
m_keywords.emplace_back("and");
m_keywords.emplace_back("or");

m_words_colors.reserve(keywords.size() + Language::listInstructions.size() + Language::operators.size() + Builtins::builtins.size() + 4);
for (auto keyword : keywords)
m_words_colors.emplace_back(keyword, Replxx::Color::BRIGHTRED);
for (auto inst : Language::listInstructions)
m_words_colors.emplace_back(inst, Replxx::Color::GREEN);
for (auto op : Language::operators)
{
auto safe_op = std::string(op);
if (const auto it = safe_op.find_first_of(R"(-+=/*<>[]()?")"); it != std::string::npos)
safe_op.insert(it, "\\");
m_words_colors.emplace_back(safe_op, Replxx::Color::BRIGHTBLUE);
}
for (const auto& builtin : std::ranges::views::keys(Builtins::builtins))
m_words_colors.emplace_back(builtin, Replxx::Color::GREEN);

m_words_colors.emplace_back("and", Replxx::Color::BRIGHTBLUE);
m_words_colors.emplace_back("or", Replxx::Color::BRIGHTBLUE);
m_words_colors.emplace_back("[\\-|+]?[0-9]+(\\.[0-9]+)?", Replxx::Color::YELLOW);
m_words_colors.emplace_back("\".*\"", Replxx::Color::MAGENTA);
m_keywords = getAllKeywords();
m_words_colors = getColorPerKeyword();
}

int Repl::run()
Expand Down
49 changes: 49 additions & 0 deletions src/arkscript/REPL/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <numeric>
#include <ranges>

#include <Ark/Builtins/Builtins.hpp>
#include <Ark/Compiler/Common.hpp>

namespace Ark::internal
{
long countOpenEnclosures(const std::string& line, const char open, const char close)
Expand All @@ -22,6 +25,52 @@ namespace Ark::internal
}
}

std::vector<std::string> getAllKeywords()
{
std::vector<std::string> output;
output.reserve(keywords.size() + Language::listInstructions.size() + Language::operators.size() + Builtins::builtins.size() + 2);
for (auto keyword : keywords)
output.emplace_back(keyword);
for (auto inst : Language::listInstructions)
output.emplace_back(inst);
for (auto op : Language::operators)
output.emplace_back(op);
for (const auto& builtin : std::ranges::views::keys(Builtins::builtins))
output.push_back(builtin);
output.emplace_back("and");
output.emplace_back("or");

return output;
}

std::vector<std::pair<std::string, replxx::Replxx::Color>> getColorPerKeyword()
{
using namespace replxx;

std::vector<std::pair<std::string, Replxx::Color>> output;
output.reserve(keywords.size() + Language::listInstructions.size() + Language::operators.size() + Builtins::builtins.size() + 4);
for (auto keyword : keywords)
output.emplace_back(keyword, Replxx::Color::BRIGHTRED);
for (auto inst : Language::listInstructions)
output.emplace_back(inst, Replxx::Color::GREEN);
for (auto op : Language::operators)
{
auto safe_op = std::string(op);
if (const auto it = safe_op.find_first_of(R"(-+=/*<>[]()?")"); it != std::string::npos)
safe_op.insert(it, "\\");
output.emplace_back(safe_op, Replxx::Color::BRIGHTBLUE);
}
for (const auto& builtin : std::ranges::views::keys(Builtins::builtins))
output.emplace_back(builtin, Replxx::Color::GREEN);

output.emplace_back("and", Replxx::Color::BRIGHTBLUE);
output.emplace_back("or", Replxx::Color::BRIGHTBLUE);
output.emplace_back("[\\-|+]?[0-9]+(\\.[0-9]+)?", Replxx::Color::YELLOW);
output.emplace_back("\".*\"", Replxx::Color::MAGENTA);

return output;
}

std::size_t codepointLength(const std::string& str)
{
return std::accumulate(
Expand Down

0 comments on commit abdc173

Please sign in to comment.