Make the newLine constructor parameter a reference & add a "separator" member and a "uniqueLineSeparator" virtual method to Target.hpp

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2022-02-14 12:11:03 +01:00
parent 63b24752d6
commit 3c130529a4
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 9 additions and 7 deletions

View File

@ -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<const char*, const char*> print_functions_pair;
virtual unordered_task_map getTaskMap() = 0;
virtual print_functions_pair printFunctions() = 0;
typedef optional<const char*> 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<Target> 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;
};

View File

@ -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,

View File

@ -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 << " = ";),