From dbb08a52999dce7f6e76a6f0ee9d49db1da67eed Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Tue, 31 Dec 2024 20:22:38 +0100 Subject: [PATCH] Share the Reference analysis task with the Define one Signed-off-by: Username404-59 --- src/headers/SemanticAnalysis.hpp | 6 +----- src/headers/parsing/ParseComponents.hpp | 10 +++++----- src/headers/parsing/Parser.hpp | 7 ++++++- src/headers/transpiler/Target.hpp | 3 ++- src/headers/transpiler/implementations/GodotScript.hpp | 2 +- src/headers/transpiler/implementations/Js.hpp | 2 +- src/headers/transpiler/implementations/Lua.hpp | 2 +- src/headers/transpiler/implementations/Py.hpp | 2 +- 8 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/headers/SemanticAnalysis.hpp b/src/headers/SemanticAnalysis.hpp index 4d46953..4257cb0 100644 --- a/src/headers/SemanticAnalysis.hpp +++ b/src/headers/SemanticAnalysis.hpp @@ -35,11 +35,7 @@ private: ), share_task(Reference, Function), share_task(Reference, Class), - make_nonlocal_task(Define, - const optional previousDefinition = parsedTree.findReferenceByName(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")); - ) + share_task(Reference, Define), }; }; }; diff --git a/src/headers/parsing/ParseComponents.hpp b/src/headers/parsing/ParseComponents.hpp index 13de2d0..d02bd17 100644 --- a/src/headers/parsing/ParseComponents.hpp +++ b/src/headers/parsing/ParseComponents.hpp @@ -105,12 +105,12 @@ public: #undef IS_PARSECOMPONENT namespace StandardComponents { - struct Define: NamedIdentifier { - const bool final; + template + struct Define: NamedIdentifier { + const bool final = BOOL; 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): Define(false, nameText, move(content)) {} - Define(Define&& define) noexcept: Define(define.final, define.name, move(define.content)) {} + explicit Define(string_view nameText, ParseTree&& content): NamedIdentifier(nameText), content(move(content)) {} + Define(Define&& define) noexcept: Define(define.name, move(define.content)) {} }; struct Reference: NamedIdentifier { using NamedIdentifier::NamedIdentifier; diff --git a/src/headers/parsing/Parser.hpp b/src/headers/parsing/Parser.hpp index 49754f5..1339ecc 100644 --- a/src/headers/parsing/Parser.hpp +++ b/src/headers/parsing/Parser.hpp @@ -142,7 +142,12 @@ namespace Parser { const auto end = find_if(beginning, lexed.end(), [¤t](const tok& it){ return it.toktype == SEMICOLON || it.line != current.line; }); - parseTree << Define(isFinalDefine, current.toktext, parse(beginning, end)); + if (isFinalDefine) { + parseTree << Define(current.toktext, parse(beginning, end)); + } else { + parseTree << Define(current.toktext, parse(beginning, end)); + } + i += 1 + isFinalDefine + distance(beginning, end); } else { const bool method = nextAre({DOT, IDENTIFIER, LPAR}); diff --git a/src/headers/transpiler/Target.hpp b/src/headers/transpiler/Target.hpp index 9bea246..4ea9794 100644 --- a/src/headers/transpiler/Target.hpp +++ b/src/headers/transpiler/Target.hpp @@ -132,7 +132,8 @@ public: output << ')'; ), 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, StandardComponents::Define) })); return fullMap; }; diff --git a/src/headers/transpiler/implementations/GodotScript.hpp b/src/headers/transpiler/implementations/GodotScript.hpp index f69cd0b..f521557 100644 --- a/src/headers/transpiler/implementations/GodotScript.hpp +++ b/src/headers/transpiler/implementations/GodotScript.hpp @@ -6,7 +6,7 @@ struct GsTarget: Target { print_functions_pair printFunctions() final { return make_pair("printraw", "print"); } unordered_task_map getTaskMap() final { return { - make_task(Define, + make_task(Define, output << (parseComponent.final ? "const " : "var ") << parseComponent.name << " = "; transpileTree(parseComponent.content); ), diff --git a/src/headers/transpiler/implementations/Js.hpp b/src/headers/transpiler/implementations/Js.hpp index b32aac3..7121f59 100644 --- a/src/headers/transpiler/implementations/Js.hpp +++ b/src/headers/transpiler/implementations/Js.hpp @@ -7,7 +7,7 @@ struct JsTarget: Target { bool use_uniqueLineSeparator() final { return true; } unordered_task_map getTaskMap() final { return { - make_task(Define, + make_task(Define, output << (parseComponent.final ? "const " : "let ") << parseComponent.name << " = "; transpileTree(parseComponent.content); ), diff --git a/src/headers/transpiler/implementations/Lua.hpp b/src/headers/transpiler/implementations/Lua.hpp index 17c79a7..368e821 100644 --- a/src/headers/transpiler/implementations/Lua.hpp +++ b/src/headers/transpiler/implementations/Lua.hpp @@ -7,7 +7,7 @@ struct LuaTarget: Target { optional_string uniqueLineSeparator() final { return " "; } unordered_task_map getTaskMap() final { return { - make_task(Define, + make_task(Define, 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 77aaef8..f9ed112 100644 --- a/src/headers/transpiler/implementations/Py.hpp +++ b/src/headers/transpiler/implementations/Py.hpp @@ -7,7 +7,7 @@ struct PyTarget: Target { optional_string uniqueLineSeparator() final { return {}; } unordered_task_map getTaskMap() final { return { - make_task(Define, output << parseComponent.name << " = "; transpileTree(parseComponent.content);), + make_task(Define, output << parseComponent.name << " = "; transpileTree(parseComponent.content);), make_task(types::String, stringInterpolation(R"(""")", parseComponent.content);), make_task(Function, output << "def " << parseComponent.name << '(';