From d77fa6522ad444047d96fb1cb0aa75a8385701b6 Mon Sep 17 00:00:00 2001 From: SingularityT3 <44658109+SingularityT3@users.noreply.github.com> Date: Sun, 5 Nov 2023 17:16:23 +0530 Subject: [PATCH] Use readline library --- CMakeLists.txt | 10 +++++++++- src/launch/repl.cpp | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index be3a58d..c681cd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,15 @@ else() target_compile_options(PseudoEngine2 PRIVATE -Wall -Wextra -pedantic) endif() -if (CMAKE_BUILD_TYPE STREQUAL "Release") +if (NOT WIN32) + find_library(HAS_READLINE readline) + if (HAS_READLINE) + target_compile_definitions(PseudoEngine2 PRIVATE "READLINE") + target_link_libraries(PseudoEngine2 PRIVATE readline) + endif() +endif() + +if (CMAKE_BUILD_TYPE STREQUAL "Release" AND WIN32) target_link_options(PseudoEngine2 PRIVATE -static) endif() diff --git a/src/launch/repl.cpp b/src/launch/repl.cpp index 62db134..03c4b63 100644 --- a/src/launch/repl.cpp +++ b/src/launch/repl.cpp @@ -3,6 +3,12 @@ #include #include "launch/run.h" +#ifdef READLINE +#include +#include +#include +#endif + extern std::string psfilename; extern bool REPLMode; @@ -17,23 +23,42 @@ static const std::string multilineKeywords[] = { "TYPE" }; +bool getLine(std::string &line, const char *prompt) { +#ifdef READLINE + char *buf = readline(prompt); + if (buf != NULL) { + line = buf; + if (buf[0] != '\0') add_history(buf); + free(buf); + } else { + line.clear(); + return false; + } +#else + std::cout << prompt << std::flush; + std::getline(std::cin, line); + if (std::cin.eof()) return false; +#endif + + return true; +} + bool startREPL() { std::cout << "PseudoEngine2 v0.5.1 REPL\nEnter 'EXIT' to quit\n"; +#ifdef READLINE + rl_bind_key('\t', rl_tab_insert); +#endif + Lexer lexer; Parser parser; auto globalCtx = PSC::Context::createGlobalContext(); while (true) { - std::cout << "> " << std::flush; std::string code; - std::getline(std::cin, code); + if (!getLine(code, "> ")) break; size_t size = code.size(); - if (std::cin.eof()) { - std::cout << std::endl; - break; - } if (size == 0) continue; if (code == "EXIT") break; if (code.starts_with("RUNFILE")) { @@ -63,8 +88,7 @@ bool startREPL() { std::string line = " "; while (line.size() > 0) { code += "\n"; - std::cout << ". " << std::flush; - std::getline(std::cin, line); + if (!getLine(line, ". ")) return true; code += line; } break;