-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlabrat.h
More file actions
1334 lines (1121 loc) · 35.9 KB
/
labrat.h
File metadata and controls
1334 lines (1121 loc) · 35.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
// labrat.h v0.1 - public domain (see license at bottom of file)
//
// authored 2016 by Doug Thayer
//
// This library allows decentralized specification of test routines, without
// forcing you to use build system X. Just build this file with the flag
// LR_GEN_EXECUTABLE set to generate an executable. This executable will
// find tests in the current directory and build a list of them that it can
// consume.
//
// Here's an example:
//
// ```c
// // inside calculator.c:
//
// ...
//
// TEST_CASE(test_calculates)
// {
// const char* calc_str = "15 5 +";
// int result = calculate(calc_str);
//
// ASSERT_EQ(result, 20, "%d"); // actual, expected, format
// }
// ```
//
// ```c
// // inside main.c
//
// #define LR_IMPLEMENTATION
// #include "labrat.h"
//
// int main(int argc, char const *argv[])
// {
// LR_PRELUDE(argc, argv);
//
// ... // do what we would do if tests didn't run.
// }
// ```
//
// ### Using GCC:
// ```sh
// # generate the labrat executable
// gcc -x c labrat.h -D LR_GEN_EXECUTABLE -o labrat
//
// # run labrat to generate test metadata
// ./labrat
//
// # compile our program
// gcc program.c calculator.c -o program
//
// # run the tests
// ./program --lr-run-tests
//
// # run benchmarks
// ./program --lr-run-benchmarks 1000
//
// ```
//
// ### Using MSVC (command line):
// ```bat
// "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
// cl /TC /DLR_GEN_EXECUTABLE labrat.h
// labrat.exe
// cl program.c calculator.c
// program.exe --lr-run-tests
// program.exe --lr-run-benchmarks 1000
// ```
//
// ## How it Works
//
// It works by recursively grabbing every source and header file in the
// directory, tokenizing them, and searching for the `TEST_CASE` and
// `BENCHMARK` tokens. It then builds a `labrat_data.c` file which includes
// all of the necessary information for running the tests. Then when
// you want to run your tests, it just includes this file in such a way
// that it generates code which handles running the tests.
/******************************************************************************/
///////////////////////////////// BEGIN HEADER /////////////////////////////////
/******************************************************************************/
#ifndef LABRAT_H
#define LABRAT_H
#pragma warning(push, 0)
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#pragma warning(pop)
// This will forward declare all of our test and benchmark functions so they
// may be referenced here.
#if !defined(LR_GEN_EXECUTABLE) || defined(LR_SELF_TEST) ///////////////////////
#define TEST_DEFINITION(id) void id(void);
#define BENCH_DEFINITION(id) void id(int64_t iterations);
#include "labrat_data.c"
#undef TEST_DEFINITION
#undef BENCH_DEFINITION
#endif
////////////////////////////////////////////////////////////////////////////////
void _lr_set_color_grn();
void _lr_set_color_red();
void _lr_set_color_def();
void _lr_set_color_yel();
void _lr_set_color_wht();
void _lr_begin_benchmark();
void _lr_end_benchmark();
void _lr_fail_current_test();
#define _LR_GETCYCLES() __rdtsc()
#define _LR_ARRAY_COUNT(array) sizeof(array) / sizeof(array[0])
#define _LR_DIR(file) (strrchr((file), '\\') ? \
strrchr((file), '\\') + 1 : (strrchr((file), '/') ? \
strrchr((file), \
'/') + 1 : (file)))
#define _LR_FILENAME _LR_DIR(__FILE__)
#define TEST_CASE(__lr_test_id__) void __lr_test_id__(void)
#define BENCHMARK(__lr_bench_id__, __lr_iterations__) \
void __lr_bench_id__(int64_t __lr_iterations__)
#define BEGIN_BENCHMARK() __lr_benchmark_start = _LR_GETCYCLES()
#define END_BENCHMARK() __lr_benchmark_end = _LR_GETCYCLES()
#define ASSERT_TRUE(exp) do { \
if (!(exp)) { \
_lr_set_color_yel(); \
printf("Assertion Failed:\n" \
"Expected (%s) to be true -- %s, line %d\n", \
#exp, _LR_FILENAME, __LINE__); \
_lr_fail_current_test(); \
_lr_set_color_def(); \
return; \
} \
} while (0)
#define ASSERT_FALSE(exp) do { \
if (exp) { \
_lr_set_color_yel(); \
printf("Assertion Failed:\n" \
"Expected (%s) to be false -- %s, line %d\n", \
#exp, _LR_FILENAME, __LINE__); \
_lr_fail_current_test(); \
_lr_set_color_def(); \
return; \
} \
} while (0)
#define ASSERT_EQ(actual, expected, format) do { \
if ((expected) != (actual)) { \
_lr_set_color_yel(); \
printf("Assertion Failed:\n" \
"Expected "format " to equal "format " -- %s, line %d\n", \
(actual), (expected), _LR_FILENAME, __LINE__); \
_lr_fail_current_test(); \
_lr_set_color_def(); \
return; \
} \
} while (0)
#define ASSERT_NOT_EQ(actual, comp, format) do { \
if ((comp) == (actual)) { \
_lr_set_color_yel(); \
printf("Assertion Failed:\n" \
"Expected "format " to not equal "format " -- %s, line %d\n", \
actual, comp, _LR_FILENAME, __LINE__); \
_lr_fail_current_test(); \
_lr_set_color_def(); \
return; \
} \
} while (0)
#define ASSERT_GT(actual, comp, format) do { \
if ((comp) <= (actual)) { \
_lr_set_color_yel(); \
printf("Assertion Failed:\n" \
"Expected "format " to be greater than "format " -- %s, line %d\n", \
actual, comp, _LR_FILENAME, __LINE__); \
_lr_fail_current_test(); \
_lr_set_color_def(); \
return; \
} \
} while (0)
#define ASSERT_LT(actual, comp, format) do { \
if ((comp) >= (actual)) { \
_lr_set_color_yel(); \
printf("Assertion Failed:\n" \
"Expected "format " to be less than "format " -- %s, line %d\n", \
actual, comp, _LR_FILENAME, __LINE__); \
_lr_fail_current_test(); \
_lr_set_color_def(); \
return; \
} \
} while (0)
#define ASSERT_GT_OR_EQ(actual, comp, format) do { \
if ((comp) < (actual)) { \
_lr_set_color_yel(); \
printf("Assertion Failed:\n" \
"Expected "format " to be greater than or equal to "format " -- %s, " \
"line %d\n", \
actual, comp, _LR_FILENAME, __LINE__); \
_lr_fail_current_test(); \
_lr_set_color_def(); \
return; \
} \
} while (0)
#define ASSERT_LT_OR_EQ(actual, comp, format) do { \
if ((comp) > (actual)) { \
_lr_set_color_yel(); \
printf("Assertion Failed:\n" \
"Expected "format " to be less than or equal to " \
format " -- %s, line %d\n", \
actual, comp, _LR_FILENAME, __LINE__); \
_lr_fail_current_test(); \
_lr_set_color_def(); \
return; \
} \
} while (0)
#ifdef _WIN32
#define LR_PRELUDE(argc, argv) do {\
if (argc == 2 && strcmp("--lr-run-tests", argv[1]) == 0) {\
_lr_console_h = GetStdHandle(STD_OUTPUT_HANDLE);\
lr_run_tests();\
exit(0);\
} else if (argc == 3 && strcmp("--lr-run-benchmarks", argv[1]) == 0) {\
_lr_console_h = GetStdHandle(STD_OUTPUT_HANDLE);\
uint64_t _lr_iterations = atoi(argv[2]);\
lr_run_benchmarks(_lr_iterations);\
exit(0);\
}\
} while(0)
#else
#define LR_PRELUDE(argc, argv) do {\
if (argc == 2 && strcmp("--lr-run-tests", argv[1]) == 0) {\
lr_run_tests();\
exit(0);\
} else if (argc == 3 && strcmp("--lr-run-benchmarks", argv[1]) == 0) {\
uint64_t _lr_iterations = atoi(argv[2]);\
lr_run_benchmarks(_lr_iterations);\
exit(0);\
}\
} while(0)
#endif
void lr_run_tests(void);
#endif //#ifndef LABRAT_H
/******************************************************************************/
////////////////////////////////// END HEADER //////////////////////////////////
/******************************************************************************/
#ifdef LR_GEN_EXECUTABLE
#define LR_IMPLEMENTATION
#endif //#ifdef LR_GEN_EXECUTABLE
/******************************************************************************/
///////////////////////////// BEGIN IMPLEMENTATION /////////////////////////////
/******************************************************************************/
#ifdef LR_IMPLEMENTATION
#ifdef _WIN32
#include <windows.h>
#include <intrin.h>
HANDLE _lr_console_h = 0;
void _lr_set_color_grn()
{
SetConsoleTextAttribute(_lr_console_h, 2);
}
void _lr_set_color_red()
{
SetConsoleTextAttribute(_lr_console_h, 4);
}
void _lr_set_color_def()
{
SetConsoleTextAttribute(_lr_console_h, 7);
}
void _lr_set_color_yel()
{
SetConsoleTextAttribute(_lr_console_h, 6);
}
void _lr_set_color_wht()
{
SetConsoleTextAttribute(_lr_console_h, 15);
}
#else
#include <dirent.h>
#include <x86intrin.h>
void _lr_set_color_grn()
{
printf("\x1b[32m");
}
void _lr_set_color_red()
{
printf("\x1b[31m");
}
void _lr_set_color_def()
{
printf("\x1b[0m");
}
void _lr_set_color_yel()
{
printf("\x1b[33m");
}
void _lr_set_color_wht()
{
printf("\x1b[34m");
}
#endif
bool __lr_test_passed;
int64_t __lr_benchmark_start;
int64_t __lr_benchmark_end;
void _lr_begin_benchmark()
{
__lr_benchmark_start = _LR_GETCYCLES();
}
void _lr_end_benchmark()
{
__lr_benchmark_end = _LR_GETCYCLES();
}
void _lr_fail_current_test()
{
__lr_test_passed = false;
}
/******************************************************************************/
////////////////////// Sean Barrett's Stretchy Buffer///////////////////////////
//////// https://github.com/nothings/stb/blob/master/stretchy_buffer.h /////////
/******************************************************************************/
#define lr_sb_free(a) ((a) ? free(lr__sbraw(a)), 0 : 0)
#define lr_sb_push(a, v) (lr__sbmaybegrow(a, 1), (a)[lr__sbn(a)++] = (v))
#define lr_sb_count(a) ((a) ? lr__sbn(a) : 0)
#define lr_sb_add(a, n) (lr__sbmaybegrow(a, n), \
lr__sbn(a) += (n), \
& (a)[lr__sbn(a) - (n)])
#define lr_sb_last(a) ((a)[lr__sbn(a) - 1])
#define lr__sbraw(a) ((int64_t *)(a) - 2)
#define lr__sbm(a) lr__sbraw(a)[0]
#define lr__sbn(a) lr__sbraw(a)[1]
#define lr__sbneedgrow(a, n) ((a) == 0 || lr__sbn(a) + (n) >= lr__sbm(a))
#define lr__sbmaybegrow(a, n) (lr__sbneedgrow(a, (n)) ? lr__sbgrow(a, n) : 0)
#define lr__sbgrow(a, \
n) (*((void **)&a) = \
lr__sbgrowf(((void *)a), (n), sizeof(*(a))))
static void *lr__sbgrowf(void *arr, int64_t increment, int64_t itemsize)
{
int64_t dbl_cur = arr ? 2 * lr__sbm(arr) : 0;
int64_t min_needed = lr_sb_count(arr) + increment;
int64_t m = dbl_cur > min_needed ? dbl_cur : min_needed;
int64_t *p = (int64_t *)realloc(arr ?
lr__sbraw(arr) :
0, itemsize * m + sizeof(int64_t) * 2);
if (p) {
if (!arr)
p[1] = 0;
p[0] = m;
return p + 2;
} else {
#ifdef STRETCHY_BUFFER_OUT_OF_MEMORY
STRETCHY_BUFFER_OUT_OF_MEMORY;
#endif
return (void *)(2 * sizeof(int64_t));
}
}
/******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/******************************************************************************/
#if !defined(LR_GEN_EXECUTABLE) || defined(LR_SELF_TEST)
bool __lr_test_definition(void (*func)(void), const char *name)
{
__lr_test_passed = true;
func();
if (__lr_test_passed) {
_lr_set_color_grn();
printf(" [ PASSED ] -- %s\n", name);
_lr_set_color_def();
return true;
} else {
_lr_set_color_red();
printf(" [ FAILED ] -- %s\n", name);
_lr_set_color_def();
return false;
}
}
void lr_run_tests(void)
{
#ifndef LR_OFF // just produce an empty function if LR_OFF
_lr_set_color_wht();
printf("\nRunning tests:\n\n");
_lr_set_color_def();
bool tests[] = {
0,
#define TEST_DEFINITION(id) 0,
#include "labrat_data.c"
#undef TEST_DEFINITION
};
int32_t ix = 1;
#define TEST_DEFINITION(id) tests[ix++] = __lr_test_definition(id, #id);
#include "labrat_data.c"
#undef TEST_DEFINITION
int32_t passed = 0;
int32_t total = _LR_ARRAY_COUNT(tests) - 1;
for (int64_t i = 1; i <= total; i++)
if (tests[i])
passed++;
bool all_passed = passed == total;
puts("\nFinished running tests: ");
if (all_passed) {
_lr_set_color_grn();
} else {
_lr_set_color_red();
}
printf("%d ", passed);
_lr_set_color_wht();
printf("of %d tests passed (", total);
if (all_passed) {
_lr_set_color_grn();
} else {
_lr_set_color_red();
}
printf("%d", total - passed);
_lr_set_color_wht();
puts(" failed)\n");
_lr_set_color_def();
#endif // #ifndef LR_OFF
}
void lr_run_benchmarks(uint64_t iterations)
{
#ifndef LR_OFF // just produce an empty function if LR_OFF
int32_t tests_passed = 0;
int32_t total_tests = 0;
_lr_set_color_wht();
printf("\nRunning benchmarks:\n\n");
_lr_set_color_def();
uint64_t start_time;
uint64_t end_time;
int32_t max_bench_name_size = -1;
int32_t bench_name_sizes[] = {
-1,
#define BENCH_DEFINITION(id) (sizeof(#id) - 1),
#include "labrat_data.c"
#undef BENCH_DEFINITION
};
for (int64_t i = 0; i < _LR_ARRAY_COUNT(bench_name_sizes); i++)
if (bench_name_sizes[i] > max_bench_name_size)
max_bench_name_size = bench_name_sizes[i];
#define BENCH_DEFINITION(id) \
__lr_benchmark_start = -1; \
__lr_benchmark_end = -1; \
start_time = _LR_GETCYCLES(); \
id(iterations); \
end_time = _LR_GETCYCLES(); \
if (__lr_benchmark_start != -1) { \
start_time = __lr_benchmark_start; \
} \
if (__lr_benchmark_end != -1) { \
end_time = __lr_benchmark_end; \
} \
_lr_set_color_wht();\
printf(" [ FINISHED ] -- " \
"%-*s: %12I64u cycles / iteration\n", \
max_bench_name_size + 2, \
#id, \
(end_time - start_time) / iterations);\
_lr_set_color_def();
#include "labrat_data.c"
#undef BENCH_DEFINITION
bool all_passed = tests_passed == total_tests;
_lr_set_color_wht();
printf("\nFinished running benchmarks.");
_lr_set_color_def();
#endif // #ifndef LR_OFF
}
#endif // #if !defined(LR_GEN_EXECUTABLE) || defined(LR_SELF_TEST)
enum {
LR_TOKEN_L_PAREN,
LR_TOKEN_R_PAREN,
LR_TOKEN_L_BRACE,
LR_TOKEN_R_BRACE,
LR_TOKEN_L_BRACKET,
LR_TOKEN_R_BRACKET,
LR_TOKEN_COMMA,
LR_TOKEN_PERIOD,
LR_TOKEN_ASTERISK,
LR_TOKEN_MINUS,
LR_TOKEN_PLUS,
LR_TOKEN_EXCLAMATION,
LR_TOKEN_TILDE,
LR_TOKEN_BACKSLASH,
LR_TOKEN_SLASH,
LR_TOKEN_EQ,
LR_TOKEN_AMPERSAND,
LR_TOKEN_POUND,
LR_TOKEN_SEMICOLON,
LR_TOKEN_COLON,
LR_TOKEN_QUESTION_MARK,
LR_TOKEN_IDENTIFIER,
LR_TOKEN_NUMBER,
LR_TOKEN_CHARACTER,
LR_TOKEN_STRING,
LR_TOKEN_UNKNOWN,
LR_TOKEN_EOF,
};
typedef struct {
int64_t len;
char *data;
} lr_slice_t;
typedef struct {
uint32_t type;
lr_slice_t slice;
} c_token_t;
char *get_token_type_name(uint32_t type)
{
switch (type) {
case LR_TOKEN_L_PAREN: { return "LR_TOKEN_L_PAREN"; }
case LR_TOKEN_R_PAREN: { return "LR_TOKEN_R_PAREN"; }
case LR_TOKEN_L_BRACE: { return "LR_TOKEN_L_BRACE"; }
case LR_TOKEN_R_BRACE: { return "LR_TOKEN_R_BRACE"; }
case LR_TOKEN_L_BRACKET: { return "LR_TOKEN_L_BRACKET"; }
case LR_TOKEN_R_BRACKET: { return "LR_TOKEN_R_BRACKET"; }
case LR_TOKEN_ASTERISK: { return "LR_TOKEN_ASTERISK"; }
case LR_TOKEN_COMMA: { return "LR_TOKEN_COMMA"; }
case LR_TOKEN_PERIOD: { return "LR_TOKEN_PERIOD"; }
case LR_TOKEN_MINUS: { return "LR_TOKEN_MINUS"; }
case LR_TOKEN_PLUS: { return "LR_TOKEN_PLUS"; }
case LR_TOKEN_EXCLAMATION: { return "LR_TOKEN_EXCLAMATION"; }
case LR_TOKEN_TILDE: { return "LR_TOKEN_TILDE"; }
case LR_TOKEN_BACKSLASH: { return "LR_TOKEN_BACKSLASH"; }
case LR_TOKEN_SLASH: { return "LR_TOKEN_SLASH"; }
case LR_TOKEN_EQ: { return "LR_TOKEN_EQ"; }
case LR_TOKEN_AMPERSAND: { return "LR_TOKEN_AMPERSAND"; }
case LR_TOKEN_POUND: { return "LR_TOKEN_POUND"; }
case LR_TOKEN_SEMICOLON: { return "LR_TOKEN_SEMICOLON"; }
case LR_TOKEN_COLON: { return "LR_TOKEN_COLON"; }
case LR_TOKEN_QUESTION_MARK: { return "LR_TOKEN_QUESTION_MARK"; }
case LR_TOKEN_IDENTIFIER: { return "LR_TOKEN_IDENTIFIER"; }
case LR_TOKEN_NUMBER: { return "LR_TOKEN_NUMBER"; }
case LR_TOKEN_CHARACTER: { return "LR_TOKEN_CHARACTER"; }
case LR_TOKEN_STRING: { return "LR_TOKEN_STRING"; }
case LR_TOKEN_UNKNOWN: { return "LR_TOKEN_UNKNOWN"; }
case LR_TOKEN_EOF: { return "LR_TOKEN_EOF"; }
default: { return "---"; }
}
}
lr_slice_t lr_as_slice(char *s, int64_t len)
{
lr_slice_t result;
result.len = len;
result.data = s;
return result;
}
lr_slice_t lr_slice(lr_slice_t s, int64_t start, int64_t end)
{
lr_slice_t result;
result.len = end - start;
result.data = s.data + start;
return result;
}
lr_slice_t lr_slice_r(lr_slice_t s, int64_t start)
{
lr_slice_t result;
result.len = s.len - start;
result.data = s.data + start;
return result;
}
lr_slice_t lr_slice_l(lr_slice_t s, int64_t end)
{
lr_slice_t result;
result.len = end;
result.data = s.data;
return result;
}
lr_slice_t lr_copy_slice(lr_slice_t s)
{
lr_slice_t result;
result.data = (char *)malloc(s.len);
result.len = s.len;
memcpy(result.data, s.data, s.len);
return result;
}
bool lr_slices_equal(lr_slice_t lhs, lr_slice_t rhs)
{
if (lhs.len != rhs.len)
return false;
else
return memcmp(lhs.data, rhs.data, lhs.len) != 0;
}
#undef LR_IMPLEMENTATION
#endif // #ifdef LR_IMPLEMENTATION
/******************************************************************************/
////////////////////////////// END IMPLEMENTATION //////////////////////////////
/******************************************************************************/
/******************************************************************************/
/////////////////////////////// BEGIN EXECUTABLE ///////////////////////////////
/******************************************************************************/
#ifdef LR_GEN_EXECUTABLE
int32_t lr_min(int32_t a, int32_t b)
{
return a > b ? b : a;
}
char **lr_get_directory(char *start_dir, int64_t *file_count)
{
char **result = 0;
char *stack[1024] = { 0 };
int32_t stack_size = 0;
stack[stack_size++] = start_dir;
while (stack_size) {
char *dir_path = stack[--stack_size];
#ifdef _WIN32
char path[2048];
snprintf(path, _LR_ARRAY_COUNT(path), "%s/*.*", dir_path);
WIN32_FIND_DATA fd;
HANDLE handle = NULL;
if ((handle = FindFirstFile(path, &fd)) == INVALID_HANDLE_VALUE)
continue;
do {
if (strcmp(fd.cFileName, ".") != 0 &&
strcmp(fd.cFileName, "..") != 0) {
snprintf(path, _LR_ARRAY_COUNT(path), "%s/%s", dir_path,
fd.cFileName);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
int64_t path_length = strlen(path) + 1;
stack[stack_size] = (char *)malloc(path_length);
memcpy(stack[stack_size], path, path_length);
stack_size++;
} else {
int64_t path_length = strlen(path) + 1;
char *filename = (char *)malloc(path_length);
memcpy(filename, path, path_length);
lr_sb_push(result, filename);
}
}
} while (FindNextFile(handle, &fd));
FindClose(handle);
#else // !_WIN32
char path[2048];
snprintf(path, _LR_ARRAY_COUNT(path), "%s", dir_path);
DIR *dir = 0;
struct dirent *fd = {0};
dir = opendir(path);
if (!dir) {
printf("Could not open directory %s.", path);
continue;
}
fd = readdir(dir);
if (!fd) {
printf("Could not read directory %s.", path);
continue;
}
do {
if (strcmp(fd->d_name, ".") != 0 &&
strcmp(fd->d_name, "..") != 0) {
snprintf(path, _LR_ARRAY_COUNT(path), "%s/%s", dir_path,
fd->d_name);
if (fd->d_type == DT_DIR) {
int64_t path_length = strlen(path) + 1;
stack[stack_size] = (char *)malloc(path_length);
memcpy(stack[stack_size], path, path_length);
stack_size++;
} else {
int64_t path_length = strlen(path) + 1;
char *filename = (char *)malloc(path_length);
memcpy(filename, path, path_length);
lr_sb_push(result, filename);
}
}
} while ((fd = readdir(dir)));
closedir(dir);
#endif // /_WIN32
if (dir_path != start_dir)
free(dir_path);
}
*file_count = lr_sb_count(result);
return result;
// char **result = 0;
// DIR *d;
// struct dirent *dir;
// d = opendir(".");
// if (d) {
// while ((dir = readdir(d)) != NULL)
// {
// printf("%s\n", dir->d_name);
// }
// closedir(d);
// }
}
int32_t lr_is_whitespace(char c)
{
return c == ' ' || (c >= '\t' && c <= '\r');
}
lr_slice_t lr_eat_whitespace_and_comments(lr_slice_t s)
{
char *data = s.data;
int64_t len = s.len;
while (len > 0) {
if (lr_is_whitespace(*data)) {
++data;
--len;
} else if (len > 1 && data[0] == '/' && data[1] == '/') {
data += 2;
len -= 2;
while (len > 0 && *data != '\n') {
++data;
--len;
}
} else if (len > 1 && data[0] == '/' && data[1] == '*') {
data += 2;
len -= 2;
while (len > 0 && !(data[0] == '*' && len > 1 && data[1] == '/')) {
++data;
--len;
}
if (len > 2) {
data += 2;
len -= 2;
} else {
data += len;
len = 0;
}
} else {
break;
}
}
lr_slice_t result;
result.data = data;
result.len = len;
return result;
}
int32_t lr_is_digit(char c)
{
return c >= '0' && c <= '9';
}
int32_t lr_is_word_char(char c)
{
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c == '_');
}
int32_t lr_is_identifier(char c)
{
return lr_is_word_char(c) || lr_is_digit(c);
}
bool lr_lex_identifier(lr_slice_t *c, c_token_t *token)
{
if (lr_is_word_char(*c->data)) {
int32_t len = 1;
while (c->len - len) {
if (lr_is_word_char(c->data[len]) || lr_is_digit(c->data[len]))
++len;
else
break;
}
lr_slice_t token_slice = lr_slice_l(*c, len);
*c = lr_slice_r(*c, len);
token->type = LR_TOKEN_IDENTIFIER;
token->slice = token_slice;
return true;
} else {
return false;
}
}
bool lr_lex_constant(lr_slice_t *c, c_token_t *token)
{
uint32_t token_type;
if (lr_is_digit(*c->data)) {
token_type = LR_TOKEN_NUMBER;
int32_t len = 1;
while (c->len - len) {
char ch = c->data[len];
if (!lr_is_digit(ch) &&
ch != '.' &&
ch != 'x' &&
ch != 'X')
break;
len++;
}
lr_slice_t token_slice = lr_slice_l(*c, len);
*c = lr_slice_r(*c, len);
token->type = token_type;
token->slice = token_slice;
return true;
} else if (*c->data == '\'') {
token_type = LR_TOKEN_CHARACTER;
int32_t len = 1;
while (c->len - len - 1) {
if (c->data[len] == '\\') {
if (c->data[len + 1] == 'x') {
len = lr_min(len + 4, (int32_t)c->len + 1);
continue;
} else {
len = lr_min(len + 2, (int32_t)c->len + 1);
continue;
}
}
if (c->data[len] == '\'')
break;
len++;
}
lr_slice_t token_slice = lr_slice_l(*c, len + 1);
*c = lr_slice_r(*c, len + 1);
token->type = token_type;
token->slice = token_slice;
return true;
} else {
return false;
}
}
bool lr_lex_string(lr_slice_t *c, c_token_t *token)
{
if (*c->data == '"') {
int32_t len = 1;
while (c->len - len - 1) {
if (c->data[len] == '\\') {
if (c->data[len + 1] == 'x') {
len = lr_min(len + 4, (int32_t)c->len + 1);
continue;
} else {
len = lr_min(len + 2, (int32_t)c->len + 1);
continue;
}
}
if (c->data[len] == '"')
break;
len++;
}
lr_slice_t token_slice = lr_slice_l(*c, len + 1);
*c = lr_slice_r(*c, len + 1);
token->type = LR_TOKEN_STRING;
token->slice = token_slice;
return true;
} else {
return false;
}
}
bool lr_lex_single_char_token(lr_slice_t *c, c_token_t *token)
{
uint32_t token_type;
lr_slice_t token_slice;
switch (*c->data) {
case '(': { token_type = LR_TOKEN_L_PAREN; break; }
case ')': { token_type = LR_TOKEN_R_PAREN; break; }
case '{': { token_type = LR_TOKEN_L_BRACE; break; }
case '}': { token_type = LR_TOKEN_R_BRACE; break; }
case '[': { token_type = LR_TOKEN_L_BRACKET; break; }
case ']': { token_type = LR_TOKEN_R_BRACKET; break; }
case '*': { token_type = LR_TOKEN_ASTERISK; break; }
case ',': { token_type = LR_TOKEN_COMMA; break; }
case '.': { token_type = LR_TOKEN_PERIOD; break; }
case '-': { token_type = LR_TOKEN_MINUS; break; }
case '+': { token_type = LR_TOKEN_PLUS; break; }
case '!': { token_type = LR_TOKEN_EXCLAMATION; break; }
case '~': { token_type = LR_TOKEN_TILDE; break; }
case '\\': { token_type = LR_TOKEN_BACKSLASH; break; }
case '/': { token_type = LR_TOKEN_SLASH; break; }
case '=': { token_type = LR_TOKEN_EQ; break; }
case '&': { token_type = LR_TOKEN_AMPERSAND; break; }
case '#': { token_type = LR_TOKEN_POUND; break; }
case ';': { token_type = LR_TOKEN_SEMICOLON; break; }
case ':': { token_type = LR_TOKEN_COLON; break; }
case '?': { token_type = LR_TOKEN_QUESTION_MARK; break; }
default: { token_type = LR_TOKEN_UNKNOWN; break; }
}
if (token_type != LR_TOKEN_UNKNOWN) {
token_slice = lr_slice_l(*c, 1);
*c = lr_slice_r(*c, 1);
token->type = token_type;
token->slice = token_slice;
return true;
} else {
return false;
}
}
c_token_t *lr_lex_file(lr_slice_t s, int32_t *count)
{
int32_t capacity = 64;
c_token_t *result = (c_token_t *)malloc(capacity * sizeof(c_token_t));
lr_slice_t c = s;