From: Mikko Rasa Date: Fri, 21 Jun 2019 10:19:47 +0000 (+0300) Subject: Add an intermediate node type for statements X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=16a9d75c8e124a0faf89c56533792cb97a214e37;p=libs%2Fgl.git Add an intermediate node type for statements --- diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 9a3986a1..ca3913cd 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -168,7 +168,7 @@ void ProgramCompiler::append_stage(Stage &stage) target = &*i; } - for(NodeList::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i) + for(NodeList::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i) target->content.body.push_back(*i); apply(*target); } @@ -241,8 +241,8 @@ void ProgramCompiler::finalize(Stage &stage) void ProgramCompiler::inject_block(Block &target, const Block &source) { - list >::iterator insert_point = target.body.begin(); - for(list >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i) + list >::iterator insert_point = target.body.begin(); + for(list >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i) target.body.insert(insert_point, (*i)->clone()); } @@ -284,7 +284,7 @@ void ProgramCompiler::BlockModifier::flatten_block(Block &block) remove_node = true; } -void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list >::iterator &i) +void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list >::iterator &i) { block.body.insert(i, insert_nodes.begin(), insert_nodes.end()); insert_nodes.clear(); @@ -298,7 +298,7 @@ void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list >::iterator i=block.body.begin(); i!=block.body.end(); ) + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) { (*i)->visit(*this); apply_and_increment(block, i); @@ -410,7 +410,7 @@ void ProgramCompiler::Formatter::visit(Block &block) SetForScope set(indent, indent+(indent>0 || use_braces)); string spaces(indent*2, ' '); - for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) { if(i!=block.body.begin()) formatted += '\n'; @@ -865,7 +865,7 @@ void ProgramCompiler::InterfaceGenerator::apply(Stage &s) void ProgramCompiler::InterfaceGenerator::visit(Block &block) { SetForScope set(scope_level, scope_level+1); - for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) { (*i)->visit(*this); @@ -873,7 +873,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block) { for(map >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j) { - list >::iterator k = block.body.insert(i, j->second); + list >::iterator k = block.body.insert(i, j->second); (*k)->visit(*this); } iface_declarations.clear(); @@ -1069,13 +1069,13 @@ void ProgramCompiler::DeclarationReorderer::visit(Block &block) if(scope_level>1) return Visitor::visit(block); - list >::iterator struct_insert_point = block.body.end(); - list >::iterator variable_insert_point = block.body.end(); - list >::iterator function_insert_point = block.body.end(); + list >::iterator struct_insert_point = block.body.end(); + list >::iterator variable_insert_point = block.body.end(); + list >::iterator function_insert_point = block.body.end(); unsigned unordered_func_count = 0; bool ordered_any_funcs = false; - for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) { kind = NO_DECLARATION; (*i)->visit(*this); @@ -1210,7 +1210,7 @@ void ProgramCompiler::FunctionInliner::visit(Block &block) if(extract_result) --extract_result; - for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) { (*i)->visit(*this); if(extract_result) @@ -1693,7 +1693,7 @@ ProgramCompiler::NodeRemover::NodeRemover(const set &r): void ProgramCompiler::NodeRemover::visit(Block &block) { - for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) { (*i)->visit(*this); if(to_remove.count(i->get())) diff --git a/source/programcompiler.h b/source/programcompiler.h index 76e9eef3..5a2f4240 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -29,12 +29,12 @@ private: struct BlockModifier: Visitor { bool remove_node; - std::vector > insert_nodes; + std::vector > insert_nodes; BlockModifier(); void flatten_block(ProgramSyntax::Block &); - void apply_and_increment(ProgramSyntax::Block &, std::list >::iterator &); + void apply_and_increment(ProgramSyntax::Block &, std::list >::iterator &); using Visitor::visit; virtual void visit(ProgramSyntax::Block &); }; diff --git a/source/programparser.cpp b/source/programparser.cpp index 4f08b977..07fa19de 100644 --- a/source/programparser.cpp +++ b/source/programparser.cpp @@ -112,7 +112,7 @@ void ProgramParser::parse_source() source_end = source.end(); current_line = 1; allow_preprocess = true; - while(RefPtr statement = parse_global_declaration()) + while(RefPtr statement = parse_global_declaration()) cur_stage->content.body.push_back(statement); } @@ -426,7 +426,7 @@ void ProgramParser::preprocess_stage() cur_stage = &module->stages.back(); } -RefPtr ProgramParser::parse_global_declaration() +RefPtr ProgramParser::parse_global_declaration() { allow_stage_change = true; string token = peek_token(); @@ -480,7 +480,7 @@ RefPtr ProgramParser::parse_global_declaration() throw runtime_error(format_syntax_error("a global declaration")); } -RefPtr ProgramParser::parse_statement() +RefPtr ProgramParser::parse_statement() { string token = peek_token(); if(token=="if") diff --git a/source/programparser.h b/source/programparser.h index 70781540..72817e7e 100644 --- a/source/programparser.h +++ b/source/programparser.h @@ -90,8 +90,8 @@ private: void preprocess_pragma_msp(); void preprocess_stage(); - RefPtr parse_global_declaration(); - RefPtr parse_statement(); + RefPtr parse_global_declaration(); + RefPtr parse_statement(); RefPtr parse_import(); RefPtr parse_precision(); RefPtr parse_layout(); diff --git a/source/programsyntax.cpp b/source/programsyntax.cpp index 47b59f6c..46f51c2c 100644 --- a/source/programsyntax.cpp +++ b/source/programsyntax.cpp @@ -213,7 +213,7 @@ void NodeVisitor::visit(Assignment &assign) void TraversingVisitor::visit(Block &block) { - for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) (*i)->visit(*this); } diff --git a/source/programsyntax.h b/source/programsyntax.h index 1f26bc56..3db059c4 100644 --- a/source/programsyntax.h +++ b/source/programsyntax.h @@ -64,9 +64,14 @@ struct StructDeclaration; struct VariableDeclaration; struct FunctionDeclaration; +struct Statement: Node +{ + virtual Statement *clone() const = 0; +}; + struct Block: Node { - NodeList body; + NodeList body; bool use_braces; std::map types; std::map variables; @@ -166,7 +171,7 @@ struct FunctionCall: Expression virtual void visit(NodeVisitor &); }; -struct ExpressionStatement: Node +struct ExpressionStatement: Statement { NodePtr expression; @@ -174,7 +179,7 @@ struct ExpressionStatement: Node virtual void visit(NodeVisitor &); }; -struct Import: Node +struct Import: Statement { std::string module; @@ -182,7 +187,7 @@ struct Import: Node virtual void visit(NodeVisitor &); }; -struct Precision: Node +struct Precision: Statement { std::string precision; std::string type; @@ -206,7 +211,7 @@ struct Layout: Node virtual void visit(NodeVisitor &); }; -struct InterfaceLayout: Node +struct InterfaceLayout: Statement { std::string interface; Layout layout; @@ -215,7 +220,7 @@ struct InterfaceLayout: Node virtual void visit(NodeVisitor &); }; -struct StructDeclaration: Node +struct StructDeclaration: Statement { std::string name; Block members; @@ -226,7 +231,7 @@ struct StructDeclaration: Node virtual void visit(NodeVisitor &); }; -struct VariableDeclaration: Node +struct VariableDeclaration: Statement { bool constant; std::string sampling; @@ -248,7 +253,7 @@ struct VariableDeclaration: Node virtual void visit(NodeVisitor &); }; -struct InterfaceBlock: Node +struct InterfaceBlock: Statement { std::string interface; std::string name; @@ -262,7 +267,7 @@ struct InterfaceBlock: Node virtual void visit(NodeVisitor &); }; -struct FunctionDeclaration: Node +struct FunctionDeclaration: Statement { std::string return_type; std::string name; @@ -277,7 +282,7 @@ struct FunctionDeclaration: Node virtual void visit(NodeVisitor &); }; -struct Conditional: Node +struct Conditional: Statement { NodePtr condition; Block body; @@ -287,7 +292,7 @@ struct Conditional: Node virtual void visit(NodeVisitor &); }; -struct Iteration: Node +struct Iteration: Statement { NodePtr init_statement; NodePtr condition; @@ -298,7 +303,7 @@ struct Iteration: Node virtual void visit(NodeVisitor &); }; -struct Passthrough: Node +struct Passthrough: Statement { NodePtr subscript; @@ -306,7 +311,7 @@ struct Passthrough: Node virtual void visit(NodeVisitor &); }; -struct Return: Node +struct Return: Statement { NodePtr expression; @@ -314,7 +319,7 @@ struct Return: Node virtual void visit(NodeVisitor &); }; -struct Jump: Node +struct Jump: Statement { std::string keyword;