Make const parse trees actually constant, fix formatting in Target.hpp and classes derived from Target
This commit is contained in:
parent
ed3da58e31
commit
beea9bfadf
@ -48,6 +48,12 @@ namespace StandardComponents {
|
|||||||
|
|
||||||
#define IS_PARSECOMPONENT IS(ParseComponent)
|
#define IS_PARSECOMPONENT IS(ParseComponent)
|
||||||
class ParseTree {
|
class ParseTree {
|
||||||
|
IS_PARSECOMPONENT
|
||||||
|
void addComponent(const T& component) const {
|
||||||
|
subComponents.emplace_back(new T(component));
|
||||||
|
}; IS_PARSECOMPONENT void addAllComponents(const initializer_list<T>& components) const {
|
||||||
|
for (const T& comp: components) addComponent<T>(comp);
|
||||||
|
}
|
||||||
protected:
|
protected:
|
||||||
mutable vector<unique_ptr<ParseComponent>> subComponents;
|
mutable vector<unique_ptr<ParseComponent>> subComponents;
|
||||||
using array_type = decltype(subComponents);
|
using array_type = decltype(subComponents);
|
||||||
@ -79,19 +85,20 @@ public:
|
|||||||
return optional<reference_wrapper<T>>();
|
return optional<reference_wrapper<T>>();
|
||||||
};
|
};
|
||||||
inline size_t getCompCount() const { return subComponents.size(); }
|
inline size_t getCompCount() const { return subComponents.size(); }
|
||||||
|
IS_PARSECOMPONENT inline void add(const T& component) { addComponent<T>(component); };
|
||||||
|
IS_PARSECOMPONENT inline void addAll(const initializer_list<T>& components) { addAllComponents<T>(components); }
|
||||||
IS_PARSECOMPONENT
|
IS_PARSECOMPONENT
|
||||||
void add(const T& component) const {
|
inline ParseTree& operator<<(const T& component) { add(component); return *this; }
|
||||||
subComponents.emplace_back(new T(component));
|
|
||||||
}; IS_PARSECOMPONENT void addAll(const initializer_list<T>& components) const {
|
|
||||||
for (const auto& comp: components) add<T>(comp);
|
|
||||||
}
|
|
||||||
IS_PARSECOMPONENT
|
|
||||||
inline const ParseTree& operator<<(const T& component) const { add(component); return *this; }
|
|
||||||
ParseTree(): subComponents() {};
|
ParseTree(): subComponents() {};
|
||||||
IS_PARSECOMPONENT
|
IS_PARSECOMPONENT
|
||||||
explicit ParseTree(const T& element): ParseTree() { add(element); }
|
constexpr explicit ParseTree(const T& element): ParseTree() { addComponent(element); }
|
||||||
IS_PARSECOMPONENT
|
IS_PARSECOMPONENT
|
||||||
ParseTree(const initializer_list<T>& elements): ParseTree() { addAll(elements); }
|
constexpr ParseTree(const initializer_list<T>& elements): ParseTree() { addAllComponents(elements); }
|
||||||
|
ParseTree(const initializer_list<ParseComponent*>& elements): ParseTree() {
|
||||||
|
for_each(elements.begin(), elements.end(), [&](ParseComponent* component){
|
||||||
|
subComponents.emplace_back(component);
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#undef IS_PARSECOMPONENT
|
#undef IS_PARSECOMPONENT
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ inline string transpile(const ParseTree& tree, const string_view& language, cons
|
|||||||
#include "implementations/Py.hpp"
|
#include "implementations/Py.hpp"
|
||||||
|
|
||||||
enum LANGUAGE: signed short { NONE = -1, LUA, JS, PY };
|
enum LANGUAGE: signed short { NONE = -1, LUA, JS, PY };
|
||||||
constinit const array<string_view, 3> languages { ".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) {
|
shared_ptr<Target> Target::forName(string_view name, const bool newLines = true) {
|
||||||
LANGUAGE selected = NONE;
|
LANGUAGE selected = NONE;
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
#define JS_HPP JsTarget
|
#define JS_HPP JsTarget
|
||||||
|
|
||||||
struct JsTarget: Target {
|
struct JsTarget: Target {
|
||||||
void on(const StandardComponents::Define &parseComponent) override {
|
void on(const StandardComponents::Define& parseComponent) override {
|
||||||
output << (parseComponent.final ? "const " : "let ") << parseComponent.name << " = ";
|
output << (parseComponent.final ? "const " : "let ") << parseComponent.name << " = ";
|
||||||
}
|
}
|
||||||
void on(const StandardComponents::types::String &parseComponent) override {
|
void on(const StandardComponents::types::String& parseComponent) override {
|
||||||
stringInterpolation(parseComponent.content);
|
stringInterpolation(parseComponent.content);
|
||||||
}
|
}
|
||||||
using Target::Target;
|
using Target::Target;
|
||||||
|
@ -2,13 +2,13 @@
|
|||||||
#define LUA_HPP LuaTarget
|
#define LUA_HPP LuaTarget
|
||||||
|
|
||||||
struct LuaTarget: Target {
|
struct LuaTarget: Target {
|
||||||
void on(const StandardComponents::Define &parseComponent) override {
|
void on(const StandardComponents::Define& parseComponent) override {
|
||||||
if (parseComponent.final) output << "local ";
|
if (parseComponent.final) output << "local ";
|
||||||
output << parseComponent.name;
|
output << parseComponent.name;
|
||||||
if (parseComponent.final) output << " <const>"; // TODO Find an alternative to <const> for lua <5.4
|
if (parseComponent.final) output << " <const>"; // TODO Find an alternative to <const> for lua <5.4
|
||||||
output << " = ";
|
output << " = ";
|
||||||
}
|
}
|
||||||
void on(const StandardComponents::types::String &parseComponent) override {
|
void on(const StandardComponents::types::String& parseComponent) override {
|
||||||
stringInterpolation(parseComponent.content, "[[", "]]", "..");
|
stringInterpolation(parseComponent.content, "[[", "]]", "..");
|
||||||
}
|
}
|
||||||
using Target::Target;
|
using Target::Target;
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
|
|
||||||
struct PyTarget: Target {
|
struct PyTarget: Target {
|
||||||
bool supportsOneLine() final { return false; }
|
bool supportsOneLine() final { return false; }
|
||||||
void on(const StandardComponents::Define &parseComponent) override {
|
void on(const StandardComponents::Define& parseComponent) override {
|
||||||
output << parseComponent.name << " = ";
|
output << parseComponent.name << " = ";
|
||||||
}
|
}
|
||||||
void on(const StandardComponents::types::String &parseComponent) override {
|
void on(const StandardComponents::types::String& parseComponent) override {
|
||||||
stringInterpolation(R"(""")", parseComponent.content);
|
stringInterpolation(R"(""")", parseComponent.content);
|
||||||
}
|
}
|
||||||
using Target::Target;
|
using Target::Target;
|
||||||
|
Loading…
Reference in New Issue
Block a user