main.cpp: Use a map instead of a set and a vector in main.cpp
Target.hpp: make staticMap thread-local to avoid issues with the "--parallel" argument, and don't include dots in the languages array Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
parent
2b596e691c
commit
49dc7d0ebb
|
@ -123,7 +123,7 @@ protected:
|
||||||
static constexpr const char* indentation = " ";
|
static constexpr const char* indentation = " ";
|
||||||
public:
|
public:
|
||||||
const unordered_task_map& getTaskMapInstance() {
|
const unordered_task_map& getTaskMapInstance() {
|
||||||
static unordered_task_map staticMap = getTaskMap();
|
static thread_local unordered_task_map staticMap = getTaskMap();
|
||||||
// Default / Shared tasks:
|
// Default / Shared tasks:
|
||||||
staticMap.merge(unordered_task_map({
|
staticMap.merge(unordered_task_map({
|
||||||
make_task(ParseTree, transpileTree(parseComponent);),
|
make_task(ParseTree, transpileTree(parseComponent);),
|
||||||
|
@ -155,7 +155,7 @@ public:
|
||||||
#include "implementations/Py.hpp"
|
#include "implementations/Py.hpp"
|
||||||
|
|
||||||
enum LANGUAGE: signed short { NONE = -1, LUA, JS, PY };
|
enum LANGUAGE: signed short { NONE = -1, LUA, JS, PY };
|
||||||
constinit const array<string_view, 3> languages { ".lua", ".js", ".py" };
|
constinit const array<string_view, 3> languages { "lua", "js", "py" };
|
||||||
|
|
||||||
shared_ptr<Target> Target::forName(string_view name, const bool newLines = true) {
|
shared_ptr<Target> Target::forName(string_view name, const bool newLines = true) {
|
||||||
LANGUAGE selected = NONE;
|
LANGUAGE selected = NONE;
|
||||||
|
|
84
src/main.cpp
84
src/main.cpp
|
@ -1,5 +1,5 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <set>
|
#include <map>
|
||||||
#include "headers/Yerbacon.hpp"
|
#include "headers/Yerbacon.hpp"
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
@ -11,18 +11,26 @@ using namespace std;
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
string target = ".lua";
|
string target = "lua";
|
||||||
bool printResult = false,
|
bool printResult = false,
|
||||||
parallel = false,
|
parallel = false,
|
||||||
newLines = true;
|
newLines = true;
|
||||||
set<string_view> files;
|
using unit_result = pair<string, optional<Yerbacon::Exception>>;
|
||||||
|
using unit = future<unit_result>;
|
||||||
|
map<string_view, unit> Units;
|
||||||
|
const auto compile = [&target, &newLines](string_view name) -> string {
|
||||||
|
string transpiledString = Target::forName(target, newLines)->transpileWithTree(parseString(getFileContent(name.data())));
|
||||||
|
name.remove_suffix(6);
|
||||||
|
outputFileContent(string(name) + '.' + target, transpiledString);
|
||||||
|
return transpiledString;
|
||||||
|
};
|
||||||
for (signed int i = 1; i < argc; ++i)
|
for (signed int i = 1; i < argc; ++i)
|
||||||
{
|
{
|
||||||
const string_view currentArg (argv[i]);
|
const string_view currentArg (argv[i]);
|
||||||
if (currentArg == ArgumentShort("printresult")) printResult = true;
|
if (currentArg == ArgumentShort("printresult")) printResult = true;
|
||||||
else if (currentArg == ArgumentAssignable("target")) {
|
else if (currentArg == ArgumentAssignable("target")) {
|
||||||
const string_view value = ArgumentAssignable::getValueFor(currentArg);
|
const string_view value = ArgumentAssignable::getValueFor(currentArg);
|
||||||
if (not value.empty()) (target = '.') += value;
|
if (not value.empty()) target = value;
|
||||||
else Yerbacon::fail("No target was provided.");
|
else Yerbacon::fail("No target was provided.");
|
||||||
}
|
}
|
||||||
else if (currentArg == Argument("parallel")) parallel = true;
|
else if (currentArg == Argument("parallel")) parallel = true;
|
||||||
|
@ -34,8 +42,25 @@ int main(int argc, char* argv[]) {
|
||||||
newLines = true;
|
newLines = true;
|
||||||
} else goto invalid_argument;
|
} else goto invalid_argument;
|
||||||
}
|
}
|
||||||
else if (currentArg.ends_with(".ybcon")) files.insert(currentArg);
|
else if (currentArg.ends_with(".ybcon")) Units.insert_or_assign(currentArg, async(not parallel ? launch::deferred : launch::async, [fileName = currentArg, &compile]() {
|
||||||
else {
|
unit_result resultingPair;
|
||||||
|
try {
|
||||||
|
resultingPair.first = compile(fileName);
|
||||||
|
} catch (const Yerbacon::Exception& error) {
|
||||||
|
size_t lastSlash = 0;
|
||||||
|
const size_t position1 = fileName.find_last_of('/');
|
||||||
|
if (cmp_not_equal(position1, string_view::npos)) lastSlash = position1;
|
||||||
|
if constexpr(filesystem::path::preferred_separator != '/') {
|
||||||
|
const size_t position2 = fileName.find_last_of(filesystem::path::preferred_separator);
|
||||||
|
if (cmp_not_equal(position2, string_view::npos)) {
|
||||||
|
lastSlash = max(lastSlash, position2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resultingPair.first = fileName.substr(lastSlash + 1);
|
||||||
|
resultingPair.second.emplace(error);
|
||||||
|
}
|
||||||
|
return resultingPair;
|
||||||
|
})); else {
|
||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
if (currentArg == Argument("version")) {
|
if (currentArg == Argument("version")) {
|
||||||
cout << Yerbacon::getVersion();
|
cout << Yerbacon::getVersion();
|
||||||
|
@ -46,45 +71,10 @@ int main(int argc, char* argv[]) {
|
||||||
} else invalid_argument: Yerbacon::fail({"\"", currentArg.data(), "\" is not a valid argument."});
|
} else invalid_argument: Yerbacon::fail({"\"", currentArg.data(), "\" is not a valid argument."});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const auto current_target = Target::forName(target, newLines);
|
if (!Units.empty()) {
|
||||||
const auto compile = [&target, current_target](string_view name) -> string {
|
|
||||||
string transpiledString = current_target->transpileWithTree(parseString(getFileContent(name.data())));
|
|
||||||
name.remove_suffix(6);
|
|
||||||
string outputFile;
|
|
||||||
(outputFile = name).append(target);
|
|
||||||
outputFileContent(outputFile, transpiledString);
|
|
||||||
return transpiledString;
|
|
||||||
};
|
|
||||||
int8_t exit_code = EXIT_SUCCESS;
|
|
||||||
if (!files.empty()) {
|
|
||||||
using unit_result = pair<string, optional<Yerbacon::Exception>>;
|
|
||||||
using unit = future<unit_result>;
|
|
||||||
vector<unit> Units(files.size());
|
|
||||||
const launch& Policy = not parallel ? launch::deferred : launch::async;
|
|
||||||
transform(files.cbegin(), files.cend(), Units.begin(), [&Policy, &compile](const string_view& fileName){
|
|
||||||
return async(Policy, [&fileName, &compile]() {
|
|
||||||
unit_result resultingPair;
|
|
||||||
try {
|
|
||||||
resultingPair.first = compile(fileName);
|
|
||||||
} catch (const Yerbacon::Exception& error) {
|
|
||||||
size_t lastSlash = 0;
|
|
||||||
const size_t position1 = fileName.find_last_of('/');
|
|
||||||
if (cmp_not_equal(position1, string_view::npos)) lastSlash = position1;
|
|
||||||
if constexpr(filesystem::path::preferred_separator != '/') {
|
|
||||||
const size_t position2 = fileName.find_last_of(filesystem::path::preferred_separator);
|
|
||||||
if (cmp_not_equal(position2, string_view::npos)) {
|
|
||||||
lastSlash = max(lastSlash, position2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resultingPair.first = fileName.substr(lastSlash + 1);
|
|
||||||
resultingPair.second.emplace(error);
|
|
||||||
}
|
|
||||||
return resultingPair;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n";
|
if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n";
|
||||||
if (any_of(Units.rbegin(), Units.rend(), [&printResult](unit& currentFuture) -> bool {
|
for (auto entry_iterator = Units.rbegin(); entry_iterator != Units.rend(); ++entry_iterator) {
|
||||||
const pair result = currentFuture.get();
|
const pair result = entry_iterator->second.get();
|
||||||
const bool is_exception = result.second.has_value();
|
const bool is_exception = result.second.has_value();
|
||||||
if (not is_exception) {
|
if (not is_exception) {
|
||||||
if (printResult && !(result.first.empty() || all_of(result.first.begin(), result.first.end(), [](const char& character){
|
if (printResult && !(result.first.empty() || all_of(result.first.begin(), result.first.end(), [](const char& character){
|
||||||
|
@ -95,11 +85,9 @@ int main(int argc, char* argv[]) {
|
||||||
} else {
|
} else {
|
||||||
cout << "Compilation of " << result.first << " has failed with the following error:" << endl;
|
cout << "Compilation of " << result.first << " has failed with the following error:" << endl;
|
||||||
cerr << result.second.value().what() << '\n';
|
cerr << result.second.value().what() << '\n';
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
return is_exception;
|
|
||||||
})) {
|
|
||||||
exit_code = EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
} else cout << "No valid file provided.\n";
|
} else cout << "No valid file provided.\n";
|
||||||
return exit_code;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
Loading…
Reference in New Issue