OSDN Git Service

Phrase2IRCompiler の実相を少し進める
authorstarg <starg@users.osdn.me>
Sun, 24 Jul 2016 08:56:14 +0000 (17:56 +0900)
committerstarg <starg@users.osdn.me>
Sun, 24 Jul 2016 08:56:14 +0000 (17:56 +0900)
include/ir/module.hpp
include/message/id.hpp
src/ast2ir/composition2ir.cpp
src/ast2ir/composition2ir.hpp
src/ast2ir/containerutil.hpp
src/ast2ir/module2ir.cpp
src/ast2ir/pch.hpp
src/ast2ir/phrase2ir.cpp
src/ast2ir/phrase2ir.hpp

index d685e5d..906378f 100644 (file)
@@ -16,7 +16,7 @@ namespace IR
 class Module final
 {
 public:
-    std::unordered_map<std::string, std::size_t> BlockNameMap;
+    std::unordered_map<std::string, BlockReference> BlockNameMap;
     std::vector<Block> Blocks;
     std::string Name;
 };
index 8491787..ae13013 100644 (file)
@@ -10,6 +10,7 @@ namespace Message
 enum class MessageID
 {
     Unknown,
+    UnknownInPhrase2IR,
     DuplicatedCompositionName,
     DuplicatedPhraseName
 };
index f4a484d..5b5d1b8 100644 (file)
@@ -12,7 +12,7 @@ Composition2IRCompiler::Composition2IRCompiler(Compiler::CompilerBase& parentCom
 {
 }
 
-bool Composition2IRCompiler::Compile(const AST::Composition& ast, std::size_t index)
+bool Composition2IRCompiler::Compile(const AST::Composition& ast, IR::BlockReference index)
 {
     return false;
 }
index d9138a7..7359b10 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <ast/composition.hpp>
 #include <compiler/nested.hpp>
+#include <ir/block.hpp>
 #include <ir/module.hpp>
 
 namespace YAMML
@@ -18,7 +19,7 @@ class Composition2IRCompiler final : public Compiler::NestedCompilerBase
 public:
     Composition2IRCompiler(Compiler::CompilerBase& parentCompiler, IR::Module& ir);
 
-    bool Compile(const AST::Composition& ast, std::size_t index);
+    bool Compile(const AST::Composition& ast, IR::BlockReference index);
 
 private:
     IR::Module& m_IR;
index a7c33f1..13c0004 100644 (file)
@@ -34,7 +34,7 @@ private:
 template<typename T>
 auto Concat(const T& container)
 {
-    std::vector<typename T::value_type> v;
+    std::vector<typename T::value_type::value_type> v;
 
     for (auto&& i : container)
     {
index 862fac4..b1cf36a 100644 (file)
@@ -1,4 +1,5 @@
 
+#include <ir/block.hpp>
 #include <message/message.hpp>
 
 #include "module2ir.hpp"
@@ -32,8 +33,9 @@ bool ProcessItem(TParentCompiler& parentCompiler, IR::Module& ir, const TItem& i
         return false;
     }
 
-    auto newIndex = ir.Blocks.size();
+    auto newIndex = IR::BlockReference{ir.Blocks.size()};
     ir.BlockNameMap[item.Name] = newIndex;
+    ir.Blocks.emplace_back();
 
     return TCompiler(parentCompiler, ir).Compile(item, newIndex);
 }
index 59b6d4e..86d631a 100644 (file)
@@ -1,8 +1,11 @@
 
 #pragma once
 
+#include <cassert>
+
 #include <algorithm>
 #include <deque>
+#include <exception>
 #include <functional>
 #include <string>
 #include <utility>
index bf74b8e..64a1388 100644 (file)
@@ -1,5 +1,8 @@
 
+#include <cassert>
+
 #include <deque>
+#include <exception>
 #include <string>
 #include <vector>
 
@@ -10,6 +13,7 @@
 #include <ast/phrase.hpp>
 #include <compiler/nested.hpp>
 #include <message/message.hpp>
+#include <ir/block.hpp>
 #include <ir/module.hpp>
 
 #include "containerutil.hpp"
@@ -26,18 +30,89 @@ Phrase2IRCompiler::Phrase2IRCompiler(Compiler::CompilerBase& parentCompiler, IR:
 {
 }
 
-bool Phrase2IRCompiler::Compile(const AST::Phrase& ast, std::size_t index)
+bool Phrase2IRCompiler::Compile(const AST::Phrase& ast, IR::BlockReference index)
+{
+    try
+    {
+        m_AttributeStack.push_back(ast.Attributes);
+        AutoPop<decltype(m_AttributeStack)> autoPop(m_AttributeStack);
+
+        Compile(ast.Block, index);
+        return true;
+    }
+    catch (const std::exception& e)
+    {
+        AddMessage(
+        {
+            Message::MessageKind::FetalError,
+            Message::MessageID::UnknownInPhrase2IR,
+            m_IR.Name,
+            ast.Location,
+            {ast.Name, e.what()}
+        }
+        );
+
+        return false;
+    }
+}
+
+IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteSequenceStatement& ast)
+{
+    auto newIndex = IR::BlockReference{m_IR.Blocks.size()};
+    m_IR.Blocks.emplace_back();
+
+    {
+        m_AttributeStack.push_back(ast.Attributes);
+        AutoPop<decltype(m_AttributeStack)> autoPop(m_AttributeStack);
+
+        // with bounds checking
+        m_IR.Blocks.at(newIndex.ID).Attributes = Concat(m_AttributeStack);
+
+        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)
 {
+    auto newIndex = IR::BlockReference{m_IR.Blocks.size()};
+    m_IR.Blocks.emplace_back();
+
     m_AttributeStack.push_back(ast.Attributes);
+    AutoPop<decltype(m_AttributeStack)> autoPop(m_AttributeStack);
+
+    return {};
+}
+
+IR::BlockReference Phrase2IRCompiler::operator()(const AST::NoteSequence& ast)
+{
+    auto newIndex = IR::BlockReference{m_IR.Blocks.size()};
+    m_IR.Blocks.emplace_back();
 
-    AutoPop<decltype(m_AttributeStack)> ap(m_AttributeStack);
+    // bounds checking
+    m_IR.Blocks.at(newIndex.ID);
 
-    return (*this)(ast.Block);
+    for (auto&& i : ast.Notes)
+    {
+        m_IR.Blocks[newIndex.ID].Events.emplace_back((*this)(i));
+    }
+
+    return newIndex;
 }
 
-bool Phrase2IRCompiler::operator()(const AST::NoteSequenceBlockWithoutAttributes& ast)
+void Phrase2IRCompiler::Compile(const AST::NoteSequenceBlockWithoutAttributes& ast, IR::BlockReference index)
 {
-    return false;
+    // with bounds checking
+    m_IR.Blocks.at(index.ID).Attributes = Concat(m_AttributeStack);
+
+    for (auto&& i : ast.Sequences)
+    {
+        m_IR.Blocks[index.ID].Events.emplace_back(i.apply_visitor(*this));
+    }
 }
 
 } // namespace AST2IR
index 9f27fc7..9fb63ef 100644 (file)
@@ -19,17 +19,23 @@ namespace YAMML
 namespace AST2IR
 {
 
-class Phrase2IRCompiler final : public Compiler::NestedCompilerBase, public boost::static_visitor<bool>
+class Phrase2IRCompiler final : public Compiler::NestedCompilerBase, public boost::static_visitor<IR::BlockReference>
 {
 public:
     Phrase2IRCompiler(Compiler::CompilerBase& parentCompiler, IR::Module& ir);
 
-    // Compiles ast into m_IR.Blocks[index]
-    bool Compile(const AST::Phrase& ast, std::size_t index);
+    // Compiles ast into m_IR.Blocks[index.ID]
+    bool Compile(const AST::Phrase& ast, IR::BlockReference index);
 
-    bool operator()(const AST::NoteSequenceBlockWithoutAttributes& ast);
+    IR::BlockReference operator()(const AST::NoteSequenceStatement& ast);
+    IR::BlockReference operator()(const AST::NoteSequenceBlock& ast);
+
+    IR::BlockReference operator()(const AST::NoteSequence& ast);
+    IR::BlockReference operator()(const AST::NoteAndExpression& ast);
 
 private:
+    void Compile(const AST::NoteSequenceBlockWithoutAttributes& ast, IR::BlockReference index);
+
     IR::Module& m_IR;
     std::deque<std::vector<AST::Attribute>> m_AttributeStack;
 };