-
Notifications
You must be signed in to change notification settings - Fork 1
/
pascal.pas
2084 lines (1954 loc) · 78.2 KB
/
pascal.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
type
set_of_string = set of string;
node = class
private
_nodes: list<node>;
_value: string;
public
property Nodes[x: integer]: node
read _nodes[x];default;
property Value: string
read _value;
property Count: integer
read _nodes.Count;
constructor(v: string);
begin
_value := v;
_nodes := new List<node>();
end;
function AddNode(v: string): node;
begin
var t := new node(v);
_nodes.Add(t);
Result := t;
end;
function AddNode(n: node): node;
begin
_nodes.Add(n);
Result := n;
end;
end;
Lexem = auto class
start_index: integer;
value: string;
end;
procedure PrettyPrint(n: node; level: integer; pre_write: string);
const
width = 3;
begin
writeln(n.Value);
for var i := 0 to n.Count - 2 do
begin
write(pre_write + '├' + '─' * (width - 1));
PrettyPrint(n[i], level + 1, pre_write + '│' + ' ' * (width - 1));
end;
if n.Count > 0 then
begin
write(pre_write + '└' + '─' * (width - 1));
PrettyPrint(n[n.Count - 1], level + 1, pre_write + ' ' * width);
end;
end;
procedure PrintLexemes(lexemes: List<lexem>);
begin
for var i := 0 to lexemes.Count - 1 do
println($'[{i}] {lexemes[i].start_index}: "{lexemes[i].value}" ({lexemes[i].GetType.Name})');
end;
type
LPascalIdentifier = class(Lexem);
LPascalNumber = class(Lexem);
LPascalNInteger = class(LPascalNumber);
LPascalNReal = class(LPascalNumber);
LPascalString = class(Lexem);
LPascalReserved = class(Lexem);
LPascalSymbol = class(Lexem);
Relation_ = (Equal, NotEqual, InSet);
function SafeCharIn(str: string; cur_pos: integer; &set: set of char): boolean;
begin
if cur_pos > str.Length then
Result := false
else
Result := str[cur_pos] in &set;
end;
function SafeCharComp(str: string; cur_pos: integer; r: Relation_; c: char): boolean;
begin
if cur_pos > str.Length then
Result := false
else
begin
if r = Relation_.Equal then
Result := str[cur_pos] = c
else
if r = Relation_.NotEqual then
Result := str[cur_pos] <> c;
end;
end;
function SafeLastLexemValueComp(lexemes: List<Lexem>; r: Relation_; val: string): boolean;
begin
var Count := lexemes.Count;
if Count = 0 then
Result := false
else
begin
if r = Relation_.Equal then
Result := lexemes[Count - 1].value = val
else
if r = Relation_.NotEqual then
Result := lexemes[Count - 1].value <> val;
end;
end;
function SafeLexemComp(lexemes: List<Lexem>; cur_pos: integer; r: Relation_; val: object): boolean;
begin
if cur_pos >= lexemes.Count then
Result := false
else
begin
if r = Relation_.Equal then
Result := lexemes[cur_pos].value = val.ToString
else
if r = Relation_.NotEqual then
Result := lexemes[cur_pos].value <> val.ToString
else
if r = Relation_.InSet then
Result := lexemes[cur_pos].value in set_of_string(val);
end;
end;
type
PascalLexicalAnalyzer = class
static letters := ['a'..'z', 'A'..'Z', '_'];
static digits := ['0'..'9'];
static simblos := [',', '.', ';', ':', '^', '(', ')', '[', ']', '<', '>', '=', '+', '-', '*', '/', '$', '@', '&', '#'];
static reserved_words := ['and', 'downto', 'if', 'or', 'then', 'array', 'else', 'in', 'packed', 'to', 'begin', 'end', 'label', 'procedure', 'type', 'case', 'file', 'mod', 'program', 'until', 'const', 'for', 'nil', 'record', 'var', 'div', 'function', 'not', 'repeat', 'while', 'do', 'goto', 'of', 'set', 'with', 'class', 'private', 'public', 'protected', 'internal', 'constructor', 'destructor', 'property', 'auto', 'sealed', 'abstract', 'forward', 'extensionmethod', 'default', 'try', 'except', 'finally', 'on', 'operator', 'static', 'new'];
static function Analyze(str: string; var lexemes: List<lexem>): boolean;
begin
Result := true;
var cur_index := 1;
while cur_index <= str.Length do
begin
var l: lexem;
var next_index: integer;
var isNotFind := true;
var cur_char := str[cur_index];
//var cur_ord := ord(cur_char);
while (cur_char = #9) or (cur_char = #10) or (cur_char = #13) or (cur_char = ' ') do
begin
cur_index += 1;
cur_char := str[cur_index];
end;
if (cur_char = '(') and (str[cur_index + 1] = '*') then
begin
cur_index := Pos('*)', str, cur_index + 2) + 2;
continue;
end;
if (cur_char = '{') then
begin
cur_index := Pos('}', str, cur_index) + 1;
continue;
end;
if (cur_char = '/') and (str[cur_index + 1] = '/') then
begin
cur_index := Pos(#10, str, cur_index) + 1;
continue;
end;
if Symbol(str, cur_index, next_index) then
begin
l := new LPascalSymbol();
isNotFind := false;
end;
if isNotFind and Reserved(str, cur_index, next_index) then
begin
if SafeLastLexemValueComp(lexemes, Relation_.Equal, '&') then
begin
lexemes.RemoveAt(lexemes.Count - 1);
l := new LPascalIdentifier();
AddNewPascalLexem(lexemes, str, l, cur_index - 1, next_index);
cur_index := next_index;
continue;
end else
l := new LPascalReserved();
isNotFind := false;
end;
if isNotFind and Identifier(str, cur_index, next_index) then
begin
if SafeLastLexemValueComp(lexemes, Relation_.Equal, '&') then
begin
lexemes.RemoveAt(lexemes.Count - 1);
l := new LPascalIdentifier();
AddNewPascalLexem(lexemes, str, l, cur_index - 1, next_index);
cur_index := next_index;
continue;
end else
l := new LPascalIdentifier();
isNotFind := false;
end;
if isNotFind and RealNumber(str, cur_index, next_index) then
begin
l := new LPascalNReal();
isNotFind := false;
end;
if isNotFind and IntegerNumber(str, cur_index, next_index) then
begin
if SafeLastLexemValueComp(lexemes, Relation_.Equal, '#') then
begin
lexemes.RemoveAt(lexemes.Count - 1);
l := new LPascalString();
AddNewPascalLexem(lexemes, str, l, cur_index - 1, next_index);
cur_index := next_index;
continue;
end else
l := new LPascalNInteger();
isNotFind := false;
end;
if isNotFind and StringValue(str, cur_index, next_index) then
begin
l := new LPascalString();
isNotFind := false;
end;
if isNotFind then
begin
writeln();
writeln($'Error: [{cur_index}] {str.Substring(cur_index-1, min(20, str.Length))} ...');
Result := false;
exit;
end
else
begin
AddNewPascalLexem(lexemes, str, l, cur_index, next_index);
cur_index := next_index;
end;
end;
end;
static procedure AddNewPascalLexem(lexemes: List<lexem>; str: string; l: lexem; cur_pos: integer; next_pos: integer);
begin
l.start_index := cur_pos;
l.value := str.Substring(cur_pos - 1, next_pos - cur_pos);
if not (l is LPascalString) then
l.value := l.value.ToLower();
lexemes.Add(l);
end;
/// Letter = "A" | "B" | "C" | "D" | "E" | "F" | "G"
/// | "H" | "I" | "J" | "K" | "L" | "M" | "N"
/// | "O" | "P" | "Q" | "R" | "S" | "T" | "U"
/// | "V" | "W" | "X" | "Y" | "Z" | "a" | "b"
/// | "c" | "d" | "e" | "f" | "g" | "h" | "i"
/// | "j" | "k" | "l" | "m" | "n" | "o" | "p"
/// | "q" | "r" | "s" | "t" | "u" | "v" | "w"
/// | "x" | "y" | "z" | "_" ;
static function Letter(str: string; cur_pos: integer): boolean;
begin
Result := SafeCharIn(str, cur_pos, letters);
end;
/// Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
static function Digit(str: string; cur_pos: integer): boolean;
begin
Result := SafeCharIn(str, cur_pos, digits);
end;
/// Identifier = letter , { letter | digit } ;
static function Identifier(str: string; cur_pos: integer; var next_pos: integer): boolean;
begin
if Letter(str, cur_pos) then
begin
Result := true;
next_pos := cur_pos + 1;
while(Letter(str, next_pos) or Digit(str, next_pos)) do
next_pos += 1;
end
else Result := false;
end;
/// IntegerNumber = digit, { digit } ;
static function IntegerNumber(str: string; cur_pos: integer; var next_pos: integer): boolean;
begin
if Digit(str, cur_pos) then
begin
Result := true;
next_pos := cur_pos + 1;
while(Digit(str, next_pos)) do
next_pos += 1;
end
else Result := false;
end;
/// ExpDesignator = "e" | "E" ;
static function ExpDesignator(str: string; cur_pos: integer): boolean;
begin
var str_cur_pos := str[cur_pos];
Result := (str_cur_pos = 'e') or (str_cur_pos = 'E');
end;
/// Exponent = ExpDesignator, [ "+" | "-" ], IntegerNumber ;
static function Exponent(str: string; cur_pos: integer; var next_pos: integer): boolean;
begin
if ExpDesignator(str, cur_pos) then
begin
var t := cur_pos + 1;
var _chr := str[next_pos];
if (_chr = '+') or (_chr = '-') then
t += 1;
Result := IntegerNumber(str, t, next_pos);
exit;
end;
Result := false;
end;
/// RealNumber = IntegerNumber, ".", IntegerNumber, Exponent ;
static function RealNumber(str: string; cur_pos: integer; var next_pos: integer): boolean;
begin
if IntegerNumber(str, cur_pos, next_pos) then
if SafeCharComp(str, next_pos, Relation_.Equal, '.') then
if IntegerNumber(str, next_pos + 1, next_pos) then
begin
Result := true;
Exponent(str, next_pos, next_pos);
exit;
end;
Result := false;
end;
/// Number = RealNumber | IntegerNumber ;
static function Number(str: string; cur_pos: integer; var next_pos: integer): boolean;
begin
Result := RealNumber(str, cur_pos, next_pos) or IntegerNumber(str, cur_pos, next_pos);
end;
/// StringValue = '"', { all_characters - '"' }, '"'
/// | "'", { all_characters - ''' }, "'" ;
static function StringValue(str: string; cur_pos: integer; var next_pos: integer): boolean;
begin
var ch := #39; //'
if (str[cur_pos] <> ch) then
ch := #34; //"
if (str[cur_pos] = ch) then
begin
next_pos := str.IndexOf(ch, cur_pos) + 2;
if next_pos <> 1 then
begin
Result := true;
exit;
end;
end;
Result := false;
end;
/// Reserved = "and" | "downto" | "if" | "or" | "then" | "array" | "else"
/// | "in" | "packed" | "to" | "begin" | "end" | "label" | "procedure"
/// | "type" | "case" | "file" | "mod" | "program" | "until" | "const"
/// | "for" | "nil" | "record" | "var" | "div" | "function" | "not"
/// | "repeat" | "while" | "do" | "goto" | "of" | "set" | "with"
/// | "class" | "private" | "public" | "protected" | "internal"
/// | "constructor" | "destructor" | "property" | "auto" | "sealed"
/// | "abstract" | "forward" | "extensionmethod" | "default" | "try"
/// | "except" | "finally" | "on" | "operator" | "static" ;
static function Reserved(str: string; cur_pos: integer; var next_pos: integer): boolean;
begin
Result := false;
if Identifier(str, cur_pos, next_pos) then
begin
Result := str.Substring(cur_pos - 1, next_pos - cur_pos).ToLower() in reserved_words;
end;
end;
/// Symbol = ":=" | "+=" | "-=" | "*=" | "/=" | "<=" | "<>" | ">=" | ".."
/// | "," | "." | ";" | ":" | "^" | "(" | ")" | "[" | "]" | "<"
/// | ">" | "=" | "+" | "-" | "*" | "/" | "$" | "@" | "&" | "#" ;
static function Symbol(str: string; cur_pos: integer; var next_pos: integer): boolean;
begin
var cur_char := str[cur_pos];
Result := cur_char in simblos;
if Result then
begin
next_pos := cur_pos + 1;
if cur_char = ':' then
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '=') then
begin
next_pos += 1;
exit;
end;
if cur_char = '+' then
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '=') then
begin
next_pos += 1;
exit;
end;
if cur_char = '-' then
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '=') then
begin
next_pos += 1;
exit;
end;
if cur_char = '*' then
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '=') then
begin
next_pos += 1;
exit;
end;
if cur_char = '/' then
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '=') then
begin
next_pos += 1;
exit;
end;
if cur_char = '<' then
begin
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '=') then
begin
next_pos += 1;
exit;
end;
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '>') then
begin
next_pos += 1;
exit;
end;
end;
if cur_char = '>' then
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '=') then
begin
next_pos += 1;
exit;
end;
if cur_char = '.' then
if SafeCharComp(str, cur_pos + 1, Relation_.Equal, '.') then
begin
next_pos += 1;
exit;
end;
end;
end;
end;
PascalGrammarAnalyzer = class
static Unary_Operator := ['+', '-', 'not', '@', '^'];
static Mult_Operator := ['*', '/', 'div', 'mod', 'and', 'shl', 'shr'];
static Add_Operator := ['+', '-', 'or', 'xor'];
static _Relation_ := ['=', '<>', '<', '>', '<=', '>=', 'in', 'is'];
static Assign_Operator := [':=', '+=', '-=', '*=', '/='];
/// OperatorForOverloading = "+" | "-" | "not" | "@" | "^" | "*" | "/" | "div" | "mod" | "and" | "shl" | "shr"
/// | "or" | "xor" | "=" | "<>" | "<" | ">" | "<=" | ">=" | "in" | "implicit" | "explicit" ;
static OperatorForOverloading := Unary_Operator + Mult_Operator + Add_Operator + _Relation_ - ['is'] + ['implicit', 'explicit'];
static class_prefix := ['auto', 'sealed', 'abstract'];
static function AssociativeToLeft(n: node): node;
begin
Result := new node(n.value);
if n.Count >= 3 then
begin
Result.AddNode(n[0]);
Result.AddNode(n[1]);
Result.AddNode(n[2]);
for var i := 1 to ((n.Count - 1) div 2) - 1 do
begin
var t := new node(n.value);
t.AddNode(Result);
Result := t;
Result.AddNode(n[i * 2 + 1]);
Result.AddNode(n[(i + 1) * 2]);
end;
end else
Result.AddNode(n[0]);
end;
/// UnaryOperator = "+" | "-" | "not" | "@" | "^" ;
static function UnaryOperator(lexemes: List<lexem>; cur_pos: integer; upper_command: node): boolean;
begin
Result := SafeLexemComp(lexemes, cur_pos, Relation_.InSet, Unary_Operator);
if Result then
upper_command.AddNode('UnaryOperator').AddNode($' {lexemes[cur_pos].value}');
end;
/// MultOperator = "*" | "/" | "div" | "mod" | "and" | "shl" | "shr" ;
static function MultOperator(lexemes: List<lexem>; cur_pos: integer; upper_command: node): boolean;
begin
Result := SafeLexemComp(lexemes, cur_pos, Relation_.InSet, Mult_Operator);
if Result then
upper_command.AddNode('MultOperator').AddNode($' {lexemes[cur_pos].value}');
end;
/// AddOperator = "+" | "-" | "or" | "xor" ;
static function AddOperator(lexemes: List<lexem>; cur_pos: integer; upper_command: node): boolean;
begin
Result := SafeLexemComp(lexemes, cur_pos, Relation_.InSet, Add_Operator);
if Result then
upper_command.AddNode('AddOperator').AddNode($' {lexemes[cur_pos].value}');
end;
/// Relation = "=" | "<>" | "<" | ">" | "<=" | ">=" | "in" | "is" ;
static function Relation(lexemes: List<lexem>; cur_pos: integer; upper_command: node): boolean;
begin
Result := SafeLexemComp(lexemes, cur_pos, Relation_.InSet, _Relation_);
if Result then
upper_command.AddNode('Relation').AddNode($' {lexemes[cur_pos].value}');
end;
/// AssignmentOperator = ":=" | "+=" | "-=" | "*=" | "/=" ;
static function AssignmentOperator(lexemes: List<lexem>; cur_pos: integer; upper_command: node): boolean;
begin
Result := SafeLexemComp(lexemes, cur_pos, Relation_.InSet, Assign_Operator);
if Result then
upper_command.AddNode('AssignmentOperator').AddNode($' {lexemes[cur_pos].value}');
end;
/// OneFormalParam = [ "var" ], IdentList, ":", Type ;
static function OneFormalParam(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('OneFormalParam');
if (lexemes[cur_pos].value = 'var') then
begin
cur_pos += 1;
cur_command.AddNode('var');
end;
Result :=
IdentList(lexemes, cur_pos, next_pos, cur_command) and
(lexemes[next_pos].value = ':') and
&Type(lexemes, next_pos + 1, next_pos, cur_command);
if Result then
upper_command.AddNode(cur_command);
end;
/// FormalParameters = "(", OneFormalParam, { ";", OneFormalParam }, ")" ;
static function FormalParameters(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('FormalParameters');
Result :=
(lexemes[cur_pos].value = '(') and
OneFormalParam(lexemes, cur_pos + 1, next_pos, cur_command);
if Result then
begin
repeat
cur_pos := next_pos;
until not ((lexemes[cur_pos].value = ';') and OneFormalParam(lexemes, cur_pos + 1, next_pos, cur_command));
Result := (lexemes[cur_pos].value = ')');
if Result then
begin
next_pos := cur_pos + 1;
upper_command.AddNode(cur_command);
end;
end;
end;
/// FunctionHeading = [ "static" ], "function", ( Designator | ( "operator", OperatorForOverloading ) ), [ FormalParameters ] ;
static function FunctionHeading(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('FunctionHeading');
Result := false;
if (lexemes[cur_pos].value = 'static') then
begin
cur_command.AddNode('static');
cur_pos += 1;
end;
if (lexemes[cur_pos].value = 'function') then
begin
if Designator(lexemes, cur_pos + 1, next_pos, cur_command) then
begin
Result := true;
cur_pos := next_pos;
end else
if (lexemes[cur_pos + 1].value = 'operator') and
(lexemes[cur_pos + 2].value in OperatorForOverloading) then
begin
Result := true;
cur_command.AddNode('operator').AddNode($' {lexemes[cur_pos + 2].value}');
cur_pos += 3;
end;
end;
if Result then
begin
if not FormalParameters(lexemes, cur_pos, next_pos, cur_command) then
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// ProcedureHeading = [ "static" ], "procedure", ( Designator | ( "operator", AssignmentOperator ) ), [ FormalParameters ] ;
static function ProcedureHeading(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('ProcedureHeading');
if (lexemes[cur_pos].value = 'static') then
begin
cur_command.AddNode('static');
cur_pos += 1;
end;
Result := false;
if (lexemes[cur_pos].value = 'procedure') then
begin
if Designator(lexemes, cur_pos + 1, next_pos, cur_command) then
begin
Result := true;
cur_pos := next_pos;
end else
if (lexemes[cur_pos + 1].value = 'operator') and
(lexemes[cur_pos + 2].value in Assign_Operator) then
begin
Result := true;
cur_command.AddNode('operator').AddNode($' {lexemes[cur_pos + 2].value}');
cur_pos += 3;
end;
end;
if Result then
begin
if not FormalParameters(lexemes, cur_pos, next_pos, cur_command) then
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// FunctionDecl = FunctionHeading, ":", Type, ";" ( "forward" | ( [ "extensionmethod", ";" ], Block ) ) ;
static function FunctionDecl(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('FunctionDecl');
Result := FunctionHeading(lexemes, cur_pos, next_pos, cur_command) and
(lexemes[next_pos].value = ':') and
&Type(lexemes, next_pos + 1, next_pos, cur_command);
if Result then
begin
cur_pos := next_pos;
Result := (lexemes[cur_pos].value = ';') and (lexemes[cur_pos + 1].value = 'forward');
if Result then
begin
next_pos := cur_pos + 2;
cur_command.AddNode(lexemes[cur_pos + 1].value);
end
else
begin
if (lexemes[cur_pos].value = ';') and (lexemes[cur_pos + 1].value = 'extensionmethod') then
begin
cur_command.AddNode('extensionmethod');
cur_pos += 2;
end;
Result := (lexemes[cur_pos].value = ';') and Block(lexemes, cur_pos + 1, next_pos, cur_command);
end;
if Result then
upper_command.AddNode(cur_command);
end;
end;
/// ProcedureDecl = ProcedureHeading, ";", ( "forward" | ( [ "extensionmethod", ";" ], Block ) ) ;
static function ProcedureDecl(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('ProcedureDecl');
Result := ProcedureHeading(lexemes, cur_pos, next_pos, cur_command);
if Result then
begin
cur_pos := next_pos;
Result := (lexemes[cur_pos].value = ';') and (lexemes[cur_pos + 1].value = 'forward');
if Result then
begin
next_pos := cur_pos + 2;
cur_command.AddNode(lexemes[cur_pos + 1].value);
end
else
begin
if (lexemes[cur_pos].value = ';') and (lexemes[cur_pos + 1].value = 'extensionmethod') then
begin
cur_command.AddNode('extensionmethod');
cur_pos += 2;
end;
Result := (lexemes[cur_pos].value = ';') and Block(lexemes, cur_pos + 1, next_pos, cur_command);
end;
if Result then
upper_command.AddNode(cur_command);
end;
end;
/// SubprogDeclList = ( ProcedureDecl | FunctionDecl ), ";", { ( ProcedureDecl | FunctionDecl ), ";" } ;
static function SubprogDeclList(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('SubprogDeclList');
Result := false;
while (ProcedureDecl(lexemes, cur_pos, next_pos, cur_command) or
FunctionDecl(lexemes, cur_pos, next_pos, cur_command))
and
(lexemes[next_pos].value = ';') do
begin
Result := true;
next_pos += 1;
cur_pos := next_pos;
end;
if Result then
upper_command.AddNode(cur_command);
end;
/// Element = Expression, [ "..", Expression ] ;
static function Element(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('Element');
Result := Expression(lexemes, cur_pos, next_pos, cur_command);
if Result then
begin
cur_pos := next_pos;
if not ((lexemes[cur_pos].value = '..') and Expression(lexemes, cur_pos + 1, next_pos, cur_command)) then
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// FunctionCall = Designator, ActualParameters
/// | "new", Type, [ ActualParameters ] ;
static function FunctionCall(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('FunctionCall');
Result :=
(lexemes[cur_pos].value = 'new') and
&Type(lexemes, cur_pos + 1, next_pos, cur_command);
if Result then
begin
cur_pos := next_pos;
if not ActualParameters(lexemes, next_pos, next_pos, cur_command) then
next_pos := cur_pos;
end else
Result :=
Designator(lexemes, cur_pos, next_pos, cur_command) and
ActualParameters(lexemes, next_pos, next_pos, cur_command);
if Result then
upper_command.AddNode(cur_command);
end;
/// SetValue = "[", [ Element, { ",", Element } ], "]" ;
static function SetValue(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('SetValue');
Result := (lexemes[cur_pos].value = '[');
if Result then
begin
cur_pos += 1;
if Element(lexemes, cur_pos, next_pos, cur_command) then
begin
repeat
cur_pos := next_pos;
until not ((lexemes[cur_pos].value = ',') and Element(lexemes, cur_pos + 1, next_pos, cur_command));
next_pos := cur_pos;
end;
Result := (lexemes[cur_pos].value = ']');
if Result then
begin
next_pos := cur_pos + 1;
upper_command.AddNode(cur_command);
end;
end;
end;
/// Factor = ( FunctionCall | SetValue | Number | StringValue | "$", StringValue | "nil" | Identifier | "(", Expression, ")" ), [ DesignatorStuff ] ;
static function Factor(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('Factor');
Result := FunctionCall(lexemes, cur_pos, next_pos, cur_command) or
SetValue(lexemes, cur_pos, next_pos, cur_command);
if not Result then
begin
Result := (lexemes[cur_pos] is LPascalNumber) or
(lexemes[cur_pos] is LPascalString) or
(lexemes[cur_pos].value = 'nil') or
(lexemes[cur_pos] is LPascalIdentifier);
if Result then
begin
next_pos := cur_pos + 1;
cur_command.AddNode(lexemes[cur_pos].value)
end else
begin
Result :=
(lexemes[cur_pos].value = '$') and
(lexemes[cur_pos + 1] is LPascalString);
if Result then
begin
cur_command.AddNode('$' + lexemes[cur_pos + 1].value);
next_pos := cur_pos + 2;
end
else
begin
Result := (lexemes[cur_pos].value = '(') and
Expression(lexemes, cur_pos + 1, next_pos, cur_command) and
(lexemes[next_pos].value = ')');
if Result then
next_pos := next_pos + 1;
end;
end;
end;
if Result then
begin
cur_pos := next_pos;
if not DesignatorStuff(lexemes, cur_pos, next_pos, cur_command) then
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// Term = Factor
/// | Factor, MultOperator, Factor
/// | Term, MultOperator, Factor ;
static function Term(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('Term');
Result := Factor(lexemes, cur_pos, next_pos, cur_command);
if Result then
begin
repeat
cur_pos := next_pos;
until not (MultOperator(lexemes, cur_pos, cur_command) and Factor(lexemes, cur_pos + 1, next_pos, cur_command));
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// SimpleExpression = [ UnaryOperator ], Term
/// | [ UnaryOperator ], Term, AddOperator, Term
/// | SimpleExpression, AddOperator, Term ;
static function SimpleExpression(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('SimpleExpression');
if UnaryOperator(lexemes, cur_pos, cur_command) then
cur_pos += 1;
var term_expr := new node('');
Result := Term(lexemes, cur_pos, next_pos, term_expr);
if Result then
begin
repeat
cur_command.AddNode(AssociativeToLeft(term_expr[0]));
cur_pos := next_pos;
term_expr := new node('');
until not (AddOperator(lexemes, cur_pos, cur_command) and Term(lexemes, cur_pos + 1, next_pos, term_expr));
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// Expression = SimpleExpression, [ Relation, SimpleExpression ] ;
static function Expression(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('Expression');
var simp_expr := new node('');
Result := SimpleExpression(lexemes, cur_pos, next_pos, simp_expr);
if Result then
begin
cur_command.AddNode(AssociativeToLeft(simp_expr[0]));
cur_pos := next_pos;
simp_expr := new node('');
if not (Relation(lexemes, cur_pos, cur_command) and SimpleExpression(lexemes, cur_pos + 1, next_pos, simp_expr)) then
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// MemoryStatement = "new", "(", Identifier, ")"
/// | "dispose", "(", Identifier, ")" ;
static function MemoryStatement(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('MemoryStatement');
Result := ((lexemes[cur_pos].value = 'new') or
(lexemes[cur_pos].value = 'dispose'))
and
(lexemes[cur_pos + 1].value = '(') and
(lexemes[cur_pos + 2] is LPascalIdentifier) and
(lexemes[cur_pos + 3].value = ')');
if Result then
begin
cur_command.AddNode(lexemes[cur_pos].value);
cur_command.AddNode(lexemes[cur_pos + 2].value);
upper_command.AddNode(cur_command);
next_pos := cur_pos + 4;
end;
end;
/// ExpList = Expression, { ",", Expression } ;
static function ExpList(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('ExpList');
Result := Expression(lexemes, cur_pos, next_pos, cur_command);
if Result then
begin
repeat
cur_pos := next_pos;
until not ((lexemes[cur_pos].value = ',') and Expression(lexemes, cur_pos + 1, next_pos, cur_command));
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// ActualParameters = "(", [ ExpList ], ")" ;
static function ActualParameters(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('ActualParameters');
Result := (lexemes[cur_pos].value = '(');
if Result then
begin
cur_pos += 1;
if ExpList(lexemes, cur_pos, next_pos, cur_command) then
cur_pos := next_pos;
Result := SafeLexemComp(lexemes, cur_pos, Relation_.Equal, ')');
if Result then
begin
next_pos := cur_pos + 1;
upper_command.AddNode(cur_command);
end;
end;
end;
/// DesignatorStuff = ( ".", Identifier, [ ActualParameters ] | "[", ExpList, "]" | "^" ), [ DesignatorStuff ] ;
static function DesignatorStuff(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('DesignatorStuff');
Result := false;
if SafeLexemComp(lexemes, cur_pos, Relation_.Equal, '.') and
(lexemes[cur_pos + 1] is LPascalIdentifier) then
begin
cur_command.AddNode(lexemes[cur_pos + 1].value);
next_pos := cur_pos + 2;
cur_pos := next_pos;
if ActualParameters(lexemes, cur_pos, next_pos, cur_command) then
cur_pos := next_pos;
Result := true;
end;
if not Result and SafeLexemComp(lexemes, cur_pos, Relation_.Equal, '[') and
ExpList(lexemes, cur_pos + 1, next_pos, cur_command) and
(lexemes[next_pos].value = ']') then
begin
next_pos += 1;
cur_pos := next_pos;
Result := true;
end;
if not Result and SafeLexemComp(lexemes, cur_pos, Relation_.Equal, '^') then
begin
cur_command.AddNode(lexemes[cur_pos].value);
next_pos := cur_pos + 1;
cur_pos := next_pos;
Result := true;
end;
if Result then
begin
DesignatorStuff(lexemes, cur_pos, next_pos, cur_command);
upper_command.AddNode(cur_command);
end;
end;
/// Designator = Identifier, [ DesignatorStuff ] ;
static function Designator(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('Designator');
Result := (lexemes[cur_pos] is LPascalIdentifier);
if Result then
begin
cur_command.AddNode(lexemes[cur_pos].value);
if not DesignatorStuff(lexemes, cur_pos + 1, next_pos, cur_command) then
next_pos := cur_pos + 1;
upper_command.AddNode(cur_command);
end;
end;
/// DesignatorList = Designator, { "," Designator } ;
static function DesignatorList(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
var cur_command := new node('DesignatorList');
Result := Designator(lexemes, cur_pos, next_pos, cur_command);
if Result then
begin
repeat
cur_pos := next_pos;
until not ((lexemes[cur_pos].value = ',') and Designator(lexemes, cur_pos + 1, next_pos, cur_command));
next_pos := cur_pos;
upper_command.AddNode(cur_command);
end;
end;
/// IOStatement = "read", "(", DesignatorList, ")"
/// | "write", "(", ExpList, ")"
/// | "readln", [ "(", DesignatorList, ")" ]
/// | "writeln", [ "(", ExpList, ")" ] ;
static function IOStatement(lexemes: List<lexem>; cur_pos: integer; var next_pos: integer; upper_command: node): boolean;
begin
Result := false;
var cur_command := new node('IOStatement');
if (lexemes[cur_pos].value = 'read') or
(lexemes[cur_pos].value = 'write') or
(lexemes[cur_pos].value = 'readln') or