Browse Source

Repl+Env+Example: Load global outer environment in Repl::init

master
Riyyi 1 year ago
parent
commit
092ada8479
  1. 21
      example/src/main.cpp
  2. 3
      src/blaze/env/environment.cpp
  3. 14
      src/blaze/repl.cpp
  4. 4
      src/blaze/repl.h

21
example/src/main.cpp

@ -21,6 +21,13 @@
namespace blaze { namespace blaze {
static auto cleanup(int signal) -> void
{
print("\033[0m\n");
Repl::cleanup();
std::exit(signal);
}
auto main(int argc, char* argv[]) -> int auto main(int argc, char* argv[]) -> int
{ {
bool dump_lexer = false; bool dump_lexer = false;
@ -39,17 +46,17 @@ auto main(int argc, char* argv[]) -> int
arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No); arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No);
arg_parser.parse(argc, argv); arg_parser.parse(argc, argv);
Repl::init();
// Signal callbacks
std::signal(SIGINT, cleanup);
std::signal(SIGTERM, cleanup);
// Set settings // Set settings
g_outer_env->set("*DUMP-LEXER*", makePtr<Constant>(dump_lexer)); g_outer_env->set("*DUMP-LEXER*", makePtr<Constant>(dump_lexer));
g_outer_env->set("*DUMP-READER*", makePtr<Constant>(dump_reader)); g_outer_env->set("*DUMP-READER*", makePtr<Constant>(dump_reader));
g_outer_env->set("*PRETTY-PRINT*", makePtr<Constant>(pretty_print)); 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); Repl::makeArgv(g_outer_env, arguments);
if (arguments.size() > 0) { if (arguments.size() > 0) {
@ -73,6 +80,8 @@ auto main(int argc, char* argv[]) -> int
print("\033[0m"); print("\033[0m");
} }
Repl::cleanup();
return 0; return 0;
} }

3
src/blaze/env/environment.cpp vendored

@ -79,6 +79,9 @@ EnvironmentPtr Environment::create(const ValuePtr lambda, ValueVector&& argument
void Environment::loadFunctions() void Environment::loadFunctions()
{ {
s_function_parts.clear();
s_lambdas.clear();
loadCollectionAccess(); loadCollectionAccess();
loadCollectionConstructor(); loadCollectionConstructor();
loadCollectionModify(); loadCollectionModify();

14
src/blaze/repl.cpp

@ -25,12 +25,18 @@
namespace blaze { namespace blaze {
Readline g_readline; Readline g_readline;
EnvironmentPtr g_outer_env = Environment::create(); EnvironmentPtr g_outer_env;
auto Repl::cleanup(int signal) -> void auto Repl::init() -> void
{ {
::print("\033[0m\n"); g_outer_env = Environment::create();
std::exit(signal); Environment::loadFunctions();
Environment::installFunctions(g_outer_env);
}
auto Repl::cleanup() -> void
{
g_outer_env = nullptr;
} }
auto Repl::readline(const std::string& prompt) -> ValuePtr auto Repl::readline(const std::string& prompt) -> ValuePtr

4
src/blaze/repl.h

@ -17,7 +17,9 @@ namespace blaze {
class Repl { class Repl {
public: public:
static auto cleanup(int signal) -> void; static auto init() -> void;
static auto cleanup() -> void;
static auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr; static auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr;
static auto makeArgv(EnvironmentPtr env, std::vector<std::string> arguments) -> void; static auto makeArgv(EnvironmentPtr env, std::vector<std::string> arguments) -> void;
static auto print(ValuePtr value) -> std::string; static auto print(ValuePtr value) -> std::string;

Loading…
Cancel
Save