Yerbacon/src/main.cpp

81 lines
3.6 KiB
C++

#include <iostream>
#include <set>
#include "headers/Yerbacon.hpp"
#include <future>
using namespace std;
#include "headers/misc.hpp"
#include "headers/arguments.hpp"
#include "headers/transpiler/Target.hpp"
int main(int argc, char* argv[]) {
string target = ".lua";
bool printResult = false,
parallel = false,
newLines = true;
set<string_view> files;
for (signed int i = 1; 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_view value = ArgumentAssignable::getValueFor(currentArg);
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 {
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<future<pair<string, optional<Yerbacon::Exception>>>> Units(files.size());
const launch& Policy = not parallel ? launch::deferred : launch::async;
transform(files.begin(), files.end(), Units.begin(), [&Policy, &compile](const string_view& fileName){
return async(Policy, [&fileName, &compile]() {
pair<string, optional<Yerbacon::Exception>> resultingPair;
try {
resultingPair.first = compile(fileName);
} catch (const Yerbacon::Exception& error) {
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(error);
}
return resultingPair;
});
});
if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n";
for_each(Units.begin(), Units.end(), [&printResult](future<pair<string, optional<Yerbacon::Exception>>>& currentFuture) {
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;
}