-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.mpl
1244 lines (1135 loc) · 34.2 KB
/
parser.mpl
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
# Copyright (C) 2021 Matway Burkow
#
# This repository and all its contents belong to Matway Burkow (referred here and below as "the owner").
# The content is for demonstration purposes only.
# It is forbidden to use the content or any part of it for any purpose without explicit permission from the owner.
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.
"Array" use
"String" use
"algorithm" use
"ascii" use
"control" use
"astNodeType" use
codepointHex?: [
codepoint:;
codepoint 48 57 between [codepoint 65 70 between] ||
];
codepointHexValue: [
codepoint:;
codepoint codepoint 65 < [48] [55] if -
];
codeunitHead?: [
codeunit:;
codeunit 0x80n8 < [codeunit 0xC0n8 0xF8n8 within] ||
];
codeunitTail?: [
codeunit:;
codeunit 0xC0n8 and 0x80n8 =
];
fillPositionInfo: [
astNode:;
lastPosition @astNode.@positionInfo set
currentPosition.offsetStart new @astNode.@positionInfo.!offsetEnd
];
addToMainResult: [
astArray:;
newIndex: mainResult.memory.size;
@astArray @mainResult.@memory.append
newIndex
];
addToLastUnfinished: [
astNode:;
@astNode @unfinishedNodes.last.@nodes.append
];
makeLabelNode: [
children:;
name:;
result: AstNode;
@result fillPositionInfo
(name ":") assembleString @result.@positionInfo.@token set
AstNodeType.Label @result.@data.setTag
branch: AstNodeType.Label @result.@data.get;
@children addToMainResult @branch.@children set
name @branch.@name set
@result
];
makeCodeNode: [
children:;
result: AstNode;
@result fillPositionInfo
"[" toString @result.@positionInfo.@token set
AstNodeType.Code @result.@data.setTag
branch: AstNodeType.Code @result.@data.get;
@children addToMainResult @branch set
@result
];
makeObjectNode: [
children:;
result: AstNode;
@result fillPositionInfo
"{" toString @result.@positionInfo.@token set
AstNodeType.Object @result.@data.setTag
branch: AstNodeType.Object @result.@data.get;
@children addToMainResult @branch set
@result
];
makeListNode: [
children:;
result: AstNode;
@result fillPositionInfo
"(" toString @result.@positionInfo.@token set
AstNodeType.List @result.@data.setTag
branch: AstNodeType.List @result.@data.get;
@children addToMainResult @branch set
@result
];
makeNameNode: [
name:;
result: AstNode;
@result fillPositionInfo
name toString @result.@positionInfo.@token set
AstNodeType.Name @result.@data.setTag
branch: AstNodeType.Name @result.@data.get;
name toString @branch.@name set
@result
];
makeNameReadNode: [
name:;
result: AstNode;
@result fillPositionInfo
("@" name) assembleString @result.@positionInfo.@token set
AstNodeType.NameRead @result.@data.setTag
branch: AstNodeType.NameRead @result.@data.get;
name toString @branch.@name set
@result
];
makeNameWriteNode: [
name:;
result: AstNode;
@result fillPositionInfo
("!" name) assembleString @result.@positionInfo.@token set
AstNodeType.NameWrite @result.@data.setTag
branch: AstNodeType.NameWrite @result.@data.get;
name toString @branch.@name set
@result
];
makeNameMemberNode: [
name:;
result: AstNode;
@result fillPositionInfo
("." name) assembleString @result.@positionInfo.@token set
AstNodeType.NameMember @result.@data.setTag
branch: AstNodeType.NameMember @result.@data.get;
name toString @branch.@name set
@result
];
makeNameReadMemberNode: [
name:;
result: AstNode;
@result fillPositionInfo
(".@" name) assembleString @result.@positionInfo.@token set
AstNodeType.NameReadMember @result.@data.setTag
branch: AstNodeType.NameReadMember @result.@data.get;
name toString @branch.@name set
@result
];
makeNameWriteMemberNode: [
name:;
result: AstNode;
@result fillPositionInfo
(".!" name) assembleString @result.@positionInfo.@token set
AstNodeType.NameWriteMember @result.@data.setTag
branch: AstNodeType.NameWriteMember @result.@data.get;
name toString @branch.@name set
@result
];
makeStringNode: [
value:;
result: AstNode;
@result fillPositionInfo
"TEXT" toString @result.@positionInfo.@token set
AstNodeType.String @result.@data.setTag
branch: AstNodeType.String @result.@data.get;
value @branch set
@result
];
makeNumberi8Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numberi8 @result.@data.setTag
branch: AstNodeType.Numberi8 @result.@data.get;
number @branch set
@result
];
makeNumberi16Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numberi16 @result.@data.setTag
branch: AstNodeType.Numberi16 @result.@data.get;
number @branch set
@result
];
makeNumberi32Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numberi32 @result.@data.setTag
branch: AstNodeType.Numberi32 @result.@data.get;
number @branch set
@result
];
makeNumberi64Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numberi64 @result.@data.setTag
branch: AstNodeType.Numberi64 @result.@data.get;
number @branch set
@result
];
makeNumberixNode: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numberix @result.@data.setTag
branch: AstNodeType.Numberix @result.@data.get;
number @branch set
@result
];
makeNumbern8Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numbern8 @result.@data.setTag
branch: AstNodeType.Numbern8 @result.@data.get;
number @branch set
@result
];
makeNumbern16Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numbern16 @result.@data.setTag
branch: AstNodeType.Numbern16 @result.@data.get;
number @branch set
@result
];
makeNumbern32Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numbern32 @result.@data.setTag
branch: AstNodeType.Numbern32 @result.@data.get;
number @branch set
@result
];
makeNumbern64Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numbern64 @result.@data.setTag
branch: AstNodeType.Numbern64 @result.@data.get;
number @branch set
@result
];
makeNumbernxNode: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Numbernx @result.@data.setTag
branch: AstNodeType.Numbernx @result.@data.get;
number @branch set
@result
];
makeReal32Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Real32 @result.@data.setTag
branch: AstNodeType.Real32 @result.@data.get;
number @branch set
@result
];
makeReal64Node: [
token:;
number: new;
result: AstNode;
@result fillPositionInfo
token @result.@positionInfo.@token set
AstNodeType.Real64 @result.@data.setTag
branch: AstNodeType.Real64 @result.@data.get;
number @branch set
@result
];
makeParserConstants: [{
eof: [ 0n32];
makeLookupTable: [
av:;
result: Cond Array;
256 @result.resize
@result [p:; FALSE @p set] each
av [
v:;
TRUE v 0 cast @result.at set
] each
result
];
joinLookupTables: [
right:;
left:;
result: Cond Array;
256 @result.resize
result.size [
i left.at i right.at or i @result.at set
] times
result
];
starters: (ascii.openRBr ascii.openSBr ascii.openFBr) makeLookupTable;
terminators: (ascii.null ascii.closeRBr ascii.closeSBr ascii.closeFBr ascii.semicolon) makeLookupTable;
digits: (ascii.zero ascii.one ascii.two ascii.three ascii.four ascii.five ascii.six ascii.seven ascii.eight ascii.nine) makeLookupTable;
numberSigns: (ascii.plus ascii.minus) makeLookupTable;
specials: (ascii.space ascii.cr ascii.lf ascii.colon ascii.grid) makeLookupTable starters joinLookupTables terminators joinLookupTables;
begExp: (ascii.eCode ascii.eCodeBig) makeLookupTable;
endNumbers: specials (ascii.comma) makeLookupTable joinLookupTables;
endNames: specials new;
hexDigits: (
ascii.zero ascii.one ascii.two ascii.three ascii.four ascii.five ascii.six ascii.seven ascii.eight ascii.nine
ascii.aCode ascii.bCode ascii.cCode ascii.dCode ascii.eCode ascii.fCode
ascii.aCodeBig ascii.bCodeBig ascii.cCodeBig ascii.dCodeBig ascii.eCodeBig ascii.fCodeBig
) makeLookupTable;
hexToInt: [
result: Nat32 Array;
256 @result.resize
10 [i 0n32 cast ascii.zero 0 cast i + @result.at set] times
6 [i 10 + 0n32 cast ascii.aCode 0 cast i + @result.at set] times
6 [i 10 + 0n32 cast ascii.aCodeBig 0 cast i + @result.at set] times
result
] call;
}];
inArray: [
where:;
code: new;
code 256n32 < [code 0 cast where.at new] &&
];
undo: [
mainResult.success [
prevPosition @currentPosition set
currentPosition.offsetStart 0 < ~ [
currentPosition.offsetStart splittedString.chars.at @currentSymbol set
currentSymbol.data Nat32 cast @currentCode set
] [
StringView @currentSymbol set
ascii.null @currentCode set
] if
] when
];
iterate: [
mainResult.success [
currentPosition @prevPosition set
currentCode ascii.lf = [
0 dynamic @currentPosition.@column set
currentPosition.line 1 + @currentPosition.@line set
] when
fileId @currentPosition.@fileId set
currentPosition.offsetStart 1 + @currentPosition.@offsetStart set
currentPosition.column 1 + @currentPosition.@column set
currentPosition.offsetStart splittedString.chars.size < [
currentPosition.offsetStart splittedString.chars.at @currentSymbol set
currentSymbol.data Nat32 cast @currentCode set
] [
StringView @currentSymbol set
ascii.null @currentCode set
] if
] when
];
lexicalError: [
toString @mainResult.@errorInfo.@message set
currentPosition @mainResult.@errorInfo.@position set
FALSE @mainResult.@success set
];
parseStringConstant: [
nameSymbols: String;
iterate
stop: FALSE;
error: [lexicalError TRUE !stop];
[
skip: FALSE;
term: [body:; [body TRUE !skip] when];
(
[skip ~] [
currentCode ascii.null = ["unterminated string" error] term
] [
currentCode ascii.quote = [TRUE !stop] term
] [
currentCode ascii.backSlash = ~ [currentSymbol @nameSymbols.cat] term
] [
iterate
currentCode ascii.null = ["unterminated escape sequence" error] term
] [
currentCode ascii.quote = [currentSymbol @nameSymbols.cat] term
] [
currentCode ascii.nCode = [LF makeStringView @nameSymbols.cat] term
] [
currentCode ascii.rCode = [code: 13n8; (code storageAddress Nat8 addressToReference const 1) toStringView @nameSymbols.cat] term
] [
currentCode ascii.tCode = [code: 9n8; (code storageAddress Nat8 addressToReference const 1) toStringView @nameSymbols.cat] term
] [
currentCode ascii.backSlash = [currentSymbol @nameSymbols.cat] term
] [
currentCode Int32 cast codepointHex? ~ ["invalid escape sequence" error] term
] [
first: currentCode Int32 cast codepointHexValue Nat8 cast;
iterate
currentCode ascii.null = ["unterminated hex sequence" error] term
] [
currentCode Int32 cast codepointHex? ~ ["invalid hex sequence" error] term
] [
first 4n8 lshift currentCode Int32 cast codepointHexValue Nat8 cast or !first
first codeunitHead? ~ ["invalid unicode sequence" error] term
] [
(first storageAddress Nat8 addressToReference const 1) toStringView @nameSymbols.cat
first 0x80n8 < [] term
] [
iterate
currentCode ascii.null = ["unterminated hex sequence" error] term
] [
currentCode Int32 cast codepointHex? ~ ["invalid hex sequence" error] term
] [
next: currentCode Int32 cast codepointHexValue Nat8 cast;
iterate
currentCode ascii.null = ["unterminated hex sequence" error] term
] [
currentCode Int32 cast codepointHex? ~ ["invalid hex sequence" error] term
] [
next 4n8 lshift currentCode Int32 cast codepointHexValue Nat8 cast or !next
next codeunitTail? ~ ["invalid unicode sequence" error] term
] [
(next storageAddress Nat8 addressToReference const 1) toStringView @nameSymbols.cat
first 0xE0n8 < [] term
] [
iterate
currentCode ascii.null = ["unterminated hex sequence" error] term
] [
currentCode Int32 cast codepointHex? ~ ["invalid hex sequence" error] term
] [
next: currentCode Int32 cast codepointHexValue Nat8 cast;
iterate
currentCode ascii.null = ["unterminated hex sequence" error] term
] [
currentCode Int32 cast codepointHex? ~ ["invalid hex sequence" error] term
] [
next 4n8 lshift currentCode Int32 cast codepointHexValue Nat8 cast or !next
next codeunitTail? ~ ["invalid unicode sequence" error] term
] [
(next storageAddress Nat8 addressToReference const 1) toStringView @nameSymbols.cat
first 0xF0n8 < [] term
] [
iterate
currentCode ascii.null = ["unterminated hex sequence" error] term
] [
currentCode Int32 cast codepointHex? ~ ["invalid hex sequence" error] term
] [
next: currentCode Int32 cast codepointHexValue Nat8 cast;
iterate
currentCode ascii.null = ["unterminated hex sequence" error] term
] [
currentCode Int32 cast codepointHex? ~ ["invalid hex sequence" error] term
] [
next 4n8 lshift currentCode Int32 cast codepointHexValue Nat8 cast or !next
next codeunitTail? ~ ["invalid unicode sequence" error] term
] [
(next storageAddress Nat8 addressToReference const 1) toStringView @nameSymbols.cat
]
) sequence
iterate stop ~
] loop
@nameSymbols makeStringNode addToLastUnfinished
TRUE
];
tryParseNumberAfterSign: [
currentCode pc.digits inArray [
undo parseNumber
] [
undo parseName
] if
];
dCheck: [currentCode pc.digits inArray ~ ["wrong number constant" lexicalError] when];
xCheck: [
currentCode pc.digits inArray ~ [
currentCode ascii.xCode = [
-2 @currentArray.append
iterate
] [
"wrong number constant" lexicalError
] if
] when
];
parseDecNumber: [
hasMinus: new;
IntArray: [Int32 Array];
beforeDot: IntArray;
afterDot: IntArray;
afterE: IntArray;
afterT: IntArray;
currentArray: @beforeDot;
hasEMinus: FALSE;
typeClass: 0 dynamic;
stage: 0 dynamic;
#0 before dot or else
#1 after dot
#2 after e
#3 after n/r
result: 0 dynamic;
tokenBegin: currentPosition.offsetStart hasMinus [1 -][new] if;
tokenEnd: tokenBegin new;
[
currentCode pc.digits inArray [
currentCode ascii.zero - 0 cast @currentArray.append
iterate TRUE
] [
currentCode ascii.dot = [
stage 0 = ~ [
FALSE
] [
1 @stage set
iterate
currentCode pc.digits inArray [
@afterDot !currentArray
2 @typeClass set
TRUE
] [
undo FALSE
] if
] if
] [
currentCode ascii.nCode = [
stage 0 = ~ ["wrong number constant" lexicalError] when
3 @stage set
iterate TRUE
@afterT !currentArray
xCheck
1 @typeClass set
] [
currentCode ascii.iCode = [
stage 0 = ~ ["wrong number constant" lexicalError] when
3 @stage set
iterate TRUE
@afterT !currentArray
xCheck
] [
currentCode ascii.rCode = [
stage 1 = stage 2 = or ~ ["wrong number constant" lexicalError] when
3 @stage set
iterate TRUE
@afterT !currentArray
dCheck
] [
currentCode pc.begExp inArray [
stage 1 = ~ ["wrong number constant" lexicalError] when
iterate TRUE
@afterE !currentArray
currentCode pc.numberSigns inArray [
currentCode ascii.minus = @hasEMinus set
iterate
] when
dCheck
] [
currentCode pc.endNumbers inArray [
FALSE
] [
"error in number constant" lexicalError
FALSE
] if
] if
] if
] if
] if
] if
] if
p:;
p [currentPosition.offsetStart @tokenEnd set] when
p mainResult.success and
] loop
token: splittedString.chars tokenBegin tokenEnd range assembleString;
afterT.size 2 > [ "error in number constant" lexicalError ] when
typeName: 0 dynamic;
afterT.size [typeName 100 * i afterT.at + 1 + @typeName set] times
typeClass 2 = [
type: 0.0r64 dynamic;
ten: 10.0r64 dynamic;
result: 0.0r64 dynamic;
beforeDot.size [result 10.0r64 * i beforeDot.at type cast + @result set] times
tenRcp: 0.1r64 dynamic;
fracPartFactor: tenRcp new;
afterDot.size [
digit: i afterDot.at type cast;
digit fracPartFactor * result + @result set
fracPartFactor tenRcp * @fracPartFactor set
] times
decOrder: 0.0r64 dynamic;
afterE.size [decOrder 10.0r64 * i afterE.at type cast + @decOrder set] times
hasEMinus [decOrder neg @decOrder set] when
hasMinus [result neg @result set] when
result 10.0r64 decOrder ^ * @result set
typeName 705 = typeName 0 = or [
result token makeReal64Node addToLastUnfinished
] [
typeName 403 = [
result token makeReal32Node addToLastUnfinished
] [
"wrong real constant suffix" lexicalError
] if
] if
] [
typeClass 1 = [
hasMinus ["negative natural constants not allowed" lexicalError] when
type: 0n64 dynamic;
ten: 10n64 dynamic;
result: 0n64 dynamic;
beforeDot.size [result 10n64 * i beforeDot.at 0i64 cast type cast + @result set] times
typeName 705 = [
result token makeNumbern64Node addToLastUnfinished
] [
typeName 403 = typeName 0 = or [
result token makeNumbern32Node addToLastUnfinished
] [
typeName 207 = [
result token makeNumbern16Node addToLastUnfinished
] [
typeName 9 = [
result token makeNumbern8Node addToLastUnfinished
] [
typeName -1 = [
result token makeNumbernxNode addToLastUnfinished
] [
"wrong natural constant suffix" lexicalError
] if
] if
] if
] if
] if
] [
type: 0i64 dynamic;
ten: 10i64 dynamic;
result: 0i64 dynamic;
beforeDot.size [result 10i64 * i beforeDot.at type cast + @result set] times
hasMinus [result neg @result set] when
typeName 705 = [
result token makeNumberi64Node addToLastUnfinished
] [
typeName 403 = typeName 0 = or [
result token makeNumberi32Node addToLastUnfinished
] [
typeName 207 = [
result token makeNumberi16Node addToLastUnfinished
] [
typeName 9 = [
result token makeNumberi8Node addToLastUnfinished
] [
typeName -1 = [
result token makeNumberixNode addToLastUnfinished
] [
"wrong integer constant suffix" lexicalError
] if
] if
] if
] if
] if
] if
] if
mainResult.success new
];
parseHexNumber: [
hasMinus: new;
IntArray: [Int32 Array];
beforeT: IntArray;
afterT: IntArray;
currentArray: @beforeT;
typeClass: 0 dynamic;
stage: 0 dynamic;
#0 before n
#1 after n
result: 0 dynamic;
tokenBegin: currentPosition.offsetStart hasMinus [3 -][2 -] if;
tokenEnd: tokenBegin new;
[
currentCode pc.hexDigits inArray [
currentCode 0 cast pc.hexToInt.at 0 cast @currentArray.append
iterate TRUE
] [
currentCode ascii.dot = [
FALSE
] [
currentCode ascii.nCode = [
stage 0 = ~ ["wrong number constant" lexicalError] when
3 @stage set
iterate TRUE
@afterT !currentArray
xCheck
1 @typeClass set
] [
currentCode ascii.iCode = [
stage 0 = ~ ["wrong number constant" lexicalError] when
3 @stage set
iterate TRUE
@afterT !currentArray
xCheck
] [
currentCode pc.endNumbers inArray [
FALSE
] [
"error in number constant" lexicalError
FALSE
] if
] if
] if
] if
] if
p:;
p [ currentPosition.offsetStart @tokenEnd set ] when
p mainResult.success and
] loop
token: splittedString.chars tokenBegin tokenEnd range assembleString;
afterT.size 2 > [ "error in number constant" lexicalError ] when
typeName: 0 dynamic;
afterT.size [typeName 100 * i afterT.at + 1 + @typeName set] times
hasMinus ["negative hex constants not allowed" lexicalError] when
typeClass 1 = [
type: 0n64;
ten: 10n64;
result: 0n64;
beforeT.size 0 = ["empty hex constant" lexicalError] when
beforeT.size [result 16n64 * i beforeT.at 0i64 cast type cast + @result set] times
typeName 705 = [
result token makeNumbern64Node addToLastUnfinished
] [
typeName 403 = typeName 0 = or [
result token makeNumbern32Node addToLastUnfinished
] [
typeName 207 = [
result token makeNumbern16Node addToLastUnfinished
] [
typeName 9 = [
result token makeNumbern8Node addToLastUnfinished
] [
typeName -1 = [
result token makeNumbernxNode addToLastUnfinished
] [
"wrong natural constant suffix" lexicalError
] if
] if
] if
] if
] if
] [
type: 0i64 dynamic;
ten: 10i64 dynamic;
result: 0i64 dynamic;
beforeT.size 0 = ["empty hex constant" lexicalError] when
beforeT.size [result 16i64 * i beforeT.at type cast + @result set] times
typeName 705 = [
result token makeNumberi64Node addToLastUnfinished
] [
typeName 403 = typeName 0 = or [
result token makeNumberi32Node addToLastUnfinished
] [
typeName 207 = [
result token makeNumberi16Node addToLastUnfinished
] [
typeName 9 = [
result token makeNumberi8Node addToLastUnfinished
] [
typeName -1 = [
result token makeNumberixNode addToLastUnfinished
] [
"wrong integer constant suffix" lexicalError
] if
] if
] if
] if
] if
] if
mainResult.success new
];
parseNumber: [
hasMinus: FALSE dynamic;
currentCode ascii.minus = [
TRUE dynamic @hasMinus set
iterate
] [
currentCode ascii.plus = [iterate] when
] if
currentCode ascii.zero = [
iterate
currentCode ascii.xCode = [
iterate hasMinus parseHexNumber
] [
currentCode ascii.iCode = currentCode ascii.nCode = or currentCode ascii.dot = or currentCode pc.endNumbers inArray or [
undo hasMinus parseDecNumber
] [
"lead zeros in number are not allowed" lexicalError
FALSE
] if
] if
] [
hasMinus parseDecNumber
] if
];
makeLabel: [
name:;
lastPosition @unfinishedPositions.append
ascii.semicolon @unfinishedTerminators.append
name toString @unfinishedLabelNames.append
newAstNodeArray: AstNodeArray;
lastPosition @newAstNodeArray.@positionInfo set
@newAstNodeArray @unfinishedNodes.append
];
parseName: [
DotState: (
"UNKNOWN"
"MULTI_DOT"
"NOT_A_MEMBER"
"WAS_FIRST_DOT"
"MEMBER"
) Int32 enum;
dotState: DotState.UNKNOWN dynamic;
read: FALSE dynamic;
write: FALSE dynamic;
label: FALSE dynamic;
checkOffset: currentPosition.offsetStart new;
checkFirst: [currentPosition.offsetStart checkOffset > ["invalid identifier" lexicalError] when];
nameSymbols: StringView Array;
first: TRUE dynamic;
[
first [currentCode pc.digits inArray] && [
"idendifiers can't begin from number" lexicalError
FALSE
] [
currentCode ascii.quote = [
"quote cannot be part of identifier" lexicalError
FALSE
] [
currentCode ascii.dot = [
dotState (
DotState.MULTI_DOT [
currentSymbol @nameSymbols.append
iterate TRUE
]
DotState.NOT_A_MEMBER [
currentSymbol @nameSymbols.append
DotState.MULTI_DOT @dotState set
iterate TRUE
]
DotState.WAS_FIRST_DOT [
currentSymbol @nameSymbols.append
DotState.MULTI_DOT @dotState set
iterate TRUE
]
[
nameSymbols.size 0 > [
FALSE
] [
checkFirst
checkOffset 1 + @checkOffset set
currentSymbol @nameSymbols.append
DotState.WAS_FIRST_DOT @dotState set
iterate TRUE
] if
]
) case
] [
currentCode ascii.at = [
checkFirst
dotState DotState.UNKNOWN = [DotState.NOT_A_MEMBER @dotState set] when
TRUE @read set
iterate TRUE
] [
currentCode ascii.exclamation = [
checkFirst
dotState DotState.UNKNOWN = [DotState.NOT_A_MEMBER @dotState set] when
TRUE @write set
iterate TRUE
] [
currentCode ascii.comma = nameSymbols.size 0 > and [
FALSE
] [
currentCode pc.endNames inArray [
currentCode ascii.colon = [
TRUE @label set
iterate
] when
FALSE
] [
dotState
(
DotState.MULTI_DOT [
"identifier cannot start from many dots" lexicalError
]
DotState.WAS_FIRST_DOT [
DotState.MEMBER @dotState set
@nameSymbols.clear
]
DotState.MEMBER [
]
[
DotState.UNKNOWN @dotState set
]
) case
nameSymbols.size 1 > [0 nameSymbols.at "," =] && [
"identifier cannot start from comma" lexicalError
] when
currentSymbol @nameSymbols.append
iterate
FALSE @first set
TRUE
] if
] if
] if
] if
] if
] if
] if
mainResult.success and