Share the Reference analysis task with the Define one

Signed-off-by: Username404-59 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404-59 2024-12-31 20:22:38 +01:00
parent 2c0cec16d6
commit dbb08a5299
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
8 changed files with 18 additions and 16 deletions

View File

@ -35,11 +35,7 @@ private:
), ),
share_task(Reference, Function), share_task(Reference, Function),
share_task(Reference, Class), share_task(Reference, Class),
make_nonlocal_task(Define, share_task(Reference, Define<true>),
const optional previousDefinition = parsedTree.findReferenceByName<Define>(parseComponent.name).value();
if (&previousDefinition.value().get() != &parseComponent && (previousDefinition.value().get().final || parseComponent.final))
error(parseComponent.name + string(previousDefinition->get().final ? " cannot be redefined as it is final" : " cannot be made final after it has been declared"));
)
}; };
}; };
}; };

View File

@ -105,12 +105,12 @@ public:
#undef IS_PARSECOMPONENT #undef IS_PARSECOMPONENT
namespace StandardComponents { namespace StandardComponents {
struct Define: NamedIdentifier<true> { template<bool BOOL>
const bool final; struct Define: NamedIdentifier<BOOL> {
const bool final = BOOL;
ParseTree content; ParseTree content;
explicit Define(const bool& isFinal, string_view nameText, ParseTree&& content): NamedIdentifier(nameText), final(isFinal), content(move(content)) {} explicit Define(string_view nameText, ParseTree&& content): NamedIdentifier<BOOL>(nameText), content(move(content)) {}
explicit Define(string_view nameText, ParseTree&& content): Define(false, nameText, move(content)) {} Define(Define&& define) noexcept: Define(define.name, move(define.content)) {}
Define(Define&& define) noexcept: Define(define.final, define.name, move(define.content)) {}
}; };
struct Reference: NamedIdentifier<false> { struct Reference: NamedIdentifier<false> {
using NamedIdentifier::NamedIdentifier; using NamedIdentifier::NamedIdentifier;

View File

@ -142,7 +142,12 @@ namespace Parser {
const auto end = find_if(beginning, lexed.end(), [&current](const tok& it){ const auto end = find_if(beginning, lexed.end(), [&current](const tok& it){
return it.toktype == SEMICOLON || it.line != current.line; return it.toktype == SEMICOLON || it.line != current.line;
}); });
parseTree << Define(isFinalDefine, current.toktext, parse(beginning, end)); if (isFinalDefine) {
parseTree << Define<true>(current.toktext, parse(beginning, end));
} else {
parseTree << Define<false>(current.toktext, parse(beginning, end));
}
i += 1 + isFinalDefine + distance(beginning, end); i += 1 + isFinalDefine + distance(beginning, end);
} else { } else {
const bool method = nextAre({DOT, IDENTIFIER, LPAR}); const bool method = nextAre({DOT, IDENTIFIER, LPAR});

View File

@ -132,7 +132,8 @@ public:
output << ')'; output << ')';
), ),
make_task(StandardComponents::types::Integer, output << setprecision(parseComponent.precision) << fixed << parseComponent.value;), make_task(StandardComponents::types::Integer, output << setprecision(parseComponent.precision) << fixed << parseComponent.value;),
make_task(StandardComponents::Condition, output << '('; transpileTree(parseComponent); output << ')';) make_task(StandardComponents::Condition, output << '('; transpileTree(parseComponent); output << ')';),
share_task(StandardComponents::Define<false>, StandardComponents::Define<true>)
})); }));
return fullMap; return fullMap;
}; };

View File

@ -6,7 +6,7 @@ struct GsTarget: Target {
print_functions_pair printFunctions() final { return make_pair("printraw", "print"); } print_functions_pair printFunctions() final { return make_pair("printraw", "print"); }
unordered_task_map getTaskMap() final { unordered_task_map getTaskMap() final {
return { return {
make_task(Define, make_task(Define<false>,
output << (parseComponent.final ? "const " : "var ") << parseComponent.name << " = "; output << (parseComponent.final ? "const " : "var ") << parseComponent.name << " = ";
transpileTree(parseComponent.content); transpileTree(parseComponent.content);
), ),

View File

@ -7,7 +7,7 @@ struct JsTarget: Target {
bool use_uniqueLineSeparator() final { return true; } bool use_uniqueLineSeparator() final { return true; }
unordered_task_map getTaskMap() final { unordered_task_map getTaskMap() final {
return { return {
make_task(Define, make_task(Define<false>,
output << (parseComponent.final ? "const " : "let ") << parseComponent.name << " = "; output << (parseComponent.final ? "const " : "let ") << parseComponent.name << " = ";
transpileTree(parseComponent.content); transpileTree(parseComponent.content);
), ),

View File

@ -7,7 +7,7 @@ struct LuaTarget: Target {
optional_string uniqueLineSeparator() final { return " "; } optional_string uniqueLineSeparator() final { return " "; }
unordered_task_map getTaskMap() final { unordered_task_map getTaskMap() final {
return { return {
make_task(Define, make_task(Define<false>,
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

View File

@ -7,7 +7,7 @@ struct PyTarget: Target {
optional_string uniqueLineSeparator() final { return {}; } optional_string uniqueLineSeparator() final { return {}; }
unordered_task_map getTaskMap() final { unordered_task_map getTaskMap() final {
return { return {
make_task(Define, output << parseComponent.name << " = "; transpileTree(parseComponent.content);), make_task(Define<false>, output << parseComponent.name << " = "; transpileTree(parseComponent.content);),
make_task(types::String, stringInterpolation(R"(""")", parseComponent.content);), make_task(types::String, stringInterpolation(R"(""")", parseComponent.content);),
make_task(Function, make_task(Function,
output << "def " << parseComponent.name << '('; output << "def " << parseComponent.name << '(';