Add a --parallel argument to enable multithreading.
This commit is contained in:
parent
74a7a5f2f7
commit
314cff7f49
24
src/main.cpp
24
src/main.cpp
@ -1,5 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <thread>
|
||||
using namespace std;
|
||||
|
||||
#include "headers/misc.hpp"
|
||||
@ -9,6 +10,7 @@ int main(int argc, char* argv[]) {
|
||||
string target = ".lua";
|
||||
string_view currentArg;
|
||||
bool printResult = false;
|
||||
bool parallel = false;
|
||||
if (argc > 0) {
|
||||
vector<string_view> files;
|
||||
for (signed int i = 0; i < argc; ++i)
|
||||
@ -19,20 +21,30 @@ int main(int argc, char* argv[]) {
|
||||
currentArg.remove_prefix(9);
|
||||
(target = '.') += currentArg;
|
||||
}
|
||||
else if (currentArg == "--parallel") parallel = true;
|
||||
else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg);
|
||||
}
|
||||
if (!files.empty()) {
|
||||
const set<string_view> uniqueFiles(files.begin(), files.end());
|
||||
vector<thread> threads;
|
||||
for (string_view fileName: uniqueFiles) {
|
||||
if (fileName != "none") {
|
||||
const string transpiledString = transpile(parseString(getFileContent(fileName.data())), target);
|
||||
if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n";
|
||||
fileName.remove_suffix(6);
|
||||
string outputFile;
|
||||
(outputFile = fileName).append(target);
|
||||
outputFileContent(outputFile, transpiledString);
|
||||
thread newThread = thread([printResult, fileName, target]() mutable {
|
||||
const string transpiledString = transpile(parseString(getFileContent(fileName.data())), target);
|
||||
if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n";
|
||||
fileName.remove_suffix(6);
|
||||
string outputFile;
|
||||
(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";
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
|
Loading…
Reference in New Issue
Block a user