Target.hpp: Simplify the Target::forName function and change its return type to unique_ptr instead of shared_ptr

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2022-05-06 22:06:37 +02:00
parent b63bf0d480
commit 34134dda71
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
4 changed files with 11 additions and 31 deletions

View File

@ -140,7 +140,7 @@ public:
}));
return staticMap;
};
static shared_ptr<Target> forName(string_view name, bool newLines);
static unique_ptr<Target> forName(string_view name, bool newLines);
string transpileWithTree(const ParseTree& tree) {
separator = newLines ? "\n" : (use_uniqueLineSeparator() ? uniqueLineSeparator().value() : throw Yerbacon::Exception("--newlines=off is not supported by the current target"));
output.str(string());
@ -156,39 +156,19 @@ public:
#include "implementations/Js.hpp"
#include "implementations/Py.hpp"
enum LANGUAGE: signed short { NONE = -1, 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) {
LANGUAGE selected = NONE;
for (unsigned int i = 0; i < languages.size(); ++i) {
if (name == languages[i]) {
selected = static_cast<LANGUAGE>(i);
break;
}
}
shared_ptr<Target> target;
#define ADDTARGET(X) target = shared_ptr<X>(new X(newLines))
switch (selected) {
#ifdef LUA_HPP
case LUA: ADDTARGET(LUA_HPP); break;
#endif
#ifdef JS_HPP
case JS: ADDTARGET(JS_HPP); break;
#endif
#ifdef PY_HPP
case PY: ADDTARGET(PY_HPP); break;
#endif
case NONE:
default: Yerbacon::fail({"\"", string(1, (char) toupper(name.at(1))).data(), name.substr(2).data(), "\" is not a valid target."});
}
unique_ptr<Target> Target::forName(string_view name, const bool newLines = true) {
#define ADDTARGET(X, target_class) if (name == X) return unique_ptr<target_class>(new target_class(newLines))
ADDTARGET("lua", LuaTarget);
ADDTARGET("js", JsTarget);
ADDTARGET("py", PyTarget);
#undef ADDTARGET
#undef make_nonlocal_task
#undef make_task_noR
#undef make_task
#undef make_task_base_R
#undef make_task_base
return target;
Yerbacon::fail({"\"", to_string(toupper(name.at(1))).data(), name.substr(2).data(), "\" is not a valid target."});
return nullptr;
}
#endif //YERBACON_TARGET_HPP

View File

@ -1,5 +1,5 @@
#ifndef JS_HPP
#define JS_HPP JsTarget
#define JS_HPP
using namespace StandardComponents;
struct JsTarget: Target {

View File

@ -1,5 +1,5 @@
#ifndef LUA_HPP
#define LUA_HPP LuaTarget
#define LUA_HPP
using namespace StandardComponents;
struct LuaTarget: Target {

View File

@ -1,5 +1,5 @@
#ifndef PY_HPP
#define PY_HPP PyTarget
#define PY_HPP
using namespace StandardComponents;
struct PyTarget: Target {