From 49dc7d0ebb88b0beacdd9398eff977df0282c2b1 Mon Sep 17 00:00:00 2001 From: Username404 Date: Fri, 8 Apr 2022 19:44:57 +0200 Subject: [PATCH] main.cpp: Use a map instead of a set and a vector in main.cpp Target.hpp: make staticMap thread-local to avoid issues with the "--parallel" argument, and don't include dots in the languages array Signed-off-by: Username404 --- src/headers/transpiler/Target.hpp | 4 +- src/main.cpp | 84 +++++++++++++------------------ 2 files changed, 38 insertions(+), 50 deletions(-) diff --git a/src/headers/transpiler/Target.hpp b/src/headers/transpiler/Target.hpp index c649356..aee6af8 100644 --- a/src/headers/transpiler/Target.hpp +++ b/src/headers/transpiler/Target.hpp @@ -123,7 +123,7 @@ protected: static constexpr const char* indentation = " "; public: const unordered_task_map& getTaskMapInstance() { - static unordered_task_map staticMap = getTaskMap(); + static thread_local unordered_task_map staticMap = getTaskMap(); // Default / Shared tasks: staticMap.merge(unordered_task_map({ make_task(ParseTree, transpileTree(parseComponent);), @@ -155,7 +155,7 @@ public: #include "implementations/Py.hpp" enum LANGUAGE: signed short { NONE = -1, LUA, JS, PY }; -constinit const array languages { ".lua", ".js", ".py" }; +constinit const array languages { "lua", "js", "py" }; shared_ptr Target::forName(string_view name, const bool newLines = true) { LANGUAGE selected = NONE; diff --git a/src/main.cpp b/src/main.cpp index 031c72e..2f16f21 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include "headers/Yerbacon.hpp" #include #include @@ -11,18 +11,26 @@ using namespace std; int main(int argc, char* argv[]) { setlocale(LC_ALL, ""); - string target = ".lua"; + string target = "lua"; bool printResult = false, parallel = false, newLines = true; - set files; + using unit_result = pair>; + using unit = future; + map Units; + const auto compile = [&target, &newLines](string_view name) -> string { + string transpiledString = Target::forName(target, newLines)->transpileWithTree(parseString(getFileContent(name.data()))); + name.remove_suffix(6); + outputFileContent(string(name) + '.' + target, transpiledString); + return transpiledString; + }; for (signed int i = 1; i < argc; ++i) { const string_view currentArg (argv[i]); if (currentArg == ArgumentShort("printresult")) printResult = true; else if (currentArg == ArgumentAssignable("target")) { const string_view value = ArgumentAssignable::getValueFor(currentArg); - if (not value.empty()) (target = '.') += value; + if (not value.empty()) target = value; else Yerbacon::fail("No target was provided."); } else if (currentArg == Argument("parallel")) parallel = true; @@ -34,8 +42,25 @@ int main(int argc, char* argv[]) { newLines = true; } else goto invalid_argument; } - else if (currentArg.ends_with(".ybcon")) files.insert(currentArg); - else { + else if (currentArg.ends_with(".ybcon")) Units.insert_or_assign(currentArg, async(not parallel ? launch::deferred : launch::async, [fileName = currentArg, &compile]() { + unit_result resultingPair; + try { + resultingPair.first = compile(fileName); + } catch (const Yerbacon::Exception& error) { + size_t lastSlash = 0; + const size_t position1 = fileName.find_last_of('/'); + if (cmp_not_equal(position1, string_view::npos)) lastSlash = position1; + if constexpr(filesystem::path::preferred_separator != '/') { + const size_t position2 = fileName.find_last_of(filesystem::path::preferred_separator); + if (cmp_not_equal(position2, string_view::npos)) { + lastSlash = max(lastSlash, position2); + } + } + resultingPair.first = fileName.substr(lastSlash + 1); + resultingPair.second.emplace(error); + } + return resultingPair; + })); else { if (argc == 2) { if (currentArg == Argument("version")) { cout << Yerbacon::getVersion(); @@ -46,45 +71,10 @@ int main(int argc, char* argv[]) { } else invalid_argument: Yerbacon::fail({"\"", currentArg.data(), "\" is not a valid argument."}); } } - const auto current_target = Target::forName(target, newLines); - const auto compile = [&target, current_target](string_view name) -> string { - string transpiledString = current_target->transpileWithTree(parseString(getFileContent(name.data()))); - name.remove_suffix(6); - string outputFile; - (outputFile = name).append(target); - outputFileContent(outputFile, transpiledString); - return transpiledString; - }; - int8_t exit_code = EXIT_SUCCESS; - if (!files.empty()) { - using unit_result = pair>; - using unit = future; - vector Units(files.size()); - const launch& Policy = not parallel ? launch::deferred : launch::async; - transform(files.cbegin(), files.cend(), Units.begin(), [&Policy, &compile](const string_view& fileName){ - return async(Policy, [&fileName, &compile]() { - unit_result resultingPair; - try { - resultingPair.first = compile(fileName); - } catch (const Yerbacon::Exception& error) { - size_t lastSlash = 0; - const size_t position1 = fileName.find_last_of('/'); - if (cmp_not_equal(position1, string_view::npos)) lastSlash = position1; - if constexpr(filesystem::path::preferred_separator != '/') { - const size_t position2 = fileName.find_last_of(filesystem::path::preferred_separator); - if (cmp_not_equal(position2, string_view::npos)) { - lastSlash = max(lastSlash, position2); - } - } - resultingPair.first = fileName.substr(lastSlash + 1); - resultingPair.second.emplace(error); - } - return resultingPair; - }); - }); + if (!Units.empty()) { if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n"; - if (any_of(Units.rbegin(), Units.rend(), [&printResult](unit& currentFuture) -> bool { - const pair result = currentFuture.get(); + for (auto entry_iterator = Units.rbegin(); entry_iterator != Units.rend(); ++entry_iterator) { + const pair result = entry_iterator->second.get(); const bool is_exception = result.second.has_value(); if (not is_exception) { if (printResult && !(result.first.empty() || all_of(result.first.begin(), result.first.end(), [](const char& character){ @@ -95,11 +85,9 @@ int main(int argc, char* argv[]) { } else { cout << "Compilation of " << result.first << " has failed with the following error:" << endl; cerr << result.second.value().what() << '\n'; + return EXIT_FAILURE; } - return is_exception; - })) { - exit_code = EXIT_FAILURE; } } else cout << "No valid file provided.\n"; - return exit_code; + return EXIT_SUCCESS; } \ No newline at end of file