From 34134dda7122502166dfac7c1d949a090632cf99 Mon Sep 17 00:00:00 2001 From: Username404 <w.iron.zombie@gmail.com> Date: Fri, 6 May 2022 22:06:37 +0200 Subject: [PATCH] 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> --- src/headers/transpiler/Target.hpp | 36 +++++-------------- src/headers/transpiler/implementations/Js.hpp | 2 +- .../transpiler/implementations/Lua.hpp | 2 +- src/headers/transpiler/implementations/Py.hpp | 2 +- 4 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/headers/transpiler/Target.hpp b/src/headers/transpiler/Target.hpp index 5dbdc2b..3ff70cc 100644 --- a/src/headers/transpiler/Target.hpp +++ b/src/headers/transpiler/Target.hpp @@ -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 \ No newline at end of file diff --git a/src/headers/transpiler/implementations/Js.hpp b/src/headers/transpiler/implementations/Js.hpp index 78bb491..ce8f5c7 100644 --- a/src/headers/transpiler/implementations/Js.hpp +++ b/src/headers/transpiler/implementations/Js.hpp @@ -1,5 +1,5 @@ #ifndef JS_HPP -#define JS_HPP JsTarget +#define JS_HPP using namespace StandardComponents; struct JsTarget: Target { diff --git a/src/headers/transpiler/implementations/Lua.hpp b/src/headers/transpiler/implementations/Lua.hpp index d512bb2..e6283bc 100644 --- a/src/headers/transpiler/implementations/Lua.hpp +++ b/src/headers/transpiler/implementations/Lua.hpp @@ -1,5 +1,5 @@ #ifndef LUA_HPP -#define LUA_HPP LuaTarget +#define LUA_HPP using namespace StandardComponents; struct LuaTarget: Target { diff --git a/src/headers/transpiler/implementations/Py.hpp b/src/headers/transpiler/implementations/Py.hpp index 829caf3..16081a7 100644 --- a/src/headers/transpiler/implementations/Py.hpp +++ b/src/headers/transpiler/implementations/Py.hpp @@ -1,5 +1,5 @@ #ifndef PY_HPP -#define PY_HPP PyTarget +#define PY_HPP using namespace StandardComponents; struct PyTarget: Target {