Browse Source

Main: Use GNU Readline library as the line editor

master
Riyyi 2 years ago
parent
commit
a92eede277
  1. 2
      CMakeLists.txt
  2. 51
      src/readline.cpp
  3. 29
      src/readline.h
  4. 41
      src/step2_eval.cpp

2
CMakeLists.txt

@ -68,7 +68,7 @@ file(GLOB_RECURSE PROJECT_SOURCES "src/*.cpp")
add_executable(${PROJECT} ${PROJECT_SOURCES}) add_executable(${PROJECT} ${PROJECT_SOURCES})
target_include_directories(${PROJECT} PRIVATE target_include_directories(${PROJECT} PRIVATE
"src") "src")
target_link_libraries(${PROJECT} ruc) target_link_libraries(${PROJECT} readline ruc)
# ------------------------------------------ # ------------------------------------------
# Execute target # Execute target

51
src/readline.cpp

@ -0,0 +1,51 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <cstdlib> // std::free
#include <iostream> // FILE
#include <readline/history.h>
#include <readline/readline.h>
#include <readline/tilde.h>
#include "ruc/format/color.h"
#include "readline.h"
namespace blaze {
Readline::Readline(bool pretty_print, std::string_view history_path)
: m_pretty_print(pretty_print)
, m_history_path(history_path)
{
if (!pretty_print) {
m_prompt = "user> ";
}
else {
m_prompt = format(fg(ruc::format::TerminalColor::Blue), "user>");
m_prompt += format(" \033[1m");
}
read_history(tilde_expand(history_path.data()));
}
bool Readline::get(std::string& output)
{
char* line = readline(m_prompt.c_str());
if (line == nullptr) {
return false;
}
// Add input to in-memory history
add_history(line);
append_history(1, m_history_path.data());
output = line;
std::free(line);
return true;
}
} // namespace blaze

29
src/readline.h

@ -0,0 +1,29 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <string>
#include <string_view>
#include "ruc/singleton.h"
namespace blaze {
class Readline {
public:
Readline(bool pretty_print, std::string_view history_path);
virtual ~Readline() {}
bool get(std::string& output);
private:
bool m_pretty_print { false };
std::string m_prompt;
std::string_view m_history_path;
};
} // namespace blaze

41
src/step2_eval.cpp

@ -1,19 +1,18 @@
#include <csignal> // std::signal #include <csignal> // std::signal
#include <cstdlib> // std::exit #include <cstdlib> // std::exit
#include <iostream> // std::cin #include <string>
#include <string> // std::getline
#include <string_view> #include <string_view>
#include "environment.h"
#include "eval.h"
#include "ruc/argparser.h" #include "ruc/argparser.h"
#include "ruc/format/color.h" #include "ruc/format/color.h"
#include "ast.h" #include "ast.h"
#include "error.h" #include "environment.h"
#include "eval.h"
#include "lexer.h" #include "lexer.h"
#include "printer.h" #include "printer.h"
#include "reader.h" #include "reader.h"
#include "readline.h"
#include "settings.h" #include "settings.h"
#if 1 #if 1
@ -58,7 +57,7 @@ auto rep(std::string_view input) -> void
static auto cleanup(int signal) -> void static auto cleanup(int signal) -> void
{ {
print("\033[0m"); print("\033[0m\n");
std::exit(signal); std::exit(signal);
} }
@ -67,12 +66,14 @@ auto main(int argc, char* argv[]) -> int
bool dump_lexer = false; bool dump_lexer = false;
bool dump_reader = false; bool dump_reader = false;
bool pretty_print = false; bool pretty_print = false;
std::string_view history_path = "~/.mal-history";
// CLI arguments // CLI arguments
ruc::ArgParser arg_parser; ruc::ArgParser arg_parser;
arg_parser.addOption(dump_lexer, 'l', "dump-lexer", nullptr, nullptr); arg_parser.addOption(dump_lexer, 'l', "dump-lexer", nullptr, nullptr);
arg_parser.addOption(dump_reader, 'r', "dump-reader", nullptr, nullptr); arg_parser.addOption(dump_reader, 'r', "dump-reader", nullptr, nullptr);
arg_parser.addOption(pretty_print, 'c', "color", nullptr, nullptr); arg_parser.addOption(pretty_print, 'c', "color", nullptr, nullptr);
arg_parser.addOption(history_path, 'h', "history", nullptr, nullptr);
arg_parser.parse(argc, argv); arg_parser.parse(argc, argv);
// Set settings // Set settings
@ -84,26 +85,18 @@ auto main(int argc, char* argv[]) -> int
std::signal(SIGINT, cleanup); std::signal(SIGINT, cleanup);
std::signal(SIGTERM, cleanup); std::signal(SIGTERM, cleanup);
while (true) { blaze::Readline readline(pretty_print, history_path);
if (!pretty_print) {
print("user> "); std::string input;
} while (readline.get(input)) {
else {
print(fg(ruc::format::TerminalColor::Blue), "user>");
print(" \033[1m");
}
std::string line;
std::getline(std::cin, line);
if (pretty_print) { if (pretty_print) {
print("\033[0m"); print("\033[0m");
} }
rep(input);
}
// Exit with Ctrl-D if (pretty_print) {
if (std::cin.eof() || std::cin.fail()) { print("\033[0m");
break;
}
rep(line);
} }
return 0; return 0;

Loading…
Cancel
Save