@@ -111,12 +111,36 @@ static void foldStmt(Stmt* stmt) {
111111 break ;
112112 case STMT_IF :
113113 stmt -> as .if_stmt .condition = foldExpr (stmt -> as .if_stmt .condition );
114- foldStmt (stmt -> as .if_stmt .then_branch );
115- foldStmt (stmt -> as .if_stmt .else_branch );
114+ if (stmt -> as .if_stmt .condition -> type == EXPR_LITERAL ) {
115+ Value cond = stmt -> as .if_stmt .condition -> as .literal .value ;
116+ bool is_true = !IS_NIL (cond ) && (!IS_BOOL (cond ) || AS_BOOL (cond ));
117+ if (is_true ) {
118+ // Only keep then branch (simplified for now by just folding children)
119+ foldStmt (stmt -> as .if_stmt .then_branch );
120+ } else {
121+ // Only keep else branch
122+ if (stmt -> as .if_stmt .else_branch )
123+ foldStmt (stmt -> as .if_stmt .else_branch );
124+ }
125+ } else {
126+ foldStmt (stmt -> as .if_stmt .then_branch );
127+ foldStmt (stmt -> as .if_stmt .else_branch );
128+ }
116129 break ;
117130 case STMT_WHILE :
118131 stmt -> as .while_stmt .condition = foldExpr (stmt -> as .while_stmt .condition );
119- foldStmt (stmt -> as .while_stmt .body );
132+ if (stmt -> as .while_stmt .condition -> type == EXPR_LITERAL ) {
133+ Value cond = stmt -> as .while_stmt .condition -> as .literal .value ;
134+ bool is_true = !IS_NIL (cond ) && (!IS_BOOL (cond ) || AS_BOOL (cond ));
135+ if (!is_true ) {
136+ // Loop will never run. We could technically remove the statement.
137+ // For now, we just mark it as potentially skippable or do nothing.
138+ } else {
139+ foldStmt (stmt -> as .while_stmt .body );
140+ }
141+ } else {
142+ foldStmt (stmt -> as .while_stmt .body );
143+ }
120144 break ;
121145 case STMT_BLOCK :
122146 if (stmt -> as .block .statements ) {
0 commit comments