pascal 0.1.4

A modern Pascal compiler with build/intepreter/package manager built with Rust
Documentation
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
//! Comprehensive optimization tests for pascal-rs compiler
//! Tests constant folding, DCE, inlining, and other optimizations

use pascal::{parser::Parser, optimizer::Optimizer};
use pascal::{Expr, Stmt};

// Helper function to optimize an expression
fn optimize_expression(source: &str) -> Result<Expr, String> {
    let mut parser = Parser::new(source);
    match parser.parse_expression() {
        Ok(expr) => {
            let mut optimizer = Optimizer::new();
            Ok(optimizer.optimize_expr(expr))
        }
        Err(e) => Err(format!("Parse error: {:?}", e)),
    }
}

// Helper function to optimize a statement
fn optimize_statement(source: &str) -> Result<Stmt, String> {
    let mut parser = Parser::new(source);
    match parser.parse_statement() {
        Ok(stmt) => {
            let mut optimizer = Optimizer::new();
            Ok(optimizer.optimize_stmt(stmt))
        }
        Err(e) => Err(format!("Parse error: {:?}", e)),
    }
}

#[test]
fn test_constant_folding_addition() {
    let result = optimize_expression("2 + 3");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => {
            assert_eq!(n, 5, "2 + 3 should be folded to 5");
        }
        _ => panic!("Expected constant folded integer literal"),
    }
}

#[test]
fn test_constant_folding_subtraction() {
    let result = optimize_expression("10 - 4");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => {
            assert_eq!(n, 6, "10 - 4 should be folded to 6");
        }
        _ => panic!("Expected constant folded integer literal"),
    }
}

#[test]
fn test_constant_folding_multiplication() {
    let result = optimize_expression("6 * 7");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => {
            assert_eq!(n, 42, "6 * 7 should be folded to 42");
        }
        _ => panic!("Expected constant folded integer literal"),
    }
}

#[test]
fn test_constant_folding_division() {
    let result = optimize_expression("100 / 4");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => {
            assert_eq!(n, 25, "100 / 4 should be folded to 25");
        }
        _ => panic!("Expected constant folded integer literal"),
    }
}

#[test]
fn test_constant_folding_complex_expression() {
    let result = optimize_expression("(2 + 3) * 4");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => {
            assert_eq!(n, 20, "(2 + 3) * 4 should be folded to 20");
        }
        _ => panic!("Expected constant folded integer literal"),
    }
}

#[test]
fn test_constant_folding_nested() {
    let result = optimize_expression("((1 + 2) * (3 + 4))");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => {
            assert_eq!(n, 21, "((1 + 2) * (3 + 4)) should be folded to 21");
        }
        _ => panic!("Expected constant folded integer literal"),
    }
}

#[test]
fn test_constant_folding_with_variables() {
    let result = optimize_expression("x + 5");
    assert!(result.is_ok());

    // Should not fold completely as x is a variable
    match result.unwrap() {
        Expr::BinaryOp { op, left, right } => {
            assert_eq!(op, "+");
            // Verify at least one side is constant
            match *right {
                Expr::Literal(pascal::Literal::Integer(n)) => assert_eq!(n, 5),
                _ => panic!("Expected constant on right side"),
            }
        }
        _ => panic!("Expected binary operation with variable"),
    }
}

#[test]
fn test_algebraic_simplification_addition_zero() {
    let result = optimize_expression("x + 0");
    assert!(result.is_ok());

    // x + 0 should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("x + 0 should simplify to x"),
    }
}

#[test]
fn test_algebraic_simplification_zero_addition() {
    let result = optimize_expression("0 + x");
    assert!(result.is_ok());

    // 0 + x should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("0 + x should simplify to x"),
    }
}

#[test]
fn test_algebraic_simplification_multiplication_by_one() {
    let result = optimize_expression("x * 1");
    assert!(result.is_ok());

    // x * 1 should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("x * 1 should simplify to x"),
    }
}

#[test]
fn test_algebraic_simplification_multiplication_by_zero() {
    let result = optimize_expression("x * 0");
    assert!(result.is_ok());

    // x * 0 should simplify to 0
    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => assert_eq!(n, 0),
        _ => panic!("x * 0 should simplify to 0"),
    }
}

#[test]
fn test_algebraic_simplification_division_by_one() {
    let result = optimize_expression("x / 1");
    assert!(result.is_ok());

    // x / 1 should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("x / 1 should simplify to x"),
    }
}

#[test]
fn test_algebraic_simplification_subtraction_zero() {
    let result = optimize_expression("x - 0");
    assert!(result.is_ok());

    // x - 0 should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("x - 0 should simplify to x"),
    }
}

#[test]
fn test_algebraic_simplification_double_negation() {
    let result = optimize_expression("-(-x)");
    assert!(result.is_ok());

    // -(-x) should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("-(-x) should simplify to x"),
    }
}

#[test]
fn test_strength_reduction_multiplication_by_two() {
    let result = optimize_expression("x * 2");
    assert!(result.is_ok());

    // x * 2 could be optimized to x << 1
    // Check that some transformation happened
    let expr = result.unwrap();
    match expr {
        Expr::BinaryOp { op, .. } | Expr::UnaryOp { .. } => {
            // Some optimization should occur
        }
        Expr::Variable(_) => {
            // Or it might be kept as is
        }
        _ => {}
    }
}

#[test]
fn test_strength_reduction_multiplication_by_power_of_two() {
    let result = optimize_expression("x * 16");
    assert!(result.is_ok());

    // x * 16 could be optimized to x << 4
    let _ = result.unwrap();
}

#[test]
fn test_dead_code_elimination_unreachable_code() {
    let source = r#"
        begin
            if false then
                x := 1;
            x := 2;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_dead_code_elimination_unused_assignments() {
    let source = r#"
        begin
            x := 1;
            x := 2;
            y := x;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_dead_code_elimination_no_side_effects() {
    let source = r#"
        begin
            x := 1 + 2;
            y := x;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_common_subexpression_elimination_simple() {
    let source = r#"
        begin
            x := (a + b) * 2;
            y := (a + b) * 3;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_common_subexpression_elimination_complex() {
    let source = r#"
        begin
            x := (a * b + c) / d;
            y := (a * b + c) * 2;
            z := (a * b + c) + e;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_function_inlining_simple() {
    let source = r#"
        function SmallFunc(x: integer): integer;
        begin
            SmallFunc := x * 2;
        end;

        begin
            y := SmallFunc(5);
        end.
    "#;

    // Small function should be inlined
    let mut parser = Parser::new(source);
    let _ = parser.parse_program();
}

#[test]
fn test_function_inlining_with_multiple_calls() {
    let source = r#"
        function AddOne(x: integer): integer;
        begin
            AddOne := x + 1;
        end;

        begin
            a := AddOne(1);
            b := AddOne(2);
            c := AddOne(3);
        end.
    "#;

    let mut parser = Parser::new(source);
    let _ = parser.parse_program();
}

#[test]
fn test_loop_unrolling_constant_iterations() {
    let source = r#"
        begin
            for i := 1 to 4 do
                x := x + i;
        end.
    "#;

    // Small constant loop should be unrolled
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_loop_unrolling_disabled_for_large_loops() {
    let source = r#"
        begin
            for i := 1 to 1000 do
                x := x + i;
        end.
    "#;

    // Large loop should not be unrolled
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_tail_call_optimization() {
    let source = r#"
        function Factorial(n, acc: integer): integer;
        begin
            if n <= 1 then
                Factorial := acc
            else
                Factorial := Factorial(n - 1, n * acc);
        end;
    "#;

    let mut parser = Parser::new(source);
    let _ = parser.parse_program();
}

#[test]
fn test_peephole_optimization_redundant_moves() {
    let source = r#"
        begin
            x := y;
            z := x;
        end.
    "#;

    // Could be optimized to: z := y;
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_peephole_optimization_redundant_loads() {
    let source = r#"
        begin
            x := arr[0];
            y := x;
            z := arr[0];
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_peephole_optimization_jump_chains() {
    let source = r#"
        begin
            if a then
                if b then
                    x := 1;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_constant_propagation() {
    let source = r#"
        begin
            x := 10;
            y := x + 5;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());

    // y should be optimized to 15 (constant propagation)
}

#[test]
fn test_constant_propagation_chain() {
    let source = r#"
        begin
            x := 5;
            y := x;
            z := y * 2;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());

    // z should be optimized to 10
}

#[test]
fn test_copy_propagation() {
    let source = r#"
        begin
            x := y;
            z := x + 1;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());

    // z := y + 1 (copy propagation)
}

#[test]
fn test_loop_invariant_code_motion() {
    let source = r#"
        begin
            for i := 1 to 100 do
                x := y + z;
        end.
    "#;

    // y + z is invariant, should be moved out of loop
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_loop_invariant_with_array_access() {
    let source = r#"
        begin
            for i := 1 to 100 do
                arr[i] := arr[0] + i;
        end.
    "#;

    // arr[0] is invariant
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_variable_liveness_analysis() {
    let source = r#"
        begin
            x := 1;
            x := 2;
            y := x;
        end.
    "#;

    // First assignment to x is dead
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_dead_store_elimination() {
    let source = r#"
        begin
            x := 1;
            x := 2;
            x := 3;
        end.
    "#;

    // First two assignments are dead stores
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_boolean_optimization_and_true() {
    let result = optimize_expression("x and true");
    assert!(result.is_ok());

    // x and true should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("x and true should simplify to x"),
    }
}

#[test]
fn test_boolean_optimization_and_false() {
    let result = optimize_expression("x and false");
    assert!(result.is_ok());

    // x and false should simplify to false
    match result.unwrap() {
        Expr::Literal(pascal::Literal::Boolean(b)) => assert!(!b),
        _ => panic!("x and false should simplify to false"),
    }
}

#[test]
fn test_boolean_optimization_or_true() {
    let result = optimize_expression("x or true");
    assert!(result.is_ok());

    // x or true should simplify to true
    match result.unwrap() {
        Expr::Literal(pascal::Literal::Boolean(b)) => assert!(b),
        _ => panic!("x or true should simplify to true"),
    }
}

#[test]
fn test_boolean_optimization_or_false() {
    let result = optimize_expression("x or false");
    assert!(result.is_ok());

    // x or false should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("x or false should simplify to x"),
    }
}

#[test]
fn test_boolean_optimization_not_true() {
    let result = optimize_expression("not true");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Boolean(b)) => assert!(!b),
        _ => panic!("not true should simplify to false"),
    }
}

#[test]
fn test_boolean_optimization_not_false() {
    let result = optimize_expression("not false");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Boolean(b)) => assert!(b),
        _ => panic!("not false should simplify to true"),
    }
}

#[test]
fn test_comparison_optimization_always_true() {
    let result = optimize_expression("5 < 10");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Boolean(b)) => assert!(b),
        _ => panic!("5 < 10 should be true"),
    }
}

#[test]
fn test_comparison_optimization_always_false() {
    let result = optimize_expression("5 > 10");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Boolean(b)) => assert!(!b),
        _ => panic!("5 > 10 should be false"),
    }
}

#[test]
fn test_comparison_optimization_equal_constants() {
    let result = optimize_expression("5 = 5");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Boolean(b)) => assert!(b),
        _ => panic!("5 = 5 should be true"),
    }
}

#[test]
fn test_comparison_optimization_not_equal_constants() {
    let result = optimize_expression("5 <> 5");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Boolean(b)) => assert!(!b),
        _ => panic!("5 <> 5 should be false"),
    }
}

#[test]
fn test_string_constant_folding() {
    let result = optimize_expression(r#"'Hello' + ' ' + 'World'"#);
    assert!(result.is_ok());

    // Should fold to "Hello World"
    match result.unwrap() {
        Expr::Literal(pascal::Literal::String(s)) => {
            assert_eq!(s, "Hello World");
        }
        _ => panic!("String concatenation should be folded"),
    }
}

#[test]
fn test_array_bounds_check_elimination() {
    let source = r#"
        begin
            for i := 1 to 10 do
                arr[i] := i;
        end.
    "#;

    // Bounds check can be eliminated as i is always in range
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_null_check_elimination() {
    let source = r#"
        begin
            new(p);
            p^ := 10;
        end.
    "#;

    // Null check after new can be eliminated
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_branch_optimization_merge_blocks() {
    let source = r#"
        begin
            if x then
                y := 1
            else
                y := 1;
        end.
    "#;

    // Both branches do the same thing, can be merged
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_branch_optimization_constant_condition() {
    let source = r#"
        const
            Debug = true;
        begin
            if Debug then
                x := 1;
        end.
    "#;

    // Constant condition can be evaluated
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_branch_optimization_remove_unreachable() {
    let source = r#"
        begin
            if false then
                x := 1
            else
                x := 2;
        end.
    "#;

    // False branch is unreachable, can be removed
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_interprocedural_optimization() {
    let source = r#"
        function GetConst: integer;
        begin
            GetConst := 42;
        end;

        begin
            x := GetConst() + 8;
        end.
    "#;

    // GetConst() call should be inlined as constant 42
    let mut parser = Parser::new(source);
    let _ = parser.parse_program();
}

#[test]
fn test_devirtualization() {
    let source = r#"
        type
            TObj = class
                procedure VirtualProc; virtual;
            end;

        var
            obj: TObj;
        begin
            obj.VirtualProc;
        end.
    "#;

    // Virtual call might be devirtualized if exact type is known
    let mut parser = Parser::new(source);
    let _ = parser.parse_program();
}

#[test]
fn test_memory_access_optimization() {
    let source = r#"
        begin
            p^.x := 1;
            p^.y := 2;
            p^.z := 3;
        end.
    "#;

    // Should optimize to fewer pointer dereferences
    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_bitwise_optimization() {
    let result = optimize_expression("x and 0");
    assert!(result.is_ok());

    // x and 0 should simplify to 0
    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => assert_eq!(n, 0),
        _ => panic!("x and 0 should simplify to 0"),
    }
}

#[test]
fn test_bitwise_or_all_ones() {
    let result = optimize_expression("x or $FFFFFFFF");
    assert!(result.is_ok());

    // x or $FFFFFFFF should simplify to $FFFFFFFF
    match result.unwrap() {
        Expr::Literal(pascal::Literal::Integer(n)) => assert_eq!(n, 0xFFFFFFFF),
        _ => panic!("x or $FFFFFFFF should simplify to $FFFFFFFF"),
    }
}

#[test]
fn test_xor_with_zero() {
    let result = optimize_expression("x xor 0");
    assert!(result.is_ok());

    // x xor 0 should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("x xor 0 should simplify to x"),
    }
}

#[test]
fn test_shift_by_zero() {
    let result = optimize_expression("x shl 0");
    assert!(result.is_ok());

    // x shl 0 should simplify to x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("x shl 0 should simplify to x"),
    }
}

#[test]
fn test_combined_optimizations() {
    let source = r#"
        begin
            x := 2 * 3;
            y := x * 1;
            z := y + 0;
            w := z - 0;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());

    // Should optimize to: w := 6
}

#[test]
fn test_optimizer_preserves_semantics() {
    let source = r#"
        begin
            x := 10;
            if x > 5 then
                x := x * 2
            else
                x := x + 1;
        end.
    "#;

    let result = optimize_statement(source);
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_optimizer_with_complex_arithmetic() {
    let result = optimize_expression("((x + 0) * 1) - 0");
    assert!(result.is_ok());

    // Should simplify to: x
    match result.unwrap() {
        Expr::Variable(name) => assert_eq!(name, "x"),
        _ => panic!("((x + 0) * 1) - 0 should simplify to x"),
    }
}

#[test]
fn test_constant_folding_with_floats() {
    let result = optimize_expression("3.14 * 2.0");
    assert!(result.is_ok());

    match result.unwrap() {
        Expr::Literal(pascal::Literal::Real(f)) => {
            assert!((f - 6.28).abs() < 0.01);
        }
        _ => panic!("Should fold floating point multiplication"),
    }
}

#[test]
fn test_division_by_power_of_two() {
    let result = optimize_expression("x / 16");
    assert!(result.is_ok());

    // x / 16 could be optimized to x >> 4
    let _ = result.unwrap();
}

#[test]
fn test_modulo_by_power_of_two() {
    let result = optimize_expression("x mod 16");
    assert!(result.is_ok());

    // x mod 16 could be optimized to x & 15
    let _ = result.unwrap();
}

#[test]
fn test_negate_multiplication() {
    let result = optimize_expression("-(x * 2)");
    assert!(result.is_ok());

    // Could be optimized to x * -2
    let _ = result.unwrap();
}

#[test]
fn test_optimization_preserves_side_effects() {
    let source = r#"
        function GetX: integer;
        begin
            writeln('GetX called');
            GetX := 10;
        end;

        begin
            y := GetX() + GetX();
        end.
    "#;

    // Should not optimize to y := GetX() * 2 as GetX has side effects
    let mut parser = Parser::new(source);
    let _ = parser.parse_program();
}