-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.div
More file actions
2199 lines (1849 loc) · 92.7 KB
/
parser.div
File metadata and controls
2199 lines (1849 loc) · 92.7 KB
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 2025-2026 Yusuf Rençber
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include "divio", "divstr", "divfile", "divvec", "divmap", "lexer.div", "preprocessor.div" : *;
enum ASTNodeType: i32 {
ROOT, INLINE_ASM,
VARIABLE_DECL, CONSTANT_DECL, STATIC_DECL,
FUNCTION_DECL, CLASS_DECL, ENUM_DECL, TYPE_DECL,
EXTERN_BLOCK, IMPL_BLOCK, PARAMETER, FIELD, VARIANT,
IF_STMT, IF_LET_STMT, MATCH_STMT, CASE_STMT, DEFAULT_STMT,
WHILE_STMT, WHILE_LET_STMT, FOR_STMT, LOOP_STMT, BLOCK_STMT,
EXPRESSION_STMT, RETURN_STMT, BREAK_STMT, CONTINUE_STMT,
UNARY_OP, BINARY_OP, ASSIGNMENT, TERNARY, CALL, CAST, NEW, DELETE,
LITERAL, IDENTIFIER, SCOPE_RESOLUTION, MEMBER_ACCESS, INDEX_ACCESS,
RANGE_EXPR, AWAIT_EXPR, INITIALIZER_LIST, STRUCT_INIT, OR_PATTERN,
PRIMITIVE_TYPE, POINTER_TYPE, REFERENCE_TYPE, ARRAY_TYPE, FUNCTION_TYPE
};
func ASTNodeTypeToString(_type: ASTNodeType) -> *const i8 {
if _type == ASTNodeType::ROOT { ret "ROOT"; }
else if _type == ASTNodeType::VARIABLE_DECL { ret "VARIABLE_DECL"; }
else if _type == ASTNodeType::CONSTANT_DECL { ret "CONSTANT_DECL"; }
else if _type == ASTNodeType::STATIC_DECL { ret "STATIC_DECL"; }
else if _type == ASTNodeType::FUNCTION_DECL { ret "FUNCTION_DECL"; }
else if _type == ASTNodeType::CLASS_DECL { ret "CLASS_DECL"; }
else if _type == ASTNodeType::ENUM_DECL { ret "ENUM_DECL"; }
else if _type == ASTNodeType::TYPE_DECL { ret "TYPE_DECL"; }
else if _type == ASTNodeType::EXTERN_BLOCK { ret "EXTERN_BLOCK"; }
else if _type == ASTNodeType::IMPL_BLOCK { ret "IMPL_BLOCK"; }
else if _type == ASTNodeType::PARAMETER { ret "PARAMETER"; }
else if _type == ASTNodeType::FIELD { ret "FIELD"; }
else if _type == ASTNodeType::VARIANT { ret "VARIANT"; }
else if _type == ASTNodeType::IF_STMT { ret "IF_STMT"; }
else if _type == ASTNodeType::IF_LET_STMT { ret "IF_LET_STMT"; }
else if _type == ASTNodeType::MATCH_STMT { ret "MATCH_STMT"; }
else if _type == ASTNodeType::CASE_STMT { ret "CASE_STMT"; }
else if _type == ASTNodeType::DEFAULT_STMT { ret "DEFAULT_STMT"; }
else if _type == ASTNodeType::WHILE_STMT { ret "WHILE_STMT"; }
else if _type == ASTNodeType::WHILE_LET_STMT { ret "WHILE_LET_STMT"; }
else if _type == ASTNodeType::FOR_STMT { ret "FOR_STMT"; }
else if _type == ASTNodeType::LOOP_STMT { ret "LOOP_STMT"; }
else if _type == ASTNodeType::BLOCK_STMT { ret "BLOCK_STMT"; }
else if _type == ASTNodeType::EXPRESSION_STMT { ret "EXPRESSION_STMT"; }
else if _type == ASTNodeType::RETURN_STMT { ret "RETURN_STMT"; }
else if _type == ASTNodeType::BREAK_STMT { ret "BREAK_STMT"; }
else if _type == ASTNodeType::CONTINUE_STMT { ret "CONTINUE_STMT"; }
else if _type == ASTNodeType::UNARY_OP { ret "UNARY_OP"; }
else if _type == ASTNodeType::BINARY_OP { ret "BINARY_OP"; }
else if _type == ASTNodeType::ASSIGNMENT { ret "ASSIGNMENT"; }
else if _type == ASTNodeType::TERNARY { ret "TERNARY"; }
else if _type == ASTNodeType::CALL { ret "CALL"; }
else if _type == ASTNodeType::CAST { ret "CAST"; }
else if _type == ASTNodeType::NEW { ret "NEW"; }
else if _type == ASTNodeType::DELETE { ret "DELETE"; }
else if _type == ASTNodeType::LITERAL { ret "LITERAL"; }
else if _type == ASTNodeType::IDENTIFIER { ret "IDENTIFIER"; }
else if _type == ASTNodeType::SCOPE_RESOLUTION { ret "SCOPE_RESOLUTION"; }
else if _type == ASTNodeType::MEMBER_ACCESS { ret "MEMBER_ACCESS"; }
else if _type == ASTNodeType::INDEX_ACCESS { ret "INDEX_ACCESS"; }
else if _type == ASTNodeType::RANGE_EXPR { ret "RANGE_EXPR"; }
else if _type == ASTNodeType::AWAIT_EXPR { ret "AWAIT_EXPR"; }
else if _type == ASTNodeType::INITIALIZER_LIST { ret "INITIALIZER_LIST"; }
else if _type == ASTNodeType::STRUCT_INIT { ret "STRUCT_INIT"; }
else if _type == ASTNodeType::OR_PATTERN { ret "OR_PATTERN"; }
else if _type == ASTNodeType::PRIMITIVE_TYPE { ret "PRIMITIVE_TYPE"; }
else if _type == ASTNodeType::POINTER_TYPE { ret "POINTER_TYPE"; }
else if _type == ASTNodeType::REFERENCE_TYPE { ret "REFERENCE_TYPE"; }
else if _type == ASTNodeType::ARRAY_TYPE { ret "ARRAY_TYPE"; }
else if _type == ASTNodeType::FUNCTION_TYPE { ret "FUNCTION_TYPE"; }
else { ret "UNKNOWN"; }
}
class ASTNode {
pub node_type: ASTNodeType,
pub row: u64, pub column: u64,
pub file: String
} impl {
ASTNode(this: &mut ASTNode, _type: ASTNodeType, row: u64, column: u64, file: String) {
(this.node_type, this.row, this.column, this.file) = (_type, row, column, file);
}
};
class PrimitiveTypeNode : ASTNode {
pub name: String,
pub generic: bool = false,
pub types: Vector<*const ASTNode>
} impl {
PrimitiveTypeNode(this: &mut PrimitiveTypeNode, name: String, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::PRIMITIVE_TYPE, row, column, file) {
this.name = name;
}
};
class PointerTypeNode : ASTNode {
pub base_type: *const ASTNode,
pub mutable: bool = false
} impl {
PointerTypeNode(this: &mut PointerTypeNode, base_type: *const ASTNode, mutable: bool, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::POINTER_TYPE, row, column, file) {
(this.base_type, this.mutable) = (base_type, mutable);
}
};
class ReferenceTypeNode : ASTNode {
pub base_type: *const ASTNode,
pub mutable: bool = false
} impl {
ReferenceTypeNode(this: &mut ReferenceTypeNode, base_type: *const ASTNode, mutable: bool, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::REFERENCE_TYPE, row, column, file) {
(this.base_type, this.mutable) = (base_type, mutable);
}
};
class ArrayTypeNode : ASTNode {
pub element_type: *const ASTNode,
pub size: *const ASTNode
} impl {
ArrayTypeNode(this: &mut ArrayTypeNode, element_type: *const ASTNode, size: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::ARRAY_TYPE, row, column, file) {
(this.element_type, this.size) = (element_type, size);
}
};
class LiteralNode : ASTNode {
pub value: String,
pub literal_type: TokenType
} impl {
LiteralNode(this: &mut LiteralNode, value: String, literal_type: TokenType, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::LITERAL, row, column, file) {
(this.value, this.literal_type) = (value, literal_type);
}
};
class IdentifierNode : ASTNode {
pub name: String
} impl {
IdentifierNode(this: &mut IdentifierNode, name: String, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::IDENTIFIER, row, column, file) {
this.name = name;
}
};
class ScopeResolutionNode : ASTNode {
pub scope: *const ASTNode,
pub identifier: *const IdentifierNode
} impl {
ScopeResolutionNode(this: &mut ScopeResolutionNode, scope: *const ASTNode, identifier: *const IdentifierNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::SCOPE_RESOLUTION, row, column, file) {
(this.scope, this.identifier) = (scope, identifier);
}
};
class BinaryOpNode : ASTNode {
pub left: *const ASTNode,
pub right: *const ASTNode,
pub operator: TokenType
} impl {
BinaryOpNode(this: &mut BinaryOpNode, left: *const ASTNode, right: *const ASTNode, operator: TokenType, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::BINARY_OP, row, column, file) {
(this.left, this.right, this.operator) = (left, right, operator);
}
};
class UnaryOpNode : ASTNode {
pub operand: *const ASTNode,
pub operator: TokenType,
pub mutable: bool = false
} impl {
UnaryOpNode(this: &mut UnaryOpNode, operand: *const ASTNode, operator: TokenType, mutable: bool, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::UNARY_OP, row, column, file) {
(this.operand, this.operator, this.mutable) = (operand, operator, mutable);
}
};
class TernaryNode : ASTNode {
pub condition: *const ASTNode,
pub then_expr: *const ASTNode,
pub else_expr: *const ASTNode
} impl {
TernaryNode(this: &mut TernaryNode, condition: *const ASTNode, then_expr: *const ASTNode, else_expr: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::TERNARY, row, column, file) {
(this.condition, this.then_expr, this.else_expr) = (condition, then_expr, else_expr);
}
};
class CallNode : ASTNode {
pub expression: *const ASTNode,
pub arguments: Vector<*const ASTNode>
} impl {
CallNode(this: &mut CallNode, expression: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::CALL, row, column, file) {
this.expression = expression;
}
};
class MemberAccessNode : ASTNode {
pub expression: *const ASTNode,
pub member: String,
pub arrow: bool = false
} impl {
MemberAccessNode(this: &mut MemberAccessNode, expression: *const ASTNode, arrow: bool, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::MEMBER_ACCESS, row, column, file) {
(this.expression, this.arrow) = (expression, arrow);
}
};
class IndexAccessNode : ASTNode {
pub expression: *const ASTNode,
pub index: *const ASTNode
} impl {
IndexAccessNode(this: &mut IndexAccessNode, expression: *const ASTNode, index: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::INDEX_ACCESS, row, column, file) {
(this.expression, this.index) = (expression, index);
}
};
class CastNode : ASTNode {
pub expression: *const ASTNode,
pub target_type: *const ASTNode
} impl {
CastNode(this: &mut CastNode, expression: *const ASTNode, target_type: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::CAST, row, column, file) {
(this.expression, this.target_type) = (expression, target_type);
}
};
class InitializerListNode : ASTNode {
pub expressions: Vector<*const ASTNode>
} impl {
InitializerListNode(this: &mut InitializerListNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::INITIALIZER_LIST, row, column, file) {}
};
class NewNode : ASTNode {
pub _type: *const ASTNode,
pub arguments: Vector<*const ASTNode>,
pub size: *const ASTNode = null
} impl {
NewNode(this: &mut NewNode, _type: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::NEW, row, column, file) {
this._type = _type;
}
};
class DeleteNode : ASTNode {
pub expression: *const ASTNode,
pub array: bool = false
} impl {
DeleteNode(this: &mut DeleteNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::DELETE, row, column, file) {}
};
class ExpressionStmtNode : ASTNode {
pub expression: *const ASTNode
} impl {
ExpressionStmtNode(this: &mut ExpressionStmtNode, expression: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::EXPRESSION_STMT, row, column, file) {
this.expression = expression;
}
};
class VariableDeclNode : ASTNode {
pub name: String,
pub _type: *const ASTNode,
pub initializer: *const ASTNode,
pub mutable: bool = false,
pub next: *const VariableDeclNode = null
} impl {
VariableDeclNode(this: &mut VariableDeclNode, name: String, _type: *const ASTNode, initializer: *const ASTNode, mutable: bool, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::VARIABLE_DECL, row, column, file) {
(this.name, this._type, this.initializer, this.mutable) = (name, _type, initializer, mutable);
}
};
class ConstantDeclNode : ASTNode {
pub name: String,
pub _type: *const ASTNode,
pub initializer: *const ASTNode
} impl {
ConstantDeclNode(this: &mut ConstantDeclNode, name: String, _type: *const ASTNode, initializer: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::CONSTANT_DECL, row, column, file) {
(this.name, this._type, this.initializer) = (name, _type, initializer);
}
};
class StaticDeclNode : ASTNode {
pub name: String,
pub _type: *const ASTNode,
pub initializer: *const ASTNode,
pub mutable: bool
} impl {
StaticDeclNode(this: &mut StaticDeclNode, name: String, _type: *const ASTNode, initializer: *const ASTNode, mutable: bool, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::STATIC_DECL, row, column, file) {
(this.name, this._type, this.initializer, this.mutable) = (name, _type, initializer, mutable);
}
};
class AssignmentNode : ASTNode {
pub left: *const ASTNode,
pub right: *const ASTNode,
pub operator: TokenType,
pub next: *const AssignmentNode = null
} impl {
AssignmentNode(this: &mut AssignmentNode, left: *const ASTNode, right: *const ASTNode, operator: TokenType, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::ASSIGNMENT, row, column, file) {
(this.left, this.right, this.operator) = (left, right, operator);
}
};
class IfStmtNode : ASTNode {
pub condition: *const ASTNode,
pub then_stmt: *const ASTNode,
pub else_stmt: *const ASTNode
} impl {
IfStmtNode(this: &mut IfStmtNode, condition: *const ASTNode, then_stmt: *const ASTNode, else_stmt: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::IF_STMT, row, column, file) {
(this.condition, this.then_stmt, this.else_stmt) = (condition, then_stmt, else_stmt);
}
};
class WhileStmtNode : ASTNode {
pub condition: *const ASTNode,
pub loop_stmt: *const ASTNode,
pub else_stmt: *const ASTNode
} impl {
WhileStmtNode(this: &mut WhileStmtNode, condition: *const ASTNode, loop_stmt: *const ASTNode, else_stmt: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::WHILE_STMT, row, column, file) {
(this.condition, this.loop_stmt, this.else_stmt) = (condition, loop_stmt, else_stmt);
}
};
class LoopStmtNode : ASTNode {
pub loop_stmt: *const ASTNode
} impl {
LoopStmtNode(this: &mut LoopStmtNode, loop_stmt: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::LOOP_STMT, row, column, file) {
this.loop_stmt = loop_stmt;
}
};
class ReturnStmtNode : ASTNode {
pub expression: *const ASTNode
} impl {
ReturnStmtNode(this: &mut ReturnStmtNode, expression: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::RETURN_STMT, row, column, file) {
this.expression = expression;
}
};
class BreakStmtNode : ASTNode {} impl {
BreakStmtNode(this: &mut BreakStmtNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::BREAK_STMT, row, column, file) {}
};
class ContinueStmtNode : ASTNode {} impl {
ContinueStmtNode(this: &mut ContinueStmtNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::CONTINUE_STMT, row, column, file) {}
};
class BlockStmtNode : ASTNode {
pub statements: Vector<*const ASTNode>
} impl {
BlockStmtNode(this: &mut BlockStmtNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::BLOCK_STMT, row, column, file) {}
};
class ParameterNode : ASTNode {
pub name: String,
pub param_type: *const ASTNode,
pub mutable: bool = false
} impl {
ParameterNode(this: &mut ParameterNode, name: String, param_type: *const ASTNode, mutable: bool, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::PARAMETER, row, column, file) {
(this.name, this.param_type, this.mutable) = (name, param_type, mutable);
}
};
class FunctionTypeNode : ASTNode {
pub parameters: Vector<*const ParameterNode>,
pub return_type: *const ASTNode,
pub var_arg: bool = false
} impl {
FunctionTypeNode(this: &mut FunctionTypeNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::FUNCTION_TYPE, row, column, file) {}
};
class FunctionDeclNode : ASTNode {
pub name: String,
pub _type: *const FunctionTypeNode,
pub body: *const BlockStmtNode = null,
pub generic: bool = false,
pub types: Vector<String>,
pub constructors: Vector<*const CallNode>,
pub is_async: bool = false
} impl {
FunctionDeclNode(this: &mut FunctionDeclNode, name: String, _type: *const FunctionTypeNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::FUNCTION_DECL, row, column, file) {
(this.name, this._type) = (name, _type);
}
};
class AwaitExprNode : ASTNode {
pub expression: *const ASTNode
} impl {
AwaitExprNode(this: &mut AwaitExprNode, expression: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::AWAIT_EXPR, row, column, file) {
this.expression = expression;
}
};
class TypeDeclNode : ASTNode {
pub name: String,
pub _type: *const ASTNode
} impl {
TypeDeclNode(this: &mut TypeDeclNode, name: String, _type: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::TYPE_DECL, row, column, file) {
(this.name, this._type) = (name, _type);
}
};
class FieldNode : ASTNode {
pub name: String,
pub field_type: *const ASTNode,
pub initializer: *const ASTNode
} impl {
FieldNode(this: &mut FieldNode, name: String, field_type: *const ASTNode, initializer: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::FIELD, row, column, file) {
(this.name, this.field_type, this.initializer) = (name, field_type, initializer);
}
};
class ClassDeclNode : ASTNode {
pub name: String,
pub fields: Vector<Pair<TokenType, *const FieldNode>>,
pub methods: Vector<Pair<TokenType, *const FunctionDeclNode>>,
pub constructors: Vector<*const FunctionDeclNode>,
pub destructor: *const FunctionDeclNode = null,
pub parents: Vector<*const ASTNode>,
pub generic: bool = false,
pub types: Vector<String>
} impl {
ClassDeclNode(this: &mut ClassDeclNode, name: String, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::CLASS_DECL, row, column, file) {
this.name = name;
}
};
class VariantNode : ASTNode {
pub name: String,
pub types: Vector<*const ASTNode>,
pub field_names: Vector<String>,
pub value: *const ASTNode = null
} impl {
VariantNode(this: &mut VariantNode, name: String, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::VARIANT, row, column, file) {
this.name = name;
}
};
class EnumDeclNode : ASTNode {
pub name: String,
pub variants: Vector<*const VariantNode>,
pub methods: Vector<Pair<TokenType, *const FunctionDeclNode>>,
pub underlying_type: *const ASTNode,
pub generic: bool,
pub types: Vector<String>
} impl {
EnumDeclNode(this: &mut EnumDeclNode, name: String, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::ENUM_DECL, row, column, file) {
(this.name, this.underlying_type, this.generic) = (name, null, false);
}
};
class CaseStmtNode : ASTNode {
pub pattern: *const ASTNode,
pub body: *const ASTNode,
pub guard: *const ASTNode
} impl {
CaseStmtNode(this: &mut CaseStmtNode, pattern: *const ASTNode, body: *const ASTNode, guard: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::CASE_STMT, row, column, file) {
(this.pattern, this.body, this.guard) = (pattern, body, guard);
}
};
class DefaultStmtNode : ASTNode {
pub body: *const ASTNode
} impl {
DefaultStmtNode(this: &mut DefaultStmtNode, body: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::DEFAULT_STMT, row, column, file) {
this.body = body;
}
};
class MatchStmtNode : ASTNode {
pub expression: *const ASTNode,
pub cases: Vector<*const CaseStmtNode>,
pub default_case: *const DefaultStmtNode
} impl {
MatchStmtNode(this: &mut MatchStmtNode, expression: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::MATCH_STMT, row, column, file) {
(this.expression, this.default_case) = (expression, null);
}
};
class StructInitNode : ASTNode {
pub expression: *const ASTNode,
pub field_names: Vector<String>,
pub field_values: Vector<*const ASTNode>
} impl {
StructInitNode(this: &mut StructInitNode, expression: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::STRUCT_INIT, row, column, file) {
this.expression = expression;
}
};
class OrPatternNode : ASTNode {
pub left: *const ASTNode,
pub right: *const ASTNode
} impl {
OrPatternNode(this: &mut OrPatternNode, left: *const ASTNode, right: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::OR_PATTERN, row, column, file) {
(this.left, this.right) = (left, right);
}
};
class IfLetStmtNode : ASTNode {
pub pattern: *const ASTNode,
pub expression: *const ASTNode,
pub then_stmt: *const ASTNode,
pub else_stmt: *const ASTNode = null
} impl {
IfLetStmtNode(this: &mut IfLetStmtNode, pattern: *const ASTNode, expression: *const ASTNode, then_stmt: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::IF_LET_STMT, row, column, file) {
(this.pattern, this.expression, this.then_stmt) = (pattern, expression, then_stmt);
}
};
class WhileLetStmtNode : ASTNode {
pub pattern: *const ASTNode,
pub expression: *const ASTNode,
pub loop_stmt: *const ASTNode
} impl {
WhileLetStmtNode(this: &mut WhileLetStmtNode, pattern: *const ASTNode, expression: *const ASTNode, loop_stmt: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::WHILE_LET_STMT, row, column, file) {
(this.pattern, this.expression, this.loop_stmt) = (pattern, expression, loop_stmt);
}
};
class RangeExprNode : ASTNode {
pub start: *const ASTNode,
pub end: *const ASTNode,
pub inclusive: bool = false
} impl {
RangeExprNode(this: &mut RangeExprNode, start: *const ASTNode, end: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::RANGE_EXPR, row, column, file) {
(this.start, this.end) = (start, end);
}
};
class ForStmtNode : ASTNode {
pub variable: String,
pub iterable: *const ASTNode,
pub loop_stmt: *const ASTNode
} impl {
ForStmtNode(this: &mut ForStmtNode, variable: String, iterable: *const ASTNode, loop_stmt: *const ASTNode, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::FOR_STMT, row, column, file) {
(this.variable, this.iterable, this.loop_stmt) = (variable, iterable, loop_stmt);
}
};
class ExternBlockNode : ASTNode {
pub linkage: String,
pub declarations: Vector<*const ASTNode>
} impl {
ExternBlockNode(this: &mut ExternBlockNode, linkage: String, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::EXTERN_BLOCK, row, column, file) {
this.linkage = linkage;
}
};
class InlineAsmNode : ASTNode {
pub _volatile: bool, pub code: String,
pub inputs: Vector<Pair<String, *const ASTNode>>,
pub outputs: Vector<Pair<String, *const ASTNode>>,
pub clobbers: Vector<String>
} impl {
InlineAsmNode(this: &mut InlineAsmNode, _volatile: bool, row: u64, column: u64, file: String) : ASTNode(ASTNodeType::INLINE_ASM, row, column, file) {
this._volatile = _volatile;
}
};
class RootNode : ASTNode {
pub declarations: Vector<*const ASTNode>
} impl {
RootNode(this: &mut RootNode) : ASTNode(ASTNodeType::ROOT, 1u64, 1u64, String("<root>")) {}
};
class Parser {
priv root: *mut RootNode,
priv types: Vector<String>,
pub defines: Vector<String>,
pub include_paths: Vector<String>,
priv included_paths: Vector<String>,
priv file: String, priv index: u64,
priv tokens: Vector<Token>
} impl {
priv Current(this: &Parser) -> Token {
if this.index >= this.tokens.Size() {
ret this.tokens[this.tokens.Size() - 1u64];
} else {
ret this.tokens[this.index];
}
}
priv Peek(this: &Parser, offset: u64) -> Token {
let index: u64 = this.index + offset;
if index >= this.tokens.Size() {
ret this.tokens[this.tokens.Size() - 1u64];
} else {
ret this.tokens[index];
}
}
priv Advance(this: &mut Parser) -> Token {
let token: Token = this.Current();
if this.index < this.tokens.Size() {
this.index += 1u64;
} ret token;
}
priv Match(this: &mut Parser, _type: TokenType) -> bool {
if this.Current()._type == _type {
this.Advance();
ret true;
} else {
ret false;
}
}
priv Expect(this: &mut Parser, _type: TokenType) -> Token {
let token: Token = this.Current();
if token._type == _type {
ret this.Advance();
}
Logger::Log(this.file, String("Expected '") + TokenTypeToString(_type) + "' but got '" + TokenTypeToString(token._type) + "'.", token, LogSeverity::Error);
let mut error: Token;
(error._type, error.row, error.column) = (TokenType::UNKNOWN, token.row, token.column);
ret error;
}
priv Expect2(this: &mut Parser, _type: TokenType) -> bool {
let token: Token = this.Current();
if token._type == _type {
this.Advance();
ret true;
}
Logger::Log(this.file, String("Expected '") + TokenTypeToString(_type) + "' but got '" + TokenTypeToString(token._type) + "'.", token, LogSeverity::Error);
ret false;
}
priv GetBinaryOpPrecedence(this: &Parser, _type: TokenType) -> i32 {
if _type == TokenType::OR { ret 1i32; }
else if _type == TokenType::AND { ret 2i32; }
else if _type == TokenType::BITWISE_OR { ret 3i32; }
else if _type == TokenType::BITWISE_XOR { ret 4i32; }
else if _type == TokenType::BITWISE_AND { ret 5i32; }
else if _type == TokenType::EQUAL || _type == TokenType::NOT_EQUAL { ret 6i32; }
else if _type == TokenType::LESS || _type == TokenType::LESS_EQUAL || _type == TokenType::GREATER || _type == TokenType::GREATER_EQUAL { ret 7i32; }
else if _type == TokenType::LSHIFT || _type == TokenType::RSHIFT { ret 8i32; }
else if _type == TokenType::PLUS || _type == TokenType::MINUS { ret 9i32; }
else if _type == TokenType::MUL || _type == TokenType::DIV || _type == TokenType::MOD { ret 10i32; }
else if _type == TokenType::AS { ret 11i32; }
else { ret -1i32; }
}
priv IsBinaryOperator(this: &Parser, _type: TokenType) -> bool {
let precedence: i32 = this.GetBinaryOpPrecedence(_type);
ret precedence >= 0i32 && precedence != 11i32;
}
priv IsUnaryOperator(this: &Parser, _type: TokenType) -> bool {
ret _type == TokenType::NOT || _type == TokenType::MINUS || _type == TokenType::BITWISE_AND || _type == TokenType::MUL;
}
priv IsStructInit(this: &Parser, node_type: ASTNodeType) -> bool {
let after_brace: Token = this.Peek(1u64);
if node_type == ASTNodeType::SCOPE_RESOLUTION {
if after_brace._type == TokenType::RBRACE { ret true; }
if after_brace._type == TokenType::TWO_DOT { ret true; }
}
if after_brace._type == TokenType::IDENTIFIER {
let after_name: Token = this.Peek(2u64);
if after_name._type == TokenType::COLON { ret true; }
if node_type == ASTNodeType::SCOPE_RESOLUTION {
ret after_name._type == TokenType::COMMA || after_name._type == TokenType::RBRACE;
}
} ret false;
}
priv IsAssignmentOperator(this: &Parser, _type: TokenType) -> bool {
ret _type == TokenType::ASSIGN || _type == TokenType::PLUS_ASSIGN || _type == TokenType::MINUS_ASSIGN || _type == TokenType::MUL_ASSIGN || _type == TokenType::DIV_ASSIGN || _type == TokenType::MOD_ASSIGN
|| _type == TokenType::LSHIFT_ASSIGN || _type == TokenType::RSHIFT_ASSIGN || _type == TokenType::AND_ASSIGN || _type == TokenType::OR_ASSIGN || _type == TokenType::XOR_ASSIGN;
}
priv ParsePrimary(this: &mut Parser) -> *const ASTNode {
let token: Token = this.Current();
if token._type == TokenType::INTEGER || token._type == TokenType::FLOAT || token._type == TokenType::STRING || token._type == TokenType::CHAR || token._type == TokenType::TRUE || token._type == TokenType::FALSE || token._type == TokenType::NULL {
this.Advance();
ret new LiteralNode(token.value, token._type, token.row, token.column, this.file) as *const ASTNode;
} else if token._type == TokenType::IDENTIFIER || token._type == TokenType::OPERATOR || token._type == TokenType::DOUBLE_COLON {
let mut node: *const ASTNode = null;
if token._type != TokenType::DOUBLE_COLON {
if this.types.Index(token.value) != -1i64 && this.Peek(1u64)._type != TokenType::DOUBLE_COLON {
node = this.ParseType();
if node == null { ret null; }
} else {
if this.Match(TokenType::OPERATOR) {
let mut identifier: String = "op";
while this.Current()._type != TokenType::LPAREN && this.Current()._type != TokenType::LESS {
identifier += this.Current().value;
this.Advance();
}
node = new IdentifierNode(identifier, token.row, token.column, this.file) as *const ASTNode;
} else {
let identifier: Token = this.Expect(TokenType::IDENTIFIER);
if identifier._type == TokenType::UNKNOWN { ret null; }
node = new IdentifierNode(identifier.value, token.row, token.column, this.file) as *const ASTNode;
}
}
}
while this.Match(TokenType::DOUBLE_COLON) {
let mut identifier: *const IdentifierNode = null;
if this.Match(TokenType::OPERATOR) {
let mut _identifier: String = "op";
while this.Current()._type != TokenType::LPAREN && this.Current()._type != TokenType::LESS {
_identifier += this.Current().value;
this.Advance();
}
identifier = new IdentifierNode(_identifier, token.row, token.column, this.file);
} else {
let _identifier: Token = this.Expect(TokenType::IDENTIFIER);
if _identifier._type == TokenType::UNKNOWN { ret null; }
identifier = new IdentifierNode(_identifier.value, token.row, token.column, this.file);
} node = new ScopeResolutionNode(node, identifier, token.row, token.column, this.file) as *const ASTNode;
} ret node;
} else if token._type == TokenType::LBRACE {
this.Advance();
let initializer_list: *mut InitializerListNode = new InitializerListNode(token.row, token.column, this.file);
if !this.Match(TokenType::RBRACE) {
let expression: *const ASTNode = this.ParseExpression();
if expression == null { ret null; }
initializer_list->expressions.PushBack(expression);
while this.Match(TokenType::COMMA) {
let expression: *const ASTNode = this.ParseExpression();
if expression == null { ret null; }
initializer_list->expressions.PushBack(expression);
}
if !this.Expect2(TokenType::RBRACE) { ret null; }
} ret initializer_list as *const ASTNode;
} else if token._type == TokenType::LPAREN {
this.Advance();
let expression: *const ASTNode = this.ParseExpression();
if expression == null || !this.Expect2(TokenType::RPAREN) { ret null; }
ret expression;
} else {
this.Advance();
Logger::Log(this.file, String("Unexpected token '") + token.value + "'.", token, LogSeverity::Error);
ret null;
}
}
priv ParsePostfix(this: &mut Parser) -> *const ASTNode {
let mut expression: *const ASTNode = this.ParsePrimary();
if expression == null { ret null; }
loop {
let token: Token = this.Current();
if token._type == TokenType::LPAREN {
this.Advance();
let call: *mut CallNode = new CallNode(expression, token.row, token.column, this.file);
if !this.Match(TokenType::RPAREN) {
let expression: *const ASTNode = this.ParseExpression();
if expression == null { ret null; }
call->arguments.PushBack(expression);
while this.Match(TokenType::COMMA) {
let expression: *const ASTNode = this.ParseExpression();
if expression == null { ret null; }
call->arguments.PushBack(expression);
}
if !this.Expect2(TokenType::RPAREN) { ret null; }
} expression = call as *const ASTNode;
} else if token._type == TokenType::DOT || token._type == TokenType::ARROW {
this.Advance();
let member_access: *mut MemberAccessNode = new MemberAccessNode(expression, token._type == TokenType::ARROW, token.row, token.column, this.file);
if this.Match(TokenType::OPERATOR) {
member_access->member += "op";
while this.Current()._type != TokenType::LPAREN && this.Current()._type != TokenType::LESS {
member_access->member += this.Current().value;
this.Advance();
}
} else {
let identifier: Token = this.Expect(TokenType::IDENTIFIER);
if identifier._type == TokenType::UNKNOWN { ret null; }
member_access->member += identifier.value;
} expression = member_access as *const ASTNode;
} else if token._type == TokenType::LBRACKET {
this.Advance();
let _expression: *const ASTNode = this.ParseExpression();
if _expression == null { ret null; }
let index_access: *const IndexAccessNode = new IndexAccessNode(expression, _expression, token.row, token.column, this.file);
if !this.Expect2(TokenType::RBRACKET) { ret null; }
expression = index_access as *const ASTNode;
} else if token._type == TokenType::LBRACE && (expression->node_type == ASTNodeType::SCOPE_RESOLUTION || expression->node_type == ASTNodeType::IDENTIFIER) && this.IsStructInit(expression->node_type) {
this.Advance();
let struct_init: *mut StructInitNode = new StructInitNode(expression, token.row, token.column, this.file);
while this.Current()._type != TokenType::RBRACE && this.Current()._type != TokenType::EOF {
if this.Current()._type == TokenType::TWO_DOT {
this.Advance();
break;
}
let field_name: Token = this.Expect(TokenType::IDENTIFIER);
if field_name._type == TokenType::UNKNOWN { ret null; }
if this.Match(TokenType::COLON) {
let expression: *const ASTNode = this.ParseExpression();
if expression == null { ret null; }
struct_init->field_values.PushBack(expression);
} else {
struct_init->field_values.PushBack(new IdentifierNode(field_name.value, field_name.row, field_name.column, this.file) as *const ASTNode);
}
struct_init->field_names.PushBack(field_name.value);
if !this.Match(TokenType::COMMA) { break; }
}
if !this.Expect2(TokenType::RBRACE) { ret null; }
expression = struct_init as *const ASTNode;
} else {
break;
}
} ret expression;
}
priv ParseUnary(this: &mut Parser) -> *const ASTNode {
let token: Token = this.Current();
let mut mutable: bool = false;
if this.IsUnaryOperator(token._type) {
this.Advance();
if token._type == TokenType::BITWISE_AND {
mutable = this.Match(TokenType::MUT);
}
let expression: *const ASTNode = this.ParseUnary();
if expression == null { ret null; }
ret new UnaryOpNode(expression, token._type, mutable, token.row, token.column, this.file) as *const ASTNode;
} else if token._type == TokenType::NEW {
this.Advance();
let _type: *const ASTNode = this.ParseType();
if _type == null { ret null; }
let _new: *mut NewNode = new NewNode(_type, token.row, token.column, this.file);
if this.Match(TokenType::LBRACKET) {
_new->size = this.ParseExpression();
if _new->size == null || !this.Expect2(TokenType::RBRACKET) {
ret null;
}
} else if this.Match(TokenType::LPAREN) {
if !this.Match(TokenType::RPAREN) {
let argument: *const ASTNode = this.ParseExpression();
if argument == null { ret null; }
_new->arguments.PushBack(argument);
while this.Match(TokenType::COMMA) {
let argument: *const ASTNode = this.ParseExpression();
if argument == null { ret null; }
_new->arguments.PushBack(argument);
}
if !this.Expect2(TokenType::RPAREN) { ret null; }
}
} ret _new as *const ASTNode;
} else if token._type == TokenType::DELETE {
this.Advance();
let delete: *mut DeleteNode = new DeleteNode(token.row, token.column, this.file);
if this.Match(TokenType::LBRACKET) {
if !this.Expect2(TokenType::RBRACKET) { ret null; }
delete->array = true;
}
delete->expression = this.ParseExpression();
if delete->expression == null { ret null; }
ret delete as *const ASTNode;
} else if token._type == TokenType::AWAIT {
this.Advance();
let expression: *const ASTNode = this.ParseUnary();
if expression == null { ret null; }
ret new AwaitExprNode(expression, token.row, token.column, this.file) as *const ASTNode;
} else {
ret this.ParsePostfix();
}
}
priv ParseBinaryExpression(this: &mut Parser, min_precedence: i32) -> *const ASTNode {
let mut left: *const ASTNode = this.ParseUnary();
if left == null { ret null; }
loop {
let mut token: Token = this.Current();
if this.Peek(1u64)._type == token._type {
if token._type == TokenType::GREATER {
token._type = TokenType::RSHIFT;
this.Advance();
}
if token._type == TokenType::LESS {
token._type = TokenType::LSHIFT;
this.Advance();
}
}
let precedence: i32 = this.GetBinaryOpPrecedence(token._type);
if precedence < min_precedence { break; }
this.Advance();
if token._type == TokenType::AS {
let _type: *const ASTNode = this.ParseType();
if _type == null { ret null; }
left = new CastNode(left, _type, token.row, token.column, this.file) as *const ASTNode;
} else {
let right: *const ASTNode = this.ParseBinaryExpression(precedence + 1i32);
if right == null { ret null; }
left = new BinaryOpNode(left, right, token._type, token.row, token.column, this.file) as *const ASTNode;
}
} ret left;
}
priv ParseExpression(this: &mut Parser) -> *const ASTNode {
let mut condition: *const ASTNode = this.ParseBinaryExpression(0i32);
if condition == null { ret null; }
let token: Token = this.Current();
if token._type == TokenType::QUESTION_MARK {
this.Advance();
let then_expr: *const ASTNode = this.ParseExpression();
if then_expr == null || !this.Expect2(TokenType::COLON) { ret null; }
let else_expr: *const ASTNode = this.ParseExpression();
if else_expr == null { ret null; }
condition = new TernaryNode(condition, then_expr, else_expr, token.row, token.column, this.file) as *const ASTNode;
} ret condition;
}
priv ParseType(this: &mut Parser) -> *const ASTNode {
let mut references: Vector<bool>;
if this.Match(TokenType::BITWISE_AND) {
references.PushBack(this.Match(TokenType::MUT));
}
let mut pointers: Vector<bool>;
while this.Match(TokenType::MUL) {
if this.Match(TokenType::MUT) {
pointers.PushBack(true);
} else {
if !this.Expect2(TokenType::CONST) { ret null; }
pointers.PushBack(false);
}
}
let token: Token = this.Current();
let mut node: *const ASTNode = null;
if this.Match(TokenType::FUNC) {
if !this.Expect2(TokenType::LPAREN) { ret null; }
let func_type: *mut FunctionTypeNode = new FunctionTypeNode(token.row, token.column, this.file);
if this.Current()._type != TokenType::RPAREN {
func_type->var_arg = this.Match(TokenType::THREE_DOT);
if !func_type->var_arg {
let param: *const ParameterNode = this.ParseParameter();
if param == null { ret null; }
func_type->parameters.PushBack(param);
while this.Match(TokenType::COMMA) {
func_type->var_arg = this.Match(TokenType::THREE_DOT);
if func_type->var_arg { break; }
let param: *const ParameterNode = this.ParseParameter();
if param == null { ret null; }
func_type->parameters.PushBack(param);
}
}
}
if !this.Expect2(TokenType::RPAREN) { ret null; }