From 079d99e08af85f830b04e080229b3e852d218768 Mon Sep 17 00:00:00 2001 From: Username404 Date: Mon, 23 Aug 2021 12:49:22 +0200 Subject: [PATCH] Remove the parameter of Target.get(), add it to Target as a property and rename Target.get() to Target.on() --- src/headers/misc.hpp | 2 +- src/headers/transpiler/Target.hpp | 17 +++++++++-------- src/headers/transpiler/implementations/Js.hpp | 2 +- src/headers/transpiler/implementations/Lua.hpp | 2 +- src/headers/transpiler/implementations/Py.hpp | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/headers/misc.hpp b/src/headers/misc.hpp index b723cd1..0962dee 100644 --- a/src/headers/misc.hpp +++ b/src/headers/misc.hpp @@ -2,7 +2,7 @@ #define YERBACON_MISC_HPP string getFileContent(const string& file); -void outputFileContent(const string& file, const string_view content); +void outputFileContent(const string& file, string_view content); #include "lex.hpp" #include "parsing/Parser.hpp" diff --git a/src/headers/transpiler/Target.hpp b/src/headers/transpiler/Target.hpp index fec7ed1..bd65a5f 100644 --- a/src/headers/transpiler/Target.hpp +++ b/src/headers/transpiler/Target.hpp @@ -9,29 +9,30 @@ #include "../parsing/ParseComponents.hpp" -#define GETCOMP(X) virtual void get(const X& parseComponent, std::stringstream& output) {} +#define INCLUDECOMPONENT(X) virtual void on(const X& parseComponent) {} class Target { protected: - GETCOMP(StandardComponents::Define); - GETCOMP(StandardComponents::Reference); - GETCOMP(StandardComponents::Class); + std::stringstream output; + INCLUDECOMPONENT(StandardComponents::Define); + INCLUDECOMPONENT(StandardComponents::Reference); + INCLUDECOMPONENT(StandardComponents::Class); public: static shared_ptr forName(string_view name); - #define with(X) if (id == typeid(X)) { this->get(reinterpret_cast(*component), result); continue; } + #define with(X) if (id == typeid(X)) { this->on(reinterpret_cast(*component)); continue; } string transpileWithTree(const ParseTree& tree) { - stringstream result; + output.str(string()); for (const unique_ptr& component: tree) { const type_info& id = (*component).getId(); with(StandardComponents::Define); with(StandardComponents::Reference); with(StandardComponents::Class); } - return result.str(); + return output.str(); }; #undef with }; -#undef GETCOMP +#undef INCLUDECOMPONENT inline string transpile(const ParseTree& tree, const string_view& language) { return Target::forName(language)->transpileWithTree(tree); diff --git a/src/headers/transpiler/implementations/Js.hpp b/src/headers/transpiler/implementations/Js.hpp index 6aa95a6..f850a20 100644 --- a/src/headers/transpiler/implementations/Js.hpp +++ b/src/headers/transpiler/implementations/Js.hpp @@ -2,7 +2,7 @@ #define JS_HPP JsTarget struct JsTarget: Target { - void get(const StandardComponents::Define &parseComponent, std::stringstream &output) override { + void on(const StandardComponents::Define &parseComponent) override { output << (parseComponent.final ? "const " : "let ") << parseComponent.name << " = "; } }; diff --git a/src/headers/transpiler/implementations/Lua.hpp b/src/headers/transpiler/implementations/Lua.hpp index 1700910..6d02c8a 100644 --- a/src/headers/transpiler/implementations/Lua.hpp +++ b/src/headers/transpiler/implementations/Lua.hpp @@ -2,7 +2,7 @@ #define LUA_HPP LuaTarget struct LuaTarget: Target { - void get(const StandardComponents::Define &parseComponent, std::stringstream& output) override { + void on(const StandardComponents::Define &parseComponent) override { if (parseComponent.final) output << "local "; output << parseComponent.name; if (parseComponent.final) output << " "; // TODO Find an alternative to for lua >5.4 diff --git a/src/headers/transpiler/implementations/Py.hpp b/src/headers/transpiler/implementations/Py.hpp index 0fd874e..5e81c71 100644 --- a/src/headers/transpiler/implementations/Py.hpp +++ b/src/headers/transpiler/implementations/Py.hpp @@ -2,7 +2,7 @@ #define PY_HPP PyTarget struct PyTarget: Target { - void get(const StandardComponents::Define &parseComponent, std::stringstream &output) override { + void on(const StandardComponents::Define &parseComponent) override { output << parseComponent.name << " = "; } };