Compare commits
2
Commits
9271b9fe01
...
a92eede277
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a92eede277 | ||
|
|
9fa6314378 |
+1
-1
@@ -68,7 +68,7 @@ file(GLOB_RECURSE PROJECT_SOURCES "src/*.cpp")
|
||||
add_executable(${PROJECT} ${PROJECT_SOURCES})
|
||||
target_include_directories(${PROJECT} PRIVATE
|
||||
"src")
|
||||
target_link_libraries(${PROJECT} ruc)
|
||||
target_link_libraries(${PROJECT} readline ruc)
|
||||
|
||||
# ------------------------------------------
|
||||
# Execute target
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ struct Token {
|
||||
At, // @
|
||||
String, // "foobar"
|
||||
Keyword, // :keyword
|
||||
Value, // numbers, "true", "false", and "nil", symbols
|
||||
Value, // number, "true", "false", "nil", symbol
|
||||
Comment, // ;
|
||||
Error,
|
||||
};
|
||||
|
||||
+17
-37
@@ -38,32 +38,18 @@ void Reader::read()
|
||||
|
||||
m_node = readImpl();
|
||||
|
||||
// TODO: Move these to the appropriate functions
|
||||
// Error checking
|
||||
|
||||
if (m_invalid_syntax) {
|
||||
Error::the().addError("invalid read syntax: '" + std::string(1, m_error_character) + "'");
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_is_unbalanced) {
|
||||
Error::the().addError("expected '" + std::string(1, m_error_character) + "', got EOF");
|
||||
if (Error::the().hasOtherError()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for multiple expressions
|
||||
if (!isEOF()) {
|
||||
Token::Type type = peek().type;
|
||||
switch (type) {
|
||||
case Token::Type::ParenOpen: // (
|
||||
case Token::Type::ParenClose: // )
|
||||
case Token::Type::String:
|
||||
case Token::Type::Value:
|
||||
Error::the().addError("more than one sexp in input");
|
||||
break;
|
||||
case Token::Type::Comment:
|
||||
break;
|
||||
default:
|
||||
Error::the().addError("unknown error");
|
||||
Error::the().addError("more than one sexp in input");
|
||||
break;
|
||||
};
|
||||
}
|
||||
@@ -79,28 +65,25 @@ ASTNode* Reader::readImpl()
|
||||
case Token::Type::Special: // ~@
|
||||
return readSpliceUnquote();
|
||||
break;
|
||||
case Token::Type::ParenOpen: // (
|
||||
return readList();
|
||||
break;
|
||||
case Token::Type::ParenClose: // )
|
||||
Error::the().addError("invalid read syntax: ')'");
|
||||
return nullptr;
|
||||
break;
|
||||
case Token::Type::BracketOpen: // [
|
||||
return readVector();
|
||||
break;
|
||||
case Token::Type::BracketClose: // ]
|
||||
m_invalid_syntax = true;
|
||||
m_error_character = ']';
|
||||
Error::the().addError("invalid read syntax: ']'");
|
||||
return nullptr;
|
||||
break;
|
||||
case Token::Type::BraceOpen: // {
|
||||
return readHashMap();
|
||||
break;
|
||||
case Token::Type::BraceClose: // }
|
||||
m_invalid_syntax = true;
|
||||
m_error_character = '}';
|
||||
return nullptr;
|
||||
break;
|
||||
case Token::Type::ParenOpen: // (
|
||||
return readList();
|
||||
break;
|
||||
case Token::Type::ParenClose: // )
|
||||
m_invalid_syntax = true;
|
||||
m_error_character = ')';
|
||||
Error::the().addError("invalid read syntax: '}'");
|
||||
return nullptr;
|
||||
break;
|
||||
case Token::Type::Quote: // '
|
||||
@@ -166,8 +149,8 @@ ASTNode* Reader::readList()
|
||||
}
|
||||
|
||||
if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // )
|
||||
m_error_character = ')';
|
||||
m_is_unbalanced = true;
|
||||
Error::the().addError("expected ')', got EOF");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return list;
|
||||
@@ -183,8 +166,7 @@ ASTNode* Reader::readVector()
|
||||
}
|
||||
|
||||
if (!consumeSpecific(Token { .type = Token::Type::BracketClose })) { // ]
|
||||
m_error_character = ']';
|
||||
m_is_unbalanced = true;
|
||||
Error::the().addError("expected ']', got EOF");
|
||||
}
|
||||
|
||||
return vector;
|
||||
@@ -218,8 +200,7 @@ ASTNode* Reader::readHashMap()
|
||||
}
|
||||
|
||||
if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // }
|
||||
m_error_character = '}';
|
||||
m_is_unbalanced = true;
|
||||
Error::the().addError("expected '}', got EOF");
|
||||
}
|
||||
|
||||
return hash_map;
|
||||
@@ -316,8 +297,7 @@ ASTNode* Reader::readString()
|
||||
|
||||
// Unbalanced string
|
||||
if (symbol.size() < 2 || symbol.front() != '"' || symbol.back() != '"') {
|
||||
m_error_character = '"';
|
||||
m_is_unbalanced = true;
|
||||
Error::the().addError("expected '\"', got EOF");
|
||||
}
|
||||
|
||||
return new String(symbol);
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ private:
|
||||
ASTNode* readDeref(); // @
|
||||
ASTNode* readString(); // "foobar"
|
||||
ASTNode* readKeyword(); // :keyword
|
||||
ASTNode* readValue(); // true, false, nil
|
||||
ASTNode* readValue(); // number, "true", "false", "nil", symbol
|
||||
|
||||
void dumpImpl(ASTNode* node);
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
+14
-21
@@ -1,19 +1,18 @@
|
||||
#include <csignal> // std::signal
|
||||
#include <cstdlib> // std::exit
|
||||
#include <iostream> // std::cin
|
||||
#include <string> // std::getline
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "environment.h"
|
||||
#include "eval.h"
|
||||
#include "ruc/argparser.h"
|
||||
#include "ruc/format/color.h"
|
||||
|
||||
#include "ast.h"
|
||||
#include "error.h"
|
||||
#include "environment.h"
|
||||
#include "eval.h"
|
||||
#include "lexer.h"
|
||||
#include "printer.h"
|
||||
#include "reader.h"
|
||||
#include "readline.h"
|
||||
#include "settings.h"
|
||||
|
||||
#if 1
|
||||
@@ -58,7 +57,7 @@ auto rep(std::string_view input) -> void
|
||||
|
||||
static auto cleanup(int signal) -> void
|
||||
{
|
||||
print("\033[0m");
|
||||
print("\033[0m\n");
|
||||
std::exit(signal);
|
||||
}
|
||||
|
||||
@@ -67,12 +66,14 @@ auto main(int argc, char* argv[]) -> int
|
||||
bool dump_lexer = false;
|
||||
bool dump_reader = false;
|
||||
bool pretty_print = false;
|
||||
std::string_view history_path = "~/.mal-history";
|
||||
|
||||
// CLI arguments
|
||||
ruc::ArgParser arg_parser;
|
||||
arg_parser.addOption(dump_lexer, 'l', "dump-lexer", nullptr, nullptr);
|
||||
arg_parser.addOption(dump_reader, 'r', "dump-reader", 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);
|
||||
|
||||
// Set settings
|
||||
@@ -84,26 +85,18 @@ auto main(int argc, char* argv[]) -> int
|
||||
std::signal(SIGINT, cleanup);
|
||||
std::signal(SIGTERM, cleanup);
|
||||
|
||||
while (true) {
|
||||
if (!pretty_print) {
|
||||
print("user> ");
|
||||
}
|
||||
else {
|
||||
print(fg(ruc::format::TerminalColor::Blue), "user>");
|
||||
print(" \033[1m");
|
||||
}
|
||||
std::string line;
|
||||
std::getline(std::cin, line);
|
||||
blaze::Readline readline(pretty_print, history_path);
|
||||
|
||||
std::string input;
|
||||
while (readline.get(input)) {
|
||||
if (pretty_print) {
|
||||
print("\033[0m");
|
||||
}
|
||||
|
||||
// Exit with Ctrl-D
|
||||
if (std::cin.eof() || std::cin.fail()) {
|
||||
break;
|
||||
rep(input);
|
||||
}
|
||||
|
||||
rep(line);
|
||||
if (pretty_print) {
|
||||
print("\033[0m");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user