]> git.tdb.fi Git - libs/gl.git/commitdiff
Derive ProgramCompiler::DeclarationCombiner from BlockModifier
authorMikko Rasa <tdb@tdb.fi>
Tue, 14 Nov 2017 11:00:18 +0000 (13:00 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 14 Nov 2017 11:00:18 +0000 (13:00 +0200)
source/programcompiler.cpp
source/programcompiler.h

index c54a33d6b6fbf0147fe21dd069db9f391d42cc16..c35fa275c2b1bbadbcfbfb8f113cf68d165d4a8a 100644 (file)
@@ -230,6 +230,38 @@ void ProgramCompiler::Visitor::apply(Stage &s)
 }
 
 
+ProgramCompiler::BlockModifier::BlockModifier():
+       remove_node(false)
+{ }
+
+void ProgramCompiler::BlockModifier::flatten_block(Block &block)
+{
+       insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
+       remove_node = true;
+}
+
+void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Node> >::iterator &i)
+{
+       block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
+       insert_nodes.clear();
+
+       if(remove_node)
+               block.body.erase(i++);
+       else
+               ++i;
+       remove_node = false;
+}
+
+void ProgramCompiler::BlockModifier::visit(Block &block)
+{
+       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       {
+               (*i)->visit(*this);
+               apply_and_increment(block, i);
+       }
+}
+
+
 ProgramCompiler::Formatter::Formatter():
        indent(0),
        parameter_list(false),
@@ -496,8 +528,7 @@ void ProgramCompiler::Formatter::visit(Jump &jump)
 
 
 ProgramCompiler::DeclarationCombiner::DeclarationCombiner():
-       toplevel(true),
-       remove_node(false)
+       toplevel(true)
 { }
 
 void ProgramCompiler::DeclarationCombiner::visit(Block &block)
@@ -506,15 +537,7 @@ void ProgramCompiler::DeclarationCombiner::visit(Block &block)
                return;
 
        SetForScope<bool> set(toplevel, false);
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
-       {
-               remove_node = false;
-               (*i)->visit(*this);
-               if(remove_node)
-                       block.body.erase(i++);
-               else
-                       ++i;
-       }
+       BlockModifier::visit(block);
 }
 
 void ProgramCompiler::DeclarationCombiner::visit(FunctionDeclaration &func)
@@ -714,38 +737,6 @@ void ProgramCompiler::FunctionResolver::visit(FunctionDeclaration &func)
 }
 
 
-ProgramCompiler::BlockModifier::BlockModifier():
-       remove_node(false)
-{ }
-
-void ProgramCompiler::BlockModifier::flatten_block(Block &block)
-{
-       insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
-       remove_node = true;
-}
-
-void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Node> >::iterator &i)
-{
-       block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
-       insert_nodes.clear();
-
-       if(remove_node)
-               block.body.erase(i++);
-       else
-               ++i;
-       remove_node = false;
-}
-
-void ProgramCompiler::BlockModifier::visit(Block &block)
-{
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
-       {
-               (*i)->visit(*this);
-               apply_and_increment(block, i);
-       }
-}
-
-
 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
        scope_level(0)
 { }
index daf2a5db7904988f9e782e300b25e88ebcc47835..6b1e1cdafe79f9be2ad0df6858cd89b95942e859 100644 (file)
@@ -26,6 +26,19 @@ private:
                void get_result() const { }
        };
 
+       struct BlockModifier: Visitor
+       {
+               bool remove_node;
+               std::list<RefPtr<ProgramSyntax::Node> > insert_nodes;
+
+               BlockModifier();
+
+               void flatten_block(ProgramSyntax::Block &);
+               void apply_and_increment(ProgramSyntax::Block &, std::list<RefPtr<ProgramSyntax::Node> >::iterator &);
+               using Visitor::visit;
+               virtual void visit(ProgramSyntax::Block &);
+       };
+
        struct Formatter: Visitor
        {
                typedef std::string ResultType;
@@ -77,12 +90,11 @@ private:
                virtual void visit(T &n) { nodes.push_back(&n); }
        };
 
-       struct DeclarationCombiner: Visitor
+       struct DeclarationCombiner: BlockModifier
        {
                bool toplevel;
                std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
                std::map<std::string, ProgramSyntax::VariableDeclaration *> variables;
-               bool remove_node;
 
                DeclarationCombiner();
 
@@ -125,19 +137,6 @@ private:
                virtual void visit(ProgramSyntax::FunctionDeclaration &);
        };
 
-       struct BlockModifier: Visitor
-       {
-               bool remove_node;
-               std::list<RefPtr<ProgramSyntax::Node> > insert_nodes;
-
-               BlockModifier();
-
-               void flatten_block(ProgramSyntax::Block &);
-               void apply_and_increment(ProgramSyntax::Block &, std::list<RefPtr<ProgramSyntax::Node> >::iterator &);
-               using Visitor::visit;
-               virtual void visit(ProgramSyntax::Block &);
-       };
-
        struct InterfaceGenerator: BlockModifier
        {
                std::string in_prefix;