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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! RED PHASE Tests for Pointer Arithmetic (DECY-041)
//!
//! These tests are EXPECTED TO FAIL initially.
//! They define the behavior we want to implement for:
//! - Increment/decrement operators (++, --)
//! - Compound assignment operators (+=, -=, *=, /=, %=)
//! - Pointer arithmetic patterns
use super::*;
#[cfg(test)]
mod tests {
use super::*;
// ============================================================================
// RED PHASE: Post-increment operator (ptr++)
// ============================================================================
#[test]
fn test_post_increment_in_for_loop() {
// RED: Test that ptr++ is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void traverse(int* arr, int size) {
int* ptr = arr;
int i;
for (i = 0; i < size; i++) {
ptr++;
}
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Check that ptr++ appears in the for loop body
if let Statement::For { body, .. } = &func.body[2] {
// Should have ptr++ as a statement
assert_eq!(body.len(), 1, "For loop should have 1 statement");
// RED: This will fail because we don't have PostIncrement yet
match &body[0] {
Statement::PostIncrement { target } => {
assert_eq!(target, "ptr", "Should increment ptr");
}
other => panic!("Expected PostIncrement statement, got {:?}", other),
}
} else {
panic!("Expected for loop as third statement");
}
}
#[test]
fn test_pre_increment() {
// RED: Test that ++ptr is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int* ptr;
++ptr;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail because we don't have PreIncrement yet
match &func.body[1] {
Statement::PreIncrement { target } => {
assert_eq!(target, "ptr", "Should increment ptr");
}
other => panic!("Expected PreIncrement statement, got {:?}", other),
}
}
#[test]
fn test_post_decrement() {
// RED: Test that ptr-- is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int* ptr;
ptr--;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail because we don't have PostDecrement yet
match &func.body[1] {
Statement::PostDecrement { target } => {
assert_eq!(target, "ptr", "Should decrement ptr");
}
other => panic!("Expected PostDecrement statement, got {:?}", other),
}
}
#[test]
fn test_pre_decrement() {
// RED: Test that --ptr is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int* ptr;
--ptr;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail because we don't have PreDecrement yet
match &func.body[1] {
Statement::PreDecrement { target } => {
assert_eq!(target, "ptr", "Should decrement ptr");
}
other => panic!("Expected PreDecrement statement, got {:?}", other),
}
}
// ============================================================================
// RED PHASE: Compound assignment operators (+=, -=, etc.)
// ============================================================================
#[test]
fn test_plus_equals_operator() {
// RED: Test that += is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int* ptr;
int offset = 5;
ptr += offset;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail because we don't have CompoundAssignment yet
match &func.body[2] {
Statement::CompoundAssignment { target, op, value } => {
assert_eq!(target, "ptr", "Should assign to ptr");
assert_eq!(*op, BinaryOperator::Add, "Should use + operator");
match value {
Expression::Variable(name) => assert_eq!(name, "offset"),
_ => panic!("Expected variable 'offset' as value"),
}
}
other => panic!("Expected CompoundAssignment statement, got {:?}", other),
}
}
#[test]
fn test_minus_equals_operator() {
// RED: Test that -= is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int* ptr;
ptr -= 3;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail
match &func.body[1] {
Statement::CompoundAssignment { target, op, value } => {
assert_eq!(target, "ptr", "Should assign to ptr");
assert_eq!(*op, BinaryOperator::Subtract, "Should use - operator");
match value {
Expression::IntLiteral(n) => assert_eq!(*n, 3),
_ => panic!("Expected int literal 3"),
}
}
other => panic!("Expected CompoundAssignment statement, got {:?}", other),
}
}
#[test]
fn test_multiply_equals_operator() {
// RED: Test that *= is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int x = 10;
x *= 2;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail
match &func.body[1] {
Statement::CompoundAssignment { target, op, value: _ } => {
assert_eq!(target, "x", "Should assign to x");
assert_eq!(*op, BinaryOperator::Multiply, "Should use * operator");
}
other => panic!("Expected CompoundAssignment statement, got {:?}", other),
}
}
#[test]
fn test_divide_equals_operator() {
// RED: Test that /= is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int x = 10;
x /= 2;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail
match &func.body[1] {
Statement::CompoundAssignment { target, op, value: _ } => {
assert_eq!(target, "x", "Should assign to x");
assert_eq!(*op, BinaryOperator::Divide, "Should use / operator");
}
other => panic!("Expected CompoundAssignment statement, got {:?}", other),
}
}
#[test]
fn test_modulo_equals_operator() {
// RED: Test that %= is parsed correctly
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int x = 10;
x %= 3;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail
match &func.body[1] {
Statement::CompoundAssignment { target, op, value: _ } => {
assert_eq!(target, "x", "Should assign to x");
assert_eq!(*op, BinaryOperator::Modulo, "Should use % operator");
}
other => panic!("Expected CompoundAssignment statement, got {:?}", other),
}
}
// ============================================================================
// RED PHASE: Increment/Decrement in expressions (not statements)
// ============================================================================
#[test]
fn test_post_increment_in_expression() {
// RED: Test that ptr++ can be used in expressions
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int test() {
int* ptr;
int x;
x = *ptr++;
return x;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail because we don't support ++ in expressions yet
match &func.body[2] {
Statement::Assignment { target, value } => {
assert_eq!(target, "x");
// value should be Dereference(PostIncrement(ptr))
match value {
Expression::Dereference(inner) => match &**inner {
Expression::PostIncrement { operand } => match &**operand {
Expression::Variable(name) => assert_eq!(name, "ptr"),
_ => panic!("Expected variable ptr"),
},
_ => panic!("Expected PostIncrement inside dereference"),
},
_ => panic!("Expected dereference expression"),
}
}
other => panic!("Expected assignment, got {:?}", other),
}
}
#[test]
fn test_pre_increment_in_expression() {
// RED: Test that ++ptr can be used in expressions
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int test() {
int* ptr;
int x;
x = *++ptr;
return x;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// RED: This will fail
match &func.body[2] {
Statement::Assignment { target, value } => {
assert_eq!(target, "x");
// value should be Dereference(PreIncrement(ptr))
match value {
Expression::Dereference(inner) => match &**inner {
Expression::PreIncrement { operand } => match &**operand {
Expression::Variable(name) => assert_eq!(name, "ptr"),
_ => panic!("Expected variable ptr"),
},
_ => panic!("Expected PreIncrement inside dereference"),
},
_ => panic!("Expected dereference expression"),
}
}
other => panic!("Expected assignment, got {:?}", other),
}
}
// ============================================================================
// RED PHASE: Real-world pointer arithmetic patterns
// ============================================================================
#[test]
fn test_array_traversal_pattern() {
// RED: Test common array traversal with pointer arithmetic
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int sum_array(int* arr, int size) {
int sum = 0;
int* end = arr + size;
while (arr < end) {
sum += *arr;
arr++;
}
return sum;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Should have: sum=0, end=arr+size, while loop with arr++ inside
assert_eq!(func.body.len(), 4, "Should have 4 statements");
// RED: Multiple things will fail here:
// 1. arr + size in pointer context
// 2. += operator
// 3. arr++ statement
}
#[test]
fn test_pointer_difference() {
// RED: Test pointer subtraction for size calculation
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
int get_size(int* start, int* end) {
return end - start;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Should parse end - start as binary operation
match &func.body[0] {
Statement::Return(Some(expr)) => {
match expr {
Expression::BinaryOp { op, left: _, right: _ } => {
assert_eq!(*op, BinaryOperator::Subtract);
// This should work already, but let's verify
}
_ => panic!("Expected binary operation"),
}
}
_ => panic!("Expected return statement"),
}
}
// ============================================================================
// DECY-185: Compound assignment to MemberAccess and Dereference targets
// These tests verify that compound assignments like `sb->capacity *= 2;`
// and `*ptr *= 2;` are correctly parsed and not dropped.
// ============================================================================
#[test]
fn test_decy185_compound_assignment_to_deref() {
// DECY-185: `*ptr *= 2;` should be parsed as DerefCompoundAssignment
// Currently fails because extract_compound_assignment_stmt only handles
// Variable targets, returning None for Dereference targets.
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void double_value(int* ptr) {
*ptr *= 2;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// The body should NOT be empty - it should contain the compound assignment
assert!(
!func.body.is_empty(),
"DECY-185: Function body should not be empty, *ptr *= 2 was dropped!"
);
// Should parse as DerefCompoundAssignment
match &func.body[0] {
Statement::DerefCompoundAssignment { target, op, value } => {
// Target should be the variable 'ptr' (the thing being dereferenced)
match target {
Expression::Variable(name) => assert_eq!(name, "ptr"),
_ => panic!("Expected Variable target 'ptr', got {:?}", target),
}
assert_eq!(*op, BinaryOperator::Multiply, "Should use * operator");
match value {
Expression::IntLiteral(2) => {}
_ => panic!("Expected int literal 2 as value, got {:?}", value),
}
}
other => {
panic!("DECY-185: Expected DerefCompoundAssignment for *ptr *= 2, got {:?}", other)
}
}
}
#[test]
fn test_decy185_compound_assignment_to_struct_field() {
// DECY-185: `sb->capacity *= 2;` should be parsed as DerefCompoundAssignment
// Currently fails because extract_compound_assignment_stmt only handles
// Variable targets, returning None for PointerFieldAccess targets.
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
typedef struct { int capacity; } Struct;
void double_capacity(Struct* sb) {
sb->capacity *= 2;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// The body should NOT be empty - it should contain the compound assignment
assert!(
!func.body.is_empty(),
"DECY-185: Function body should not be empty, sb->capacity *= 2 was dropped!"
);
// Should parse as DerefCompoundAssignment with PointerFieldAccess target
match &func.body[0] {
Statement::DerefCompoundAssignment { target, op, value } => {
// Target should be PointerFieldAccess (sb->capacity)
match target {
Expression::PointerFieldAccess { pointer, field } => {
assert_eq!(field, "capacity", "Field should be 'capacity'");
// Pointer should be 'sb'
match &**pointer {
Expression::Variable(name) => assert_eq!(name, "sb"),
_ => panic!("Expected Variable 'sb' as pointer, got {:?}", pointer),
}
}
_ => panic!(
"Expected PointerFieldAccess target for sb->capacity, got {:?}",
target
),
}
assert_eq!(*op, BinaryOperator::Multiply, "Should use * operator");
match value {
Expression::IntLiteral(2) => {}
_ => panic!("Expected int literal 2 as value, got {:?}", value),
}
}
other => panic!(
"DECY-185: Expected DerefCompoundAssignment for sb->capacity *= 2, got {:?}",
other
),
}
}
#[test]
fn test_decy185_while_loop_body_with_compound_assignment() {
// DECY-185: While loop body should NOT be empty when containing
// compound assignment to struct field
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
typedef struct { int capacity; } Struct;
void grow_capacity(Struct* sb, int target) {
while (sb->capacity < target) {
sb->capacity *= 2;
}
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
// Should have one while loop statement
assert_eq!(func.body.len(), 1, "Should have one while loop");
match &func.body[0] {
Statement::While { condition: _, body } => {
// The while body should NOT be empty!
assert!(
!body.is_empty(),
"DECY-185: While loop body should not be empty! sb->capacity *= 2 was dropped!"
);
// Should contain the compound assignment
assert_eq!(body.len(), 1, "While body should have one statement");
}
other => panic!("Expected While statement, got {:?}", other),
}
}
}