Yerbacon/src/main.cpp

83 lines
3.5 KiB
C++

#include <iostream>
#include <set>
#include "headers/Yerbacon.hpp"
#include <future>
#include <variant>
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;
bool parallel = false;
bool 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 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 {
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;
const launch& Policy = not parallel ? launch::deferred : launch::async;
for (const string_view& fileName: files) {
Units.push_back(async(Policy, [&fileName, &compile]() {
pair<string, optional<Yerbacon::Exception>> 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<pair<string, optional<Yerbacon::Exception>>>& 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;
}