diff --git a/src/main.cpp b/src/main.cpp index 222dabf..bf062ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,71 +15,69 @@ int main(int argc, char* argv[]) { bool parallel = false; bool newLines = true; - if (argc > 0) { - set files; - for (signed int i = 0; i < argc; ++i) - { - const string_view currentArg (argv[i]); - if ((argc == 2) && (currentArg == Argument("version"))) { cout << Yerbacon::getVersion() << endl; exit(EXIT_SUCCESS); } - else if (currentArg == ArgumentShort("printresult")) printResult = true; - else if (currentArg == ArgumentAssignable("target")) { - const string value = ArgumentAssignable::getValueFor(currentArg.data()); - if (!value.empty()) (target = '.') += value; - } - else if (currentArg == Argument("parallel")) parallel = true; - else if (currentArg == Argument("newlinesoff")) newLines = false; - else if (currentArg.ends_with(".ybcon")) files.insert(currentArg); - else if (i > 0) { - cout << '"' << currentArg << "\" is not a valid argument." << endl; - exit(EXIT_FAILURE); - } + set files; + for (signed int i = 0; i < argc; ++i) + { + const string_view currentArg (argv[i]); + if ((argc == 2) && (currentArg == Argument("version"))) { cout << Yerbacon::getVersion() << endl; exit(EXIT_SUCCESS); } + else if (currentArg == ArgumentShort("printresult")) printResult = true; + else if (currentArg == ArgumentAssignable("target")) { + const string value = ArgumentAssignable::getValueFor(currentArg.data()); + if (!value.empty()) (target = '.') += value; + } + else if (currentArg == Argument("parallel")) parallel = true; + else if (currentArg == Argument("newlinesoff")) newLines = false; + else if (currentArg.ends_with(".ybcon")) files.insert(currentArg); + else if (i > 0) { + cout << '"' << currentArg << "\" is not a valid argument." << endl; + exit(EXIT_FAILURE); } - const auto compile = [&target, &newLines](string_view name) -> string { - string transpiledString = transpile(parseString(getFileContent(name.data())), target, newLines); - name.remove_suffix(6); - string outputFile; - (outputFile = name).append(target); - outputFileContent(outputFile, transpiledString); - return transpiledString; - }; - if (!files.empty()) { - vector>>> Units; - const launch& Policy = not parallel ? launch::deferred : launch::async; - for (const string_view& fileName: files) { - Units.push_back(async(Policy, [&fileName, &compile]() { - pair> resultingPair; - try { - resultingPair.first = compile(fileName); - } catch (const Yerbacon::Exception& e) { - unsigned long lastSlash = 0; - unsigned long position1 = fileName.find_last_of('/'); - if (cmp_not_equal(position1, string_view::npos)) { - #ifndef _WIN32 - lastSlash = position1; - #else - unsigned long position2 = fileName.find_last_of('\\'); - if (cmp_not_equal(position2, string_view::npos)) { - lastSlash = max(position1, position2); - } - #endif - } - resultingPair.first = fileName.substr(lastSlash + 1); - resultingPair.second.emplace(e); - } - return resultingPair; - })); - } - if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n"; - for (future>>& currentFuture: Units) { - const auto&& result = currentFuture.get(); - if (not result.second.has_value()) { - if (printResult) cout << result.first; - } else { - cout << "Compilation of " << result.first << " has failed with the following error:\n" << result.second.value().what(); - } - cout << "\n\n"; - } - } else cout << "No valid file provided.\n"; } + const auto compile = [&target, &newLines](string_view name) -> string { + string transpiledString = transpile(parseString(getFileContent(name.data())), target, newLines); + name.remove_suffix(6); + string outputFile; + (outputFile = name).append(target); + outputFileContent(outputFile, transpiledString); + return transpiledString; + }; + if (!files.empty()) { + vector>>> Units; + const launch& Policy = not parallel ? launch::deferred : launch::async; + for (const string_view& fileName: files) { + Units.push_back(async(Policy, [&fileName, &compile]() { + pair> resultingPair; + try { + resultingPair.first = compile(fileName); + } catch (const Yerbacon::Exception& e) { + unsigned long lastSlash = 0; + unsigned long position1 = fileName.find_last_of('/'); + if (cmp_not_equal(position1, string_view::npos)) { + #ifndef _WIN32 + lastSlash = position1; + #else + unsigned long position2 = fileName.find_last_of('\\'); + if (cmp_not_equal(position2, string_view::npos)) { + lastSlash = max(position1, position2); + } + #endif + } + resultingPair.first = fileName.substr(lastSlash + 1); + resultingPair.second.emplace(e); + } + return resultingPair; + })); + } + if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n"; + for (future>>& currentFuture: Units) { + const auto&& result = currentFuture.get(); + if (not result.second.has_value()) { + if (printResult) cout << result.first; + } else { + cout << "Compilation of " << result.first << " has failed with the following error:\n" << result.second.value().what(); + } + cout << "\n\n"; + } + } else cout << "No valid file provided.\n"; return EXIT_SUCCESS; } \ No newline at end of file