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})
|
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
|
||||||
|
|||||||
+1
-1
@@ -33,7 +33,7 @@ struct Token {
|
|||||||
At, // @
|
At, // @
|
||||||
String, // "foobar"
|
String, // "foobar"
|
||||||
Keyword, // :keyword
|
Keyword, // :keyword
|
||||||
Value, // numbers, "true", "false", and "nil", symbols
|
Value, // number, "true", "false", "nil", symbol
|
||||||
Comment, // ;
|
Comment, // ;
|
||||||
Error,
|
Error,
|
||||||
};
|
};
|
||||||
|
|||||||
+17
-37
@@ -38,32 +38,18 @@ void Reader::read()
|
|||||||
|
|
||||||
m_node = readImpl();
|
m_node = readImpl();
|
||||||
|
|
||||||
// TODO: Move these to the appropriate functions
|
if (Error::the().hasOtherError()) {
|
||||||
// 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");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for multiple expressions
|
||||||
if (!isEOF()) {
|
if (!isEOF()) {
|
||||||
Token::Type type = peek().type;
|
Token::Type type = peek().type;
|
||||||
switch (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:
|
case Token::Type::Comment:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Error::the().addError("unknown error");
|
Error::the().addError("more than one sexp in input");
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -79,28 +65,25 @@ ASTNode* Reader::readImpl()
|
|||||||
case Token::Type::Special: // ~@
|
case Token::Type::Special: // ~@
|
||||||
return readSpliceUnquote();
|
return readSpliceUnquote();
|
||||||
break;
|
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: // [
|
case Token::Type::BracketOpen: // [
|
||||||
return readVector();
|
return readVector();
|
||||||
break;
|
break;
|
||||||
case Token::Type::BracketClose: // ]
|
case Token::Type::BracketClose: // ]
|
||||||
m_invalid_syntax = true;
|
Error::the().addError("invalid read syntax: ']'");
|
||||||
m_error_character = ']';
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
break;
|
break;
|
||||||
case Token::Type::BraceOpen: // {
|
case Token::Type::BraceOpen: // {
|
||||||
return readHashMap();
|
return readHashMap();
|
||||||
break;
|
break;
|
||||||
case Token::Type::BraceClose: // }
|
case Token::Type::BraceClose: // }
|
||||||
m_invalid_syntax = true;
|
Error::the().addError("invalid read syntax: '}'");
|
||||||
m_error_character = '}';
|
|
||||||
return nullptr;
|
|
||||||
break;
|
|
||||||
case Token::Type::ParenOpen: // (
|
|
||||||
return readList();
|
|
||||||
break;
|
|
||||||
case Token::Type::ParenClose: // )
|
|
||||||
m_invalid_syntax = true;
|
|
||||||
m_error_character = ')';
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
break;
|
break;
|
||||||
case Token::Type::Quote: // '
|
case Token::Type::Quote: // '
|
||||||
@@ -166,8 +149,8 @@ ASTNode* Reader::readList()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // )
|
if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // )
|
||||||
m_error_character = ')';
|
Error::the().addError("expected ')', got EOF");
|
||||||
m_is_unbalanced = true;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
@@ -183,8 +166,7 @@ ASTNode* Reader::readVector()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!consumeSpecific(Token { .type = Token::Type::BracketClose })) { // ]
|
if (!consumeSpecific(Token { .type = Token::Type::BracketClose })) { // ]
|
||||||
m_error_character = ']';
|
Error::the().addError("expected ']', got EOF");
|
||||||
m_is_unbalanced = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return vector;
|
return vector;
|
||||||
@@ -218,8 +200,7 @@ ASTNode* Reader::readHashMap()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // }
|
if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // }
|
||||||
m_error_character = '}';
|
Error::the().addError("expected '}', got EOF");
|
||||||
m_is_unbalanced = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return hash_map;
|
return hash_map;
|
||||||
@@ -316,8 +297,7 @@ ASTNode* Reader::readString()
|
|||||||
|
|
||||||
// Unbalanced string
|
// Unbalanced string
|
||||||
if (symbol.size() < 2 || symbol.front() != '"' || symbol.back() != '"') {
|
if (symbol.size() < 2 || symbol.front() != '"' || symbol.back() != '"') {
|
||||||
m_error_character = '"';
|
Error::the().addError("expected '\"', got EOF");
|
||||||
m_is_unbalanced = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new String(symbol);
|
return new String(symbol);
|
||||||
|
|||||||
+1
-1
@@ -46,7 +46,7 @@ private:
|
|||||||
ASTNode* readDeref(); // @
|
ASTNode* readDeref(); // @
|
||||||
ASTNode* readString(); // "foobar"
|
ASTNode* readString(); // "foobar"
|
||||||
ASTNode* readKeyword(); // :keyword
|
ASTNode* readKeyword(); // :keyword
|
||||||
ASTNode* readValue(); // true, false, nil
|
ASTNode* readValue(); // number, "true", "false", "nil", symbol
|
||||||
|
|
||||||
void dumpImpl(ASTNode* node);
|
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 <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 (std::cin.eof() || std::cin.fail()) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rep(line);
|
if (pretty_print) {
|
||||||
|
print("\033[0m");
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user