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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
|
/*
* abstract syntax tree types for a catskill source.
*
* Copyright (c) 2025, Mel G. <mel@rnrd.eu>
*
* SPDX-License-Identifier: MPL-2.0
*/
#pragma once
#include "catboot.h"
enum Unary_Operation
{
UNARY_NONE,
UNARY_MINUS,
UNARY_NOT,
UNARY_BITWISE_NOT,
};
enum Unary_Operation
unary_operation_from_token(const struct Token* token)
{
switch (token->kind) {
case TOKEN_MINUS:
return UNARY_MINUS;
case TOKEN_BANG:
return UNARY_NOT;
case TOKEN_TILDE:
return UNARY_BITWISE_NOT;
default:
return UNARY_NONE;
}
}
const ascii*
unary_operation_to_string(enum Unary_Operation operation)
{
switch (operation) {
case UNARY_MINUS:
return "-";
case UNARY_NOT:
return "!";
case UNARY_BITWISE_NOT:
return "~";
default:
failure("unexpected unary operation passed to `unary_operation_to_string`");
return nil;
}
}
enum Binary_Operation
{
BINARY_NONE,
BINARY_PLUS,
BINARY_MINUS,
BINARY_MULTIPLY,
BINARY_DIVIDE,
BINARY_MODULO,
BINARY_POWER,
BINARY_EQUAL,
BINARY_NOT_EQUAL,
BINARY_GREATER_THAN,
BINARY_GREATER_THAN_EQUAL,
BINARY_LESS_THAN,
BINARY_LESS_THAN_EQUAL,
BINARY_AND,
BINARY_OR,
BINARY_RANGE,
BINARY_BITWISE_AND,
BINARY_BITWISE_OR,
BINARY_BITWISE_XOR,
BINARY_BITWISE_LEFT_SHIFT,
BINARY_BITWISE_RIGHT_SHIFT,
BINARY_ASSIGN,
BINARY_ASSIGN_PLUS,
BINARY_ASSIGN_MINUS,
BINARY_ASSIGN_MULTIPLY,
BINARY_ASSIGN_DIVIDE,
BINARY_ASSIGN_MODULO,
BINARY_ASSIGN_AND,
BINARY_ASSIGN_OR,
BINARY_ASSIGN_BITWISE_AND,
BINARY_ASSIGN_BITWISE_OR,
BINARY_ASSIGN_BITWISE_XOR,
BINARY_ASSIGN_BITWISE_LEFT_SHIFT,
BINARY_ASSIGN_BITWISE_RIGHT_SHIFT,
};
enum Binary_Operation
binary_operation_from_token(const struct Token* token)
{
switch (token->kind) {
case TOKEN_PLUS:
return BINARY_PLUS;
case TOKEN_MINUS:
return BINARY_MINUS;
case TOKEN_STAR:
return BINARY_MULTIPLY;
case TOKEN_SLASH:
return BINARY_DIVIDE;
case TOKEN_PERCENT:
return BINARY_MODULO;
case TOKEN_STAR_STAR:
return BINARY_POWER;
case TOKEN_EQUAL:
return BINARY_EQUAL;
case TOKEN_NOT_EQUAL:
return BINARY_NOT_EQUAL;
case TOKEN_GREATER:
return BINARY_GREATER_THAN;
case TOKEN_GREATER_EQUAL:
return BINARY_GREATER_THAN_EQUAL;
case TOKEN_LESS:
return BINARY_LESS_THAN;
case TOKEN_LESS_EQUAL:
return BINARY_LESS_THAN_EQUAL;
case TOKEN_AND:
return BINARY_AND;
case TOKEN_OR:
return BINARY_OR;
case TOKEN_DOT_DOT:
return BINARY_RANGE;
case TOKEN_AMPERSAND:
return BINARY_BITWISE_AND;
case TOKEN_PIPE:
return BINARY_BITWISE_OR;
case TOKEN_CARET:
return BINARY_BITWISE_XOR;
case TOKEN_LEFT_SHIFT:
return BINARY_BITWISE_LEFT_SHIFT;
case TOKEN_RIGHT_SHIFT:
return BINARY_BITWISE_RIGHT_SHIFT;
case TOKEN_ASSIGN:
return BINARY_ASSIGN;
case TOKEN_ASSIGN_PLUS:
return BINARY_ASSIGN_PLUS;
case TOKEN_ASSIGN_MINUS:
return BINARY_ASSIGN_MINUS;
case TOKEN_ASSIGN_STAR:
return BINARY_ASSIGN_MULTIPLY;
case TOKEN_ASSIGN_SLASH:
return BINARY_ASSIGN_DIVIDE;
case TOKEN_ASSIGN_PERCENT:
return BINARY_ASSIGN_MODULO;
case TOKEN_ASSIGN_AND:
return BINARY_ASSIGN_AND;
case TOKEN_ASSIGN_OR:
return BINARY_ASSIGN_OR;
case TOKEN_ASSIGN_AMPERSAND:
return BINARY_ASSIGN_BITWISE_AND;
case TOKEN_ASSIGN_PIPE:
return BINARY_ASSIGN_BITWISE_OR;
case TOKEN_ASSIGN_CARET:
return BINARY_ASSIGN_BITWISE_XOR;
case TOKEN_ASSIGN_LEFT_SHIFT:
return BINARY_ASSIGN_BITWISE_LEFT_SHIFT;
case TOKEN_ASSIGN_RIGHT_SHIFT:
return BINARY_ASSIGN_BITWISE_RIGHT_SHIFT;
default:
return BINARY_NONE;
}
}
// return the precedence of the given binary operation.
// lower values are weaker, with 1 being the weakest.
// the values are taken mostly the same as other C-family languages.
uint
binary_operation_precedence(enum Binary_Operation operation)
{
switch (operation) {
// weakest
case BINARY_ASSIGN:
case BINARY_ASSIGN_PLUS:
case BINARY_ASSIGN_MINUS:
case BINARY_ASSIGN_MULTIPLY:
case BINARY_ASSIGN_DIVIDE:
case BINARY_ASSIGN_MODULO:
case BINARY_ASSIGN_AND:
case BINARY_ASSIGN_OR:
case BINARY_ASSIGN_BITWISE_AND:
case BINARY_ASSIGN_BITWISE_OR:
case BINARY_ASSIGN_BITWISE_XOR:
case BINARY_ASSIGN_BITWISE_LEFT_SHIFT:
case BINARY_ASSIGN_BITWISE_RIGHT_SHIFT:
return 1;
case BINARY_RANGE:
return 2;
case BINARY_OR:
return 3;
case BINARY_AND:
return 4;
case BINARY_BITWISE_OR:
return 5;
case BINARY_BITWISE_XOR:
return 6;
case BINARY_BITWISE_AND:
return 7;
case BINARY_EQUAL:
case BINARY_NOT_EQUAL:
return 8;
case BINARY_GREATER_THAN:
case BINARY_GREATER_THAN_EQUAL:
case BINARY_LESS_THAN:
case BINARY_LESS_THAN_EQUAL:
return 9;
case BINARY_BITWISE_LEFT_SHIFT:
case BINARY_BITWISE_RIGHT_SHIFT:
return 10;
case BINARY_PLUS:
case BINARY_MINUS:
return 11;
case BINARY_MULTIPLY:
case BINARY_DIVIDE:
case BINARY_MODULO:
return 12;
case BINARY_POWER:
return 13;
// strongest
default:
failure("unexpected binary operation passed to `binary_operation_precedence`");
return 0;
}
}
enum Binary_Operation_Associativity
{
BINARY_ASSOCIATIVITY_NONE,
BINARY_ASSOCIATIVITY_LEFT,
BINARY_ASSOCIATIVITY_RIGHT,
};
enum Binary_Operation_Associativity
binary_operation_associativity(enum Binary_Operation operation)
{
switch (operation) {
case BINARY_ASSIGN:
case BINARY_ASSIGN_PLUS:
case BINARY_ASSIGN_MINUS:
case BINARY_ASSIGN_MULTIPLY:
case BINARY_ASSIGN_DIVIDE:
case BINARY_ASSIGN_MODULO:
case BINARY_ASSIGN_AND:
case BINARY_ASSIGN_OR:
case BINARY_ASSIGN_BITWISE_AND:
case BINARY_ASSIGN_BITWISE_OR:
case BINARY_ASSIGN_BITWISE_XOR:
case BINARY_ASSIGN_BITWISE_LEFT_SHIFT:
case BINARY_POWER:
return BINARY_ASSOCIATIVITY_RIGHT;
case BINARY_RANGE:
return BINARY_ASSOCIATIVITY_NONE;
default:
return BINARY_ASSOCIATIVITY_LEFT;
}
}
const ascii*
binary_operation_to_string(enum Binary_Operation operation)
{
switch (operation) {
case BINARY_PLUS:
return "+";
case BINARY_MINUS:
return "-";
case BINARY_MULTIPLY:
return "*";
case BINARY_DIVIDE:
return "/";
case BINARY_MODULO:
return "%";
case BINARY_POWER:
return "**";
case BINARY_EQUAL:
return "==";
case BINARY_NOT_EQUAL:
return "!=";
case BINARY_GREATER_THAN:
return ">";
case BINARY_GREATER_THAN_EQUAL:
return ">=";
case BINARY_LESS_THAN:
return "<";
case BINARY_LESS_THAN_EQUAL:
return "<=";
case BINARY_AND:
return "&&";
case BINARY_OR:
return "||";
case BINARY_RANGE:
return "..";
case BINARY_BITWISE_AND:
return "&";
case BINARY_BITWISE_OR:
return "|";
case BINARY_BITWISE_XOR:
return "^";
case BINARY_BITWISE_LEFT_SHIFT:
return "<<";
case BINARY_BITWISE_RIGHT_SHIFT:
return ">>";
case BINARY_ASSIGN:
return "=";
case BINARY_ASSIGN_PLUS:
return "+=";
case BINARY_ASSIGN_MINUS:
return "-=";
case BINARY_ASSIGN_MULTIPLY:
return "*=";
case BINARY_ASSIGN_DIVIDE:
return "/=";
case BINARY_ASSIGN_MODULO:
return "%=";
case BINARY_ASSIGN_AND:
return "&&=";
case BINARY_ASSIGN_OR:
return "||=";
case BINARY_ASSIGN_BITWISE_AND:
return "&=";
case BINARY_ASSIGN_BITWISE_OR:
return "|=";
case BINARY_ASSIGN_BITWISE_XOR:
return "^=";
case BINARY_ASSIGN_BITWISE_LEFT_SHIFT:
return "<<=";
case BINARY_ASSIGN_BITWISE_RIGHT_SHIFT:
return ">>=";
default:
failure("unexpected binary operation passed to `binary_operation_to_string`");
return nil;
}
}
enum Increment_Decrement_Operation
{
INCREMENT_DECREMENT_NONE,
INCREMENT_DECREMENT_INCREMENT,
INCREMENT_DECREMENT_DECREMENT,
};
enum Increment_Decrement_Operation
increment_decrement_operation_from_token(const struct Token* token)
{
switch (token->kind) {
case TOKEN_PLUS_PLUS:
return INCREMENT_DECREMENT_INCREMENT;
case TOKEN_MINUS_MINUS:
return INCREMENT_DECREMENT_DECREMENT;
default:
return INCREMENT_DECREMENT_NONE;
}
}
const ascii*
increment_decrement_operation_to_string(enum Increment_Decrement_Operation operation)
{
switch (operation) {
case INCREMENT_DECREMENT_INCREMENT:
return "++";
case INCREMENT_DECREMENT_DECREMENT:
return "--";
default:
failure("unexpected increment/decrement operation passed to "
"`increment_decrement_operation_to_string`");
return nil;
}
}
// nodes are parts of the syntax tree that are reused often
// and in different places.
// a block of code, enclosed in curly braces.
// represents a sequence of statements that are executed in order.
struct Block_Node
{
struct Statement* statements;
struct Span span;
struct Cursor location;
};
void block_node_print(const struct Block_Node* block);
// a function header, describing the parameters and return type of function.
// used both as a type and in full function definitions.
struct Function_Header_Node
{
// linked list of parameters.
// name, if given, is included in the type node.
struct Type_Node* parameters_type_and_name;
struct Type_Node* return_type;
struct Span span;
};
void function_header_node_print(const struct Function_Header_Node* header);
// a declaration of a variable, constant, or other binding, without a mutability
// signifier, like `let` or `var`.
// the mutability is determined by some outside context, where
// a bare declaration in a for-loop, for example, is always mutable.
struct Bare_Declaration_Node
{
struct String_Array names;
struct Expression* initializer;
struct Type_Node* type;
struct Span span;
struct Cursor location;
};
void bare_declaration_node_print(const struct Bare_Declaration_Node* declaration);
enum Type_Node_Type
{
TYPE_NODE_NONE,
TYPE_NODE_NAME,
TYPE_NODE_ARRAY, // an array of a type, `[int]`.
TYPE_NODE_REFERENCE, // a reference to a type, `&int`.
TYPE_NODE_MAYBE, // a type that may be null, `int?`.
TYPE_NODE_TUPLE, // a tuple type, `(int string)`.
TYPE_NODE_MAP, // a map type, `[string = int]`.
TYPE_NODE_FUNCTION, // a function type, `fun (int) int`.
TYPE_NODE_STRUCTURE, // a struct, invoked either with `type` or with `{}` when a type is inline.
TYPE_NODE_VARIANT, // a tagged union.
TYPE_NODE_CLASS, // a class of types, a.k.a. an interface.
};
struct Type_Node_Name
{
struct String name;
};
struct Type_Node_Array
{
struct Type_Node* element_type;
};
struct Type_Node_Reference
{
struct Type_Node* referenced_type;
};
struct Type_Node_Maybe
{
struct Type_Node* inner_type;
};
struct Type_Node_Tuple
{
struct Type_Node* head; // the first type in the tuple, if any.
};
struct Type_Node_Map
{
struct Type_Node* key_type;
struct Type_Node* value_type;
};
struct Type_Node_Function
{
struct Function_Header_Node header;
};
struct Type_Node_Structure
{
// the fields of the structure, linked list of types and (required) names.
struct Type_Node* fields;
};
struct Type_Node_Variant
{
// the variants of the tagged union, linked list of (required) variant names and backing types.
// if a variant has no backing type, it is TYPE_NODE_NONE.
struct Type_Node* variants;
};
struct Type_Node_Class
{
// linked list of the types of methods required to implement the class.
// each node is required to have a name and be of TYPE_NODE_FUNCTION.
struct Type_Node* methods;
};
union Type_Node_Value
{
struct Type_Node_Name name;
struct Type_Node_Array array;
struct Type_Node_Reference reference;
struct Type_Node_Maybe maybe;
struct Type_Node_Tuple tuple;
struct Type_Node_Map map;
struct Type_Node_Function function;
struct Type_Node_Structure structure;
struct Type_Node_Variant variant;
struct Type_Node_Class class;
};
// a type node represents a type in the syntax tree.
// currently, we only support types that are simple names.
// or null types.
// also includes the name of the field, member, parameter, etc., for which
// the type is defined.
struct Type_Node
{
enum Type_Node_Type type;
union Type_Node_Value value;
// note: we could also just include the token here i think?
struct Span span;
struct Cursor location;
// usually a type is preceded by the name of the binding
// it is assigned to, e.g. `x int`, `string y`, etc.
// this is the name of that binding, for ease of listing.
struct String value_name;
// if type is within a group of multiple types,
// points to the next type within the group.
struct Type_Node* next;
};
REGION(struct Type_Node, type_node)
// allocates a new type node in the global type node region.
struct Type_Node*
type_node_new(
enum Type_Node_Type type, union Type_Node_Value value, struct Span span, struct Cursor location)
{
check(region_type_node_cursor < REGION_SIZE, "out of type node memory");
struct Type_Node* type_node = ®ion_type_node[region_type_node_cursor++];
*type_node = (struct Type_Node){
.type = type,
.value = value,
.span = span,
.location = location,
.value_name = string_empty(),
.next = nil,
};
return type_node;
}
// allocates a new type node with no value, used for `none` types.
// this is used for types that are not specified, note that it is still
// fully allocated and can be used in the syntax tree.
struct Type_Node*
type_node_none(struct Span span, struct Cursor location)
{
return type_node_new(TYPE_NODE_NONE, (union Type_Node_Value){ 0 }, span, location);
}
bool
type_node_is_none(const struct Type_Node* type_node)
{
return type_node->type == TYPE_NODE_NONE;
}
void
type_node_print(const struct Type_Node* type_node)
{
printf("(type ");
switch (type_node->type) {
case TYPE_NODE_NONE:
printf("none");
break;
case TYPE_NODE_NAME:
printf("name %s", type_node->value.name.name.data);
break;
case TYPE_NODE_ARRAY:
printf("array of ");
type_node_print(type_node->value.array.element_type);
break;
case TYPE_NODE_REFERENCE:
printf("reference to ");
type_node_print(type_node->value.reference.referenced_type);
break;
case TYPE_NODE_MAYBE:
printf("maybe ");
type_node_print(type_node->value.maybe.inner_type);
break;
case TYPE_NODE_TUPLE: {
printf("tuple");
FOR_EACH (struct Type_Node*, current, type_node->value.tuple.head) {
printf(" ");
type_node_print(current);
}
break;
}
case TYPE_NODE_MAP:
printf("map ");
type_node_print(type_node->value.map.key_type);
printf(" = ");
type_node_print(type_node->value.map.value_type);
break;
case TYPE_NODE_FUNCTION: {
printf("function ");
function_header_node_print(&type_node->value.function.header);
break;
}
case TYPE_NODE_STRUCTURE: {
printf("structure");
FOR_EACH (struct Type_Node*, current, type_node->value.structure.fields) {
printf(" (field %s) ", current->value_name.data);
type_node_print(current);
}
break;
}
case TYPE_NODE_VARIANT: {
printf("variant");
FOR_EACH (struct Type_Node*, current, type_node->value.variant.variants) {
if (type_node_is_none(current)) {
printf(" (variant %s)", current->value_name.data);
} else {
printf(" (variant %s of ", current->value_name.data);
type_node_print(current);
printf(")");
}
}
break;
}
case TYPE_NODE_CLASS:
printf("class");
FOR_EACH (struct Type_Node*, current, type_node->value.class.methods) {
check(current->type == TYPE_NODE_FUNCTION,
"expected class method type node to be a function type");
printf(" (method %s ", current->value_name.data);
function_header_node_print(¤t->value.function.header);
printf(")");
}
break;
}
printf(")");
}
enum Expression_Kind
{
EXPRESSION_NONE,
EXPRESSION_INTEGER_LITERAL,
EXPRESSION_FLOAT_LITERAL,
EXPRESSION_STRING_LITERAL,
EXPRESSION_BOOLEAN_LITERAL,
EXPRESSION_NAME,
EXPRESSION_UNARY_OPERATION,
EXPRESSION_BINARY_OPERATION,
EXPRESSION_GROUP,
EXPRESSION_CALL_OR_CONSTRUCT,
EXPRESSION_SUBSCRIPT,
EXPRESSION_MEMBER,
EXPRESSION_INCREMENT_DECREMENT,
EXPRESSION_FUNCTION,
EXPRESSION_TYPE,
};
struct Expression_Integer_Literal
{
int64 value; // might not fit entire number given in source.
};
struct Expression_Float_Literal
{
float64 value;
};
struct Expression_String_Literal
{
struct String value;
};
struct Expression_Bool_Literal
{
bool value;
};
struct Expression_Name
{
struct String name;
};
struct Expression_Unary_Operator
{
enum Unary_Operation operation;
struct Expression* operand;
};
struct Expression_Binary_Operator
{
enum Binary_Operation operation;
struct Expression* left_operand;
struct Expression* right_operand;
};
struct Expression_Group
{
struct Expression* inner_expression;
};
struct Expression_Call_Or_Construct
{
struct Expression* subject;
// linked list of argument expressions.
struct Expression* arguments;
// names of the arguments, if given.
// an unnamed argument is represented as an empty string.
struct String_Array argument_names;
};
struct Expression_Subscript
{
struct Expression* subject;
struct Expression* index;
};
struct Expression_Member
{
struct Expression* subject;
struct String name;
};
struct Expression_Increment_Decrement
{
// whether the increment/decrement is a prefix or postfix operation.
bool prefix;
struct Expression* subject;
enum Increment_Decrement_Operation operation;
};
struct Expression_Function
{
struct Function_Header_Node header;
struct Block_Node body;
};
struct Expression_Type
{
struct Type_Node* type;
};
union Expression_Value
{
struct Expression_Integer_Literal integer_literal;
struct Expression_Float_Literal float_literal;
struct Expression_String_Literal string_literal;
struct Expression_Bool_Literal bool_literal;
struct Expression_Name name;
struct Expression_Unary_Operator unary_operator;
struct Expression_Binary_Operator binary_operator;
struct Expression_Group group;
struct Expression_Call_Or_Construct call_or_construct;
struct Expression_Subscript subscript;
struct Expression_Member member;
struct Expression_Increment_Decrement increment_decrement;
struct Expression_Function function;
struct Expression_Type type;
};
struct Expression
{
enum Expression_Kind kind;
union Expression_Value value;
struct Span span;
struct Cursor location;
// if expression is within a group of multiple expressions,
// points to the next expression within it.
struct Expression* next;
};
REGION(struct Expression, expression)
struct Expression*
expression_new(
enum Expression_Kind kind, union Expression_Value value, struct Span span,
struct Cursor location)
{
check(region_expression_cursor < REGION_SIZE, "out of expression memory");
struct Expression* expression = ®ion_expression[region_expression_cursor++];
*expression = (struct Expression){
.kind = kind,
.value = value,
.span = span,
.location = location,
.next = nil,
};
return expression;
}
void
function_header_node_print(const struct Function_Header_Node* header)
{
FOR_EACH (struct Type_Node*, current, header->parameters_type_and_name) {
if (current != header->parameters_type_and_name) printf(" ");
if (!string_is_empty(current->value_name))
printf("(param %s) ", current->value_name.data);
else
printf("(param) ");
type_node_print(current);
}
if (header->return_type) {
printf(" (returns ");
type_node_print(header->return_type);
printf(")");
}
}
void
expression_print(const struct Expression* expression)
{
printf("(expr ");
switch (expression->kind) {
case EXPRESSION_NONE:
printf("none");
break;
case EXPRESSION_INTEGER_LITERAL:
printf("%ld", expression->value.integer_literal.value);
break;
case EXPRESSION_FLOAT_LITERAL:
printf("%lf", expression->value.float_literal.value);
break;
case EXPRESSION_STRING_LITERAL:
printf("\"%s\"", expression->value.string_literal.value.data);
break;
case EXPRESSION_BOOLEAN_LITERAL:
printf("%s", expression->value.bool_literal.value ? "true" : "false");
break;
case EXPRESSION_NAME:
printf("(name %s)", expression->value.name.name.data);
break;
case EXPRESSION_UNARY_OPERATION:
printf("(unary %s ", unary_operation_to_string(expression->value.unary_operator.operation));
expression_print(expression->value.unary_operator.operand);
printf(")");
break;
case EXPRESSION_BINARY_OPERATION:
printf(
"(binary %s ", binary_operation_to_string(expression->value.binary_operator.operation));
expression_print(expression->value.binary_operator.left_operand);
printf(" ");
expression_print(expression->value.binary_operator.right_operand);
printf(")");
break;
case EXPRESSION_GROUP:
printf("(group ");
expression_print(expression->value.group.inner_expression);
printf(")");
break;
case EXPRESSION_CALL_OR_CONSTRUCT: {
const struct Expression_Call_Or_Construct* coc = &expression->value.call_or_construct;
printf("(call/construct ");
expression_print(coc->subject);
uint i = 0;
FOR_EACH (struct Expression*, argument, coc->arguments) {
struct String name = string_array_at(&coc->argument_names, i++);
if (!string_is_empty(name)) {
printf(" (named arg '%s' ", name.data);
} else {
printf(" (arg ");
}
expression_print(argument);
printf(")");
}
printf(")");
break;
}
case EXPRESSION_SUBSCRIPT:
printf("(subscript ");
expression_print(expression->value.subscript.subject);
printf(" ");
expression_print(expression->value.subscript.index);
printf(")");
break;
case EXPRESSION_MEMBER:
printf("(member of ");
expression_print(expression->value.member.subject);
printf(" named %s", expression->value.member.name.data);
printf(")");
break;
case EXPRESSION_INCREMENT_DECREMENT: {
const struct Expression_Increment_Decrement* inc_dec =
&expression->value.increment_decrement;
const ascii* prefix_or_postfix = inc_dec->prefix ? "prefix" : "postfix";
printf("(increment/decrement %s %s ",
increment_decrement_operation_to_string(inc_dec->operation), prefix_or_postfix);
expression_print(inc_dec->subject);
break;
}
case EXPRESSION_FUNCTION: {
const struct Expression_Function* fun = &expression->value.function;
printf("(function ");
function_header_node_print(&fun->header);
printf(" ");
block_node_print(&fun->body);
printf(")");
break;
}
case EXPRESSION_TYPE:
type_node_print(expression->value.type.type);
break;
default:
failure("unexpected expression kind passed to `expression_print`");
break;
}
printf(")");
}
enum Statement_Kind
{
STATEMENT_NONE,
STATEMENT_EXPRESSION,
STATEMENT_DECLARATION,
// NOTE: a block could be an expression in the future.
STATEMENT_BLOCK,
STATEMENT_CONDITIONAL,
STATEMENT_LOOP,
STATEMENT_RETURN,
STATEMENT_BREAK,
STATEMENT_CONTINUE,
STATEMENT_DEFER,
};
struct Statement_Value_Expression
{
struct Expression* inner;
};
enum Statement_Declaration_Kind
{
STATEMENT_DECLARATION_NONE,
STATEMENT_DECLARATION_VARIABLE,
STATEMENT_DECLARATION_CONSTANT,
};
enum Statement_Declaration_Kind
statement_declaration_kind_from_token(const struct Token* token)
{
switch (token->kind) {
case TOKEN_WORD_VAR:
return STATEMENT_DECLARATION_VARIABLE;
case TOKEN_WORD_LET:
return STATEMENT_DECLARATION_CONSTANT;
default:
return STATEMENT_DECLARATION_NONE;
}
}
struct Statement_Value_Declaration
{
enum Statement_Declaration_Kind kind;
struct Bare_Declaration_Node inner;
};
struct Statement_Value_Block
{
struct Block_Node inner; // the block of statements.
};
#define STATEMENT_VALUE_CONDITIONAL_MAX 8
struct Statement_Value_Conditional
{
struct Statement_Conditional_Branch
{
// if nil, the condition is always true.
struct Expression* when;
struct Block_Node then;
} conditions[STATEMENT_VALUE_CONDITIONAL_MAX];
uint condition_count;
};
enum Statement_Loop_Style
{
STATEMENT_LOOP_STYLE_NONE,
STATEMENT_LOOP_STYLE_C, // for i int = 0; i < 10; ++i {}
STATEMENT_LOOP_STYLE_FOR_EACH, // for x Obj = list {}
STATEMENT_LOOP_STYLE_WHILE, // while true {}
STATEMENT_LOOP_STYLE_ENDLESS, // while {}
};
// stands for both `for` and `while` loops.
struct Statement_Value_Loop
{
enum Statement_Loop_Style style;
struct Bare_Declaration_Node declaration;
struct Expression* condition;
struct Expression* iteration;
struct Block_Node body;
};
struct Statement_Value_Return
{
// nil if there is no return value.
struct Expression* value;
};
struct Statement_Value_Defer
{
// either a simple expression, or, if expression is nil,
// a block of code to execute.
struct Expression* expression;
struct Block_Node block;
};
union Statement_Value
{
struct Statement_Value_Expression expression;
struct Statement_Value_Declaration declaration;
struct Statement_Value_Block block;
struct Statement_Value_Conditional conditional;
struct Statement_Value_Loop loop;
struct Statement_Value_Return return_value;
struct Statement_Value_Defer defer;
};
struct Statement
{
enum Statement_Kind kind;
union Statement_Value value;
struct Span span;
struct Cursor location;
// if statement is within a group of multiple statements,
// points to the next statement within it.
struct Statement* next;
};
REGION(struct Statement, statement)
void statement_print(const struct Statement* statement);
void
block_node_print(const struct Block_Node* block)
{
printf("(block \n");
FOR_EACH (struct Statement*, statement, block->statements) {
printf("\t");
statement_print(statement);
printf("\n");
}
printf(")");
}
void
bare_declaration_node_print(const struct Bare_Declaration_Node* declaration)
{
printf("(declaration ");
STRING_ARRAY_FOR_EACH (i, name, declaration->names) { printf("%s ", name.data); }
if (!type_node_is_none(declaration->type)) {
type_node_print(declaration->type);
printf(" ");
}
if (declaration->initializer) {
printf("(initializer ");
expression_print(declaration->initializer);
printf(")");
}
printf(")");
}
struct Statement*
statement_new(
enum Statement_Kind kind, union Statement_Value value, struct Span span, struct Cursor location)
{
check(region_statement_cursor < REGION_SIZE, "out of statement memory");
struct Statement* statement = ®ion_statement[region_statement_cursor++];
*statement = (struct Statement){
.kind = kind,
.value = value,
.span = span,
.location = location,
.next = nil,
};
return statement;
}
void
statement_print(const struct Statement* statement)
{
printf("(stmt ");
switch (statement->kind) {
case STATEMENT_NONE:
printf("none");
break;
case STATEMENT_EXPRESSION: {
const struct Expression* expression = statement->value.expression.inner;
expression_print(expression);
break;
}
case STATEMENT_DECLARATION: {
if (statement->value.declaration.kind == STATEMENT_DECLARATION_VARIABLE)
printf("(variable ");
else if (statement->value.declaration.kind == STATEMENT_DECLARATION_CONSTANT)
printf("(constant ");
bare_declaration_node_print(&statement->value.declaration.inner);
printf(")");
break;
}
case STATEMENT_BLOCK:
block_node_print(&statement->value.block.inner);
break;
case STATEMENT_CONDITIONAL: {
printf("(conditional");
for (uint i = 0; i < statement->value.conditional.condition_count; ++i) {
const struct Statement_Conditional_Branch* branch =
&statement->value.conditional.conditions[i];
printf(" ");
if (branch->when) {
printf("(when ");
expression_print(branch->when);
printf(") ");
} else {
printf("(always) ");
}
block_node_print(&branch->then);
}
printf(")");
break;
}
case STATEMENT_LOOP: {
printf("(loop ");
switch (statement->value.loop.style) {
case STATEMENT_LOOP_STYLE_C:
printf("c-style ");
bare_declaration_node_print(&statement->value.loop.declaration);
printf(" (condition ");
expression_print(statement->value.loop.condition);
printf(") (iteration ");
expression_print(statement->value.loop.iteration);
printf(") ");
break;
case STATEMENT_LOOP_STYLE_FOR_EACH:
printf("for-each ");
bare_declaration_node_print(&statement->value.loop.declaration);
printf(" ");
break;
case STATEMENT_LOOP_STYLE_WHILE:
printf("while (condition ");
expression_print(statement->value.loop.condition);
printf(") ");
break;
case STATEMENT_LOOP_STYLE_ENDLESS:
printf("endless ");
break;
default:
failure("unexpected loop style in `statement_print`");
break;
}
block_node_print(&statement->value.loop.body);
printf(")");
break;
}
case STATEMENT_RETURN: {
printf("(return");
if (statement->value.return_value.value) {
printf(" ");
expression_print(statement->value.return_value.value);
}
printf(")");
break;
}
case STATEMENT_BREAK:
printf("(break)");
break;
case STATEMENT_CONTINUE:
printf("(continue)");
break;
case STATEMENT_DEFER: {
printf("(defer ");
if (statement->value.defer.expression) {
expression_print(statement->value.defer.expression);
} else {
block_node_print(&statement->value.defer.block);
}
printf(")");
break;
}
default:
failure("unexpected statement kind passed to `statement_print`");
break;
}
printf(")");
}
// the top-level tree of a single catskill source file.
struct Tree
{
struct Statement* top_level_statements;
};
void
tree_print(const struct Tree* tree)
{
FOR_EACH (struct Statement*, statement, tree->top_level_statements) {
statement_print(statement);
printf("\n");
}
}
|