Skip to content

Commit

Permalink
Merge pull request #953 from GaijinEntertainment/clean_cond_stop
Browse files Browse the repository at this point in the history
if both sides of the cond stop - block section stops
  • Loading branch information
borisbat authored Feb 8, 2024
2 parents 3a7e611 + 7630f61 commit c724d94
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ast/ast_block_folding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@ namespace das {
}
};

bool doesExprTerminates ( const ExpressionPtr & expr ) {
if ( !expr ) return false;
if ( expr->rtti_isBlock() ) {
auto pBlock = static_pointer_cast<ExprBlock>(expr);
for ( auto & e : pBlock->list ) {
if ( e->rtti_isReturn() || e->rtti_isBreak() || e->rtti_isContinue() ) {
return true;
}
if ( e->rtti_isBlock() ) {
if ( doesExprTerminates(e) ) {
return true;
}
}
}
} else if ( expr->rtti_isIfThenElse() ) {
auto pIte = static_pointer_cast<ExprIfThenElse>(expr);
if ( doesExprTerminates(pIte->if_true) && doesExprTerminates(pIte->if_false) ) {
return true;
}
} else if ( expr->rtti_isWith() ) {
auto pWith = static_pointer_cast<ExprWith>(expr);
if ( pWith->body && doesExprTerminates(pWith->body) ) {
return true;
}
}
return false;
}

class BlockFolding : public PassVisitor {
protected:
das_set<int32_t> labels;
Expand Down Expand Up @@ -165,6 +193,16 @@ namespace das {
continue;
}
}
if ( expr->rtti_isIfThenElse() && doesExprTerminates(expr) ) {
if ( stopAtExit ) {
list.push_back(expr);
break;
} else {
list.push_back(expr);
skipTilLabel = true;
continue;
}
}
if ( expr->rtti_isBlock() ) {
auto pBlock = static_pointer_cast<ExprBlock>(expr);
if ( !pBlock->isClosure && !pBlock->finalList.size() ) {
Expand Down

0 comments on commit c724d94

Please sign in to comment.