Skip to content

Commit

Permalink
if both sides of the cond stop - block section stops
Browse files Browse the repository at this point in the history
  • Loading branch information
borisbat committed Feb 8, 2024
1 parent 3a7e611 commit 7630f61
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 7630f61

Please sign in to comment.