Skip to content

Commit

Permalink
Add readline support to REPL
Browse files Browse the repository at this point in the history
This has per-session history support, searching with ^R, and
modifications with chords like ^W. History is not yet persistent to
disk. It also doesn't yet support multi-line inputs.
  • Loading branch information
jeaye committed Oct 20, 2023
1 parent 0f632ac commit 229f423
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ find_package(CLI11 CONFIG REQUIRED)

target_include_directories(jank_lib SYSTEM PRIVATE ${Boost_INCLUDE_DIRS})
target_include_directories(jank_lib SYSTEM PRIVATE ${CLING_INCLUDE_DIRS})
target_include_directories(jank_lib SYSTEM PRIVATE /home/jeaye/projects/jank/build/cling/lib)
target_include_directories(jank_lib SYSTEM PRIVATE ${CLANG_INCLUDE_DIRS})
target_include_directories(jank_lib SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS})

Expand All @@ -222,6 +221,7 @@ target_link_libraries(
BDWgc::gc BDWgc::cord BDWgc::gccpp BDWgc::gctba
libzippp::libzippp
CLI11::CLI11
readline
)

set_target_properties(jank_lib PROPERTIES LINK_FLAGS_RELEASE "-s")
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/jank/jit/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <cling/Interpreter/Value.h>
#include <clang/AST/Type.h>
//#include <Interpreter/IncrementalExecutor.h>
//#include <llvm/Support/MemoryBuffer.h>

#include <jank/util/process_location.hpp>
#include <jank/util/make_array.hpp>
Expand Down
23 changes: 16 additions & 7 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <filesystem>

#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>

#include <cling/Interpreter/Interpreter.h>
#include <cling/Interpreter/Value.h>
Expand All @@ -10,7 +11,8 @@

#include <CLI/CLI.hpp>

#include <jank/runtime/detail/object_util.hpp>
#include <readline/readline.h>
#include <readline/history.h>

#include <jank/util/mapped_file.hpp>
#include <jank/read/lex.hpp>
Expand Down Expand Up @@ -74,13 +76,22 @@ namespace jank
rt_ctx.load_module("clojure.core").expect_ok();
}

/* TODO: Readline with history. */
/* By default, RL will do tab completion for files. We disable that here. */
rl_bind_key('\t', rl_insert);

/* TODO: Completion. */
/* TODO: Syntax highlighting. */
std::string line;
std::cout << "> " << std::flush;
while(std::getline(std::cin, line))
/* TODO: Multi-line input. */
while(auto const buf = readline("> "))
{
native_string line{ buf };
boost::trim(line);
if(line.empty())
{ continue; }

/* TODO: Persist history to disk, á la .lein-repl-history. */
/* History is persisted for this session only. */
add_history(line.data());
try
{
auto const res(rt_ctx.eval_string(line));
Expand All @@ -95,8 +106,6 @@ namespace jank
{ fmt::println("Exception: {}", s); }
catch(jank::read::error const &e)
{ fmt::println("Read error: {}", e.message); }

std::cout << "> " << std::flush;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"immer",
"libguarded",
"libzippp",
"magic-enum"
"magic-enum",
"readline"
],
"default-features": [],
"features": {
Expand Down

0 comments on commit 229f423

Please sign in to comment.