From 3c130529a4e7980983681ef5e2d6695662a4ccd4 Mon Sep 17 00:00:00 2001 From: Username404 Date: Mon, 14 Feb 2022 12:11:03 +0100 Subject: [PATCH] Make the newLine constructor parameter a reference & add a "separator" member and a "uniqueLineSeparator" virtual method to Target.hpp Signed-off-by: Username404 --- src/headers/transpiler/Target.hpp | 13 +++++++------ src/headers/transpiler/implementations/Lua.hpp | 1 + src/headers/transpiler/implementations/Py.hpp | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/headers/transpiler/Target.hpp b/src/headers/transpiler/Target.hpp index a987a3a..535ae5d 100644 --- a/src/headers/transpiler/Target.hpp +++ b/src/headers/transpiler/Target.hpp @@ -19,7 +19,7 @@ class Target { constexpr static const char* const interpolationString = "${"; constexpr static const char* const interpolationCloseString = "}"; protected: - virtual constexpr bool supportsOneLine() { return true; }; + inline bool supportsOneLine() { return uniqueLineSeparator().has_value(); }; std::stringstream output; inline void stringInterpolation(const char* multiline, const string& view) { stringInterpolation(view, multiline, multiline); } void stringInterpolation(string view, const char* openMultiline = "", const char* closeMultiline = "", const char* concatenationCharacters = "+") { @@ -74,6 +74,10 @@ protected: typedef pair print_functions_pair; virtual unordered_task_map getTaskMap() = 0; virtual print_functions_pair printFunctions() = 0; + typedef optional optional_string; + virtual optional_string uniqueLineSeparator() { return ";"; }; + const bool newLines; + const char* separator; public: const unordered_task_map& getTaskMapInstance() { static unordered_task_map staticMap = getTaskMap(); @@ -101,12 +105,9 @@ public: return staticMap; }; static shared_ptr forName(string_view name, bool newLines); - const bool newLines; string transpileWithTree(const ParseTree& tree) { + separator = newLines ? "\n" : (supportsOneLine() ? uniqueLineSeparator().value() : throw Yerbacon::Exception("--newlines=off is not supported by the current target")); const unordered_task_map& taskMap = getTaskMapInstance(); - if (not newLines && !supportsOneLine()) { - throw Yerbacon::Exception("--newlines=off is not supported by the current target"); - } output.str(string()); for (unsigned int i = 0; i < tree.size(); ++i) { const component_ptr& component = tree[i]; @@ -125,7 +126,7 @@ public: } return output.str() + '\n'; }; - explicit Target(const bool newLines): newLines(newLines) {}; + explicit Target(const bool& newLines): newLines(newLines), separator() {}; virtual ~Target() = default; }; diff --git a/src/headers/transpiler/implementations/Lua.hpp b/src/headers/transpiler/implementations/Lua.hpp index 57ef702..d8c9b58 100644 --- a/src/headers/transpiler/implementations/Lua.hpp +++ b/src/headers/transpiler/implementations/Lua.hpp @@ -4,6 +4,7 @@ using namespace StandardComponents; struct LuaTarget: Target { print_functions_pair printFunctions() final { return make_pair("io.write", "print"); } + optional_string uniqueLineSeparator() final { return " "; } unordered_task_map getTaskMap() final { return { make_task(Define, diff --git a/src/headers/transpiler/implementations/Py.hpp b/src/headers/transpiler/implementations/Py.hpp index c9dde6a..2b2396a 100644 --- a/src/headers/transpiler/implementations/Py.hpp +++ b/src/headers/transpiler/implementations/Py.hpp @@ -3,8 +3,8 @@ using namespace StandardComponents; struct PyTarget: Target { - bool supportsOneLine() final { return false; } print_functions_pair printFunctions() final { return make_pair("sys.stdout.write", "print"); } + optional_string uniqueLineSeparator() final { return {}; } unordered_task_map getTaskMap() final { return { make_task(Define, output << parseComponent.name << " = ";),