From 166574273326a1f9e55a675603655855c95c8bd7 Mon Sep 17 00:00:00 2001 From: starg Date: Tue, 26 Jul 2016 22:29:17 +0900 Subject: [PATCH] =?utf8?q?Phrase2IRCompiler=20=E3=81=AE=E3=83=96=E3=83=AD?= =?utf8?q?=E3=83=83=E3=82=AF=E5=89=B2=E3=82=8A=E5=BD=93=E3=81=A6=E5=87=A6?= =?utf8?q?=E7=90=86=E3=81=AE=E5=85=B1=E9=80=9A=E5=8C=96=E3=81=A8=20operato?= =?utf8?q?r()=20=E3=81=AE=E6=88=BB=E3=82=8A=E5=80=A4=E3=81=AE=E5=9E=8B?= =?utf8?q?=E3=81=AE=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- include/ir/block.hpp | 4 ++- src/ast2ir/phrase2ir.cpp | 72 ++++++++++++++++++++++-------------------------- src/ast2ir/phrase2ir.hpp | 16 ++++++----- 3 files changed, 45 insertions(+), 47 deletions(-) diff --git a/include/ir/block.hpp b/include/ir/block.hpp index d33b136..d9b9f3d 100644 --- a/include/ir/block.hpp +++ b/include/ir/block.hpp @@ -25,8 +25,10 @@ public: class Block final { public: + using EventType = boost::variant; + std::vector Attributes; - std::vector> Events; + std::vector Events; }; } // namespace IR diff --git a/src/ast2ir/phrase2ir.cpp b/src/ast2ir/phrase2ir.cpp index 0d866d2..56c4f83 100644 --- a/src/ast2ir/phrase2ir.cpp +++ b/src/ast2ir/phrase2ir.cpp @@ -58,31 +58,27 @@ bool Phrase2IRCompiler::Compile(const AST::Phrase& ast, IR::BlockReference index } } -IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteSequenceStatement& ast) +IR::Block::EventType Phrase2IRCompiler::operator()(const AST::NoteSequenceStatement& ast) { - auto newIndex = IR::BlockReference{m_IR.Blocks.size()}; - m_IR.Blocks.emplace_back(); + auto newIndex = AllocBlock(); - { - m_AttributeStack.push_back(ast.Attributes); - AutoPop autoPop(m_AttributeStack); + m_AttributeStack.push_back(ast.Attributes); + AutoPop autoPop(m_AttributeStack); - // with bounds checking - m_IR.Blocks.at(newIndex.ID).Attributes = Concat(m_AttributeStack); + // with bounds checking + m_IR.Blocks[newIndex.ID].Attributes = Concat(m_AttributeStack); - if (ast.NoteSeq.is_initialized()) - { - m_IR.Blocks[newIndex.ID].Events.emplace_back((*this)(*ast.NoteSeq)); - } + if (ast.NoteSeq.is_initialized()) + { + m_IR.Blocks[newIndex.ID].Events.emplace_back((*this)(*ast.NoteSeq)); } return newIndex; } -IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteSequenceBlock& ast) +IR::Block::EventType Phrase2IRCompiler::operator()(const AST::NoteSequenceBlock& ast) { - auto newIndex = IR::BlockReference{m_IR.Blocks.size()}; - m_IR.Blocks.emplace_back(); + auto newIndex = AllocBlock(); m_AttributeStack.push_back(ast.Attributes); AutoPop autoPop(m_AttributeStack); @@ -91,13 +87,10 @@ IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteSequenceBlock& a return newIndex; } -IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteSequence& ast) +IR::Block::EventType Phrase2IRCompiler::operator()(const AST::NoteSequence& ast) { - auto newIndex = IR::BlockReference{m_IR.Blocks.size()}; - m_IR.Blocks.emplace_back(); - - // with bounds checking - m_IR.Blocks.at(newIndex.ID).Attributes = Concat(m_AttributeStack); + auto newIndex = AllocBlock(); + m_IR.Blocks[newIndex.ID].Attributes = Concat(m_AttributeStack); for (auto&& i : ast.Notes) { @@ -110,61 +103,62 @@ IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteSequence& ast) return newIndex; } -IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteAndDuration& ast) +IR::Block::EventType Phrase2IRCompiler::operator()(const AST::NoteAndDuration& ast) { return {}; } -IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteRepeatExpression& ast) +IR::Block::EventType Phrase2IRCompiler::operator()(const AST::NoteRepeatExpression& ast) { - auto newIndex = IR::BlockReference{m_IR.Blocks.size()}; - m_IR.Blocks.emplace_back(); - - // with bounds checking - m_IR.Blocks.at(newIndex.ID).Attributes = Concat(m_AttributeStack); + auto newIndex = AllocBlock(); + m_IR.Blocks[newIndex.ID].Attributes = Concat(m_AttributeStack); LimitRepeatCount(ast.Count, ast.Location); - std::vector childBlockRefArray(ast.Notes.size()); + std::vector childBlockEventArray(ast.Notes.size()); std::transform( ast.Notes.begin(), ast.Notes.end(), - childBlockRefArray.begin(), + childBlockEventArray.begin(), [this] (auto&& x) { return x.apply_visitor(*this); } ); for (std::size_t i = 0; i < ast.Count; i++) { auto& events = m_IR.Blocks[newIndex.ID].Events; - events.insert(events.end(), childBlockRefArray.begin(), childBlockRefArray.end()); + events.insert(events.end(), childBlockEventArray.begin(), childBlockEventArray.end()); } return newIndex; } -IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteRepeatEachExpression& ast) +IR::Block::EventType Phrase2IRCompiler::operator()(const AST::NoteRepeatEachExpression& ast) { - auto newIndex = IR::BlockReference{m_IR.Blocks.size()}; - m_IR.Blocks.emplace_back(); - - // with bounds checking - m_IR.Blocks.at(newIndex.ID).Attributes = Concat(m_AttributeStack); + auto newIndex = AllocBlock(); + m_IR.Blocks[newIndex.ID].Attributes = Concat(m_AttributeStack); LimitRepeatCount(ast.Count, ast.Location); for (auto&& i : ast.Notes) { - auto childBlockRef = i.apply_visitor(*this); + auto childBlockItem = i.apply_visitor(*this); for (std::size_t j = 0; j < ast.Count; j++) { - m_IR.Blocks[newIndex.ID].Events.emplace_back(childBlockRef); + m_IR.Blocks[newIndex.ID].Events.emplace_back(childBlockItem); } } return newIndex; } +IR::BlockReference Phrase2IRCompiler::AllocBlock() +{ + auto newIndex = IR::BlockReference{m_IR.Blocks.size()}; + m_IR.Blocks.emplace_back(); + return newIndex; +} + void Phrase2IRCompiler::Compile(const AST::NoteSequenceBlockWithoutAttributes& ast, IR::BlockReference index) { // with bounds checking diff --git a/src/ast2ir/phrase2ir.hpp b/src/ast2ir/phrase2ir.hpp index 2df80c6..5302878 100644 --- a/src/ast2ir/phrase2ir.hpp +++ b/src/ast2ir/phrase2ir.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace YAMML @@ -19,7 +20,7 @@ namespace YAMML namespace AST2IR { -class Phrase2IRCompiler final : public Compiler::NestedCompilerBase, public boost::static_visitor +class Phrase2IRCompiler final : public Compiler::NestedCompilerBase, public boost::static_visitor { public: Phrase2IRCompiler(Compiler::CompilerBase& parentCompiler, IR::Module& ir); @@ -27,16 +28,17 @@ public: // Compiles ast into m_IR.Blocks[index.ID] bool Compile(const AST::Phrase& ast, IR::BlockReference index); - IR::BlockReference operator()(const AST::NoteSequenceStatement& ast); - IR::BlockReference operator()(const AST::NoteSequenceBlock& ast); + IR::Block::EventType operator()(const AST::NoteSequenceStatement& ast); + IR::Block::EventType operator()(const AST::NoteSequenceBlock& ast); - IR::BlockReference operator()(const AST::NoteSequence& ast); + IR::Block::EventType operator()(const AST::NoteSequence& ast); - IR::BlockReference operator()(const AST::NoteAndDuration& ast); - IR::BlockReference operator()(const AST::NoteRepeatExpression& ast); - IR::BlockReference operator()(const AST::NoteRepeatEachExpression& ast); + IR::Block::EventType operator()(const AST::NoteAndDuration& ast); + IR::Block::EventType operator()(const AST::NoteRepeatExpression& ast); + IR::Block::EventType operator()(const AST::NoteRepeatEachExpression& ast); private: + IR::BlockReference AllocBlock(); void Compile(const AST::NoteSequenceBlockWithoutAttributes& ast, IR::BlockReference index); void LimitRepeatCount(std::size_t count, const AST::SourceLocation& location); -- 2.11.0