oak-pascal 0.0.11

Pascal programming language parser with support for structured programming and modern Pascal dialects.
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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
{ Pascal Test File - Comprehensive Syntax Coverage }
{ This file tests various Pascal syntax elements for lexer testing }

program ComprehensivePascalTest;

{$mode objfpc}{$H+}
{$IFDEF FPC}
    {$LONGSTRINGS ON}
    {$ASSERTIONS ON}
{$ENDIF}

uses
    SysUtils, Classes, Math, StrUtils, DateUtils, Variants, 
    {$IFDEF WINDOWS}
    Windows,
    {$ENDIF}
    {$IFDEF UNIX}
    Unix, BaseUnix,
    {$ENDIF}
    Contnrs, IniFiles, RegExpr;

{ Constants }
const
    PI = 3.14159265359;
    MAX_SIZE = 1000;
    VERSION = '1.0.0';
    DEBUG = True;
  
    { String constants }
    GREETING = 'Hello, Pascal!';
    MULTILINE_TEXT = 'This is a ' +
                                      'multi-line ' +
                                      'string constant';
  
    { Character constants }
    TAB_CHAR = #9;
    NEWLINE_CHAR = #10;
    CARRIAGE_RETURN = #13;
  
    { Numeric constants }
    HEX_VALUE = $FF;
    OCTAL_VALUE = &377;
    BINARY_VALUE = %11111111;
    FLOAT_VALUE = 3.14E+2;
    SCIENTIFIC_NOTATION = 1.23E-4;

{ Type definitions }
type
    { Enumerated types }
    TColor = (clRed, clGreen, clBlue, clYellow, clPurple, clOrange);
    TDirection = (dirNorth, dirSouth, dirEast, dirWest);
    TLogLevel = (llTrace, llDebug, llInfo, llWarn, llError, llFatal);
  
    { Subrange types }
    TPercentage = 0..100;
    TDayOfMonth = 1..31;
    TUpperCase = 'A'..'Z';
  
    { Set types }
    TColorSet = set of TColor;
    TCharSet = set of Char;
    TIntegerSet = set of 0..255;
  
    { Array types }
    TIntArray = array[1..10] of Integer;
    TMatrix = array[1..3, 1..3] of Real;
    TDynamicArray = array of Integer;
    TStringArray = array of String;
  
    { Record types }
    TPoint = record
        X, Y: Real;
    end;
  
    TPoint3D = record
        X, Y, Z: Real;
    end;
  
    TPerson = record
        FirstName: String[50];
        LastName: String[50];
        Age: Integer;
        BirthDate: TDateTime;
        Active: Boolean;
        Salary: Currency;
    end;
  
    { Variant record }
    TShape = record
        Center: TPoint;
        case ShapeType: Integer of
            1: (Radius: Real);                    { Circle }
            2: (Width, Height: Real);             { Rectangle }
            3: (SideA, SideB, SideC: Real);       { Triangle }
    end;
  
    { Object types (old-style OOP) }
    TCounter = object
        Value: Integer;
        procedure Init;
        procedure Increment;
        procedure Decrement;
        function GetValue: Integer;
    end;
  
    { Class types (modern OOP) }
    TAnimal = class
    private
        FName: String;
        FAge: Integer;
    protected
        procedure SetName(const AName: String); virtual;
        procedure SetAge(const AAge: Integer); virtual;
    public
        constructor Create(const AName: String; AAge: Integer);
        destructor Destroy; override;
    
        property Name: String read FName write SetName;
        property Age: Integer read FAge write SetAge;
    
        procedure MakeSound; virtual; abstract;
        procedure Move; virtual;
        function ToString: String; virtual;
    end;
  
    TDog = class(TAnimal)
    private
        FBreed: String;
    public
        constructor Create(const AName: String; AAge: Integer; const ABreed: String);
    
        property Breed: String read FBreed write FBreed;
    
        procedure MakeSound; override;
        procedure Fetch;
    end;
  
    TCat = class(TAnimal)
    private
        FIndoor: Boolean;
    public
        constructor Create(const AName: String; AAge: Integer; AIndoor: Boolean);
    
        property Indoor: Boolean read FIndoor write FIndoor;
    
        procedure MakeSound; override;
        procedure Purr;
    end;
  
    { Interface types }
    IDrawable = interface
        ['{12345678-1234-1234-1234-123456789012}']
        procedure Draw;
        function GetArea: Real;
    end;
  
    IMovable = interface
        ['{87654321-4321-4321-4321-210987654321}']
        procedure MoveTo(X, Y: Real);
        function GetPosition: TPoint;
    end;
  
    { Class implementing interfaces }
    TCircle = class(TInterfacedObject, IDrawable, IMovable)
    private
        FCenter: TPoint;
        FRadius: Real;
    public
        constructor Create(ACenter: TPoint; ARadius: Real);
    
        { IDrawable }
        procedure Draw;
        function GetArea: Real;
    
        { IMovable }
        procedure MoveTo(X, Y: Real);
        function GetPosition: TPoint;
    
        property Center: TPoint read FCenter;
        property Radius: Real read FRadius write FRadius;
    end;
  
    { Pointer types }
    PInteger = ^Integer;
    PPoint = ^TPoint;
    PNode = ^TNode;
  
    { Linked list node }
    TNode = record
        Data: Integer;
        Next: PNode;
    end;
  
    { Procedural types }
    TIntegerFunction = function(X: Integer): Integer;
    TCompareFunction = function(A, B: Integer): Integer;
    TNotifyEvent = procedure(Sender: TObject);
  
    { Generic types (FPC) }
    {$IFDEF FPC}
    generic TList<T> = class
    private
        FItems: array of T;
        FCount: Integer;
    public
        procedure Add(const Item: T);
        function Get(Index: Integer): T;
        procedure Clear;
        property Count: Integer read FCount;
        property Items[Index: Integer]: T read Get; default;
    end;
  
    TIntegerList = specialize TList<Integer>;
    TStringList = specialize TList<String>;
    {$ENDIF}

{ Global variables }
var
    GlobalCounter: Integer;
    GlobalMessage: String;
    GlobalArray: TIntArray;
    GlobalMatrix: TMatrix;
    GlobalColors: TColorSet;
    GlobalPersons: array of TPerson;

{ Forward declarations }
procedure ProcessData(const Data: String); forward;
function CalculateSum(const Numbers: array of Integer): Integer; forward;

{ Object method implementations }
procedure TCounter.Init;
begin
    Value := 0;
end;

procedure TCounter.Increment;
begin
    Inc(Value);
end;

procedure TCounter.Decrement;
begin
    Dec(Value);
end;

function TCounter.GetValue: Integer;
begin
    Result := Value;
end;

{ Class method implementations }
constructor TAnimal.Create(const AName: String; AAge: Integer);
begin
    inherited Create;
    FName := AName;
    FAge := AAge;
end;

destructor TAnimal.Destroy;
begin
    WriteLn('Animal ', FName, ' destroyed');
    inherited Destroy;
end;

procedure TAnimal.SetName(const AName: String);
begin
    FName := AName;
end;

procedure TAnimal.SetAge(const AAge: Integer);
begin
    if (AAge >= 0) and (AAge <= 100) then
        FAge := AAge;
end;

procedure TAnimal.Move;
begin
    WriteLn(FName, ' is moving');
end;

function TAnimal.ToString: String;
begin
    Result := Format('%s (age %d)', [FName, FAge]);
end;

{ TDog implementation }
constructor TDog.Create(const AName: String; AAge: Integer; const ABreed: String);
begin
    inherited Create(AName, AAge);
    FBreed := ABreed;
end;

procedure TDog.MakeSound;
begin
    WriteLn(Name, ' the ', Breed, ' says: Woof!');
end;

procedure TDog.Fetch;
begin
    WriteLn(Name, ' is fetching the ball');
end;

{ TCat implementation }
constructor TCat.Create(const AName: String; AAge: Integer; AIndoor: Boolean);
begin
    inherited Create(AName, AAge);
    FIndoor := AIndoor;
end;

procedure TCat.MakeSound;
begin
    WriteLn(Name, ' says: Meow!');
end;

procedure TCat.Purr;
begin
    WriteLn(Name, ' is purring contentedly');
end;

{ TCircle implementation }
constructor TCircle.Create(ACenter: TPoint; ARadius: Real);
begin
    inherited Create;
    FCenter := ACenter;
    FRadius := ARadius;
end;

procedure TCircle.Draw;
begin
    WriteLn(Format('Drawing circle at (%.2f, %.2f) with radius %.2f', 
                                  [FCenter.X, FCenter.Y, FRadius]));
end;

function TCircle.GetArea: Real;
begin
    Result := PI * FRadius * FRadius;
end;

procedure TCircle.MoveTo(X, Y: Real);
begin
    FCenter.X := X;
    FCenter.Y := Y;
end;

function TCircle.GetPosition: TPoint;
begin
    Result := FCenter;
end;

{ Generic class implementation }
{$IFDEF FPC}
procedure TList.Add(const Item: T);
begin
    SetLength(FItems, FCount + 1);
    FItems[FCount] := Item;
    Inc(FCount);
end;

function TList.Get(Index: Integer): T;
begin
    if (Index >= 0) and (Index < FCount) then
        Result := FItems[Index]
    else
        raise Exception.CreateFmt('Index %d out of bounds', [Index]);
end;

procedure TList.Clear;
begin
    SetLength(FItems, 0);
    FCount := 0;
end;
{$ENDIF}

{ Utility functions and procedures }
function Add(A, B: Integer): Integer;
begin
    Result := A + B;
end;

function Subtract(A, B: Integer): Integer;
begin
    Result := A - B;
end;

function Multiply(A, B: Integer): Integer;
begin
    Result := A * B;
end;

function Divide(A, B: Real): Real;
begin
    if B <> 0 then
        Result := A / B
    else
        raise Exception.Create('Division by zero');
end;

{ Function with default parameters }
function Power(Base: Real; Exponent: Real = 2.0): Real;
begin
    Result := Math.Power(Base, Exponent);
end;

{ Overloaded functions }
function Max(A, B: Integer): Integer; overload;
begin
    if A > B then
        Result := A
    else
        Result := B;
end;

function Max(A, B: Real): Real; overload;
begin
    if A > B then
        Result := A
    else
        Result := B;
end;

function Max(A, B, C: Integer): Integer; overload;
begin
    Result := Max(Max(A, B), C);
end;

{ Procedure with var parameters }
procedure Swap(var A, B: Integer);
var
    Temp: Integer;
begin
    Temp := A;
    A := B;
    B := Temp;
end;

{ Procedure with out parameters }
procedure DivMod(Dividend, Divisor: Integer; out Quotient, Remainder: Integer);
begin
    Quotient := Dividend div Divisor;
    Remainder := Dividend mod Divisor;
end;

{ Procedure with const parameters }
procedure PrintArray(const Arr: array of Integer);
var
    I: Integer;
begin
    Write('[');
    for I := Low(Arr) to High(Arr) do
    begin
        Write(Arr[I]);
        if I < High(Arr) then
            Write(', ');
    end;
    WriteLn(']');
end;

{ Recursive function }
function Factorial(N: Integer): Int64;
begin
    if N <= 1 then
        Result := 1
    else
        Result := N * Factorial(N - 1);
end;

{ Function returning a function }
function GetMathFunction(Operation: Char): TIntegerFunction;
begin
    case Operation of
        '+': Result := @Add;
        '-': Result := @Subtract;
        '*': Result := @Multiply;
    else
        Result := nil;
    end;
end;

{ String manipulation functions }
function ReverseString(const S: String): String;
var
    I: Integer;
begin
    Result := '';
    for I := Length(S) downto 1 do
        Result := Result + S[I];
end;

function CountWords(const S: String): Integer;
var
    Words: TStringArray;
begin
    Words := SplitString(Trim(S), ' ');
    Result := Length(Words);
end;

function IsValidEmail(const Email: String): Boolean;
var
    RegEx: TRegExpr;
begin
    RegEx := TRegExpr.Create;
    try
        RegEx.Expression := '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$';
        Result := RegEx.Exec(Email);
    finally
        RegEx.Free;
    end;
end;

{ Array manipulation procedures }
procedure InitializeArray(var Arr: TIntArray);
var
    I: Integer;
begin
    for I := Low(Arr) to High(Arr) do
        Arr[I] := I;
end;

procedure SortArray(var Arr: array of Integer);
var
    I, J, Temp: Integer;
begin
    { Bubble sort }
    for I := Low(Arr) to High(Arr) - 1 do
        for J := Low(Arr) to High(Arr) - I - 1 do
            if Arr[J] > Arr[J + 1] then
            begin
                Temp := Arr[J];
                Arr[J] := Arr[J + 1];
                Arr[J + 1] := Temp;
            end;
end;

function BinarySearch(const Arr: array of Integer; Value: Integer): Integer;
var
    Left, Right, Mid: Integer;
begin
    Left := Low(Arr);
    Right := High(Arr);
    Result := -1;
  
    while Left <= Right do
    begin
        Mid := (Left + Right) div 2;
        if Arr[Mid] = Value then
        begin
            Result := Mid;
            Break;
        end
        else if Arr[Mid] < Value then
            Left := Mid + 1
        else
            Right := Mid - 1;
    end;
end;

{ Matrix operations }
procedure InitializeMatrix(var Matrix: TMatrix);
var
    I, J: Integer;
begin
    for I := 1 to 3 do
        for J := 1 to 3 do
            Matrix[I, J] := I * J;
end;

procedure PrintMatrix(const Matrix: TMatrix);
var
    I, J: Integer;
begin
    for I := 1 to 3 do
    begin
        for J := 1 to 3 do
            Write(Format('%8.2f', [Matrix[I, J]]));
        WriteLn;
    end;
end;

{ Set operations }
procedure DemonstrateSetOperations;
var
    Set1, Set2, UnionSet, IntersectionSet: TColorSet;
begin
    Set1 := [clRed, clGreen, clBlue];
    Set2 := [clBlue, clYellow, clPurple];
  
    UnionSet := Set1 + Set2;
    IntersectionSet := Set1 * Set2;
  
    WriteLn('Set operations:');
    WriteLn('Set1 contains Red: ', clRed in Set1);
    WriteLn('Set2 contains Red: ', clRed in Set2);
    WriteLn('Union has ', SizeOf(UnionSet) * 8, ' possible elements');
    WriteLn('Intersection has Blue: ', clBlue in IntersectionSet);
end;

{ Record operations }
function CreatePerson(const FirstName, LastName: String; Age: Integer): TPerson;
begin
    with Result do
    begin
        FirstName := FirstName;
        LastName := LastName;
        Age := Age;
        BirthDate := Now - Age * 365.25;
        Active := True;
        Salary := 50000.0;
    end;
end;

procedure PrintPerson(const Person: TPerson);
begin
    with Person do
        WriteLn(Format('%s %s, Age: %d, Salary: %m', 
                                      [FirstName, LastName, Age, Salary]));
end;

{ Pointer operations }
procedure DemonstratePointers;
var
    P: PInteger;
    Value: Integer;
begin
    Value := 42;
    P := @Value;
  
    WriteLn('Value: ', Value);
    WriteLn('Pointer value: ', P^);
    WriteLn('Pointer address: ', IntPtr(P));
  
    P^ := 100;
    WriteLn('Modified value: ', Value);
end;

{ Linked list operations }
procedure AddNode(var Head: PNode; Data: Integer);
var
    NewNode: PNode;
begin
    New(NewNode);
    NewNode^.Data := Data;
    NewNode^.Next := Head;
    Head := NewNode;
end;

procedure PrintList(Head: PNode);
var
    Current: PNode;
begin
    Current := Head;
    Write('List: ');
    while Current <> nil do
    begin
        Write(Current^.Data);
        Current := Current^.Next;
        if Current <> nil then
            Write(' -> ');
    end;
    WriteLn;
end;

procedure FreeList(var Head: PNode);
var
    Current, Next: PNode;
begin
    Current := Head;
    while Current <> nil do
    begin
        Next := Current^.Next;
        Dispose(Current);
        Current := Next;
    end;
    Head := nil;
end;

{ File operations }
procedure DemonstrateFileOperations;
var
    F: TextFile;
    Line: String;
    FileName: String;
begin
    FileName := 'test_output.txt';
  
    { Write to file }
    AssignFile(F, FileName);
    Rewrite(F);
    try
        WriteLn(F, 'Hello, Pascal!');
        WriteLn(F, 'This is a test file.');
        WriteLn(F, 'Generated at: ', DateTimeToStr(Now));
    finally
        CloseFile(F);
    end;
  
    { Read from file }
    if FileExists(FileName) then
    begin
        AssignFile(F, FileName);
        Reset(F);
        try
            WriteLn('File contents:');
            while not Eof(F) do
            begin
                ReadLn(F, Line);
                WriteLn('  ', Line);
            end;
        finally
            CloseFile(F);
        end;
    
        { Clean up }
        DeleteFile(FileName);
    end;
end;

{ Exception handling }
procedure DemonstrateTryExcept;
var
    A, B, Result: Integer;
begin
    A := 10;
    B := 0;
  
    try
        Result := A div B;
        WriteLn('Result: ', Result);
    except
        on E: EDivByZero do
            WriteLn('Division by zero error: ', E.Message);
        on E: Exception do
            WriteLn('General error: ', E.Message);
    end;
end;

procedure DemonstrateTryFinally;
var
    F: TextFile;
begin
    AssignFile(F, 'temp.txt');
    try
        Rewrite(F);
        WriteLn(F, 'Temporary data');
        { Simulate an error }
        raise Exception.Create('Simulated error');
    finally
        CloseFile(F);
        if FileExists('temp.txt') then
            DeleteFile('temp.txt');
        WriteLn('Cleanup completed');
    end;
end;

{ Control structures }
procedure DemonstrateControlStructures;
var
    I, J: Integer;
    Ch: Char;
    Colors: TColorSet;
    Numbers: array[1..5] of Integer;
begin
    WriteLn('=== Control Structures ===');
  
    { If-then-else }
    I := 15;
    if I > 10 then
        WriteLn('I is greater than 10')
    else if I > 5 then
        WriteLn('I is greater than 5')
    else
        WriteLn('I is 5 or less');
  
    { Case statement }
    Ch := 'B';
    case Ch of
        'A': WriteLn('Letter A');
        'B': WriteLn('Letter B');
        'C'..'Z': WriteLn('Letter C through Z');
    else
        WriteLn('Not a capital letter');
    end;
  
    { For loops }
    Write('For loop (1 to 5): ');
    for I := 1 to 5 do
        Write(I, ' ');
    WriteLn;
  
    Write('For loop (5 downto 1): ');
    for I := 5 downto 1 do
        Write(I, ' ');
    WriteLn;
  
    { While loop }
    I := 1;
    Write('While loop: ');
    while I <= 5 do
    begin
        Write(I, ' ');
        Inc(I);
    end;
    WriteLn;
  
    { Repeat-until loop }
    I := 1;
    Write('Repeat-until loop: ');
    repeat
        Write(I, ' ');
        Inc(I);
    until I > 5;
    WriteLn;
  
    { For-in loop (FPC) }
    {$IFDEF FPC}
    Numbers[1] := 10;
    Numbers[2] := 20;
    Numbers[3] := 30;
    Numbers[4] := 40;
    Numbers[5] := 50;
  
    Write('For-in loop: ');
    for I in Numbers do
        Write(I, ' ');
    WriteLn;
    {$ENDIF}
  
    { Nested loops with labels }
    WriteLn('Nested loops with break:');
    for I := 1 to 3 do
    begin
        for J := 1 to 3 do
        begin
            if (I = 2) and (J = 2) then
                goto LoopEnd;
            Write('(', I, ',', J, ') ');
        end;
        WriteLn;
    end;
  
    LoopEnd:
    WriteLn('Broke out of nested loops');
end;

{ Advanced features }
procedure DemonstrateAdvancedFeatures;
var
    Animals: array of TAnimal;
    Dog: TDog;
    Cat: TCat;
    Circle: TCircle;
    Drawable: IDrawable;
    I: Integer;
    {$IFDEF FPC}
    IntList: TIntegerList;
    {$ENDIF}
begin
    WriteLn('=== Advanced Features ===');
  
    { Polymorphism }
    SetLength(Animals, 2);
    Animals[0] := TDog.Create('Buddy', 3, 'Golden Retriever');
    Animals[1] := TCat.Create('Whiskers', 2, True);
  
    for I := 0 to High(Animals) do
    begin
        Animals[I].MakeSound;
        Animals[I].Move;
    end;
  
    { Type casting and is/as operators }
    for I := 0 to High(Animals) do
    begin
        if Animals[I] is TDog then
        begin
            Dog := Animals[I] as TDog;
            Dog.Fetch;
        end
        else if Animals[I] is TCat then
        begin
            Cat := Animals[I] as TCat;
            Cat.Purr;
        end;
    end;
  
    { Interface usage }
    Circle := TCircle.Create(Point(100, 100), 50);
    Drawable := Circle;
    Drawable.Draw;
    WriteLn('Circle area: ', Drawable.GetArea:0:2);
  
    { Generic collections }
    {$IFDEF FPC}
    IntList := TIntegerList.Create;
    try
        IntList.Add(10);
        IntList.Add(20);
        IntList.Add(30);
    
        WriteLn('Generic list contents:');
        for I := 0 to IntList.Count - 1 do
            WriteLn('  Item ', I, ': ', IntList[I]);
    finally
        IntList.Free;
    end;
    {$ENDIF}
  
    { Cleanup }
    for I := 0 to High(Animals) do
        Animals[I].Free;
    Circle.Free;
end;

{ Forward declaration implementations }
procedure ProcessData(const Data: String);
begin
    WriteLn('Processing data: ', Data);
    WriteLn('Data length: ', Length(Data));
    WriteLn('Word count: ', CountWords(Data));
    WriteLn('Reversed: ', ReverseString(Data));
end;

function CalculateSum(const Numbers: array of Integer): Integer;
var
    I: Integer;
begin
    Result := 0;
    for I := Low(Numbers) to High(Numbers) do
        Result := Result + Numbers[I];
end;

{ Main program }
begin
    WriteLn('=== Pascal Comprehensive Test ===');
    WriteLn('Version: ', VERSION);
    WriteLn('Debug mode: ', DEBUG);
    WriteLn;
  
    { Initialize global variables }
    GlobalCounter := 0;
    GlobalMessage := GREETING;
    GlobalColors := [clRed, clGreen, clBlue];
  
    { Test basic operations }
    WriteLn('Basic arithmetic:');
    WriteLn('5 + 3 = ', Add(5, 3));
    WriteLn('5 - 3 = ', Subtract(5, 3));
    WriteLn('5 * 3 = ', Multiply(5, 3));
    WriteLn('5.0 / 2.0 = ', Divide(5.0, 2.0):0:2);
    WriteLn('2^3 = ', Power(2, 3):0:0);
    WriteLn('5! = ', Factorial(5));
    WriteLn;
  
    { Test overloaded functions }
    WriteLn('Max(10, 20) = ', Max(10, 20));
    WriteLn('Max(3.14, 2.71) = ', Max(3.14, 2.71):0:2);
    WriteLn('Max(5, 10, 3) = ', Max(5, 10, 3));
    WriteLn;
  
    { Test string operations }
    ProcessData('Hello, Pascal World!');
    WriteLn('Email validation:');
    WriteLn('  test@example.com: ', IsValidEmail('test@example.com'));
    WriteLn('  invalid-email: ', IsValidEmail('invalid-email'));
    WriteLn;
  
    { Test arrays }
    InitializeArray(GlobalArray);
    Write('Initialized array: ');
    PrintArray(GlobalArray);
  
    SortArray(GlobalArray);
    Write('Sorted array: ');
    PrintArray(GlobalArray);
  
    WriteLn('Binary search for 5: ', BinarySearch(GlobalArray, 5));
    WriteLn;
  
    { Test matrix }
    InitializeMatrix(GlobalMatrix);
    WriteLn('Matrix:');
    PrintMatrix(GlobalMatrix);
    WriteLn;
  
    { Test sets }
    DemonstrateSetOperations;
    WriteLn;
  
    { Test records }
    SetLength(GlobalPersons, 2);
    GlobalPersons[0] := CreatePerson('John', 'Doe', 30);
    GlobalPersons[1] := CreatePerson('Jane', 'Smith', 25);
  
    WriteLn('Persons:');
    PrintPerson(GlobalPersons[0]);
    PrintPerson(GlobalPersons[1]);
    WriteLn;
  
    { Test pointers }
    DemonstratePointers;
    WriteLn;
  
    { Test linked list }
    var
        Head: PNode;
        I: Integer;
    begin
        Head := nil;
        for I := 1 to 5 do
            AddNode(Head, I * 10);
    
        PrintList(Head);
        FreeList(Head);
    end;
    WriteLn;
  
    { Test control structures }
    DemonstrateControlStructures;
    WriteLn;
  
    { Test file operations }
    DemonstrateFileOperations;
    WriteLn;
  
    { Test exception handling }
    WriteLn('Exception handling:');
    DemonstrateTryExcept;
  
    try
        DemonstrateTryFinally;
    except
        on E: Exception do
            WriteLn('Caught exception: ', E.Message);
    end;
    WriteLn;
  
    { Test advanced features }
    DemonstrateAdvancedFeatures;
    WriteLn;
  
    { Test object }
    var
        Counter: TCounter;
    begin
        Counter.Init;
        Counter.Increment;
        Counter.Increment;
        WriteLn('Counter value: ', Counter.GetValue);
    end;
  
    WriteLn('=== Test completed ===');
  
    { Wait for user input }
    WriteLn('Press Enter to exit...');
    ReadLn;
end.