From fa4bd63dca2e97c0e52bf2f14b11de393efa5dc7 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 23 Aug 2023 21:39:40 +0200 Subject: [PATCH] Main: Remove step mains --- CMakeLists.txt | 28 ++---- src/{stepA_mal.cpp => repl.cpp} | 2 +- src/step0_repl.cpp | 62 ------------ src/step1_read_print.cpp | 114 --------------------- src/step2_eval.cpp | 118 ---------------------- src/step3_env.cpp | 118 ---------------------- src/step4_if_fn_do.cpp | 135 ------------------------- src/step5_tco.cpp | 141 -------------------------- src/step6_file.cpp | 161 ------------------------------ src/step7_quote.cpp | 161 ------------------------------ src/step8_macros.cpp | 168 ------------------------------- src/step9_try.cpp | 171 -------------------------------- 12 files changed, 7 insertions(+), 1372 deletions(-) rename src/{stepA_mal.cpp => repl.cpp} (98%) delete mode 100644 src/step0_repl.cpp delete mode 100644 src/step1_read_print.cpp delete mode 100644 src/step2_eval.cpp delete mode 100644 src/step3_env.cpp delete mode 100644 src/step4_if_fn_do.cpp delete mode 100644 src/step5_tco.cpp delete mode 100644 src/step6_file.cpp delete mode 100644 src/step7_quote.cpp delete mode 100644 src/step8_macros.cpp delete mode 100644 src/step9_try.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b94b9f..ec8f84a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ # User config between these lines # Set project name -set(PROJECT "stepA_mal") +set(PROJECT "blaze") if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(BLAZE_STANDALONE TRUE) @@ -64,26 +64,10 @@ add_subdirectory("vendor/ruc") # Define source files file(GLOB_RECURSE PROJECT_SOURCES "src/*.cpp") -file(GLOB_RECURSE EXCLUDED_SOURCES "src/step*.cpp") -list(REMOVE_ITEM PROJECT_SOURCES ${EXCLUDED_SOURCES}) -function(add_step TARGET_NAME MAIN_PATH) - add_executable(${TARGET_NAME} ${PROJECT_SOURCES} ${MAIN_PATH}) - target_include_directories(${TARGET_NAME} PRIVATE "src") - target_link_libraries(${TARGET_NAME} readline ruc) -endfunction() - -add_step(step0_repl "src/step0_repl.cpp") -add_step(step1_read_print "src/step1_read_print.cpp") -add_step(step2_eval "src/step2_eval.cpp") -add_step(step3_env "src/step3_env.cpp") -add_step(step4_if_fn_do "src/step4_if_fn_do.cpp") -add_step(step5_tco "src/step5_tco.cpp") -add_step(step6_file "src/step6_file.cpp") -add_step(step7_quote "src/step7_quote.cpp") -add_step(step8_macros "src/step8_macros.cpp") -add_step(step9_try "src/step9_try.cpp") -add_step(stepA_mal "src/stepA_mal.cpp") +add_executable(${PROJECT} ${PROJECT_SOURCES}) +target_include_directories(${PROJECT} PRIVATE "src") +target_link_libraries(${PROJECT} readline ruc) # ------------------------------------------ # Execute target @@ -97,8 +81,8 @@ add_custom_target(run function(make_test_target target_name step_name) add_custom_target(${target_name} - COMMAND ../vendor/mal/runtest.py --deferrable --optional ../tests/${step_name}.mal -- ./${step_name}) - add_dependencies(${target_name} ${step_name}) + COMMAND ../vendor/mal/runtest.py --deferrable --optional ../tests/${step_name}.mal -- ./${PROJECT}) + add_dependencies(${target_name} ${PROJECT}) endfunction() make_test_target("test0" "step0_repl") diff --git a/src/stepA_mal.cpp b/src/repl.cpp similarity index 98% rename from src/stepA_mal.cpp rename to src/repl.cpp index 8846cfe..cdd1bfa 100644 --- a/src/stepA_mal.cpp +++ b/src/repl.cpp @@ -158,7 +158,7 @@ auto main(int argc, char* argv[]) -> int return 0; } - rep("(println (str \"Mal [\" *host-language* \"]\"))", blaze::s_outer_env); + rep("(println (str \"Blaze [\" *host-language* \"]\"))", blaze::s_outer_env); blaze::s_readline = blaze::Readline(pretty_print, history_path); diff --git a/src/step0_repl.cpp b/src/step0_repl.cpp deleted file mode 100644 index 0c6dcc0..0000000 --- a/src/step0_repl.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include // std::cin -#include // std::getline -#include - -#include "forward.h" - -auto read(std::string_view data) -> std::string_view -{ - return data; -} - -auto eval(std::string_view data) -> std::string_view -{ - return data; -} - -auto print(std::string_view data) -> void -{ - printf("%s\n", data.data()); -} - -auto rep(std::string_view data) -> void -{ - print(eval(read(data))); -} - -auto main() -> int -{ - while (true) { - printf("user> "); - std::string line; - std::getline(std::cin, line); - - // Exit with Ctrl-D - if (std::cin.eof() || std::cin.fail()) { - break; - } - - rep(line); - } - - return 0; -} - -// Below is needed for compilation -namespace blaze { - -auto read(std::string_view) -> ValuePtr -{ - return {}; -} - -auto eval(ValuePtr, EnvironmentPtr) -> ValuePtr -{ - return {}; -} - -// Added to keep the linker happy at step A -ValuePtr readline(const std::string&) { return nullptr; } - -} // namespace blaze diff --git a/src/step1_read_print.cpp b/src/step1_read_print.cpp deleted file mode 100644 index de3fcc3..0000000 --- a/src/step1_read_print.cpp +++ /dev/null @@ -1,114 +0,0 @@ -#include // std::signal -#include // std::exit -#include // std::cin -#include // std::getline -#include - -#include "ruc/argparser.h" -#include "ruc/format/color.h" - -#include "ast.h" -#include "error.h" -#include "forward.h" -#include "lexer.h" -#include "printer.h" -#include "reader.h" -#include "settings.h" - -namespace blaze { - -auto read(std::string_view input) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr) -> ValuePtr -{ - return ast; -} - -} // namespace blaze - -auto print(blaze::ValuePtr exp) -> std::string -{ - blaze::Printer printer; - return printer.print(exp); -} - -auto rep(std::string_view input) -> void -{ - blaze::Error::the().clearErrors(); - blaze::Error::the().setInput(input); - - print("{}\n", print(blaze::eval(blaze::read(input), nullptr)).c_str()); -} - -static auto cleanup(int signal) -> void -{ - print("\033[0m"); - std::exit(signal); -} - -auto main(int argc, char* argv[]) -> int -{ - bool dump_lexer = false; - bool dump_reader = false; - bool pretty_print = false; - - // 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.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, 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); - if (pretty_print) { - print("\033[0m"); - } - - // Exit with Ctrl-D - if (std::cin.eof() || std::cin.fail()) { - break; - } - - rep(line); - } - - return 0; -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze diff --git a/src/step2_eval.cpp b/src/step2_eval.cpp deleted file mode 100644 index dbe0113..0000000 --- a/src/step2_eval.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#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" - -static blaze::EnvironmentPtr s_outer_env = blaze::Environment::create(); - -namespace blaze { - -auto read(std::string_view input) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr env) -> ValuePtr -{ - Eval eval(ast, env); - eval.eval(); - - return eval.ast(); -} - -} // namespace blaze - -auto print(blaze::ValuePtr exp) -> std::string -{ - blaze::Printer printer; - - return printer.print(exp); -} - -auto rep(std::string_view input) -> std::string -{ - blaze::Error::the().clearErrors(); - blaze::Error::the().setInput(input); - - return print(blaze::eval(blaze::read(input), s_outer_env)); -} - -static auto cleanup(int signal) -> void -{ - print("\033[0m\n"); - std::exit(signal); -} - -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, 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, cleanup); - std::signal(SIGTERM, cleanup); - - installFunctions(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)); - } - - if (pretty_print) { - print("\033[0m"); - } - - return 0; -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze diff --git a/src/step3_env.cpp b/src/step3_env.cpp deleted file mode 100644 index dbe0113..0000000 --- a/src/step3_env.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#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" - -static blaze::EnvironmentPtr s_outer_env = blaze::Environment::create(); - -namespace blaze { - -auto read(std::string_view input) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr env) -> ValuePtr -{ - Eval eval(ast, env); - eval.eval(); - - return eval.ast(); -} - -} // namespace blaze - -auto print(blaze::ValuePtr exp) -> std::string -{ - blaze::Printer printer; - - return printer.print(exp); -} - -auto rep(std::string_view input) -> std::string -{ - blaze::Error::the().clearErrors(); - blaze::Error::the().setInput(input); - - return print(blaze::eval(blaze::read(input), s_outer_env)); -} - -static auto cleanup(int signal) -> void -{ - print("\033[0m\n"); - std::exit(signal); -} - -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, 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, cleanup); - std::signal(SIGTERM, cleanup); - - installFunctions(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)); - } - - if (pretty_print) { - print("\033[0m"); - } - - return 0; -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze diff --git a/src/step4_if_fn_do.cpp b/src/step4_if_fn_do.cpp deleted file mode 100644 index 9908a26..0000000 --- a/src/step4_if_fn_do.cpp +++ /dev/null @@ -1,135 +0,0 @@ -#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" - -static blaze::EnvironmentPtr s_outer_env = blaze::Environment::create(); - -static auto cleanup(int signal) -> void; -static auto installLambdas(blaze::EnvironmentPtr env) -> void; -static auto rep(std::string_view input, blaze::EnvironmentPtr env) -> std::string; -static auto print(blaze::ValuePtr exp) -> std::string; - -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, 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, cleanup); - std::signal(SIGTERM, cleanup); - - installFunctions(s_outer_env); - installLambdas(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, s_outer_env)); - } - - if (pretty_print) { - print("\033[0m"); - } - - return 0; -} - -static auto cleanup(int signal) -> void -{ - print("\033[0m\n"); - std::exit(signal); -} - -static std::string_view lambdaTable[] = { - "(def! not (fn* (cond) (if cond false true)))", -}; - -static auto installLambdas(blaze::EnvironmentPtr env) -> void -{ - for (auto function : lambdaTable) { - rep(function, env); - } -} - -static auto rep(std::string_view input, blaze::EnvironmentPtr env) -> std::string -{ - blaze::Error::the().clearErrors(); - blaze::Error::the().setInput(input); - - return print(blaze::eval(blaze::read(input), env)); -} - -namespace blaze { - -auto read(std::string_view input) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr env) -> ValuePtr -{ - Eval eval(ast, env); - eval.eval(); - - return eval.ast(); -} - -} // namespace blaze - -static auto print(blaze::ValuePtr exp) -> std::string -{ - blaze::Printer printer; - - return printer.print(exp, true); -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze diff --git a/src/step5_tco.cpp b/src/step5_tco.cpp deleted file mode 100644 index ef6cb17..0000000 --- a/src/step5_tco.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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" - -static blaze::EnvironmentPtr s_outer_env = blaze::Environment::create(); - -static auto cleanup(int signal) -> void; -static auto installLambdas(blaze::EnvironmentPtr env) -> void; -static auto rep(std::string_view input, blaze::EnvironmentPtr env) -> std::string; -static auto print(blaze::ValuePtr exp) -> std::string; - -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, 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, cleanup); - std::signal(SIGTERM, cleanup); - - installFunctions(s_outer_env); - installLambdas(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, s_outer_env)); - } - - if (pretty_print) { - print("\033[0m"); - } - - return 0; -} - -static auto cleanup(int signal) -> void -{ - print("\033[0m\n"); - std::exit(signal); -} - -static std::string_view lambdaTable[] = { - "(def! not (fn* (cond) (if cond false true)))", -}; - -static auto installLambdas(blaze::EnvironmentPtr env) -> void -{ - for (auto function : lambdaTable) { - rep(function, env); - } -} - -static auto rep(std::string_view input, blaze::EnvironmentPtr env) -> std::string -{ - blaze::Error::the().clearErrors(); - blaze::Error::the().setInput(input); - - return print(blaze::eval(blaze::read(input), env)); -} - -namespace blaze { - -auto read(std::string_view input) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr env) -> ValuePtr -{ - Eval eval(ast, env); - eval.eval(); - - return eval.ast(); -} - -} // namespace blaze - -static auto print(blaze::ValuePtr exp) -> std::string -{ - blaze::Printer printer; - - return printer.print(exp, true); -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze diff --git a/src/step6_file.cpp b/src/step6_file.cpp deleted file mode 100644 index 3c4e421..0000000 --- a/src/step6_file.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (C) 2023 Riyyi - * - * SPDX-License-Identifier: MIT - */ - -#include // std::signal -#include // std::exit -#include -#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" - -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) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr env) -> ValuePtr -{ - if (env == nullptr) { - env = s_outer_env; - } - - Eval eval(ast, env); - eval.eval(); - - return eval.ast(); -} - -static auto print(ValuePtr 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)))", - "(def! load-file (fn* (filename) \ - (eval (read-string (str \"(do \" (slurp filename) \"\nnil)\")))))", -}; - -static auto installLambdas(EnvironmentPtr env) -> void -{ - for (auto function : lambdaTable) { - rep(function, env); - } -} - -static auto makeArgv(EnvironmentPtr env, std::vector arguments) -> void -{ - size_t count = arguments.size(); - auto nodes = ValueVector(); - for (size_t i = 1; i < count; ++i) { - nodes.push_back(makePtr(arguments[i])); - } - env->set("*ARGV*", makePtr(nodes)); -} - -} // 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 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); - arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No); - 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); - makeArgv(blaze::s_outer_env, arguments); - - if (arguments.size() > 0) { - rep(format("(load-file \"{}\")", arguments.front()), blaze::s_outer_env); - return 0; - } - - 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; -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze diff --git a/src/step7_quote.cpp b/src/step7_quote.cpp deleted file mode 100644 index 3c4e421..0000000 --- a/src/step7_quote.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (C) 2023 Riyyi - * - * SPDX-License-Identifier: MIT - */ - -#include // std::signal -#include // std::exit -#include -#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" - -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) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr env) -> ValuePtr -{ - if (env == nullptr) { - env = s_outer_env; - } - - Eval eval(ast, env); - eval.eval(); - - return eval.ast(); -} - -static auto print(ValuePtr 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)))", - "(def! load-file (fn* (filename) \ - (eval (read-string (str \"(do \" (slurp filename) \"\nnil)\")))))", -}; - -static auto installLambdas(EnvironmentPtr env) -> void -{ - for (auto function : lambdaTable) { - rep(function, env); - } -} - -static auto makeArgv(EnvironmentPtr env, std::vector arguments) -> void -{ - size_t count = arguments.size(); - auto nodes = ValueVector(); - for (size_t i = 1; i < count; ++i) { - nodes.push_back(makePtr(arguments[i])); - } - env->set("*ARGV*", makePtr(nodes)); -} - -} // 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 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); - arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No); - 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); - makeArgv(blaze::s_outer_env, arguments); - - if (arguments.size() > 0) { - rep(format("(load-file \"{}\")", arguments.front()), blaze::s_outer_env); - return 0; - } - - 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; -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze diff --git a/src/step8_macros.cpp b/src/step8_macros.cpp deleted file mode 100644 index c398676..0000000 --- a/src/step8_macros.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (C) 2023 Riyyi - * - * SPDX-License-Identifier: MIT - */ - -#include // std::signal -#include // std::exit -#include -#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" - -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) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr env) -> ValuePtr -{ - if (env == nullptr) { - env = s_outer_env; - } - - Eval eval(ast, env); - eval.eval(); - - return eval.ast(); -} - -static auto print(ValuePtr 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)))", - "(def! load-file (fn* (filename) \ - (eval (read-string (str \"(do \" (slurp filename) \"\nnil)\")))))", - "(defmacro! cond (fn* (& xs) \ - (if (> (count xs) 0) \ - (list 'if (first xs) \ - (if (> (count xs) 1) \ - (nth xs 1) \ - (throw \"odd number of forms to cond\")) \ - (cons 'cond (rest (rest xs)))))))", -}; - -static auto installLambdas(EnvironmentPtr env) -> void -{ - for (auto function : lambdaTable) { - rep(function, env); - } -} - -static auto makeArgv(EnvironmentPtr env, std::vector arguments) -> void -{ - size_t count = arguments.size(); - auto nodes = ValueVector(); - for (size_t i = 1; i < count; ++i) { - nodes.push_back(makePtr(arguments[i])); - } - env->set("*ARGV*", makePtr(nodes)); -} - -} // 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 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); - arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No); - 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); - makeArgv(blaze::s_outer_env, arguments); - - if (arguments.size() > 0) { - rep(format("(load-file \"{}\")", arguments.front()), blaze::s_outer_env); - return 0; - } - - 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; -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze diff --git a/src/step9_try.cpp b/src/step9_try.cpp deleted file mode 100644 index c8f4a06..0000000 --- a/src/step9_try.cpp +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright (C) 2023 Riyyi - * - * SPDX-License-Identifier: MIT - */ - -#include // std::signal -#include // std::exit -#include -#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" - -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) -> ValuePtr -{ - 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(ValuePtr ast, EnvironmentPtr env) -> ValuePtr -{ - if (env == nullptr) { - env = s_outer_env; - } - - Eval eval(ast, env); - eval.eval(); - - return eval.ast(); -} - -static auto print(ValuePtr 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)))", - "(def! load-file (fn* (filename) \ - (eval (read-string (str \"(do \" (slurp filename) \"\nnil)\")))))", - "(defmacro! cond (fn* (& xs) \ - (if (> (count xs) 0) \ - (list 'if (first xs) \ - (if (> (count xs) 1) \ - (nth xs 1) \ - (throw \"odd number of forms to cond\")) \ - (cons 'cond (rest (rest xs)))))))", -}; - -static auto installLambdas(EnvironmentPtr env) -> void -{ - for (auto function : lambdaTable) { - rep(function, env); - } -} - -static auto makeArgv(EnvironmentPtr env, std::vector arguments) -> void -{ - size_t count = arguments.size(); - auto nodes = ValueVector(); - for (size_t i = 1; i < count; ++i) { - nodes.push_back(makePtr(arguments[i])); - } - env->set("*ARGV*", makePtr(nodes)); -} - -} // 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 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); - arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No); - 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); - makeArgv(blaze::s_outer_env, arguments); - - if (arguments.size() > 0) { - rep(format("(load-file \"{}\")", arguments.front()), blaze::s_outer_env); - return 0; - } - - blaze::Readline readline(pretty_print, history_path); - - std::string input; - while (readline.get(input)) { - if (pretty_print) { - print("\033[0m"); - } - std::string output = rep(input, blaze::s_outer_env); - if (output.length() > 0) { - print("{}\n", output); - } - } - - if (pretty_print) { - print("\033[0m"); - } - - return 0; -} - -// Added to keep the linker happy at step A -namespace blaze { -ValuePtr readline(const std::string&) { return nullptr; } -} // namespace blaze