From 48453f59ec93d9b8f70a17d3034e5ca3e4780ecd Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 26 May 2019 00:58:17 +0300 Subject: [PATCH] Support while loops in GLSL Loops with only a condition expression present are now emitted as while loops, even if they were originally for loops. --- source/programcompiler.cpp | 33 +++++++++++++++++++++------------ source/programparser.cpp | 19 +++++++++++++++++-- source/programparser.h | 3 ++- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index fd9c50db..afb2a3c3 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -543,23 +543,32 @@ void ProgramCompiler::Formatter::visit(Conditional &cond) void ProgramCompiler::Formatter::visit(Iteration &iter) { - formatted += "for("; - if(iter.init_statement) - iter.init_statement->visit(*this); - else - formatted += ';'; - if(iter.condition) + if(!iter.init_statement && iter.condition && !iter.loop_expression) { - formatted += ' '; + formatted += "while("; iter.condition->visit(*this); + formatted += ')'; } - formatted += ';'; - if(iter.loop_expression) + else { - formatted += ' '; - iter.loop_expression->visit(*this); + formatted += "for("; + if(iter.init_statement) + iter.init_statement->visit(*this); + else + formatted += ';'; + if(iter.condition) + { + formatted += ' '; + iter.condition->visit(*this); + } + formatted += ';'; + if(iter.loop_expression) + { + formatted += ' '; + iter.loop_expression->visit(*this); + } + formatted += ')'; } - formatted += ')'; if(iter.body.body.empty()) formatted += " { }"; diff --git a/source/programparser.cpp b/source/programparser.cpp index b607e1cf..c02adb32 100644 --- a/source/programparser.cpp +++ b/source/programparser.cpp @@ -484,7 +484,9 @@ RefPtr ProgramParser::parse_statement() if(token=="if") return parse_conditional(); else if(token=="for") - return parse_iteration(); + return parse_for(); + else if(token=="while") + return parse_while(); else if(token=="passthrough") return parse_passthrough(); else if(token=="return") @@ -843,7 +845,7 @@ RefPtr ProgramParser::parse_conditional() return cond; } -RefPtr ProgramParser::parse_iteration() +RefPtr ProgramParser::parse_for() { expect("for"); expect("("); @@ -873,6 +875,19 @@ RefPtr ProgramParser::parse_iteration() return loop; } +RefPtr ProgramParser::parse_while() +{ + expect("while"); + expect("("); + RefPtr loop = new Iteration; + loop->condition = parse_expression(); + expect(")"); + + parse_block(loop->body, false); + + return loop; +} + RefPtr ProgramParser::parse_passthrough() { expect("passthrough"); diff --git a/source/programparser.h b/source/programparser.h index 903e5834..70781540 100644 --- a/source/programparser.h +++ b/source/programparser.h @@ -104,7 +104,8 @@ private: RefPtr parse_function_declaration(); RefPtr parse_interface_block(); RefPtr parse_conditional(); - RefPtr parse_iteration(); + RefPtr parse_for(); + RefPtr parse_while(); RefPtr parse_passthrough(); RefPtr parse_return(); }; -- 2.45.2