From: Mikko Rasa Date: Tue, 14 Nov 2017 11:00:18 +0000 (+0200) Subject: Derive ProgramCompiler::DeclarationCombiner from BlockModifier X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6dc2da27f0831d4172fcfeba4900616fd6c844b8;p=libs%2Fgl.git Derive ProgramCompiler::DeclarationCombiner from BlockModifier --- diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index c54a33d6..c35fa275 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -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 >::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 >::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 set(toplevel, false); - for(list >::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 >::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 >::iterator i=block.body.begin(); i!=block.body.end(); ) - { - (*i)->visit(*this); - apply_and_increment(block, i); - } -} - - ProgramCompiler::InterfaceGenerator::InterfaceGenerator(): scope_level(0) { } diff --git a/source/programcompiler.h b/source/programcompiler.h index daf2a5db..6b1e1cda 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -26,6 +26,19 @@ private: void get_result() const { } }; + struct BlockModifier: Visitor + { + bool remove_node; + std::list > insert_nodes; + + BlockModifier(); + + void flatten_block(ProgramSyntax::Block &); + void apply_and_increment(ProgramSyntax::Block &, std::list >::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 > functions; std::map variables; - bool remove_node; DeclarationCombiner(); @@ -125,19 +137,6 @@ private: virtual void visit(ProgramSyntax::FunctionDeclaration &); }; - struct BlockModifier: Visitor - { - bool remove_node; - std::list > insert_nodes; - - BlockModifier(); - - void flatten_block(ProgramSyntax::Block &); - void apply_and_increment(ProgramSyntax::Block &, std::list >::iterator &); - using Visitor::visit; - virtual void visit(ProgramSyntax::Block &); - }; - struct InterfaceGenerator: BlockModifier { std::string in_prefix;