From 524e188db183bca690f51fd79236ae87f75c12e5 Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 19 Feb 2021 17:02:49 +0100 Subject: [PATCH] fix evaluation not resolving correctly due to being unfinished --- .../Matcher/CommandsCommonMatchers.cpp | 27 ++++++++++++---- .../Commands/Sequence/SequenceArraySize.cpp | 29 +++++++++++++++++ .../Commands/Sequence/SequenceCondition.cpp | 31 +++++++++++++++++++ 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.cpp index d6360a5e..c0b19d27 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.cpp @@ -300,7 +300,7 @@ std::unique_ptr CommandsCommonMatchers::ProcessEvaluation(CommandsP if (currentType == state->GetInUse()) currentType = nullptr; - std::list> operands; + std::vector> operands; std::list> operators; while (true) @@ -333,13 +333,28 @@ std::unique_ptr CommandsCommonMatchers::ProcessEvaluation(CommandsP operators.sort([](const std::pair& p1, const std::pair& p2) { - return p1.second->m_precedence > p2.second->m_precedence; - }); + if(p1.second->m_precedence != p2.second->m_precedence) + return p1.second->m_precedence > p2.second->m_precedence; + return p1.first > p2.first; + }); + while (!operators.empty()) { - operators.pop_back(); - } + const auto [operatorIndex, operatorType] = operators.back(); - return nullptr; + auto operation = std::make_unique(operatorType, std::move(operands[operatorIndex]), std::move(operands[operatorIndex + 1])); + operands.erase(operands.begin() + operatorIndex); + operands[operatorIndex] = std::move(operation); + + operators.pop_back(); + + for(auto& [opIndex, _] : operators) + { + if (opIndex > operatorIndex) + opIndex--; + } + } + + return std::move(operands.front()); } diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceArraySize.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceArraySize.cpp index 06398eba..88a25a38 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceArraySize.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceArraySize.cpp @@ -1,5 +1,7 @@ #include "SequenceArraySize.h" + +#include "Domain/Definition/ArrayDeclarationModifier.h" #include "Parsing/Commands/Matcher/CommandsMatcherFactory.h" #include "Parsing/Commands/Matcher/CommandsCommonMatchers.h" @@ -20,4 +22,31 @@ SequenceArraySize::SequenceArraySize() void SequenceArraySize::ProcessMatch(CommandsParserState* state, SequenceResult& result) const { + const auto& typeNameToken = result.NextCapture(CAPTURE_TYPE); + + StructureInformation* structure; + std::vector memberChain; + if (!state->GetTypenameAndMembersFromTypename(typeNameToken.TypeNameValue(), structure, memberChain)) + throw ParsingException(typeNameToken.GetPos(), "Unknown type"); + + if (memberChain.empty()) + throw ParsingException(typeNameToken.GetPos(), "Must specify type with member"); + + const auto& memberDeclarationModifiers = memberChain.back()->m_member->m_type_declaration->m_declaration_modifiers; + ArrayDeclarationModifier* arrayModifier = nullptr; + for (const auto& modifier : memberDeclarationModifiers) + { + if (modifier->GetType() == DeclarationModifierType::ARRAY) + { + arrayModifier = dynamic_cast(modifier.get()); + break; + } + } + + if (arrayModifier == nullptr) + throw ParsingException(typeNameToken.GetPos(), "Specified member is not an array"); + + auto evaluation = CommandsCommonMatchers::ProcessEvaluation(state, result, structure); + + arrayModifier->m_dynamic_size_evaluation = std::move(evaluation); } diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceCondition.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceCondition.cpp index c970f679..db18a5d5 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceCondition.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceCondition.cpp @@ -1,5 +1,6 @@ #include "SequenceCondition.h" +#include "Domain/Evaluation/OperandStatic.h" #include "Parsing/Commands/Matcher/CommandsMatcherFactory.h" #include "Parsing/Commands/Matcher/CommandsCommonMatchers.h" @@ -24,4 +25,34 @@ SequenceCondition::SequenceCondition() void SequenceCondition::ProcessMatch(CommandsParserState* state, SequenceResult& result) const { + const auto& typeNameToken = result.NextCapture(CAPTURE_TYPE); + + StructureInformation* type; + std::vector memberChain; + if (!state->GetTypenameAndMembersFromTypename(typeNameToken.TypeNameValue(), type, memberChain)) + throw ParsingException(typeNameToken.GetPos(), "Unknown type"); + + if (memberChain.empty()) + throw ParsingException(typeNameToken.GetPos(), "Conditions can only be set on members and not for types"); + + std::unique_ptr conditionEvaluation; + switch(result.NextTag()) + { + case TAG_ALWAYS: + conditionEvaluation = std::make_unique(1); + break; + + case TAG_NEVER: + conditionEvaluation = std::make_unique(0); + break; + + case TAG_EVALUATION: + conditionEvaluation = CommandsCommonMatchers::ProcessEvaluation(state, result, type); + break; + + default: + throw ParsingException(TokenPos(), "Unknown evaluation type @ Condition"); + } + + memberChain.back()->m_condition = std::move(conditionEvaluation); }