From ed9fa1698e0aa6a120def0011529aca48366c274 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Tue, 4 Apr 2023 22:40:54 +0200 Subject: [PATCH] Main: Add step6 .cpp file --- CMakeLists.txt | 4 ++ src/forward.h | 3 + src/step5_tco.cpp | 2 +- src/step6_file.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/step6_file.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a2848cb..140730c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,3 +103,7 @@ add_dependencies(test4 ${PROJECT}) add_custom_target(test5 COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step5_tco.mal -- ./${PROJECT}) add_dependencies(test5 ${PROJECT}) + +add_custom_target(test6 + COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step6_file.mal -- ./${PROJECT}) +add_dependencies(test6 ${PROJECT}) diff --git a/src/forward.h b/src/forward.h index ae778fb..e240ee7 100644 --- a/src/forward.h +++ b/src/forward.h @@ -22,6 +22,9 @@ typedef std::shared_ptr EnvironmentPtr; // ----------------------------------------- // Functions +extern ASTNodePtr read(std::string_view input); +ASTNodePtr eval(ASTNodePtr ast, EnvironmentPtr env); + extern void installFunctions(EnvironmentPtr env); } // namespace blaze diff --git a/src/step5_tco.cpp b/src/step5_tco.cpp index 492eaf0..514afb7 100644 --- a/src/step5_tco.cpp +++ b/src/step5_tco.cpp @@ -23,7 +23,7 @@ #include "readline.h" #include "settings.h" -#if 1 +#if 0 static blaze::EnvironmentPtr s_outer_env = blaze::Environment::create(); static auto cleanup(int signal) -> void; diff --git a/src/step6_file.cpp b/src/step6_file.cpp new file mode 100644 index 0000000..08d80db --- /dev/null +++ b/src/step6_file.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2023 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#include // std::signal +#include // std::exit +#include +#include + +#include "ruc/argparser.h" +#include "ruc/format/color.h" + +#include "ast.h" +#include "environment.h" +#include "error.h" +#include "eval.h" +#include "forward.h" +#include "lexer.h" +#include "printer.h" +#include "reader.h" +#include "readline.h" +#include "settings.h" + +#if 1 +namespace blaze { + +static EnvironmentPtr s_outer_env = Environment::create(); + +static auto cleanup(int signal) -> void +{ + print("\033[0m\n"); + std::exit(signal); +} + +auto read(std::string_view input) -> ASTNodePtr +{ + Lexer lexer(input); + lexer.tokenize(); + if (Settings::the().get("dump-lexer") == "1") { + lexer.dump(); + } + + Reader reader(std::move(lexer.tokens())); + reader.read(); + if (Settings::the().get("dump-reader") == "1") { + reader.dump(); + } + + return reader.node(); +} + +auto eval(ASTNodePtr ast, EnvironmentPtr env) -> ASTNodePtr +{ + Eval eval(ast, env); + eval.eval(); + + return eval.ast(); +} + +static auto print(ASTNodePtr exp) -> std::string +{ + Printer printer; + + return printer.print(exp, true); +} + +static auto rep(std::string_view input, EnvironmentPtr env) -> std::string +{ + Error::the().clearErrors(); + Error::the().setInput(input); + + return print(eval(read(input), env)); +} + +static std::string_view lambdaTable[] = { + "(def! not (fn* (cond) (if cond false true)))", +}; + +static auto installLambdas(EnvironmentPtr env) -> void +{ + for (auto function : lambdaTable) { + rep(function, env); + } +} + +} // 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"; + + // 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); + arg_parser.parse(argc, argv); + + // Set settings + blaze::Settings::the().set("dump-lexer", dump_lexer ? "1" : "0"); + blaze::Settings::the().set("dump-reader", dump_reader ? "1" : "0"); + blaze::Settings::the().set("pretty-print", pretty_print ? "1" : "0"); + + // Signal callbacks + std::signal(SIGINT, blaze::cleanup); + std::signal(SIGTERM, blaze::cleanup); + + installFunctions(blaze::s_outer_env); + installLambdas(blaze::s_outer_env); + + blaze::Readline readline(pretty_print, history_path); + + std::string input; + while (readline.get(input)) { + if (pretty_print) { + print("\033[0m"); + } + print("{}\n", rep(input, blaze::s_outer_env)); + } + + if (pretty_print) { + print("\033[0m"); + } + + return 0; +} +#endif