Riyyi
1 year ago
16 changed files with 186 additions and 107 deletions
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Riyyi |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#include <csignal> // std::signal |
||||
#include <string> |
||||
#include <string_view> |
||||
#include <vector> |
||||
|
||||
#include "ruc/argparser.h" |
||||
#include "ruc/format/color.h" |
||||
#include "ruc/format/print.h" |
||||
|
||||
#include "ast.h" |
||||
#include "env/environment.h" |
||||
#include "forward.h" |
||||
#include "repl.h" |
||||
#include "settings.h" |
||||
|
||||
namespace blaze { |
||||
|
||||
auto main(int argc, char* argv[]) -> int |
||||
{ |
||||
bool dump_lexer = false; |
||||
bool dump_reader = false; |
||||
bool pretty_print = false; |
||||
std::string_view history_path = "~/.blaze-history"; |
||||
std::vector<std::string> arguments; |
||||
|
||||
// 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-path", nullptr, nullptr, nullptr, ruc::ArgParser::Required::Yes); |
||||
// TODO: Add overload for addArgument(std::vector<std::string_view>)
|
||||
arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No); |
||||
arg_parser.parse(argc, argv); |
||||
|
||||
// Set settings
|
||||
g_outer_env->set("*DUMP-LEXER*", makePtr<Constant>(dump_lexer)); |
||||
g_outer_env->set("*DUMP-READER*", makePtr<Constant>(dump_reader)); |
||||
g_outer_env->set("*PRETTY-PRINT*", makePtr<Constant>(pretty_print)); |
||||
|
||||
// Signal callbacks
|
||||
std::signal(SIGINT, Repl::cleanup); |
||||
std::signal(SIGTERM, Repl::cleanup); |
||||
|
||||
Environment::loadFunctions(); |
||||
Environment::installFunctions(g_outer_env); |
||||
Repl::makeArgv(g_outer_env, arguments); |
||||
|
||||
if (arguments.size() > 0) { |
||||
Repl::rep(format("(load-file \"{}\")", arguments.front()), g_outer_env); |
||||
return 0; |
||||
} |
||||
|
||||
Repl::rep("(println (str \"Blaze [\" *host-language* \"]\"))", g_outer_env); |
||||
|
||||
g_readline = Readline(pretty_print, history_path); |
||||
|
||||
std::string input; |
||||
while (g_readline.get(input)) { |
||||
std::string output = Repl::rep(input, g_outer_env); |
||||
if (output.length() > 0) { |
||||
print("{}\n", output); |
||||
} |
||||
} |
||||
|
||||
if (pretty_print) { |
||||
print("\033[0m"); |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
} // namespace blaze
|
||||
|
||||
auto main(int argc, char* argv[]) -> int |
||||
{ |
||||
return blaze::main(argc, argv); |
||||
} |
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Riyyi |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
#include <string_view> |
||||
#include <vector> |
||||
|
||||
#include "forward.h" |
||||
#include "readline.h" |
||||
|
||||
namespace blaze { |
||||
|
||||
class Repl { |
||||
public: |
||||
static auto cleanup(int signal) -> void; |
||||
static auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr; |
||||
static auto makeArgv(EnvironmentPtr env, std::vector<std::string> arguments) -> void; |
||||
static auto print(ValuePtr value) -> std::string; |
||||
static auto read(std::string_view input) -> ValuePtr; |
||||
static auto readline(const std::string& prompt) -> ValuePtr; |
||||
static auto rep(std::string_view input, EnvironmentPtr env) -> std::string; |
||||
}; |
||||
|
||||
} // namespace blaze
|
Loading…
Reference in new issue