-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPas2Groovy.lpr
1989 lines (1871 loc) · 61.9 KB
/
Pas2Groovy.lpr
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
{ This is a test-program for the fcl-passrc package (except writer-class).
Please notice that i have done this to find out how good the parser workes,
it is not thought to be a good example to use the fcl-passrc package but
may give you hints on using it.
It is done to test the source of these units for usability, completeness and
bugs. It is base on the fcl-passrc exampe.
It workes like a pretty-printer to compare the output of this program with
the original code, but is not thought to be a real pretty-printer as
e.g. the semicolons can sometimes not be set at the place they sould be
(this imformation is not available from the parsing-engine, as a parser
should only give you a positiv result if the source is valid, otherwise
you get a negative result).
Also the output is not always in the same order as in input as this
information is not available easily.
!!!Do not expect this program to produce executeable output!!!
Status: -workes with one Unit or Program
-Some type declarations missing
-string[n] the [n] part missing -> missing in parser
-array of const -> missing in parser
-Hints deprecated, etc. missing sometimes
-the parser splits x,y:atype
x:atype
y:atype
i tryed to put them together again
- () missing in statements: () expression and typecast
-missing forward class declaration like x=class
-incomplete !
parser: -ugly ''' quotation from scanner, why not #39 ?
-see comments in the program for hints
-incomplete !
Usage: call with one complete filename of a Unit or Program
defaults for the parser are 'linux' and 'i386'
Output: is 'pretty-printed' to stdout or unformated
The unformated output is thought to be diffed with the original
source to see differences caused by the parser (a tool to unformat
a souce file is in progress but not finished jet).
Bugs: 1. In case of unimplemented statements (like up to now asm) the parser
cause a excemtion to abort the program hard.
2. Missing implementaion in this program should not print out anything
or result in not pascal conform output.
Hit: The parser uses directives given in the source file.
Hints to read the code:
There are comments in the code with hints and marks of possible bugs.
During development some code was modified for true order output but the
old code is still available as a comment as it is easier to understand.
This is programmed using 'recursive' calls. Most options in functions are
for printing the output.
There is no writer-class used to keep it simple and see what is done.
All output is produced by direct writing to stdout, this cause problems in
furter development; a function result as string may be more usable.
The parser was written to be used for unit interface and was expanded to
work with program and implementation too. It does nearly no seperate
things for programs, they are adapted to the unit scheme (see main).
The order will change in following case:
-function with forward declaration (also overloading etc.)
Inheritance (only the important ones):
TInterfaceSection, TImplementationSection, TProgramSection
|
TPasSection
|
TPasDeclarations
|
TPasElement
|
TPasElementBase
|
TObject
TInitializationSection, TFinalizationSection
|
TPasImplBlock
|
TPasImplElement
|
TPasElement
|
TPasElementBase
|
TObject
TPasProgram
|
TPasModule
|
TPasElement
|
TPasElementBase
|
TObject
Dependance Structure :
TPasPackage = class(TPasElement)
|
Modules: TFPList;
TPasModule = class(TPasElement)
|-InterfaceSection: TInterfaceSection;
| |-Declarations -> forward part, unit only
|
|-ImplementationSection: TImplementationSection;
| |-Declarations -> full declaration, unit and program
| |-Functions: TFPList;
| |-TPasFunction = class(TPasProcedureBase)
| |-Body: TProcedureBody;
| |-Declarations -> declaration and sub function
| |-Body: TPasImplBlock; -> procedure block
|
|-InitializationSection: TInitializationSection;
| |-TPasImplBlock.Elements: TFPList; -> main block
|
|-FinalizationSection: TFinalizationSection;
|-TPasImplBlock.Elements: TFPList; -> unit only
Declarations = class(TPasElement)
|-Declarations: TFPList; -> the following are all in here
|-ResStrings: TFPList;
|-Types: TFPList;
|-Consts: TFPList;
|-Classes: TFPList;
|-Functions: TFPList;
|-Variables: TFPList;
|-Properties: TFPList;
}
program test_parser1;
{$mode objfpc}{$H+}
uses SysUtils, Classes, PParser, PasTree;
//# types the parser needs
type
{ We have to override abstract TPasTreeContainer methods.
See utils/fpdoc/dglobals.pp for an implementation of TFPDocEngine,
a "real" engine. }
TSimpleEngine = class(TPasTreeContainer)
public
function CreateElement(AClass: TPTreeElement; const AName: String;
AParent: TPasElement; AVisibility: TPasMemberVisibility;
const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
override;
function FindElement(const AName: String): TPasElement; override;
end;
function TSimpleEngine.CreateElement(AClass: TPTreeElement; const AName: String;
AParent: TPasElement; AVisibility: TPasMemberVisibility;
const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
begin
Result := AClass.Create(AName, AParent);
Result.Visibility := AVisibility;
Result.SourceFilename := ASourceFilename;
Result.SourceLinenumber := ASourceLinenumber;
end;
function TSimpleEngine.FindElement(const AName: String): TPasElement;
begin
{ dummy implementation, see TFPDocEngine.FindElement for a real example }
Result := nil;
end;
//# main var
var
M: TPasModule;
E: TPasTreeContainer;
I: Integer;
cmdl, TargetOS, TargetCPU : string;
isim, //is Impleamentation, only for GetTPasProcedureBody
Unformated:boolean; // no Formating in output
//# tools
function GetIndent(indent:integer):String;
var i:integer;
begin
Result:='';
if not Unformated then
for i:=1 to indent do Result:=Result+' ';
end;
//delete ugly quoting '''STRING'''
function DelQuot(s:String):String;
var i:integer;
const s1=#39#39#39;
begin
Result:='';
i:=pos(s1,s);
while i > 0 do
begin
if i > 0 then delete(s,i,2);
i:=pos(s1,s);
end;
//if i > 0 then delete(s,i,2);
Result:=s;
end;
//LeadingSpace only valid if Formated output (as this will be one line in output)
//UnFormated: all is printed in a new line
procedure WriteFmt(LeadingSpace:boolean; s:String; Semicolon:boolean);
begin
if Semicolon then s:=s+';';
if Unformated then writeln(s)
else if LeadingSpace then write(' ',s)
else write(s);
end;
//# parsing output
function GetTPasImplBlock(lb:TPasImplBlock; indent,declistby:integer;
LastNoSem,NoFirstIndent:boolean):boolean; forward;
function GetTPasImplElement(le:TPasImplElement; lindent:integer;
lLastNoSem,NoFirstIndent:boolean):boolean; forward;
procedure GetDecls(Decl:TPasDeclarations; indent:integer); forward;
//procedure PrintDecls(Decl:TPasDeclarations; indent:integer); forward;
//# most is for implementation or implblocks except the expr things
function ReturnTPasMemberHints(h:TPasMemberHints):String;
begin
Result:='';
if hDeprecated in h then Result:=' deprecated';
if hLibrary in h then Result:=Result+' library';
if hPlatform in h then Result:=Result+' platform';
if hExperimental in h then Result:=Result+' experimental';
if hUnimplemented in h then Result:=Result+' unimplemented';
end;
function GetTPasMemberHints(h:TPasMemberHints):Boolean;
begin
Result:=false;
if hDeprecated in h then begin write(' deprecated'); Result:=true; end;
if hLibrary in h then begin write(' library'); Result:=true; end;
if hPlatform in h then begin write(' platform'); Result:=true; end;
if hExperimental in h then begin write(' experimental'); Result:=true; end;
if hUnimplemented in h then begin write(' unimplemented'); Result:=true; end;
end;
function GetTPasExprKind(lpek:TPasExprKind):String;
begin
Result:='';
case lpek of
pekIdent:Result:='ID';
pekNumber:Result:='NUMBER';
pekString:Result:='STRING';
pekSet:Result:='SET';
pekNil:Result:='NIL';
pekBoolConst:Result:='BOOL';
pekRange:Result:='RANGE';
pekUnary:Result:='UNARY';
pekBinary:Result:='BINARY';
pekFuncParams:Result:='FUNCPAR';
pekArrayParams:Result:='ARRAYPAR';
pekListOfExp:Result:='EXPLIST';
end;
end;
procedure GetTPasExpr(lex:TPasExpr);
var lex1:TpasExpr;
lpe:TParamsExpr;
l:integer;
lbk,rbk,sep:string;
lav:TArrayValues;
lrv:TRecordValues;
rvi:TRecordValuesItem;
function GetExpKind(ek:TPasExprKind; var lbrak,rbrak:string):string;
begin
lbrak:='';
rbrak:='';
Result:='';
case ek of
pekIdent:Result:='ID';
pekNumber:Result:='NU';
pekString:begin lbrak:=#39; rbrak:=#39; Result:=#39; end;
pekSet:begin lbrak:='['; rbrak:=']'; Result:=','; end;
pekNil:Result:='NIL';
pekBoolConst:Result:='';
pekRange:Result:='..';
pekUnary:Result:='';
pekBinary:Result:='';
pekFuncParams:begin lbrak:='('; rbrak:=')'; Result:=','; end;
pekArrayParams:begin lbrak:='['; rbrak:=']'; Result:=','; end;
pekListOfExp:Result:=',';
pekInherited:Result:=' InheriteD';
pekSelf:Result:=' SelF';
end;
end;
function GetOp(lop:TExprOpCode):string;
begin
Result:='';
case lop of
eopNone:Result:='';
eopAdd:Result:='+';
eopSubtract:Result:='-';
eopMultiply:Result:='*';
eopDivide:Result:='/';
eopDiv:Result:=' div ';
eopMod:Result:=' mod ';
eopPower:Result:='^';
eopShr:Result:=' shr ';
eopSHl:Result:=' shl ';
eopNot:Result:=' not ';
eopAnd:Result:=' and ';
eopOr:Result:=' or ';
eopXor:Result:=' xor ';
eopEqual:Result:='=';
eopNotEqual:Result:='<>';
eopLessThan:Result:='<';
eopGreaterThan:Result:='>';
eopLessthanEqual:Result:='<=';
eopGreaterThanEqual:Result:='>=';
eopIn:Result:=' in ';
eopIs:Result:=' is ';
eopAs:Result:=' as ';
eopSymmetricaldifference:Result:='><';
eopAddress:Result:='@';
eopDeref:Result:='^';
eopSubIdent:Result:='.';
end;
end;
begin
if lex is TBinaryExpr then //compined constants
begin
sep:=GetExpKind(lex.Kind,lbk,rbk);
//write('|');
write(lbk);
GetTPasExpr(TBinaryExpr(lex).left);
write(GetOp(TBinaryExpr(lex).OpCode));
write(sep);
GetTPasExpr(TBinaryExpr(lex).right);
write(rbk);
//write('|');
//write(' [',lex.Name,' ',GetTPasExprKind(lex.Kind),']');
end
else
begin
//write('UNARY');
if lex is TUnaryExpr then
begin
lex1:=TUnaryExpr(lex).Operand;
if lex.OpCode = eopDeref then
begin
GetTPasExpr(lex1);
write(GetOp(lex.OpCode)); //unary last, only: p^
end
else
begin
write(GetOp(lex.OpCode)); //unary first: -1
GetTPasExpr(lex1);
end;
end;
if lex is TPrimitiveExpr then write(TPrimitiveExpr(lex).Value) //simple constant
else
if lex is TBoolConstExpr then write(TBoolConstExpr(lex).Value)
else
if lex is TNilExpr then write('nil')
else
if lex is TInheritedExpr then write('Inherited ')
else
if lex is TSelfExpr then write('Self')
else
if lex is TParamsExpr then //writeln(param1,param2,..,paramn);
begin
//write(' PAREX ');
lpe:=TParamsExpr(lex);
GetTPasExpr(lpe.Value);
if length(lpe.Params) >0 then
begin
sep:=GetExpKind(lpe.Kind,lbk,rbk);
write(lbk); //write('(');
for l:=0 to High(lpe.Params)-1 do
begin
GetTPasExpr(lpe.Params[l]);
write(sep); //seperator
end;
GetTPasExpr(lpe.Params[High(lpe.Params)]);
write(rbk);//write(')');
end
else
begin //funcion()
sep:=GetExpKind(lpe.Kind,lbk,rbk);
write(lbk,rbk);
end;
end
else if lex is TArrayValues then //const AnArrayConst: Array[1..3] of Integer = (1,2,3);
begin
write('(');
lav:=TArrayValues(lex);
if length(lav.Values) > 0 then
begin
for l:=0 to high(lav.Values)-1 do
begin
GetTPasExpr(TPasExpr(lav.Values[l]));
write(',');
end;
GetTPasExpr(TPasExpr(lav.Values[high(lav.Values)]));
end;
write(')');
end
else if lex is TRecordValues then
begin
write('(');
lrv:=TRecordValues(lex);
if length(lrv.Fields) > 0 then
begin
for l:=0 to high(lrv.Fields)-1 do
begin
rvi:=TRecordValuesItem(lrv.Fields[l]);
write(rvi.Name,':');
GetTPasExpr(rvi.ValueExp);
write(';');
end;
rvi:=TRecordValuesItem(lrv.Fields[high(lrv.Fields)]);
write(rvi.Name,':');
GetTPasExpr(rvi.ValueExp);
end;
write(')');
end
else
begin
//?
//writeln('{ Unknown Expression: ');
//if assigned(lex) then GetTPasExprKind(lex.Kind);
//writeln('}');
end;
end;
end;
//NoFirstIndent only for block in case:
procedure GetTPasSmt(lsmt:TPasImplStatement; lindent:integer; DoNoSem,NoFirstIndent:boolean);
var l:integer;
lics:TPasImplCaseStatement;
DoSem:boolean;
liwd:TPasImplWithDo;
liwhd:TPasImplWhileDo;
lieo:TPasImplExceptOn;
lifl:TPasImplForLoop;
lir:TPasImplRaise;
s,s1:String;//s1 only first line of block statement
begin
DoSem:=true;
s:=GetIndent(lindent);
if NoFirstIndent then s1:=' ' else s1:=s;
if lsmt is TPasImplSimple then
begin
write(s1); GetTPasExpr(TPasImplSimple(lsmt).expr);
//DoSem:=true;
end
else if lsmt is TPasImplAssign then
begin
write(s1); GetTPasExpr(TPasImplAssign(lsmt).left);
write(':= ');
GetTPasExpr(TPasImplAssign(lsmt).right);
//DoSem:=true;
end
else if lsmt is TPasImplCaseStatement then
begin
lics:=TPasImplCaseStatement(lsmt);
if lics.Expressions.Count>0 then
begin
write(s);
for l:=0 to lics.Expressions.Count-2 do
write(DelQuot(TPasExpr(lics.Expressions[l]).GetDeclaration(True)),',');
write(DelQuot(TPasExpr(lics.Expressions[lics.Expressions.Count-1]).GetDeclaration(True)),': '); // !!bug too much ' in expression
//if not assigned(lics.Body) then writeln('TPasImplCaseStatement missing BODY');
//if assigned(lics.Body) and (TPasImplBlock(lics.Body).Elements.Count >0) then
// GetTPasImplBlock(TPasImplBlock(lics.Body),lindent+1,0,false,true)
// else GetTPasImplBlock(TPasImplBlock(lics),lindent+1,0,false,true); // !!bug missing body, assigned but empty
if assigned(lics.Body) then
begin
if not GetTPasImplElement(lics.Body,lindent+1,false,true) then ;//writeln(';');
end
else writeln(';');
end;
DoSem:=false;
end
else if lsmt is TPasImplWithDo then
begin
liwd:=TPasImplWithDo(lsmt); // !!Bug: missing with do at following with do !solved see Bug
write(s1,'with ',liwd.Name);
if liwd.Expressions.Count>0 then
begin
for l:=0 to liwd.Expressions.Count-2 do
write(TPasExpr(liwd.Expressions[l]).GetDeclaration(true),',');
write(TPasExpr(liwd.Expressions[liwd.Expressions.Count-1]).GetDeclaration(true));
end;
writeln(' do');
//if TPasImplBlock(liwd.Body).Elements.Count >0 then
//GetTPasImplBlock(TPasImplBlock(liwd.Body),0); // !!Bug: BODY Not used
//else
GetTPasImplBlock(TPasImplBlock(liwd),lindent+1,0,false,false);
DoSem:=false;
end
else if lsmt is TPasImplWhileDo then
begin
liwhd:=TPasImplWhileDo(lsmt);
writeln(s1,'while ',DelQuot(liwhd.Condition),' do');
//if not GetTPasImplBlock(TPasImplBlock(liwhd.Body),0) then // !!Bug: BODY Not used
GetTPasImplBlock(TPasImplBlock(liwhd),lindent,0,DoNoSem,false); //OK for all constructs
DoNoSem:=false; //?
DoSem:=false;
end
else if lsmt is TPasImplExceptOn then
begin
lieo:=TPasImplExceptOn(lsmt);
writeln(s,'on ',lieo.VariableName,': ',lieo.TypeName,' do');
if TPasImplBlock(lieo.Body) is TPasImplRaise then
begin
write(s,'raise ');//raise is in TPasImplBlock in this case
GetTPasImplBlock(TPasImplBlock(lieo.Body),lindent+1,0,false,true);
end
else GetTPasImplBlock(TPasImplBlock(lieo.Body),lindent+1,0,false,false);
DoSem:=false;
end
else if lsmt is TPasImplForLoop then
begin
lifl:=TPasImplForLoop(lsmt);
//TODO variable
write(s1,'for ',lifl.Variable.Name,':= ',lifl.StartExpr.GetDeclaration(True),' ');
if lifl.Down then write('down');
writeln('to ',lifl.EndExpr.GetDeclaration(True),' do');
GetTPasImplBlock(TPasImplBlock(lifl),lindent+1,0,false,false);
DoSem:=false;
end
else if lsmt is TPasImplRaise then
begin
write(s1,'raise ');
lir:=TPasImplRaise(lsmt);
if not GetTPasImplBlock(TPasImplBlock(lir),lindent,0,DoNoSem,true) then
writeln(';');
DoNoSem:=false;
DoSem:=false;
end
else
begin
if assigned(lsmt.Elements) then
begin
writeln('{ Unknown SMT(s): '); //,lsmt.Name,' ',lsmt.ElementTypeName);
for l:=0 to lsmt.Elements.Count-1 do
write(s,' SMT ',l,' ',TPasElement(lsmt.Elements[l]).Name);
writeln('}');
end;
DoSem:=false;
end;
if not DoNoSem then
begin
if DoSem then writeln(';');
end
else writeln;
end;
//result: result of TPasImplBlock or valid element
//NoFirstIndent only for block in case:
function GetTPasImplElement(le:TPasImplElement; lindent:integer;
lLastNoSem,NoFirstIndent:boolean):boolean;
var liie:TPasImplIfElse;
lico:TPasImplCaseOf;
lice:TPasImplCaseElse;
liru:TPasImplRepeatUntil;
lit:TPasImplTry;
//lic:TPasImplCommand;
s,s1:String;//s1 only first line of block statement
begin
Result:=true;
s:=GetIndent(lindent);
if NoFirstIndent then s1:=' ' else s1:=s;
if le is TPasImplStatement then
begin
if NoFirstIndent then lindent:=0;
GetTPasSmt(TPasImplStatement(le),lindent+1,lLastNoSem,NoFirstIndent);
end
else if le is TPasImplIfElse then
begin
liie:=TPasImplIfElse(le);
write(s1,'if ',DelQuot(liie.Condition),' then ');
if assigned(liie.ElseBranch) then
begin
writeln;
GetTPasImplElement(liie.IfBranch,lindent+1,true,false);
writeln(s,'else');// {if}');
GetTPasImplElement(liie.ElseBranch,lindent+1,lLastNoSem,false);
end
else
begin //no else part
if assigned(liie.IfBranch) then
begin
writeln;
if not GetTPasImplElement(liie.IfBranch,lindent+1,false,false) then
writeln(';');
end
else writeln(';'); //empty if then;
end;
end
else if le is TPasImplCaseOf then
begin
lico:=TPasImplCaseOf(le);
writeln(s1,'case ',lico.Expression,' of ');
if assigned(lico.ElseBranch) then //workaround duplicate bug
begin //reduce count of CaseOf as CaseElse is in there
lice:=lico.ElseBranch;
GetTPasImplBlock(TPasImplBlock(lico),lindent+1,1,false,false);
end
else GetTPasImplBlock(TPasImplBlock(lico),lindent+1,0,false,false); // !! else duplicate in here
if assigned(lico.ElseBranch) then
begin
writeln(s,'else');//' {case}');
lice:=lico.ElseBranch;
GetTPasImplBlock(TPasImplBlock(lice),lindent+1,0,false,false);
end;
if lLastNoSem then writeln(s,'end')//' {case}')
else writeln(s,'end;');// {case}');
//Result:=false; ??? GetTPasImplBlock
end
else if le is TPasImplRepeatUntil then
begin
liru:=TPasImplRepeatUntil(le);
writeln(s1,'repeat');
GetTPasImplBlock(TPasImplBlock(liru),lindent+1,0,false,false);
write(s,'until ',DelQuot(liru.Condition));
if lLastNoSem then writeln
else writeln(';');
end
else if le is TPasImplTryFinally then
begin
writeln(s,'finally');
GetTPasImplBlock(TPasImplBlock(le),lindent+1,0,false,false);
end
else if le is TPasImplTryExcept then
begin
writeln(s,'except');
GetTPasImplBlock(TPasImplBlock(le),lindent+1,0,false,false);
end
else if le is TPasImplTryExceptElse then
begin
writeln(s,'else');// {try}');
GetTPasImplBlock(TPasImplBlock(le),lindent+1,0,false,false);
end
else if le is TPasImplTry then
begin
lit:=TPasImplTry(le);
writeln(s1,'try');
GetTPasImplBlock(TPasImplBlock(le),lindent+1,0,false,false);
if assigned(lit.FinallyExcept) then
GetTPasImplElement(TPasImplElement(lit.FinallyExcept),lindent+1,false,false);
if assigned(lit.ElseBranch) then
GetTPasImplElement(TPasImplElement(lit.ElseBranch),lindent+1,false,false);
if lLastNoSem then writeln(s,'end')// {try} ') //there is no ImplBeginBlock
else writeln(s,'end;');// {try} ');
end
else if le is TPasImplCommand then
begin
//ignore because empty
// lic:=TPasImplCommand(le);
// writeln(' CMD ',lic.Command,' ',lic.Name,' ',lic.ElementTypeName);
end
else if le is TPasImplLabelMark then
begin
writeln(s1,'label ',TPasImplLabelMark(le).LabelId,';');
end
else if le is TPasImplBlock then
begin
//IfElse, case:
Result:=GetTPasImplBlock(TPasImplBlock(le),lindent+1,0,lLastNoSem,NoFirstIndent);
end
else
begin
Result:=false;
//writeln(s,';');
//writeln(' EL ',l);//,' ',le.Name)//,' ',le.ElementTypeName,' ',le.FullName);
end;
end;
// indent: indent from page left side
// DecListBy: dec(elements.count) because of case duplicate else bug
// LastNoSem: only true on last expr before else in a if clause
// NoFirstIndent: if line was started by other block like in case at -> 1:Noindent;
// Result: true if elements not empty
function GetTPasImplBlock(lb:TPasImplBlock; indent,declistby:integer;
LastNoSem,NoFirstIndent:boolean):boolean;
var l,n:integer;
lbe:TPasImplElement;
NoSem:boolean;
ls:String;
begin
Result:=false;
NoSem:=false;
ls:=GetIndent(indent);
if not assigned(lb) then exit;
//if lb is TPasImplRaise then writeln('RAISE');
if assigned(lb.Elements) then
begin
if lb is TPasImplBeginBlock then
begin
NoSem:=LastNoSem;
LastNoSem:=false;
if NoFirstIndent then
begin
writeln('begin');////NFI');
NoFirstIndent:=false;
end
else writeln(ls,'begin');
inc(indent);
end;
if lb.Elements.Count >0 then
begin
Result:=true;
n:=lb.Elements.Count-1;
//workaround CaseOf duplicate bug
if (declistby >0)and(lb.Elements.Count >declistby) then dec(n,declistby);
for l:=0 to n do
begin
lbe:=TPasImplElement(lb.Elements[l]);
//write(l:2,'/',n:2,' '); //No of curent element, max element
if ((l = 0)and NoFirstIndent) then
begin //index0
if l=n then GetTPasImplElement(lbe,0,LastNoSem,false)
else GetTPasImplElement(lbe,0,false,false)
end
else if l<>n then GetTPasImplElement(lbe,indent,false,false) //other index
else GetTPasImplElement(lbe,indent,LastNoSem,false); //indexn
end;
end
else
begin //block is empty
//write(ls,' {!EMPTY!}');
{if not NoSem then
begin
if lb is TPasImplBeginBlock then writeln //empty compound need no ;
else writeln(';')
end
else
writeln;}
end;
if lb is TPasImplBeginBlock then
if not NoSem then writeln(ls,'end;')// {Block}')
else writeln(ls,'end');// {Block}');
end
else
writeln(';'); //writeln(' {!empty!};')
end;
//# Declarations (type,var,const,..)
procedure GetTPasArrayType(lpat:TPasArrayType);
begin
if lpat.IsPacked then write('packed ');
write('Array');
if lpat.IndexRange <> '' then write('[',lpat.IndexRange,']');
if assigned(lpat.ElType) then write(' of ',lpat.ElType.Name);
// BUG: of const missing in Procedure ConstArrayArgProc(A: Array of const); pparser: 643
end;
//write out one variable or constant declaration, also used in types
//In spite of the use of GetPasVariables this is still used !
procedure GetTPasVar(lpv:TPasVariable; lindent:integer; NoLF:boolean);//BUG string[] pparser: 482
var i,j:integer;
//lppt:TPasProcedureType;
//lpa:TPasArgument;
//lpat:TPasArrayType;
s,s1:string;
prct:TPasRecordType;
begin
if not Assigned(lpv) then exit;
s:=GetIndent(lindent);
write(s,lpv.Name);//,' ',lpv.value,' ',lpv.Modifiers,' ',lpv.AbsoluteLocation);
if assigned(lpv.VarType) then
begin
//if TPasType(lpa.ArgType).ElementTypeName <>'unresolved type reference' then
//,TPasType(lpa.ArgType).Name,' ');//,TPasType(lpa.ArgType).FullName,TPasType(lpa.ArgType).ElementTypeName)
// PParser 2099: ArgType := nil; if IsUntyped then => Arg.ArgType := ArgType;
// else write(':? ');
write(': ');
if lpv.VarType is TPasArrayType then
begin
GetTPasArrayType(TPasArrayType(lpv.VarType));
end
else if lpv.VarType is TPasSetType then
begin
write('set of ',TPasSetType(lpv.VarType).EnumType.Name);
end
else
begin
if lpv.VarType is TPasPointerType then
write('^',TPasPointerType(lpv.VarType).DestType.Name)
else if lpv.VarType is TPasRecordType then //var record
begin
j:=lindent+Length(lpv.Name)+4;
s1:=GetIndent(j);
prct:=TPasRecordType(lpv.VarType);
if prct.IsBitPacked then write('bitpacked ');
if prct.IsPacked then write('packed ');
writeln('Record');
for i:=0 to prct.Members.Count-1 do
begin
GetTPasVar(TPasVariable(prct.Members[i]),j+1,false);
end;
write(s1,'end');
end
else
begin
write(TPasType(lpv.VarType).Name);
//if TPasType(lpv.VarType) is TPasAliasType then write(TPasAliasType(lpv.VarType).Name);
end;
end;
end;
if lpv.Value <> '' then write('=',lpv.Value);
if assigned(lpv.Expr) then // var ?, const AnArrayConst : Array[1..3] of Integer = (1,2,3);
begin
write('=');
GetTPasExpr(lpv.Expr);
end;
if lpv.Modifiers <>'' then //Modifiers starts with ;
begin
write(' ',lpv.Modifiers,';');
if GetTPasMemberHints(lpv.Hints) then write(';');
end
else
begin
GetTPasMemberHints(lpv.Hints);
write(';');
end;
if not NoLF then writeln;
end;
//write out a list of variables only
//more compact than the output of seperate calls of GetTPasVar
procedure GetPasVariables(vl:TFPList; lindent:integer; NoLF,NoSEM:boolean);
var v,i,j:integer;
s,s1:string;
prct:TPasRecordType;
lpv:TPasVariable;
same:boolean;
samestr,tmpstr:Ansistring;
samevar:array of integer;
svi:integer;
begin
if vl.Count <= 0 then exit;
s:=GetIndent(lindent);
//> compare all variable types as string to find the ones with same type
samestr:='';
svi:=0;
SetLength(samevar,vl.count);
for v:=0 to vl.count-1 do
begin
tmpstr:='';
same:=true;
lpv:=TPasVariable(vl[v]);
//write(s,lpv.Name);
if assigned(lpv.VarType) then
begin
tmpstr:=tmpstr+': ';
if lpv.VarType is TPasArrayType then
begin
//GetTPasArrayType(TPasArrayType(lpv.VarType));
tmpstr:=tmpstr+'array'+TPasArrayType(lpv.VarType).IndexRange;
if assigned(TPasArrayType(lpv.VarType).ElType) then
tmpstr:=tmpstr+TPasArrayType(lpv.VarType).ElType.Name;
end
else if lpv.VarType is TPasSetType then
begin
tmpstr:=tmpstr+'set of '+TPasSetType(lpv.VarType).EnumType.Name;
end
else
begin
if lpv.VarType is TPasPointerType then
tmpstr:=tmpstr+'^'+TPasPointerType(lpv.VarType).DestType.Name
else if lpv.VarType is TPasRecordType then //var record
begin
prct:=TPasRecordType(lpv.VarType);
if prct.IsBitPacked then tmpstr:=tmpstr+'bitpacked ';
if prct.IsPacked then tmpstr:=tmpstr+'packed ';
tmpstr:=tmpstr+'Record ';
for i:=0 to prct.Members.Count-1 do
begin
//todo
//GetTPasVar(TPasVariable(prct.Members[i]),j+1,false);
end;
tmpstr:=tmpstr+'end';
end
else
begin
tmpstr:=tmpstr+TPasType(lpv.VarType).Name;
end;
end;
end
else same:=false;
if lpv.Value <> '' then same:=false;//=
if assigned(lpv.Expr) then // var ?, const AnArrayConst : Array[1..3] of Integer = (1,2,3);
begin
same:=false;//=
end;
if lpv.Modifiers <>'' then //Modifiers starts with ;
begin
tmpstr:=tmpstr+' '+lpv.Modifiers+';';
tmpstr:=tmpstr+ReturnTPasMemberHints(lpv.Hints);
end
else
begin
tmpstr:=tmpstr+ReturnTPasMemberHints(lpv.Hints);
end;
//if v = 0 then begin samestr:=tmpstr; end;
if (not same)or(samestr <> tmpstr) then
begin
samestr:=tmpstr;
inc(svi);
end;
samevar[v]:=svi;
end;
//compare <
//now print them
svi:=-1;
for v:=0 to vl.count-1 do
begin
lpv:=TPasVariable(vl[v]);
if not Assigned(lpv) then continue;
if svi <> samevar[v] then
begin
svi:=samevar[v];
if v>0 then writeln;
write(s,lpv.Name);//variblenname
end
else write(lpv.Name);
if (v < vl.Count-1)and(samevar[v+1]=svi) then write(',')
else
begin
if assigned(lpv.VarType) then
begin
write(': ');
if lpv.VarType is TPasArrayType then
begin
GetTPasArrayType(TPasArrayType(lpv.VarType));
end
else if lpv.VarType is TPasSetType then
begin
write('set of ',TPasSetType(lpv.VarType).EnumType.Name);
end
else
begin
if lpv.VarType is TPasPointerType then
write('^',TPasPointerType(lpv.VarType).DestType.Name)
else if lpv.VarType is TPasRecordType then //var record
begin
j:=lindent+Length(lpv.Name)+4;
s1:=GetIndent(j);
prct:=TPasRecordType(lpv.VarType);
if prct.IsBitPacked then write('bitpacked ');
if prct.IsPacked then write('packed ');
writeln('Record');
{for i:=0 to prct.Members.Count-1 do
begin
GetTPasVar(TPasVariable(prct.Members[i]),j+1,false);
end;}
if prct.Members.Count > 0 then
GetPasVariables(prct.Members,j+1,false,false);
write(s1,'end');
end
else
begin
write(TPasType(lpv.VarType).Name);
end;
end;
end;
if lpv.Value <> '' then write('=',lpv.Value);