Remove the parameter of Target.get(), add it to Target as a property and rename Target.get() to Target.on()

This commit is contained in:
Username404 2021-08-23 12:49:22 +02:00
parent 664a6a92be
commit 079d99e08a
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
5 changed files with 13 additions and 12 deletions

View File

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

View File

@ -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<Target> forName(string_view name);
#define with(X) if (id == typeid(X)) { this->get(reinterpret_cast<X&>(*component), result); continue; }
#define with(X) if (id == typeid(X)) { this->on(reinterpret_cast<X&>(*component)); continue; }
string transpileWithTree(const ParseTree& tree) {
stringstream result;
output.str(string());
for (const unique_ptr<ParseComponent>& 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);

View File

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

View File

@ -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 << " <const>"; // TODO Find an alternative to <const> for lua >5.4

View File

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