Add a --parallel argument to enable multithreading.

This commit is contained in:
Username404-59 2021-07-06 11:37:43 +02:00
parent 74a7a5f2f7
commit 314cff7f49
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1

View File

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <thread>
using namespace std; using namespace std;
#include "headers/misc.hpp" #include "headers/misc.hpp"
@ -9,6 +10,7 @@ int main(int argc, char* argv[]) {
string target = ".lua"; string target = ".lua";
string_view currentArg; string_view currentArg;
bool printResult = false; bool printResult = false;
bool parallel = false;
if (argc > 0) { if (argc > 0) {
vector<string_view> files; vector<string_view> files;
for (signed int i = 0; i < argc; ++i) for (signed int i = 0; i < argc; ++i)
@ -19,20 +21,30 @@ int main(int argc, char* argv[]) {
currentArg.remove_prefix(9); currentArg.remove_prefix(9);
(target = '.') += currentArg; (target = '.') += currentArg;
} }
else if (currentArg == "--parallel") parallel = true;
else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg); else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg);
} }
if (!files.empty()) { if (!files.empty()) {
const set<string_view> uniqueFiles(files.begin(), files.end()); const set<string_view> uniqueFiles(files.begin(), files.end());
vector<thread> threads;
for (string_view fileName: uniqueFiles) { for (string_view fileName: uniqueFiles) {
if (fileName != "none") { if (fileName != "none") {
const string transpiledString = transpile(parseString(getFileContent(fileName.data())), target); thread newThread = thread([printResult, fileName, target]() mutable {
if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n"; const string transpiledString = transpile(parseString(getFileContent(fileName.data())), target);
fileName.remove_suffix(6); if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n";
string outputFile; fileName.remove_suffix(6);
(outputFile = fileName).append(target); string outputFile;
outputFileContent(outputFile, transpiledString); (outputFile = fileName).append(target);
outputFileContent(outputFile, transpiledString);
});
if (!parallel) newThread.join(); else {
threads.push_back(move(newThread));
}
} }
} }
for (auto& currentThread: threads) {
currentThread.join();
}
} else cout << "No valid file provided.\n"; } else cout << "No valid file provided.\n";
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;