/
PREPROC.PAS
1926 lines (1781 loc) · 71.5 KB
/
PREPROC.PAS
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
unit Preproc;
{$IFDEF FPC}
{$MODE ObjFPC}
{$ENDIF}
{$H+}
{$IF DEFINED(UNIX) OR DEFINED(LINUX)}
{$linklib c}
{$linklib stdc++}
{$ENDIF}
{$CODEPAGE UTF8}
interface
uses
SysUtils, dynlibs, Common, Classes, Lexique, Evaluat;
Const PassCount: Integer = 0;
Const PreprocPasses: Integer = 1;
Const NewFileName: String = '';
procedure PreprocStage1(ApplyConds: Boolean;
Defines: TStringList;
Var IsClustered, IsVectorized: Boolean;
Var Restrictions: TRestrictions;
PreChecks: Boolean);
procedure PreprocStage1a(Var Restrictions: TRestrictions; PreChecks, IsVectorized, IsClustered: Boolean);
procedure PreprocStage2(Var Restrictions: TRestrictions);
Type PatternModes = (pmNone, pmScan, pmParse);
Var PatternsMode: PatternModes = pmNone;
PatternsOrder: TStringList = Nil;
Var Prologer: TLibHandle = DWORD(-1);
implementation
uses Consts, regexpr, xpath, xmlread, dom, Math;
(* *)
Type run_gprolog7 = Function(Dir, ConsultScript, OutFName, MainGoal, gprolog_console_out, DoneGoal, GID: PChar):Integer; cdecl;
switch_gprolog7 = Function(gprolog_console_out: PChar): Integer; cdecl;
Var (* *)runer: run_gprolog7; (* *)
(* *)initer: switch_gprolog7; (* *)
(* *)
Const BracketsOff = '$brackets_off$';
PrefixOff = '$prefix_off$';
Pruning = '$prune$';
Const aliveDB = '_alive.pl';
phoenixDB = '_phoenix.pl';
Const Predicates: PChar =
'brackets_off:-write('''+BracketsOff+''').'+CRLF+
'prefix_off:-write('''+PrefixOff+''').'+CRLF+
'prune:-write('''+Pruning+''').'+CRLF+
'plan_parallel_chain(A,B):-write(''plan_parallel_chain(''),write(A),write('',''),write(B),write('');''),nl.'+CRLF+
'plan_parallel_reverse(A,B):-write(''plan_parallel_reverse(''),write(A),write('',''),write(B),write('');''),nl.'+CRLF;
Type TDefItem = Record
Cond, Any: Boolean;
IgnoreElse: Boolean;
FirstLine: Integer
End;
Type
{ TModule }
TModule = Class
Params: TStringList;
Prefix: String;
Body: String;
Constructor Create(_Params: TStringList; Const Pref, _Body: String);
Destructor Destroy; Override;
End;
{ TPattern }
TPattern = Class
ActionID: String;
Fact: Boolean;
Params: TObjStrList;
Context1, Needle, Context2: WideString;
Constructor Create(Line: Integer; Const ModID: String; IsFact: Boolean; ModParams: TObjStrList; Const Body: WideString);
Function GetReplacement(ENV: TXPathEnvironment; Const S: RawByteString): WideString;
Destructor Destroy; Override;
End;
constructor TPattern.Create(Line: Integer; const ModID: String;
IsFact: Boolean; ModParams: TObjStrList; const Body: WideString);
Var L: TStringList;
F: Integer;
begin
ActionID := ModID;
Fact := IsFact;
Params := ModParams;
L := TStringList.Create;
L.Text := UTF8Encode(Body);
Context1 := '';
Needle := '';
Context2 := '';
F := 0;
While (F < L.Count) And (UpperCase(Trim(L[F])) <> SpecSymbol + idbegin) Do
begin
If Trim(L[F]) <> '' Then
Context1 := Context1 + Trim(L[F]);
Inc(F)
end;
If F < L.Count Then
begin
Inc(F);
While (F < L.Count) And (UpperCase(Trim(L[F])) <> SpecSymbol + idend) Do
begin
If Trim(L[F]) <> '' Then
Needle := Needle + Trim(L[F]);
Inc(F)
end;
If Needle = '' Then
begin
WriteLn('Needle part between '+SpecSymbol+idbegin, ' and '+SpecSymbol+idend, ' can''t be empty', TLine(Lines.Objects[Line]).GetDescription);
Halt(-52)
end;
If F = L.Count Then
begin
WriteLn(idend, ' expected in pattern definition ', TLine(Lines.Objects[Line]).GetDescription);
Halt(-51)
end;
Inc(F);
While F < L.Count Do
begin
If Trim(L[F]) <> '' Then
Context2 := Context2 + Trim(L[F]);
Inc(F)
end;
end;
L.Free;
Context1 := UTF8Decode(Context1);
Needle := UTF8Decode(Needle);
Context2 := UTF8Decode(Context2);
end;
function TPattern.GetReplacement(ENV: TXPathEnvironment; const S: RawByteString): WideString;
Var src: TXMLInputSource;
parser: TDOMParser;
dom: TXMLDocument;
res: TXPathVariable;
function GetList(Const First, Last: String; _Params: TObjStrList): String;
Var K, M: Integer;
Begin
Result := First;
For K := 0 To _Params.Count-1 Do
If Assigned(_Params.Objects[K]) Then
Result := Result + GetList('[',']', TObjStrList(_Params.Objects[K])) + ','
Else
Begin
res := EvaluateXPathExpression(_Params[K], dom.DocumentElement, Nil, [], Nil, Nil, ENV);
ENV.commitUndo(0);
If res is TXPathNodeSetVariable Then
If res.AsNodeSet.Count = 0 Then
Result := Result + ''''','
Else
Begin
If res.AsNodeSet.Count > 1 Then Result := Result + '[';
For M := 0 To res.AsNodeSet.Count-1 Do
Result := Result + '''' + StringReplace(TDOMNode(res.AsNodeSet[M]).TextContent, '''', '''''', [rfReplaceAll]) + ''',';
If res.AsNodeSet.Count > 0 Then
System.Delete(Result, Length(Result), 1);
If res.AsNodeSet.Count > 1 Then Result := Result + ']';
Result := Result + ','
End
Else
Result := Result + '''' + StringReplace(res.AsText, '''', '''''', [rfReplaceAll]) + ''',';
res.Free
End;
If _Params.Count > 0 Then
System.Delete(Result, Length(Result), 1);
Result := Result + Last
End;
begin
Result := UnescapeString(ActionID);
src := TXMLInputSource.Create(S);
parser := TDOMParser.Create;
parser.Parse(src, dom);
Try
If (Params.Count > 0) Or Not Fact Then
Result := Result + GetList('(', ')', Params)
Else
Result := Result + GetList('', '', Params)
finally
dom.Free;
parser.Free;
src.Free
end
end;
destructor TPattern.Destroy;
begin
Params.Free;
Inherited Destroy
end;
{ TModule }
Constructor TModule.Create(_Params: TStringList; const Pref, _Body: String);
Begin
Inherited Create;
Params := _Params;
Prefix := Pref;
Body := _Body
End;
Destructor TModule.Destroy;
begin
Params.Free;
Inherited Destroy
end;
Function HandleDefined(Const S: String): String;
Var F: Integer;
Begin
Result := StringReplace(S, ' ', '', [rfReplaceAll]);
Result := StringReplace(Result,'defined(1)','1',[rfReplaceAll]);
// Other defined(id) = 0
Repeat
F := Pos('defined(',Result);
If F > 0 Then
Begin
Inc(F, 8);
While (F < Length(Result)) And (Result[F] in IdentSet) Do
Delete(Result, F, 1);
Delete(Result, F-8, 9);
Insert('0', Result, F-8)
End
Until F = 0
End;
procedure PreprocStage1(ApplyConds: Boolean;
Defines: TStringList;
Var IsClustered, IsVectorized: Boolean;
Var Restrictions: TRestrictions;
PreChecks: Boolean);
Var L: TAnalyser; Eval: TEvaluator;
S: WideString;
S1, S2: WideString;
LSaveCounter: Integer;
HasParams: Boolean;
ID: String;
DefStack: Array Of TDefItem;
ModeGPU: Byte;
FL: TStringList;
FF: TextFile;
FS: WideString;
CommentFlag: Boolean;
NotEmpty: Boolean;
LN: TLine;
Rslt: Real;
UseLine: Boolean;
Original: WideString;
PolyLineDef: Boolean;
Flag: Boolean;
F, K: Integer;
begin
L:=TAnalyser.Create(IdentSet,[Space,Tabulation]);
SetLength(Restrictions, 0);
IsClustered := False;
IsVectorized := False;
SetLength(DefStack, 0);
ModeGPU := gpuNone;
if (PreChecks) Then
VerifyProgram(True, False, False, False);
LCounter:=1;
// First preprocessing stage
// Handle simple defines: substitute into defines and consts
While (LCounter <= Lines.Count) And Not L.Error Do
Begin
S := UTF8Decode(Lines.Strings[LCounter-1]);
L.AnlzLine := S;
PolyLineDef := False;
// Check gpu/common directives
If L.IsNext(Pound) Then // #...
Begin
L.Check(Pound);
S1 := L.GetIdent(True);
If S1 = iddefine Then
Begin
S1 := Trim(L.AnlzLine);
PolyLineDef := (Length(S1) > 0) And (S1[Length(S1)] = BackSlash)
End
Else If S1 = idpragma Then
Begin
If L.FindIdent(True) = idplan Then
Begin
L.GetIdent(True);
S1 := L.FindIdent(True);
If (S1 = idgpu) Or (S1 = idcommon) Then
Begin
L.GetIdent(True);
If (ModeGPU = gpuBoth) And (S1 = idgpu) Or
(ModeGPU = gpuOnly) And (S1 = idcommon) Then
Begin
WriteLn('Unexpected mode: previous gpu/common section is not closed',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End;
S2 := L.GetIdent(True);
If (S2 = idbegin) And (ModeGPU <> gpuNone) Then
Begin
WriteLn('Unexpected begin: gpu/common section is already open',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End;
If ModeGPU = gpuNone Then
If S2 = idbegin Then
If S1 = idgpu Then
ModeGPU := gpuOnly
Else
ModeGPU := gpuBoth
Else If S2 = idend Then
Begin
WriteLn('Unexpected end: there is no opened gpu/common section',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End
Else
Begin
WriteLn('begin expected',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End
Else If S2 = idend Then
ModeGPU := gpuNone
Else
Begin
WriteLn('end expected',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End;
TLine(Lines.Objects[LCounter-1]).Free;
Lines.Delete(LCounter-1);
Continue // return to big preprocessing loop header
End
End
End
End;
S := UTF8Decode(Lines.Strings[LCounter-1]);
L.AnlzLine := S;
// Check is this line common or gpu line
TLine(Lines.Objects[LCounter-1])._ModeGPU := ModeGPU;
LSaveCounter := LCounter;
// Check if #defs allows compilation: -> Flag
Flag := True;
For F := 0 To High(DefStack) Do
If Not DefStack[F].Cond Then
Begin
Flag := False;
Break
End;
UseLine := Flag;
If Flag Then
Begin
If L.IsNext(Pound) Then // #...
Begin
L.Check(Pound);
S1 := L.GetIdent(True);
If S1 = idpragma Then
Begin
If L.FindIdent(True) = idplan Then
Begin
L.GetIdent(True);
S2 := L.FindIdent(True);
If S2 = idclustered Then
Begin
IsClustered := True;
WriteLn('Warning: clustering mode is on',TLine(Lines.Objects[LCounter-1]).GetDescription);
TLine(Lines.Objects[LCounter-1]).Free;
Lines.Delete(LCounter-1);
Continue // return to big preprocessing loop header
End
Else If (S2 = idvectorized) Or (S2 = idtypized) Then
Begin
IsVectorized := True;
WriteLn('Warning: vectorizing mode is on',TLine(Lines.Objects[LCounter-1]).GetDescription);
TLine(Lines.Objects[LCounter-1]).Free;
Lines.Delete(LCounter-1);
Continue // return to big preprocessing loop header
End
End
End
Else If S1 = idUndef Then
Begin
ID := L.GetIdent(False);
F := _IndexOf(Defines, ID);
If F >= 0 Then
Begin
DisposeStr(PString(Defines.Objects[F]));
Defines.Delete(F)
End;
Inc(LCounter);
Continue
End
End
End;
Original := S;
For F := 0 To Defines.Count-1 Do
S := Substitute(S, '', '', Defines.Strings[F], PString(Defines.Objects[F])^);
Lines.Strings[LCounter-1] := UTF8Encode(S);
L.AnlzLine := S;
If L.IsNext(Pound) Then // #...
Begin
L.Check(Pound);
S := L.GetIdent(True);
If Flag And (S = idDefine) Then
Begin
ID := L.GetIdent(False);
NotEmpty := Length(L.AnlzLine) > 0;
HasParams := (Length(L.AnlzLine) > 0) And (L.AnlzLine[1] = LeftBracket);
S1 := '';
S := Trim(L.AnlzLine);
Repeat
Flag := (Length(S) > 0) And (S[Length(S)] = BackSlash);
If Flag Then
Begin
SetLength(S, Length(S)-1);
S := TrimRight(S)
End;
S1 := S1 + S;
If Flag Then
Begin
Inc(LCounter);
S := UTF8Decode(Lines.Strings[LCounter-1]);
TLine(Lines.Objects[LCounter-1])._ModeGPU := ModeGPU;
S := ' '+Trim(S)
End
Until Not Flag;
ID := Trim(ID);
If Length(ID) = 0 Then
WriteLn('Warning: #define is not recognized (may be duplicate). Compilation may be incorrect',TLine(Lines.Objects[LCounter-1]).GetDescription)
Else
Begin
// Substitutions
F := _IndexOf(Defines, ID);
If F >= 0 Then
WriteLn('Panic: #define ', ID, ' encountered more than once. Compilation may be incorrect',TLine(Lines.Objects[LCounter-1]).GetDescription)
Else
Begin
S1 := Trim(S1);
For F := 0 To Defines.Count-1 Do
S1 := Substitute(S1, '', '', Defines.Strings[F], PString(Defines.Objects[F])^);
If Length(S1) = 0 Then
If NotEmpty Then
S1 := ' '
Else
S1 := '1';
If HasParams Then
S2 := '#define ' + ID + S1
Else
S2 := '#define ' + ID + ' ' + S1;
If (ModeGPU <> gpuOnly) And Not HasParams Then
Defines.AddObject(ID, TObject(NewStr(S1)))
End
End
End
Else If Flag And (S = idPreprocPasses) Then
Begin
If L.Check(LeftBracket) And L.GetCheckedNumber(True, Rslt, 1.0, 100.0) Then
Begin
PreprocPasses := Round(Rslt);
If L.IsNext(Comma) Then
Begin
L.Check(Comma);
NewFileName := L.GetString(DblQuote, DblQuote);
L.Check(RightBracket)
End
Else L.Check(RightBracket)
End;
If Assigned(Lines.Objects[LCounter-1]) Then
Lines.Objects[LCounter-1].Free;
Lines.Delete(LCounter-1);
Continue
End
Else If Flag And (S = idInclude) Then
Begin
If L.IsNext(DblQuote) Then
Begin
L.Check(DblQuote);
S := Trim(L.GetBefore(True,[DblQuote]));
L.Check(DblQuote);
K := _IndexOf(Files, Asterisk+S);
If K >= 0 Then
Begin
WriteLn('Warning: preliminary preprocessing encounters more than one #include "'+S+'"',TLine(Lines.Objects[LCounter-1]).GetDescription);
Lines.Delete(LCounter-1)
End
Else
Begin
FL := TStringList.Create;
Try
FL.LoadFromFile(S);
CommentFlag := False;
For K := 0 To FL.Count-1 Do
Begin
FS := UTF8Decode(FL.Strings[K]);
StripComments(FS, CommentFlag);
FL.Strings[K] := UTF8Encode(FS)
End
Except
WriteLn('Error: can''t open file "'+S+'"',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1);
End;
K := Files.AddObject(Asterisk+S, FL);
LN := TLine(Lines.Objects[LCounter-1]);
Lines.Delete(LCounter-1);
For F := 0 To FL.Count-1 Do
Lines.InsertObject(LCounter-1+F,FL.Strings[F], TLine.Derive(LN,K,F+1));
LN.Free
End;
Continue // return to loop header without end LCounter incrementing
End
Else
WriteLn('Warning: #include'+L.AnlzLine+' is bypassed at the preliminary incremental preprocessing',TLine(Lines.Objects[LCounter-1]).GetDescription)
End
Else If (S = idIfdef) Or (S = idIfndef) Then
Begin
SetLength(DefStack, Length(DefStack)+1);
L.AnlzLine := Original;
L.Check(Pound);
L.CheckIdent(S, True);
S1 := Trim(L.AnlzLine);
If Length(S1) = 0 Then
DefStack[High(DefStack)].Cond := S = idIfndef
Else
Begin
ID := L.FindIdent(False);
If Length(ID) = 0 Then
DefStack[High(DefStack)].Cond := False
Else
DefStack[High(DefStack)].Cond := _IndexOf(Defines, ID) >= 0;
DefStack[High(DefStack)].Cond := DefStack[High(DefStack)].Cond Xor (S = idIfndef)
End;
DefStack[High(DefStack)].Any := False;
DefStack[High(DefStack)].IgnoreElse := False;
DefStack[High(DefStack)].FirstLine := LCounter+1;
UseLine := False
End
Else If S = idIf Then
Begin
L.AnlzLine := Original;
L.Check(Pound);
L.CheckIdent(S, True);
S1 := Trim(L.AnlzLine);
For F := 0 To Defines.Count-1 Do
S1 := Substitute(S1, '', '', 'defined('+Defines.Strings[F]+')', '1');
For F := 0 To Defines.Count-1 Do
S1 := Substitute(S1, '', '', Defines.Strings[F], PString(Defines.Objects[F])^);
S1 := HandleDefined(S1);
Eval := TEvaluator.Create(IdentSet,[Space, Tabulation]);
Eval.AnlzLine := S1;
SetLength(DefStack, Length(DefStack)+1);
Rslt := 0.0;
If Eval.EvalExpression([],Rslt) Then
Begin
DefStack[High(DefStack)].Cond := Abs(Rslt)>1E-8;
DefStack[High(DefStack)].Any := False;
End
Else
Begin
DefStack[High(DefStack)].Cond := True;
DefStack[High(DefStack)].Any := True;
End;
DefStack[High(DefStack)].IgnoreElse := False;
DefStack[High(DefStack)].FirstLine := LCounter+1;
If Eval.Error Then
WriteLn('Evaluator error',TLine(Lines.Objects[LCounter-1]).GetDescription);
Eval.Free;
UseLine := DefStack[High(DefStack)].Any
End
Else If S = idElse Then
Begin
If Length(DefStack) = 0 Then
Begin
WriteLn('#else without #if',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End;
If Not (DefStack[High(DefStack)].Any Or DefStack[High(DefStack)].IgnoreElse) Then
Begin
If Not DefStack[High(DefStack)].Cond Then
Begin
SetLength(Restrictions, Length(Restrictions)+1);
Restrictions[High(Restrictions)].First := DefStack[High(DefStack)].FirstLine;
Restrictions[High(Restrictions)].Last := LCounter-1
End;
DefStack[High(DefStack)].Cond := Not DefStack[High(DefStack)].Cond;
DefStack[High(DefStack)].IgnoreElse := False;
DefStack[High(DefStack)].FirstLine := LCounter+1
End;
UseLine := DefStack[High(DefStack)].Any
End
Else If S = idElif Then
Begin
If Length(DefStack) = 0 Then
Begin
WriteLn('#else without #if',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End;
If Not (DefStack[High(DefStack)].Any Or DefStack[High(DefStack)].IgnoreElse) Then
Begin
If Not DefStack[High(DefStack)].Cond Then
Begin
L.AnlzLine := Original;
L.Check(Pound);
L.CheckIdent(S, True);
S1 := Trim(L.AnlzLine);
For F := 0 To Defines.Count-1 Do
S1 := Substitute(S1, '', '', 'defined('+Defines.Strings[F]+')', '1');
For F := 0 To Defines.Count-1 Do
S1 := Substitute(S1, '', '', Defines.Strings[F], PString(Defines.Objects[F])^);
S1 := HandleDefined(S1);
Eval := TEvaluator.Create(IdentSet,[Space, Tabulation]);
Eval.AnlzLine := S1;
If Eval.EvalExpression([],Rslt) Then
Begin
DefStack[High(DefStack)].Cond := Abs(Rslt)>1E-8;
DefStack[High(DefStack)].Any := False;
End
Else
Begin
DefStack[High(DefStack)].Cond := True;
DefStack[High(DefStack)].Any := True;
End;
If DefStack[High(DefStack)].Cond Then
Begin
SetLength(Restrictions, Length(Restrictions)+1);
Restrictions[High(Restrictions)].First := DefStack[High(DefStack)].FirstLine;
Restrictions[High(Restrictions)].Last := LCounter-1;
DefStack[High(DefStack)].FirstLine := LCounter+1;
End;
DefStack[High(DefStack)].IgnoreElse := False;
If Eval.Error Then
WriteLn('Evaluator error',TLine(Lines.Objects[LCounter-1]).GetDescription);
Eval.Free
End
Else
Begin
DefStack[High(DefStack)].Cond := False;
DefStack[High(DefStack)].IgnoreElse := True;
DefStack[High(DefStack)].FirstLine := LCounter+1
End
End;
UseLine := DefStack[High(DefStack)].Any
End
Else If S = idEndif Then
Begin
If Length(DefStack) = 0 Then
Begin
WriteLn('#endif without #if',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End;
If Not (DefStack[High(DefStack)].Cond Or DefStack[High(DefStack)].Any)Then
Begin
SetLength(Restrictions, Length(Restrictions)+1);
Restrictions[High(Restrictions)].First := DefStack[High(DefStack)].FirstLine;
Restrictions[High(Restrictions)].Last := LCounter-1
End;
UseLine := DefStack[High(DefStack)].Any;
SetLength(DefStack, Length(DefStack)-1)
End
End;
If ApplyConds And Not UseLine Then
Begin
If Assigned(Lines.Objects[LCounter-1]) Then
Lines.Objects[LCounter-1].Free;
Lines.Delete(LCounter-1)
End
Else
Inc(LCounter)
End;
If Length(DefStack) > 0 Then
Begin
WriteLn('#endif expected in line ',LCounter);
Halt(-1)
End;
If ModeGPU <> gpuNone Then
Begin
WriteLn('Unclosed gpu/common section in line ',LCounter);
Halt(-1)
End;
SetLength(DefStack, 0);
L.Free
end;
procedure MakeConsultation(Const FL, GOAL: String);
Var S, L: String;
I: Integer;
Begin
S:=StringReplace(ExcludeTrailingBackSlash(ExtractFilePath(ParamStr(0))),'\','/',[rfReplaceAll]);
(* *)
If Prologer = 0 Then
Begin
Prologer := LoadLibrary({$IF DEFINED(UNIX) OR DEFINED(LINUX)}'./lib' + {$ENDIF}'PrologIntrf.' + SharedSuffix);
If Prologer = NilHandle Then
Prologer := DWORD(-1)
Else
Begin
initer := switch_gprolog7(GetProcedureAddress(Prologer, 'init_gprolog7'));
if Assigned(initer) Then
initer('_.info')
Else
Begin
FreeLibrary(Prologer);
Prologer := DWORD(-1)
End
End
End;
If Prologer <> DWORD(-1) Then
runer := run_gprolog7(GetProcedureAddress(Prologer, 'run_gprolog7'))
Else
runer := Nil;
If Assigned(runer) Then
begin
I := runer(PChar(S), PChar(FL), '_.out', PChar(GOAL), '_.info', 'true', PChar(''''''(*GID*)));
If I <> 5 Then
begin
WriteLn('Error executing gprolog library : "' + BinStr(I,16) + '"');
Halt(-50)
end
end
Else
(* *)
Begin
WriteLn(RunExtCommand(
{$IF DEFINED(UNIX) OR DEFINED(LINUX)}'run_gprolog.sh'{$ELSE}'run_gprolog.bat'{$ENDIF},
S+' '+FL+' _.out '+GOAL+' _.info',
'_.info'));
With TStringList.Create Do
Begin
LoadFromFile(S + {$IF DEFINED(UNIX) OR DEFINED(LINUX)}'/'{$ELSE}'\'{$ENDIF} + aliveDB);
Insert(0, 'phoenix:-');
For I := 1 To Count-1 Do
Begin
L := Trim(Strings[I]);
if Length(L) <> 0 Then
If L[1] <> '%' Then
If L[Length(L)] = '.' Then
Begin
L[Length(L)] := ')';
L := 'assertz(' + L + ','
End;
Strings[I] := L;
End;
Add('true,!.');
SaveToFile(S + {$IF DEFINED(UNIX) OR DEFINED(LINUX)}'/'{$ELSE}'\'{$ENDIF} + phoenixDB)
End
End
End;
procedure PreprocStage1a(Var Restrictions: TRestrictions; PreChecks, IsVectorized, IsClustered: Boolean);
Type SmallINtArray = Array Of SmallInt;
function CountBrackets(Const S: String): Integer;
Var F: Integer;
begin
Result := 0;
F := 1;
While F <= Length(S) Do
Begin
If S[F] = LeftBracket Then
Inc(Result)
Else If S[F] = '\' Then
Inc(F)
Else If S[F] = LeftSqrBracket Then
While (F <= Length(S)) And (S[F] <> RightSqrBracket) Do
Begin
If S[F] = '\' Then
Inc(F);
Inc(F)
End
Else If S[F] = LeftFBracket Then
While (F <= Length(S)) And (S[F] <> RightFBracket) Do
Begin
If S[F] = '\' Then
Inc(F);
Inc(F)
End;
Inc(F)
end
end;
function MakeReplacements(Var Map: SmallIntArray;
Begs, Ends: TList;
Replacers: TStringList; HayStack: WideString): WideString;
Var Start, Stop: Integer;
Replacement: WideString;
LL: TStringList;
LN: TLine;
F, G, K, M: Integer;
Begin
Result := '';
F := 1;
While F < Length(Map) Do
Begin
While (F < Length(Map)) And (Map[F] < 0) Do
Inc(F);
If F = Length(Map) Then Break;
Start := F;
Stop := F+1;
While (Stop < Length(Map)) And (Map[Stop] = Map[Start]) Do
Inc(Stop);
Dec(Stop);
Replacement := Replacers[Map[Start]];
If Replacers.Objects[Map[Start]] <> Nil Then
Result := Result + 'assertz(' + Replacement + '),';
If Start > Length(HayStack) Then
Begin
K := Files.AddObject(Underscore+'END', Nil);
Lines.AddObject(Replacement, TLine.Create(K, 10000, gpuNone));
Break
End;
If Replacers.Objects[Map[Start]] <> Nil Then
Begin
F := Stop + 1;
Continue
End;
K := 0;
While K < Begs.Count Do
Begin
If TObjectToInteger(TObject(Begs[K])) <= TObjectToInteger(TObject(Ends[K])) Then
If (TObjectToInteger(TObject(Begs[K])) <= Start) And (TObjectToInteger(TObject(Ends[K])) >= Start) Then
Break;
Inc(K)
End;
If K <> Begs.Count Then
Begin
LL := TStringList.Create;
LL.Text := Replacement;
G := Files.AddObject(Underscore+'PATTERN['+IntToStr(Map[Start])+']',LL);
If TObjectToInteger(TObject(Ends[K])) > Stop Then
Begin
Lines.InsertObject(K+1, UnescapeString(Copy(HayStack, Stop+1, TObjectToInteger(TObject(Ends[K]))-Stop)), TLine.Create(G, 1, TLine(Lines.Objects[K])._ModeGPU));
Begs.Insert(K+1, IntegerToTObject(Stop+1));
Ends.Insert(K+1, Ends[K])
End;
Ends[K] := IntegerToTObject(Start - 1);
If Ends[K] < Begs[K] Then
Begin
Ends.Delete(K);
Begs.Delete(K);
LN := TLine(Lines.Objects[K]);
Lines.Delete(K);
LN.Free
End
Else
Begin
Lines[K] := UTF8Encode(UnescapeString(Copy(HayStack, TObjectToInteger(TObject(Begs[K])), Start-TObjectToInteger(TObject(Begs[K])))));
LN := TLine(Lines.Objects[K]);
Lines.Objects[K] := TLine.Derive(LN, G, 1);
LN.Free;
Inc(K)
End;
// K = position of insertion of replacement
While (K < Begs.Count) And (TObjectToInteger(TObject(Ends[K])) <= Stop) Do
Begin
Ends.Delete(K);
Begs.Delete(K);
LN := TLine(Lines.Objects[K]);
Lines.Delete(K);
LN.Free
End;
If K < Begs.Count Then
Begin
Lines[K] := UTF8Encode(UnescapeString(Copy(HayStack, Stop+1, TObjectToInteger(TObject(Ends[K]))-Stop)));
LN := TLine(Lines.Objects[K]);
Lines.Objects[K] := TLine.Derive(LN, G, 1000);
LN.Free;
Begs[K] := IntegerToTObject(Start + Length(Replacement));
Ends[K] := IntegerToTObject(TObjectToInteger(TObject(Begs[K])) + (TObjectToInteger(TObject(Ends[K]))-Stop) - 1);
For M := K+1 To Begs.Count-1 Do
Begin
Begs[M] := IntegerToTObject(TObjectToInteger(TObject(Begs[M])) - (Stop - Start + 1) + Length(Replacement));
Ends[M] := IntegerToTObject(TObjectToInteger(TObject(Ends[M])) - (Stop - Start + 1) + Length(Replacement))
End
End;
If Length(Replacement) > 0 Then
Begin
If K < Lines.Count Then
Lines.InsertObject(K, Replacement, TLine.Create(G, 1, TLine(Lines.Objects[K])._ModeGPU))
Else If Lines.Count > 0 Then
Lines.InsertObject(K, Replacement, TLine.Create(G, 1, TLine(Lines.Objects[Lines.Count-1])._ModeGPU))
Else
Lines.AddObject(Replacement, TLine.Create(G, 1, gpuNone));
Begs.Insert(K, IntegerToTObject(Start));
Ends.Insert(K, IntegerToTObject(Start + Length(Replacement) - 1))
End;
System.Delete(HayStack, Start, Stop-Start+1);
Move(Map[Stop+1], Map[Start], (Length(Map)-(Stop+1))*SizeOf(Map[0]));
System.Insert(Replacement, HayStack, Start);
SetLength(Map, Length(Map) - (Stop-Start+1) + Length(Replacement));
Move(Map[Start], Map[Start+Length(Replacement)], (Length(Map)-(Start+Length(Replacement)))*SizeOf(Map[0]));
F := Start + Length(Replacement)
End
End
end;
procedure GetScheme(M: PatternModes; L: TAnalyser; Const AddMode: Boolean = False);
Begin
L.GetIdent(True);
L.Check(LeftBracket);
If Not AddMode Then PatternsOrder.Clear;
PatternsMode := M;
While Not (L.Error Or L.IsNext(RightBracket)) Do
Begin
PatternsOrder.Add(L.GetIdent(False));
If L.IsNext(Comma) Then
L.Check(Comma)
Else If Not L.IsNext(RightBracket) Then
L.Expect(RightBracket);
End;
L.Check(RightBracket);
TLine(Lines.Objects[LCounter-1]).Restricted := True
End;
function ParseComplexParam(Const S: String): TObjStrList;
Var L: TAnalyser;
S1: String;
Flag: Boolean;
Begin
L:=TAnalyser.Create(IdentSet,[Space,Tabulation], True);
L.AnlzLine := S;
If L.IsNext(LeftSqrBracket) Then
Begin
L.Check(LeftSqrBracket);
Result := TObjStrList.Create;
Flag := False;
While Not (Flag Or L.Error Or L.Empty) Do
Begin
S1 := L.GetBalancedListItem(False, [Comma, RightSqrBracket]);
If Length(S1) = 0 Then
Begin
WriteLn('Incorrect parameters list',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End;
Result.AddObject(S1, ParseComplexParam(S1));
If L.IsNext(Comma) Then
L.Check(Comma)
Else If L.IsNext(RightSqrBracket) Then
Flag := L.Check(RightSqrBracket)
End;
If L.Error Then
Begin
WriteLn('Incorrect parameters list',TLine(Lines.Objects[LCounter-1]).GetDescription);
Halt(-1)
End