From 27da4d39bfbd91293a3ed81872a3d9846f047884 Mon Sep 17 00:00:00 2001 From: starg Date: Wed, 14 Sep 2016 01:33:58 +0900 Subject: [PATCH] =?utf8?q?IRProcessor=20=E3=81=AE=E3=82=A4=E3=83=99?= =?utf8?q?=E3=83=B3=E3=83=88=E5=A4=89=E6=8F=9B=E9=83=A8=E5=88=86=E3=82=92?= =?utf8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- include/irprocessor/irprocessor.hpp | 29 +++++++- src/irprocessor/CMakeLists.txt | 2 + src/irprocessor/irprocessor.cpp | 127 ++++++++++++++++++++++++++++++++---- src/irprocessor/pch.hpp | 2 + 4 files changed, 147 insertions(+), 13 deletions(-) diff --git a/include/irprocessor/irprocessor.hpp b/include/irprocessor/irprocessor.hpp index ce24bad..324b6f3 100644 --- a/include/irprocessor/irprocessor.hpp +++ b/include/irprocessor/irprocessor.hpp @@ -1,8 +1,18 @@ #pragma once +#include +#include +#include +#include +#include + +#include + +#include #include #include +#include namespace YAMML { @@ -10,7 +20,7 @@ namespace YAMML namespace IRProcessor { -class IRCompiler : public Compiler::CompilerBase +class IRCompiler : public Compiler::CompilerBase, public boost::static_visitor<> { public: template @@ -26,10 +36,27 @@ public: std::string GetSourceName() const; const IR::Module& GetIR() const; + void operator()(IR::TrackList& trackList); + void operator()(AST::Command& command); + + void operator()(IR::Event& ev); + void operator()(IR::BlockReference& blockRef); + + void CompileBlock(IR::BlockReference blockRef); + private: void CompileTrackBlock(std::size_t index); + void AddAttributeProcessorFactory(std::unique_ptr pAttributeProcessorFactory); + void InitializeAttributeProcessorFactories(); + + void RegisterAttributeProcessors(const std::vector& attributes); + + IR::BlockReference DuplicateBlock(IR::BlockReference blockRef); + IR::Module m_IR; + std::unordered_map> m_AttributeProcessorFactoryMap; + std::deque>> m_AttributeProcessorStack; }; } // namespace IRProcessor diff --git a/src/irprocessor/CMakeLists.txt b/src/irprocessor/CMakeLists.txt index c3da93d..7d85943 100644 --- a/src/irprocessor/CMakeLists.txt +++ b/src/irprocessor/CMakeLists.txt @@ -1,5 +1,7 @@ set(IRProcessorHeaders + ../../include/irprocessor/attrproc.hpp + ../../include/irprocessor/attrprocfactory.hpp ../../include/irprocessor/irprocessor.hpp ) diff --git a/src/irprocessor/irprocessor.cpp b/src/irprocessor/irprocessor.cpp index f282b1e..7595fdb 100644 --- a/src/irprocessor/irprocessor.cpp +++ b/src/irprocessor/irprocessor.cpp @@ -1,6 +1,9 @@ +#include #include +#include +#include #include #include #include @@ -55,26 +58,126 @@ const IR::Module& IRCompiler::GetIR() const return m_IR; } +void IRCompiler::InitializeAttributeProcessorFactories() +{ + +} + +void IRCompiler::AddAttributeProcessorFactory(std::unique_ptr pAttributeProcessorFactory) +{ + auto name = pAttributeProcessorFactory->GetAttributeName(); + m_AttributeProcessorFactoryMap[name] = std::move(pAttributeProcessorFactory); +} + +void IRCompiler::RegisterAttributeProcessors(const std::vector& attributes) +{ + std::vector> attrProcessors; + attrProcessors.reserve(attributes.size()); + + for (auto&& i : attributes) + { + auto itFactory = m_AttributeProcessorFactoryMap.find(i.Name); + + if (itFactory == m_AttributeProcessorFactoryMap.end()) + { + throw Exceptions::MessageException( + Message::MessageItem{ + Message::MessageKind::Error, + Message::MessageID::InvalidAttributeName, + GetSourceName(), + i.Location, + {i.Name} + } + ); + } + else + { + attrProcessors.push_back(itFactory->second->CreateProcessor(i)); + } + } + + m_AttributeProcessorStack.push_back(std::move(attrProcessors)); +} + +IR::BlockReference IRCompiler::DuplicateBlock(IR::BlockReference blockRef) +{ + IR::BlockReference newRef{m_IR.Blocks.size()}; + m_IR.Blocks.push_back(m_IR.Blocks.at(blockRef.ID)); + return newRef; +} + void IRCompiler::CompileTrackBlock(std::size_t index) { - // TODO + RegisterAttributeProcessors(m_IR.TrackBlocks.at(index).Attributes); + Common::AutoPop autoPop(m_AttributeProcessorStack); - if (!m_IR.TrackBlocks[index].Attributes.empty()) + for (auto&& i : m_IR.TrackBlocks[index].Blocks) { - auto attribute = m_IR.TrackBlocks[index].Attributes.at(0); + i.apply_visitor(*this); + } +} - throw Exceptions::MessageException( - Message::MessageItem{ - Message::MessageKind::Error, - Message::MessageID::InvalidAttributeName, - GetSourceName(), - attribute.Location, - {attribute.Name} - } - ); +void IRCompiler::operator()(IR::TrackList& trackList) +{ + RegisterAttributeProcessors(trackList.Attributes); + Common::AutoPop autoPop(m_AttributeProcessorStack); + + for (auto&& i : trackList.Tracks) + { + RegisterAttributeProcessors(i.Attributes); + Common::AutoPop autoPop2(m_AttributeProcessorStack); + + for (auto&& j : i.Items) + { + RegisterAttributeProcessors(j.Attributes); + Common::AutoPop autoPop3(m_AttributeProcessorStack); + + auto dupRef = DuplicateBlock(j.Block); + j.Block = dupRef; + + CompileBlock(dupRef); + } } } +void IRCompiler::operator()(AST::Command&) +{ + // no-op +} + +void IRCompiler::CompileBlock(IR::BlockReference blockRef) +{ + RegisterAttributeProcessors(m_IR.Blocks.at(blockRef.ID).Attributes); + Common::AutoPop autoPop(m_AttributeProcessorStack); + + for (auto&& i : m_IR.Blocks[blockRef.ID].Events) + { + i.apply_visitor(*this); + } +} + +void IRCompiler::operator()(IR::Event& ev) +{ + std::for_each( + m_AttributeProcessorStack.rbegin(), + m_AttributeProcessorStack.rend(), + [&ev] (auto&& x) + { + for (auto&& i : x) + { + i->TransformEvent(ev); + } + } + ); +} + +void IRCompiler::operator()(IR::BlockReference& blockRef) +{ + // 'blockRef' here should be referenced by a single block, thus no need to duplicate it. + // blockRef = DuplicateBlock(blockRef); + CompileBlock(blockRef); +} + } // namespace IRProcessor } // namespace YAMML diff --git a/src/irprocessor/pch.hpp b/src/irprocessor/pch.hpp index fd4f8fd..3d402e2 100644 --- a/src/irprocessor/pch.hpp +++ b/src/irprocessor/pch.hpp @@ -2,8 +2,10 @@ #pragma once #include +#include #include #include +#include #include #include #include -- 2.11.0