Skip to content

Commit

Permalink
Use readline library
Browse files Browse the repository at this point in the history
  • Loading branch information
SingularityT3 committed Nov 5, 2023
1 parent 0ba8a8b commit d77fa65
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
40 changes: 32 additions & 8 deletions src/launch/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <string>
#include "launch/run.h"

#ifdef READLINE
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#endif

extern std::string psfilename;
extern bool REPLMode;

Expand All @@ -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")) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d77fa65

Please sign in to comment.