1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! RED PHASE Tests for Break/Continue Statements (DECY-042)
//!
//! These tests are EXPECTED TO FAIL initially.
//! They define the behavior we want to implement for:
//! - Break statements in loops
//! - Continue statements in loops
//! - Break/continue in nested loops
use super::*;
#[cfg(test)]
mod tests {
use super::*;
// ============================================================================
// RED PHASE: Break statement in while loop
// ============================================================================
#[test]
fn test_break_in_while_loop() {
// RED: Test that break is parsed correctly in a while loop
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int find_value(int target) {
int i = 0;
while (i < 100) {
if (i == target) {
break;
}
i = i + 1;
}
return i;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Check while loop structure
assert_eq!(func.body.len(), 3, "Should have 3 statements: i=0, while, return");
if let Statement::While { body, .. } = &func.body[1] {
assert_eq!(body.len(), 2, "While loop should have 2 statements");
// First statement should be an if with break inside
if let Statement::If { then_block, .. } = &body[0] {
assert_eq!(then_block.len(), 1, "If block should have 1 statement");
// RED: This will fail because we don't have Break yet
match &then_block[0] {
Statement::Break => {
// Success - we found a break statement
}
other => panic!("Expected Break statement, got {:?}", other),
}
} else {
panic!("Expected if statement as first statement in while loop");
}
} else {
panic!("Expected while loop as second statement");
}
}
// ============================================================================
// RED PHASE: Continue statement in while loop
// ============================================================================
#[test]
fn test_continue_in_while_loop() {
// RED: Test that continue is parsed correctly in a while loop
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int count_even(int n) {
int i = 0;
int count = 0;
while (i < n) {
i = i + 1;
if (i % 2 == 1) {
continue;
}
count = count + 1;
}
return count;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Check while loop structure
if let Statement::While { body, .. } = &func.body[2] {
assert_eq!(body.len(), 3, "While loop should have 3 statements");
// Second statement should be an if with continue inside
if let Statement::If { then_block, .. } = &body[1] {
assert_eq!(then_block.len(), 1, "If block should have 1 statement");
// RED: This will fail because we don't have Continue yet
match &then_block[0] {
Statement::Continue => {
// Success - we found a continue statement
}
other => panic!("Expected Continue statement, got {:?}", other),
}
} else {
panic!("Expected if statement as second statement in while loop");
}
} else {
panic!("Expected while loop as third statement");
}
}
// ============================================================================
// RED PHASE: Break statement in for loop
// ============================================================================
#[test]
fn test_break_in_for_loop() {
// RED: Test that break is parsed correctly in a for loop
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int find_divisor(int n) {
int i;
for (i = 2; i < n; i = i + 1) {
if (n % i == 0) {
break;
}
}
return i;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Check for loop structure
// Note: Variable declarations before for loops are parsed as separate statements
assert!(func.body.len() >= 2, "Should have at least 2 statements");
// Find the for loop (may not be first statement if variables declared before it)
let for_stmt_idx = func
.body
.iter()
.position(|s| matches!(s, Statement::For { .. }))
.expect("Should have a for loop");
if let Statement::For { body, .. } = &func.body[for_stmt_idx] {
assert_eq!(body.len(), 1, "For loop should have 1 statement");
// Should be an if with break inside
if let Statement::If { then_block, .. } = &body[0] {
assert_eq!(then_block.len(), 1, "If block should have 1 statement");
// RED: This will fail because we don't have Break yet
match &then_block[0] {
Statement::Break => {
// Success
}
other => panic!("Expected Break statement, got {:?}", other),
}
} else {
panic!("Expected if statement in for loop");
}
} else {
panic!("Expected for loop as first statement");
}
}
// ============================================================================
// RED PHASE: Continue statement in for loop
// ============================================================================
#[test]
fn test_continue_in_for_loop() {
// RED: Test that continue is parsed correctly in a for loop
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int sum_even(int n) {
int sum = 0;
int i;
for (i = 0; i < n; i = i + 1) {
if (i % 2 == 1) {
continue;
}
sum = sum + i;
}
return sum;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Find the for loop (may not be at index 1 if variables declared)
let for_stmt_idx = func
.body
.iter()
.position(|s| matches!(s, Statement::For { .. }))
.expect("Should have a for loop");
if let Statement::For { body, .. } = &func.body[for_stmt_idx] {
assert_eq!(body.len(), 2, "For loop should have 2 statements");
// First statement should be an if with continue inside
if let Statement::If { then_block, .. } = &body[0] {
assert_eq!(then_block.len(), 1, "If block should have 1 statement");
// RED: This will fail because we don't have Continue yet
match &then_block[0] {
Statement::Continue => {
// Success
}
other => panic!("Expected Continue statement, got {:?}", other),
}
} else {
panic!("Expected if statement in for loop");
}
} else {
panic!("Expected for loop as second statement");
}
}
// ============================================================================
// RED PHASE: Nested loops with break
// ============================================================================
#[test]
fn test_break_in_nested_loops() {
// RED: Test that break only exits the innermost loop
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int find_pair(int target) {
int i;
int j;
for (i = 0; i < 10; i = i + 1) {
for (j = 0; j < 10; j = j + 1) {
if (i + j == target) {
break;
}
}
}
return i;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Find the outer for loop
let for_stmt_idx = func
.body
.iter()
.position(|s| matches!(s, Statement::For { .. }))
.expect("Should have a for loop");
// Outer for loop
if let Statement::For { body: outer_body, .. } = &func.body[for_stmt_idx] {
// Inner for loop
if let Statement::For { body: inner_body, .. } = &outer_body[0] {
// If statement with break
if let Statement::If { then_block, .. } = &inner_body[0] {
// RED: This will fail
match &then_block[0] {
Statement::Break => {
// Success - break parsed in nested loop
}
other => panic!("Expected Break statement, got {:?}", other),
}
} else {
panic!("Expected if statement in inner loop");
}
} else {
panic!("Expected inner for loop");
}
} else {
panic!("Expected outer for loop");
}
}
// ============================================================================
// RED PHASE: Nested loops with continue
// ============================================================================
#[test]
fn test_continue_in_nested_loops() {
// RED: Test that continue only affects the innermost loop
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int count_pairs(int limit) {
int count = 0;
int i;
int j;
for (i = 0; i < 10; i = i + 1) {
for (j = 0; j < 10; j = j + 1) {
if (i == j) {
continue;
}
if (i + j < limit) {
count = count + 1;
}
}
}
return count;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Find the outer for loop
let for_stmt_idx = func
.body
.iter()
.position(|s| matches!(s, Statement::For { .. }))
.expect("Should have a for loop");
// Navigate to nested loop structure
if let Statement::For { body: outer_body, .. } = &func.body[for_stmt_idx] {
if let Statement::For { body: inner_body, .. } = &outer_body[0] {
// First if with continue
if let Statement::If { then_block, .. } = &inner_body[0] {
// RED: This will fail
match &then_block[0] {
Statement::Continue => {
// Success
}
other => panic!("Expected Continue statement, got {:?}", other),
}
} else {
panic!("Expected if statement in inner loop");
}
} else {
panic!("Expected inner for loop");
}
} else {
panic!("Expected outer for loop");
}
}
// NOTE: We don't test break/continue outside loops because clang itself
// rejects these as syntax errors during parsing. Semantic validation
// is handled by clang before we even see the AST.
// ============================================================================
// RED PHASE: Real-world pattern - early exit from search
// ============================================================================
#[test]
fn test_break_in_search_pattern() {
// RED: Test common pattern of breaking when found
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int linear_search(int* arr, int size, int target) {
int i;
int found = 0;
for (i = 0; i < size; i = i + 1) {
if (arr[i] == target) {
found = 1;
break;
}
}
return found;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Find the for loop
let for_stmt_idx = func
.body
.iter()
.position(|s| matches!(s, Statement::For { .. }))
.expect("Should have a for loop");
// Navigate to for loop
if let Statement::For { body, .. } = &func.body[for_stmt_idx] {
// If statement
if let Statement::If { then_block, .. } = &body[0] {
assert_eq!(then_block.len(), 2, "If block should have 2 statements");
// Second statement should be break
// RED: This will fail
match &then_block[1] {
Statement::Break => {
// Success
}
other => panic!("Expected Break statement, got {:?}", other),
}
} else {
panic!("Expected if statement in for loop");
}
} else {
panic!("Expected for loop");
}
}
// ============================================================================
// RED PHASE: Real-world pattern - skip processing with continue
// ============================================================================
#[test]
fn test_continue_skip_pattern() {
// RED: Test common pattern of skipping certain values
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int sum_positive(int* arr, int size) {
int sum = 0;
int i;
for (i = 0; i < size; i = i + 1) {
if (arr[i] < 0) {
continue;
}
sum = sum + arr[i];
}
return sum;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Find the for loop
let for_stmt_idx = func
.body
.iter()
.position(|s| matches!(s, Statement::For { .. }))
.expect("Should have a for loop");
// Navigate to for loop
if let Statement::For { body, .. } = &func.body[for_stmt_idx] {
assert_eq!(body.len(), 2, "For loop should have 2 statements");
// First statement is if with continue
if let Statement::If { then_block, .. } = &body[0] {
assert_eq!(then_block.len(), 1, "If block should have 1 statement");
// RED: This will fail
match &then_block[0] {
Statement::Continue => {
// Success
}
other => panic!("Expected Continue statement, got {:?}", other),
}
} else {
panic!("Expected if statement in for loop");
}
} else {
panic!("Expected for loop");
}
}
}