-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
3575 lines (3263 loc) · 94.9 KB
/
Copy pathmain.c
File metadata and controls
3575 lines (3263 loc) · 94.9 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#define len(x) (sizeof(x) / sizeof(*x))
#define list(name, T, N) typedef struct { T begin[N]; int size; } name
#define list_size(x) ((x)->size)
#define list_empty(x) (list_size(x) == 0)
#define list_last(x) (list_size(x) - 1)
#define list_begin(x) ((x)->begin)
#define list_cap(x) (len(list_begin(x)) - 1)
#define list_full(x) (list_size(x) == list_cap(x))
#define list_push(x, v) if(list_full(x)) quit("%s: out of memory", __func__); list_begin(x)[list_size(x)++] = v
static constexpr char g_program[] = "nibble";
static constexpr auto g_str_size = 128;
static constexpr auto g_list_size = 32;
static constexpr auto g_big_list_size = 1024;
static constexpr auto g_code_size = 65536;
static constexpr auto g_modules = 8;
typedef enum : int
{
g_precedence_arithmetic_0,
g_precedence_arithmetic_1,
g_precedence_shift,
g_precedence_relational_0,
g_precedence_relational_1,
g_precedence_bitwise_and,
g_precedence_bitwise_xor,
g_precedence_bitwise_or,
g_precedence_assignment,
g_precedence_count,
}
precedence_t;
typedef enum : int
{
g_scope_function,
g_scope_while,
g_scope_if,
}
scope_t;
typedef enum : int
{
g_power_1,
g_power_8,
g_power_16,
g_power_32,
g_power_64,
g_power_float,
g_power_double,
}
type_power_t;
typedef struct
{
scope_t scope;
int at;
int block;
}
defer_t;
list(str_t, char, g_str_size);
typedef struct
{
str_t name;
int stars;
bool is_function_pointer;
bool is_function;
bool must_skip_arg_check;
bool is_variadic;
bool is_prototype;
}
type_t;
typedef struct
{
str_t name;
int size;
}
str_const_t;
list(slot_list_t, int, g_list_size);
list(defer_list_t, defer_t, g_list_size);
list(type_list_t, type_t, g_list_size);
list(str_list_t, str_t, g_list_size);
typedef struct
{
type_t type;
str_t name;
int slot;
bool is_lvalue;
}
value_t;
typedef struct
{
str_t name;
type_list_t types;
str_list_t names;
str_list_t init;
slot_list_t slots;
bool is_function;
bool is_type;
}
aggregate_t;
list(char_list_t, char, g_code_size);
typedef struct
{
char_list_t list;
int at;
int line;
str_t path;
}
code_t;
list(value_big_list_t, value_t, g_big_list_size);
list(aggregate_big_list_t, aggregate_t, g_big_list_size);
list(str_const_big_list_t, str_const_t, g_big_list_size);
list(code_list_t, code_t, g_modules);
struct
{
code_list_t codes;
defer_list_t defers;
slot_list_t loop_again;
slot_list_t loop_end;
value_big_list_t values;
aggregate_big_list_t aggregates;
str_const_big_list_t str_consts;
int block;
int slot;
int tabs;
int label;
int module;
}
g_file;
typedef struct
{
int if_label;
int else_label;
int end_label;
int block;
}
branch_t;
static char* const g_escape_alert = "\a";
static char* const g_escape_backspace = "\b";
static char* const g_escape_form_feed = "\f";
static char* const g_escape_newline = "\n";
static char* const g_escape_carriage_return = "\r";
static char* const g_escape_tab = "\t";
static char* const g_escape_vertical_tab = "\v";
static char* const g_escape_backslash = "\\";
static char* const g_escape_apostrophe = "\'";
static char* const g_escape_quotation = "\"";
static char* const g_escape_question_mark = "\?";
static char* const g_escape_null = "\0";
static char* const g_llvm_escape_alert = "\\07";
static char* const g_llvm_escape_backspace = "\\08";
static char* const g_llvm_escape_tab = "\\09";
static char* const g_llvm_escape_newline = "\\0A";
static char* const g_llvm_escape_vertical_tab = "\\0B";
static char* const g_llvm_escape_form_feed = "\\0C";
static char* const g_llvm_escape_carriage_return = "\\0D";
static char* const g_llvm_escape_quotation = "\\22";
static char* const g_llvm_escape_null = "\\00";
static char* const g_llvm_escape_backslash = "\\5C";
static char* const g_llvm_escape_question_mark = "\\3F";
static char* const g_apostrophe = "'";
static char* const g_str = "%s";
static char* const g_red = "\033[1;31m";
static char* const g_green = "\033[1;32m";
static char* const g_white = "\033[1;37m";
static char* const g_normal = "\033[0m";
static char* const g_underscore = "_";
static char* const g_left_square = "[";
static char* const g_rite_square = "]";
static char* const g_left_curl = "{";
static char* const g_rite_curl = "}";
static char* const g_left_paren = "(";
static char* const g_rite_paren = ")";
static char* const g_semicolon = ";";
static char* const g_space = " ";
static char* const g_lower_begin = "a";
static char* const g_lower_end = "z";
static char* const g_upper_begin = "A";
static char* const g_upper_end = "Z";
static char* const g_digit_begin = "0";
static char* const g_digit_end = "9";
static char* const g_ellipses = "...";
static char* const g_not = "!";
static char* const g_type_cast = "<>";
static char* const g_function = "()";
static char* const g_index = "[]";
static char* const g_dot = ".";
static char* const g_add = "+";
static char* const g_subtract = "-";
static char* const g_divide = "/";
static char* const g_mod = "%";
static char* const g_multiply = "*";
static char* const g_equals = "=";
static char* const g_equal_to = "==";
static char* const g_not_equal_to = "!=";
static char* const g_less_equal_to = "<=";
static char* const g_less = "<";
static char* const g_greater_equal_to = ">=";
static char* const g_greater = ">";
static char* const g_shift_rite = ">>";
static char* const g_shift_left = "<<";
static char* const g_increment = "++";
static char* const g_decrement = "--";
static char* const g_bitwise_or = "|";
static char* const g_bitwise_xor = "^";
static char* const g_bitwise_and = "&";
static char* const g_bitwise_not = "~";
static char* const g_comma = ",";
static char* const g_ampersand = "&";
static char* const g_percent = "%";
static char* const g_hash = "#";
static char* const g_empty = "";
static char* const g_colon = ":";
static char* const g_question_mark = "?";
static char* const g_dollar = "$";
static char* const g_at = "@";
static char* const g_void = "void";
static char* const g_i1 = "i1";
static char* const g_i8 = "i8";
static char* const g_i16 = "i16";
static char* const g_i32 = "i32";
static char* const g_i64 = "i64";
static char* const g_u1 = "u1";
static char* const g_u8 = "u8";
static char* const g_u16 = "u16";
static char* const g_u32 = "u32";
static char* const g_u64 = "u64";
static char* const g_f32 = "f32";
static char* const g_float = "float";
static char* const g_double = "double";
static char* const g_f64 = "f64";
static char* const g_ret = "ret";
static char* const g_ptr = "ptr";
static char* const g_if = "if";
static char* const g_else = "else";
static char* const g_while = "while";
static char* const g_break = "break";
static char* const g_continue = "continue";
static char* const g_type = "type";
static char* const g_defer = "defer";
static char* const g_new = "new";
static char* const g_del = "del";
static char* const g_sizeof = "sizeof";
static char* const g_include = "include";
static char* const g_opcode_type_def = "%%%s = type";
static char* const g_opcode_label = "L%d:";
static char* const g_opcode_branch_if_else = "br i1 %%%d, label %%L%d, label %%L%d";
static char* const g_opcode_branch = "br label %%L%d";
static char* const g_opcode_target = "target triple = \"x86_64-pc-linux-gnu\"";
static char* const g_opcode_signed_not = "%%%d = xor %s %%%d, true";
static char* const g_opcode_signed_negative = "%%%d = mul %s %%%d, -1";
static char* const g_opcode_signed_mul = "%%%d = mul %s %%%d, %%%d";
static char* const g_opcode_signed_divide = "%%%d = sdiv %s %%%d, %%%d";
static char* const g_opcode_signed_remainder = "%%%d = srem %s %%%d, %%%d";
static char* const g_opcode_signed_add = "%%%d = add %s %%%d, %%%d";
static char* const g_opcode_signed_sub = "%%%d = sub %s %%%d, %%%d";
static char* const g_opcode_signed_equal_to = "%%%d = icmp eq %s %%%d, %%%d";
static char* const g_opcode_signed_not_equal_to = "%%%d = icmp ne %s %%%d, %%%d";
static char* const g_opcode_signed_less = "%%%d = icmp slt %s %%%d, %%%d";
static char* const g_opcode_signed_less_equal_to = "%%%d = icmp sle %s %%%d, %%%d";
static char* const g_opcode_signed_greater = "%%%d = icmp sgt %s %%%d, %%%d";
static char* const g_opcode_signed_greater_equal_to = "%%%d = icmp sge %s %%%d, %%%d";
static char* const g_opcode_signed_bitwise_or = "%%%d = or %s %%%d, %%%d";
static char* const g_opcode_signed_bitwise_not = "%%%d = xor %s %%%d, -1";
static char* const g_opcode_signed_bitwise_xor = "%%%d = xor %s %%%d, %%%d";
static char* const g_opcode_signed_bitwise_and = "%%%d = and %s %%%d, %%%d";
static char* const g_opcode_signed_shift_left = "%%%d = shl %s %%%d, %%%d";
static char* const g_opcode_signed_shift_rite = "%%%d = ashr %s %%%d, %%%d";
static char* const g_opcode_unsigned_mul = "%%%d = mul %s %%%d, %%%d";
static char* const g_opcode_unsigned_divide = "%%%d = udiv %s %%%d, %%%d";
static char* const g_opcode_unsigned_remainder = "%%%d = urem %s %%%d, %%%d";
static char* const g_opcode_unsigned_add = "%%%d = add %s %%%d, %%%d";
static char* const g_opcode_unsigned_sub = "%%%d = sub %s %%%d, %%%d";
static char* const g_opcode_unsigned_equal_to = "%%%d = icmp eq %s %%%d, %%%d";
static char* const g_opcode_unsigned_not_equal_to = "%%%d = icmp ne %s %%%d, %%%d";
static char* const g_opcode_unsigned_less = "%%%d = icmp ult %s %%%d, %%%d";
static char* const g_opcode_unsigned_less_equal_to = "%%%d = icmp ule %s %%%d, %%%d";
static char* const g_opcode_unsigned_greater = "%%%d = icmp ugt %s %%%d, %%%d";
static char* const g_opcode_unsigned_greater_equal_to = "%%%d = icmp uge %s %%%d, %%%d";
static char* const g_opcode_unsigned_bitwise_or = "%%%d = or %s %%%d, %%%d";
static char* const g_opcode_unsigned_bitwise_xor = "%%%d = xor %s %%%d, %%%d";
static char* const g_opcode_unsigned_bitwise_and = "%%%d = and %s %%%d, %%%d";
static char* const g_opcode_unsigned_shift_left = "%%%d = shl %s %%%d, %%%d";
static char* const g_opcode_unsigned_shift_rite = "%%%d = lshr %s %%%d, %%%d";
static char* const g_opcode_alloca = "%%%d = alloca %s";
static char* const g_opcode_flat_gep = "%%%d = getelementptr ptr, ptr %%%d, i32 0";
static char* const g_opcode_gep = "%%%d = getelementptr %s, ptr %%%d, %s %%%d";
static char* const g_opcode_sizeof = "%%%d = getelementptr %s, ptr null, i64 1";
static char* const g_opcode_type_field = "%%%d = getelementptr inbounds %s, ptr %%%d, i32 0, i32 %d";
static char* const g_opcode_type_field_extract = "%%%d = extractvalue %s %%%d, %d";
static char* const g_opcode_ptr_to_int = "%%%d = ptrtoint ptr %%%d to %s";
static char* const g_opcode_int_to_ptr = "%%%d = inttoptr %s %%%d to ptr";
static char* const g_opcode_load_double = "%%%d = fadd %s %s, 0.0";
static char* const g_opcode_load_signed = "%%%d = add %s %s, 0";
static char* const g_opcode_load_character = "%%%d = add %s %d, 0";
static char* const g_opcode_ret = "ret %s %%%d";
static char* const g_opcode_ret_void = "ret %s";
static char* const g_opcode_define = "define %s @%s";
static char* const g_opcode_declare = "declare %s @%s";
static char* const g_opcode_load = "%%%d = load %s, ptr %%%d";
static char* const g_opcode_store_direct = "store %s %s, ptr %%%d";
static char* const g_opcode_store_function = "store %s @%s, ptr %%%d";
static char* const g_opcode_store = "store %s %%%d, ptr %%%d";
static char* const g_opcode_zero_init = "store %s zeroinitializer, ptr %%%d";
static char* const g_opcode_type_slot = "%s %%%d";
static char* const g_opcode_indirect_call = "%%%d = call %s %%%d";
static char* const g_opcode_indirect_void_call = "call %s %%%d";
static char* const g_opcode_variadic_indirect_call = "%%%d = call %s (...) %%%d";
static char* const g_opcode_variadic_indirect_void_call = "call %s (...) %%%d";
static char* const g_opcode_entry = "entry:";
static char* const g_opcode_zero_extend = "%%%d = zext %s %%%d to %s";
static char* const g_opcode_signed_extend = "%%%d = sext %s %%%d to %s";
static char* const g_opcode_trunc = "%%%d = trunc %s %%%d to %s";
static char* const g_opcode_malloc = "%%%d = call ptr @malloc(i64 %%%d)";
static char* const g_opcode_free = "call void @free(ptr %%%d)";
static char* const g_opcode_gep_string = "%%%d = getelementptr [%d x i8], ptr @%d, i64 0";
static char* const g_opcode_string_const = "@%d = private constant [%d x i8] c\"%s\"";
static char* const g_opcode_increment = "%%%d = add %s %%%d, 1";
static char* const g_opcode_decrement = "%%%d = sub %s %%%d, 1";
static char* const g_opcode_floating_negative = "%%%d = fmul %s %%%d, -1.0";
static char* const g_opcode_floating_increment = "%%%d = fadd %s %%%d, 1.0";
static char* const g_opcode_floating_decrement = "%%%d = fsub %s %%%d, 1.0";
static char* const g_opcode_floating_mul = "%%%d = fmul %s %%%d, %%%d";
static char* const g_opcode_floating_div = "%%%d = fdiv %s %%%d, %%%d";
static char* const g_opcode_floating_add = "%%%d = fadd %s %%%d, %%%d";
static char* const g_opcode_floating_sub = "%%%d = fsub %s %%%d, %%%d";
static char* const g_opcode_floating_equal_to = "%%%d = fcmp oeq %s %%%d, %%%d";
static char* const g_opcode_floating_not_equal_to = "%%%d = fcmp one %s %%%d, %%%d";
static char* const g_opcode_floating_less = "%%%d = fcmp olt %s %%%d, %%%d";
static char* const g_opcode_floating_less_equal_to = "%%%d = fcmp ole %s %%%d, %%%d";
static char* const g_opcode_floating_greater = "%%%d = fcmp ogt %s %%%d, %%%d";
static char* const g_opcode_floating_greater_equal_to = "%%%d = fcmp oge %s %%%d, %%%d";
static char* const g_opcode_float_to_signed = "%%%d = fptosi %s %%%d to %s";
static char* const g_opcode_float_to_unsigned = "%%%d = fptoui %s %%%d to %s";
static char* const g_opcode_signed_to_floating = "%%%d = sitofp %s %%%d to %s";
static char* const g_opcode_unsigned_to_floating = "%%%d = uitofp %s %%%d to %s";
static char* const g_opcode_floating_trunc = "%%%d = fptrunc %s %%%d to %s";
static char* const g_opcode_floating_extend = "%%%d = fpext %s %%%d to %s";
static char* const g_opcode_signed_to_boolean = "%%%d = trunc %s %%%d to %s";
static char* const g_opcode_unsigned_to_boolean = "%%%d = trunc %s %%%d to %s";
static char* const g_opcode_boolean_to_signed = "%%%d = zext %s %%%d to %s";
static char* const g_opcode_boolean_to_unsigned = "%%%d = zext %s %%%d to %s";
static char* g_operator_chars[] = {
g_dot,
g_not,
g_add,
g_subtract,
g_divide,
g_mod,
g_multiply,
g_equals,
g_equal_to,
g_not_equal_to,
g_less_equal_to,
g_less,
g_greater_equal_to,
g_greater,
g_shift_rite,
g_shift_left,
g_bitwise_or,
g_bitwise_xor,
g_bitwise_and,
g_bitwise_not,
g_comma,
g_ampersand,
nullptr
};
static char* g_builtin_type_keywords[] = {
g_void,
g_i1,
g_i8,
g_i16,
g_i32,
g_i64,
g_u1,
g_u8,
g_u16,
g_u32,
g_u64,
g_f64,
g_f32,
g_ptr,
nullptr
};
static char* g_signed[] = {
g_i8,
g_i16,
g_i32,
g_i64,
nullptr
};
static char* g_unsigned[] = {
g_u8,
g_u16,
g_u32,
g_u64,
nullptr
};
static char* g_floating[] = {
g_f64,
g_f32,
nullptr
};
static char* g_boolean[] = {
g_i1,
g_u1,
nullptr
};
static char* g_control_keywords[] = {
g_ret,
g_if,
g_else,
g_while,
g_defer,
g_break,
g_continue,
nullptr
};
static char* g_construct_keywords[] = {
g_new,
g_del,
g_sizeof,
g_type,
nullptr
};
static char* g_operators_by_precedence[g_precedence_count][8] = {
[ g_precedence_arithmetic_0 ] = { g_multiply, g_divide, g_mod },
[ g_precedence_arithmetic_1 ] = { g_add, g_subtract },
[ g_precedence_shift ] = { g_shift_left, g_shift_rite },
[ g_precedence_relational_0 ] = { g_less, g_less_equal_to, g_greater, g_greater_equal_to },
[ g_precedence_relational_1 ] = { g_equal_to, g_not_equal_to },
[ g_precedence_bitwise_and ] = { g_bitwise_and },
[ g_precedence_bitwise_xor ] = { g_bitwise_xor },
[ g_precedence_bitwise_or ] = { g_bitwise_or },
[ g_precedence_assignment ] = { g_equals },
};
static code_t* get_code()
{
return &g_file.codes.begin[g_file.module];
}
static void code_rewind(int by)
{
auto code = get_code();
code->at -= by;
}
#if 0
#define quit(...) printf(__VA_ARGS__)
#else
[[noreturn]] static void quit(char* format, ...)
{
auto out = stderr;
auto args = (va_list) {};
auto code = get_code();
va_start(args, format);
fprintf(out, "%s%s: line %d:%s %serror: %s", g_white, code->path.begin, code->line, g_normal, g_red, g_normal);
vfprintf(out, format, args);
fprintf(out, g_str, g_escape_newline);
va_end(args);
exit(1);
}
#endif
#if 0
#define emit(...) printf(__VA_ARGS__)
#else
static void emit(char* format, ...)
{
auto out = stdout;
auto args = (va_list) {};
va_start(args, format);
for(auto tab = 0; tab < g_file.tabs; tab++)
{
fprintf(out, g_str, g_escape_tab);
}
vfprintf(out, format, args);
va_end(args);
fprintf(out, g_str, g_escape_newline);
}
#endif
#if 0
#define okay(...) printf(__VA_ARGS__)
#else
static void okay(char* format, ...)
{
auto out = stderr;
auto args = (va_list) {};
va_start(args, format);
fprintf(out, "%sokay: %s", g_green, g_normal);
vfprintf(out, format, args);
fprintf(out, g_str, g_escape_newline);
va_end(args);
}
#endif
static void str_append(str_t* str, char* chars)
{
while(*chars)
{
list_push(str, *chars);
chars += 1;
}
}
static str_t str_init(char* chars)
{
auto str = (str_t) {};
str_append(&str, chars);
return str;
}
static bool str_equal(char* str, char* other)
{
return strcmp(str, other) == 0;
}
static bool str_in(str_t str, char** array)
{
while(*array)
{
if(str_equal(str.begin, *array))
{
return true;
}
array += 1;
}
return false;
}
static str_t* str_in_list(str_t str, str_list_t* list)
{
for(auto i = 0; i < list->size; i++)
{
auto check = &list->begin[i];
if(str_equal(str.begin, check->begin))
{
return check;
}
}
return nullptr;
}
static char** get_operators(precedence_t precedence)
{
return g_operators_by_precedence[precedence];
}
static bool is_operator_char(char c)
{
for(auto operator = g_operator_chars; *operator; operator++)
{
if(strchr(*operator, c))
{
return true;
}
}
return false;
}
static bool is_relational(str_t operator)
{
return str_in(operator, get_operators(g_precedence_relational_0))
|| str_in(operator, get_operators(g_precedence_relational_1));
}
static bool is_increment_decrement(str_t operator)
{
return str_equal(operator.begin, g_increment)
|| str_equal(operator.begin, g_decrement);
}
static bool is_digit_char(char c)
{
return c >= *g_digit_begin
&& c <= *g_digit_end;
}
static bool is_numeric_char(char c)
{
return is_digit_char(c)
|| c == *g_dot;
}
static bool is_not_semicolon(char c)
{
return c != *g_semicolon;
}
static bool is_lower_char(char c)
{
return c >= *g_lower_begin
&& c <= *g_lower_end;
}
static bool is_upper_char(char c)
{
return c >= *g_upper_begin
&& c <= *g_upper_end;
}
static bool is_alpha_char(char c)
{
return is_lower_char(c)
|| is_upper_char(c);
}
static bool is_alnum_char(char c)
{
return is_alpha_char(c)
|| is_digit_char(c)
|| c == *g_underscore;
}
static bool is_escape_char(char c)
{
return c == *g_escape_alert
|| c == *g_escape_backspace
|| c == *g_escape_form_feed
|| c == *g_escape_newline
|| c == *g_escape_carriage_return
|| c == *g_escape_tab
|| c == *g_escape_vertical_tab
|| c == *g_escape_backslash
|| c == *g_escape_apostrophe
|| c == *g_escape_question_mark
/* quotation mark (") is removed to simplify read_chars() */
|| c == *g_escape_null;
}
static bool is_space_char(char c)
{
return c == *g_space
|| c == *g_escape_newline
|| c == *g_escape_tab;
}
static bool is_every_other_char(char c)
{
return c == *g_dot
|| c == *g_divide
|| c == *g_mod
|| c == *g_subtract
|| c == *g_add
|| c == *g_equals
|| c == *g_colon
|| c == *g_semicolon
|| c == *g_comma
|| c == *g_question_mark
|| c == *g_not
|| c == *g_less
|| c == *g_greater
|| c == *g_bitwise_or
|| c == *g_bitwise_and
|| c == *g_bitwise_xor
|| c == *g_bitwise_not
|| c == *g_hash
|| c == *g_dollar
|| c == *g_at
|| c == *g_left_paren
|| c == *g_rite_paren
|| c == *g_left_square
|| c == *g_rite_square
|| c == *g_left_curl
|| c == *g_rite_curl;
}
static bool is_string_char(char c)
{
return is_alnum_char(c)
|| is_escape_char(c)
|| is_space_char(c)
|| is_every_other_char(c);
}
static bool is_assignment_operator(str_t operator)
{
return str_in(operator, get_operators(g_precedence_assignment));
}
static bool is_builtin_type_name(str_t type_name)
{
return str_in(type_name, g_builtin_type_keywords);
}
static bool with_aggregate_function(aggregate_t* aggregate)
{
return aggregate->is_function;
}
static bool with_aggregate_type(aggregate_t* aggregate)
{
return aggregate->is_type;
}
static aggregate_t* find_aggregate(str_t type_name, bool with(aggregate_t*))
{
for(auto i = 0; i < g_file.aggregates.size; i++)
{
auto at = &g_file.aggregates.begin[i];
if(str_equal(type_name.begin, at->name.begin))
{
if(with(at))
{
return at;
}
}
}
return nullptr;
}
static aggregate_t* find_type_aggregate(str_t type_name)
{
return find_aggregate(type_name, with_aggregate_type);
}
static aggregate_t* find_function_aggregate(str_t type_name)
{
return find_aggregate(type_name, with_aggregate_function);
}
static bool is_regular_pointer(type_t type)
{
return type.stars > 0;
}
static bool is_generic_pointer(type_t type)
{
return str_equal(type.name.begin, g_ptr);
}
static bool is_function_pointer(type_t type)
{
return type.is_function_pointer;
}
static bool is_pointer(type_t type)
{
return is_regular_pointer(type)
|| is_generic_pointer(type)
|| is_function_pointer(type);
}
static bool is_function(type_t type)
{
return type.is_function;
}
static bool is_callable(type_t type)
{
return is_function_pointer(type);
}
static bool is_not_pointer(type_t type)
{
return !is_pointer(type);
}
static bool is_variadic(type_t type)
{
return type.is_variadic;
}
static bool is_variadic_decl(str_t operator)
{
return str_equal(operator.begin, g_ellipses);
}
static bool is_aggregate_type(type_t type)
{
return is_not_pointer(type) && find_type_aggregate(type.name);
}
static bool is_boolean(type_t type)
{
return is_not_pointer(type) && str_in(type.name, g_boolean);
}
static bool is_floating(type_t type)
{
return is_not_pointer(type) && str_in(type.name, g_floating);
}
static bool is_size(type_t type)
{
return is_not_pointer(type) && (str_equal(type.name.begin, g_i64) || str_equal(type.name.begin, g_u64));
}
static bool is_signed(type_t type)
{
return is_not_pointer(type) && str_in(type.name, g_signed);
}
static bool is_unsigned(type_t type)
{
return is_not_pointer(type) && str_in(type.name, g_unsigned);
}
static bool is_integral(type_t type)
{
return is_signed(type) || is_unsigned(type);
}
static bool is_numeric(type_t type)
{
return is_integral(type) || is_floating(type);
}
static bool is_member_init(type_t type)
{
return is_integral(type) || is_floating(type) || is_boolean(type);
}
static bool is_control_keyword(str_t keyword)
{
return str_in(keyword, g_control_keywords);
}
static bool is_construct_keyword(str_t keyword)
{
return str_in(keyword, g_construct_keywords);
}
static bool is_type_name(str_t type_name)
{
return is_builtin_type_name(type_name)
|| find_type_aggregate(type_name);
}
static bool is_reserved_keyword(str_t keyword)
{
return is_type_name(keyword)
|| is_control_keyword(keyword)
|| is_construct_keyword(keyword);
}
static bool is_lvalue(value_t value)
{
return value.is_lvalue;
}
static bool is_rvalue(value_t value)
{
return !is_lvalue(value);
}
static bool must_skip_arg_check(type_t type)
{
return type.must_skip_arg_check;
}
static bool is_type_def_keyword(str_t keyword)
{
return str_equal(keyword.begin, g_type);
}
static bool is_include_keyword(str_t keyword)
{
return str_equal(keyword.begin, g_include);
}
static bool is_ret(str_t keyword)
{
return str_equal(keyword.begin, g_ret);
}
static bool is_void(str_t keyword)
{
return str_equal(keyword.begin, g_void);
}
static bool is_continue(str_t keyword)
{
return str_equal(keyword.begin, g_continue);
}
static bool is_break(str_t keyword)
{
return str_equal(keyword.begin, g_break);
}
static bool is_defer(str_t keyword)
{
return str_equal(keyword.begin, g_defer);
}
static bool is_if(str_t keyword)
{
return str_equal(keyword.begin, g_if);
}
static bool is_else(str_t keyword)
{
return str_equal(keyword.begin, g_else);
}
static bool is_while(str_t keyword)
{
return str_equal(keyword.begin, g_while);
}
static bool is_multiply(str_t operator)
{
return str_equal(operator.begin, g_multiply);
}
static bool is_divide(str_t operator)
{
return str_equal(operator.begin, g_divide);
}
static bool is_mod(str_t operator)
{
return str_equal(operator.begin, g_mod);
}
static bool is_add(str_t operator)
{
return str_equal(operator.begin, g_add);
}
static bool is_subtract(str_t operator)
{
return str_equal(operator.begin, g_subtract);
}
static bool is_equal_to(str_t operator)
{
return str_equal(operator.begin, g_equal_to);
}
static bool is_not_equal_to(str_t operator)
{
return str_equal(operator.begin, g_not_equal_to);
}
static bool is_less(str_t operator)
{
return str_equal(operator.begin, g_less);
}
static bool is_less_equal_to(str_t operator)
{
return str_equal(operator.begin, g_less_equal_to);
}
static bool is_greater(str_t operator)
{
return str_equal(operator.begin, g_greater);
}
static bool is_greater_equal_to(str_t operator)
{
return str_equal(operator.begin, g_greater_equal_to);
}
static bool is_bitwise_and(str_t operator)
{
return str_equal(operator.begin, g_bitwise_and);
}
static bool is_bitwise_or(str_t operator)
{
return str_equal(operator.begin, g_bitwise_or);
}
static bool is_bitwise_xor(str_t operator)
{
return str_equal(operator.begin, g_bitwise_xor);
}
static bool is_shift_left(str_t operator)