-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsource_spec.c
2907 lines (2666 loc) · 108 KB
/
source_spec.c
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 "bdd-for-c.h"
#include "pe32.c"
#include "value.c"
#include "instruction.c"
#include "encoding.c"
#include "function.c"
#include "source.c"
#include "program.c"
typedef struct {
u64 x;
u64 y;
} Test_128bit;
static_assert(sizeof(Test_128bit) * 8 == 128, "Expected a 128bit struct");
typedef struct {
u64 x;
u64 y;
u64 z;
} Test_192bit;
static_assert(sizeof(Test_192bit) * 8 == 192, "Expected a 192bit struct");
typedef struct {
u8 x;
u8 y;
u8 z;
} Test_24bit;
static_assert(sizeof(Test_24bit) * 8 == 24, "Expected a 24bit struct");
static bool
spec_check_mass_result_internal(
Compilation *compilation,
const Mass_Result *result
) {
if (result->tag == Mass_Result_Tag_Success) return true;
const Mass_Error *error = &result->Error.error;
Fixed_Buffer *error_buffer = mass_error_to_string(compilation, error);
slice_print(fixed_buffer_as_slice(error_buffer));
fixed_buffer_destroy(error_buffer);
printf("\n at ");
source_range_print_start_position(compilation, &result->Error.error.source_range);
return false;
}
#define spec_check_mass_result(_RESULT_)\
spec_check_mass_result_internal(test_context.compilation, (_RESULT_))
typedef u64 (*Spec_Callback)();
static u64 spec_callback() { return 42; }
#define spec_check_slice(_ACTUAL_, _EXPECTED_)\
do {\
Slice actual = (_ACTUAL_);\
Slice expected = (_EXPECTED_);\
if (!slice_equal(actual, expected)) {\
check(false, "Expected %"PRIslice", got %"PRIslice,\
SLICE_EXPAND_PRINTF(expected), SLICE_EXPAND_PRINTF(actual));\
}\
} while(0)
static const Slice test_file_name = slice_literal_fields("_test_.mass");
typedef enum {
Test_Program_Source_Tag_Inline,
Test_Program_Source_Tag_File,
} Test_Program_Source_Tag;
typedef struct {
Test_Program_Source_Tag tag;
union {
const char *text;
Slice path;
};
} Test_Program_Source;
static Source_Range
test_inline_source_range(
Compilation *compilation,
const char *source
) {
Slice text = slice_from_c_string(source);
Source_File *file = allocator_allocate(compilation->allocator, Source_File);
*file = (Source_File) {
.path = test_file_name,
.text = text,
};
return (Source_Range) {
.file = file,
.offsets = {.from = 0, .to = u64_to_u32(text.length), },
};
}
static inline Value *
test_program_source_base(
const char *id,
Mass_Context *context,
Test_Program_Source source
) {
program_load_file_module_into_root_scope(context, slice_literal("std/prelude"));
if (mass_has_error(context)) return 0;
Module *test_module = 0;
switch(source.tag) {
case Test_Program_Source_Tag_Inline: {
test_module = allocator_allocate(context->allocator, Module);
Source_Range source_range = test_inline_source_range(context->compilation, source.text);
*test_module = (Module) {
.source_range = source_range,
.own_scope = context->compilation->root_scope,
};
} break;
case Test_Program_Source_Tag_File: {
test_module = program_module_from_file(context, source.path, context->compilation->root_scope);
} break;
}
program_import_module(context, test_module);
if (mass_has_error(context)) return 0;
Slice id_slice = slice_from_c_string(id);
Source_Range symbol_source_range;
INIT_LITERAL_SOURCE_RANGE(&symbol_source_range, "__test_symbol__");
const Symbol *symbol = mass_ensure_symbol(context->compilation, id_slice);
Scope_Entry *entry = scope_lookup(context->compilation->root_scope, symbol);
Value *value = scope_entry_force_value(context, entry);
if (value && value->descriptor == &descriptor_function_literal) {
const Function_Literal *literal = value_as_function_literal(value);
Function_Info info;
mass_function_info_init_for_header_and_maybe_body(
context, literal->own_scope, &literal->header, literal->body, &info
);
if (mass_has_error(context)) return 0;
return ensure_function_instance(context, value, info.parameters);
}
return value;
}
fn_type_opaque
test_program_source_function(
const char *function_id,
Mass_Context *context,
Test_Program_Source source
) {
Value *value = test_program_source_base(function_id, context, source);
if (mass_has_error(context)) return 0;
if (!value) return 0;
static Jit test_jit;
jit_init(&test_jit, context->program);
program_jit(context, &test_jit);
if (mass_has_error(context)) return 0;
fn_type_opaque fn = value_as_function(test_jit.program, value);
if (mass_has_error(context)) return 0;
return fn;
}
static Value *
test_program_inline_source_base(
const char *id,
Mass_Context *context,
const char *source
) {
return test_program_source_base(id, context, (Test_Program_Source) {
.tag = Test_Program_Source_Tag_Inline,
.text = source,
});
}
fn_type_opaque
test_program_inline_source_function(
const char *function_id,
Mass_Context *context,
const char *source
) {
return test_program_source_function(function_id, context, (Test_Program_Source) {
.tag = Test_Program_Source_Tag_Inline,
.text = source,
});
}
static Value *
test_program_external_source_base(
const char *id,
Mass_Context *context,
const char *path
) {
return test_program_source_base(id, context, (Test_Program_Source) {
.tag = Test_Program_Source_Tag_File,
.path = slice_from_c_string(path),
});
}
static fn_type_opaque
test_program_external_source_function(
const char *function_id,
Mass_Context *context,
const char *path
) {
return test_program_source_function(function_id, context, (Test_Program_Source) {
.tag = Test_Program_Source_Tag_File,
.path = slice_from_c_string(path),
});
}
spec("source") {
static Compilation test_compilation = {0};
static Mass_Context test_context = {0};
before_each() {
compilation_init(&test_compilation, host_os());
test_context = mass_context_from_compilation(&test_compilation);
}
after_each() {
compilation_deinit(&test_compilation);
}
describe("Scope") {
it("should be able to set and lookup values") {
Value *test = value_init(
allocator_allocate(test_context.allocator, Value),
&descriptor_i64, imm64(42), (Source_Range){0}
);
Scope *root_scope = scope_make_declarative(test_context.allocator, 0);
const Symbol *symbol = mass_ensure_symbol(&test_compilation, slice_literal("test"));
scope_define_value(root_scope, (Epoch){0}, (Source_Range){0}, symbol, test);
Scope_Entry *entry = scope_lookup(root_scope, symbol);
check(entry->value == test);
}
it("should be able to lookup things from parent scopes") {
Value *global = value_init(
allocator_allocate(test_context.allocator, Value),
&descriptor_i64, imm64(42), (Source_Range){0}
);
Scope *root_scope = scope_make_declarative(test_context.allocator, 0);
const Symbol *global_symbol = mass_ensure_symbol(&test_compilation, slice_literal("global"));
scope_define_value(root_scope, (Epoch){0}, (Source_Range){0}, global_symbol, global);
const Symbol *test_symbol = mass_ensure_symbol(&test_compilation, slice_literal("test"));
Value *level_1_test = value_init(
allocator_allocate(test_context.allocator, Value),
&descriptor_i64, imm64(1), (Source_Range){0}
);
Scope *scope_level_1 = scope_make_declarative(test_context.allocator, root_scope);
scope_define_value(scope_level_1, (Epoch){0}, (Source_Range){0}, test_symbol, level_1_test);
Value *level_2_test = value_init(
allocator_allocate(test_context.allocator, Value),
&descriptor_i64, imm64(2), (Source_Range){0}
);
Scope *scope_level_2 = scope_make_declarative(test_context.allocator, scope_level_1);
scope_define_value(scope_level_2, (Epoch){0}, (Source_Range){0}, test_symbol, level_2_test);
Scope_Entry *entry = scope_lookup(scope_level_2, global_symbol);
check(entry->value == global);
}
}
describe("Tokenizer") {
it("should be able to tokenize an empty string") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(result.tag == Mass_Result_Tag_Success);
check(block.first_statement == 0);
}
it("should be able to tokenize a comment") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "// foo\n");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(result.tag == Mass_Result_Tag_Success);
check(block.first_statement == 0);
}
it("should count single-line comment as a statement separator") {
Source_Range source_range = test_inline_source_range(test_context.compilation, " a//\nb");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(result.tag == Mass_Result_Tag_Success);
check(block.first_statement->next == block.last_statement, "Expected 2 statements");
}
it("should be able to tokenize ids containing letters and _") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "foo_123");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(result.tag == Mass_Result_Tag_Success);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 1);
Value *token = value_view_get(&tokens, 0);
check(value_is_symbol(token));
}
it("should be able to turn newlines into fake semicolon tokens on top level") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "foo\n");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(result.tag == Mass_Result_Tag_Success);
check(block.first_statement == block.last_statement);
}
it("should be able to parse a lone 0") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "0");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 1);
Value *token = value_view_get(&tokens, 0);
spec_check_slice(source_from_source_range(test_context.compilation, &token->source_range), slice_literal("0"));
check(value_is_i64(token));
const i64 *literal = value_as_i64(token);
check(literal->bits == 0);
}
it("should be able to parse a 0 followed by an identifier") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "0foo");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 2);
{
Value *zero = value_view_get(&tokens, 0);
spec_check_slice(source_from_source_range(test_context.compilation, &zero->source_range), slice_literal("0"));
check(value_is_i64(zero));
const i64 *literal = value_as_i64(zero);
check(literal->bits == 0);
}
{
Value *foo = value_view_get(&tokens, 1);
spec_check_slice(source_from_source_range(test_context.compilation, &foo->source_range), slice_literal("foo"));
check(value_is_symbol(foo));
}
}
it("should be able to parse hex integers") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "0xCAFE");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 1);
Value *token = value_view_get(&tokens, 0);
spec_check_slice(source_from_source_range(test_context.compilation, &token->source_range), slice_literal("0xCAFE"));
check(value_is_i64(token));
const i64 *literal = value_as_i64(token);
check(literal->bits == 0xCAFE);
}
it("should be able to parse a hex integer followed by an identifier") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "0xfffoo");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 2);
{
Value *number = value_view_get(&tokens, 0);
spec_check_slice(source_from_source_range(test_context.compilation, &number->source_range), slice_literal("0xfff"));
check(value_is_i64(number));
const i64 *literal = value_as_i64(number);
check(literal->bits == 0xfff);
}
{
Value *symbol = value_view_get(&tokens, 1);
spec_check_slice(source_from_source_range(test_context.compilation, &symbol->source_range), slice_literal("oo"));
check(value_is_symbol(symbol));
}
}
it("should be able to parse binary integers") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "0b100");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 1);
Value *token = value_view_get(&tokens, 0);
spec_check_slice(source_from_source_range(test_context.compilation, &token->source_range), slice_literal("0b100"));
check(value_is_i64(token));
const i64 *literal = value_as_i64(token);
check(literal->bits == 0b100);
}
it("should be able to tokenize a sum of integer and a symbol") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "12 + foo123");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 3);
Value *a_num = value_view_get(&tokens, 0);
spec_check_slice(source_from_source_range(test_context.compilation, &a_num->source_range), slice_literal("12"));
Value *plus = value_view_get(&tokens, 1);
check(value_is_symbol(plus));
spec_check_slice(value_as_symbol(plus)->name, slice_literal("+"));
Value *id = value_view_get(&tokens, 2);
check(value_is_symbol(id));
spec_check_slice(source_from_source_range(test_context.compilation, &id->source_range), slice_literal("foo123"));
}
it("should be able to tokenize strings") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "\"foo 123\"");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 1);
Value *string = value_view_get(&tokens, 0);
spec_check_slice(source_from_source_range(test_context.compilation, &string->source_range), slice_literal("\"foo 123\""));
}
it("should be able to tokenize groups") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "(x)");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement == block.last_statement);
Value_View tokens = block.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 1);
Value *paren = value_view_get(&tokens, 0);
check(value_is_group_paren(paren));
check(value_as_group_paren(paren)->children.length == 1);
spec_check_slice(source_from_source_range(test_context.compilation, &paren->source_range), slice_literal("(x)"));
Value *id = value_view_get(&value_as_group_paren(paren)->children, 0);
check(value_is_symbol(id));
}
it("should be able to tokenize nested groups with different braces") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "{[]}");
Ast_Block root;
Mass_Result result = tokenize(&test_context, source_range, &root);
check(root.first_statement == root.last_statement);
Value_View tokens = root.first_statement->children;
check(result.tag == Mass_Result_Tag_Success);
check(tokens.length == 1);
Value *block_value = value_view_get(&tokens, 0);
check(value_is_ast_block(block_value));
const Ast_Block *block = value_as_ast_block(block_value);
check(block->first_statement == block->last_statement);
spec_check_slice(source_from_source_range(test_context.compilation, &block_value->source_range), slice_literal("{[]}"));
Value *square = value_view_get(&block->first_statement->children, 0);
check(value_is_group_square(square));
check(value_as_group_square(square)->children.length == 0);
spec_check_slice(source_from_source_range(test_context.compilation, &square->source_range), slice_literal("[]"));
}
it("should be able to tokenize complex input") {
Source_Range source_range = test_inline_source_range(
test_context.compilation,
"foo :: fn(x: s8) -> {\n"
" return x + 3;\n"
"}"
);
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(block.first_statement);
check(block.first_statement == block.last_statement);
check(result.tag == Mass_Result_Tag_Success);
}
it("should report a failure when encountering a brace that is not closed") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "(foo");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(result.tag == Mass_Result_Tag_Error);
Mass_Error *error = &result.Error.error;
check(error->tag == Mass_Error_Tag_Tokenizer);
check(error->source_range.file == source_range.file);
check(error->source_range.offsets.from == 4);
check(error->source_range.offsets.to == 4);
}
it("should report a failure when encountering a mismatched brace") {
Source_Range source_range = test_inline_source_range(test_context.compilation, "(foo}");
Ast_Block block;
Mass_Result result = tokenize(&test_context, source_range, &block);
check(result.tag == Mass_Result_Tag_Error);
Mass_Error *error = &result.Error.error;
check(error->tag == Mass_Error_Tag_Tokenizer);
check(error->source_range.file == source_range.file);
check(error->source_range.offsets.from == 4);
check(error->source_range.offsets.to == 4);
}
}
describe("if / else") {
it("should be able to parse and run if expression") {
u64(*checker)(u64) = (u64(*)(u64))test_program_inline_source_function(
"is_positive", &test_context,
"is_positive :: fn(x : i64) -> (i64) {"
"if x == 0 then 0 else 1"
"}"
);
check(spec_check_mass_result(test_context.result));
check(checker(42) == 1);
check(checker(0) == 0);
}
it("should work with a value instead of comparison as a condition") {
bool(*is_zero)(s32) = (bool(*)(s32))test_program_inline_source_function(
"is_zero", &test_context,
"is_zero :: fn(x : s32) -> (bool) {"
"if x != 0 then false else true"
"}"
);
check(spec_check_mass_result(test_context.result));
check(is_zero(42) == false);
check(is_zero(-2) == false);
check(is_zero(0) == true);
}
it("should correctly handle always-false condition") {
s64(*checker)() = (s64(*)())test_program_inline_source_function(
"is_positive", &test_context,
"is_positive :: fn() -> (s64) {"
"if false then 21 else 42"
"}"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 42);
}
it("should correctly handle always-true condition") {
s64(*checker)() = (s64(*)())test_program_inline_source_function(
"is_positive", &test_context,
"is_positive :: fn() -> (s64) {"
"if true then 42 else 21"
"}"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 42);
}
it("should report an error on missing `then` inside of an if expression") {
test_program_inline_source_base(
"main", &test_context,
"main :: fn() -> () { if true else 42 }"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
}
it("should report an error on double `then` inside of an if expression") {
test_program_inline_source_base(
"main", &test_context,
"main :: fn() -> () { if true then 0 then 42 }"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
}
it("should be able to parse and run if / else statement ") {
s8(*checker)(s32) = (s8(*)(s32))test_program_inline_source_function(
"is_positive", &test_context,
"is_positive :: fn(x : s32) -> (s8) {\n"
"if x < 10 then { return 0 } else { return 1 }"
"}"
);
check(spec_check_mass_result(test_context.result));
check(checker(42) == 1);
check(checker(-2) == 0);
}
it("should be able to parse and run an `if` statement without an `else` block") {
s8(*checker)(s32) = (s8(*)(s32))test_program_inline_source_function(
"is_positive", &test_context,
"is_positive :: fn(x : s32) -> (s8) {\n"
"if x < 10 then { return 0 }\n"
"1"
"}"
);
check(spec_check_mass_result(test_context.result));
check(checker(42) == 1);
check(checker(-2) == 0);
}
it("should report an error for an `if` statement without a body or a condition") {
test_program_inline_source_base(
"main", &test_context,
"main :: fn() -> () { if; }"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
}
}
describe("Functions") {
it("should be able to parse and run a void -> void function") {
void(*checker)(void) = (void(*)(void))test_program_inline_source_function(
"foo", &test_context,
"foo :: fn() -> () { }"
);
check(spec_check_mass_result(test_context.result));
checker();
}
it("should be able to parse and run a void -> s64 function") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"foo", &test_context,
"foo :: fn() -> (s64) { 42 }"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 42);
}
it("should support specifying a function signature separate from the body") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"foo", &test_context,
"My_Proc :: fn() -> (s64)\n"
"foo :: My_Proc { 42 }"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 42);
}
it("should correctly deal with sign extending negative immediate integers") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"foo", &test_context,
"foo :: fn() -> (s64) { -2147483647 }"
);
check(spec_check_mass_result(test_context.result));
check(checker() == -INT64_C(2147483647));
}
it("should correctly deal with larger than INT32_MAX immediate integers") {
u64(*checker)(void) = (u64(*)(void))test_program_inline_source_function(
"foo", &test_context,
"foo :: fn() -> (u64) { 2147483649 }"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 2147483649llu);
}
it("should correctly deal with larger than INT64_MAX immediate integers") {
u64(*checker)(void) = (u64(*)(void))test_program_inline_source_function(
"foo", &test_context,
"foo :: fn() -> (u64) { 18446744073709551615 }"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 18446744073709551615llu);
}
it("should be able to parse and run a function with 5 arguments") {
s8(*checker)(s8, s8, s8, s8, s8) = (s8(*)(s8, s8, s8, s8, s8))test_program_inline_source_function(
"foo", &test_context,
"foo :: fn(x1: s8, x2 : s8, x3 : s8, x4 : s8, x5 : s8) -> (s8) { x5 }"
);
check(spec_check_mass_result(test_context.result));
check(checker(1, 2, 3, 4, 5) == 5);
}
it("should correctly work with nested 5 argument calls") {
s8(*checker)(s8, s8, s8, s8, s8) = (s8(*)(s8, s8, s8, s8, s8))test_program_inline_source_function(
"foo", &test_context,
"foo :: fn(x1: s8, x2 : s8, x3 : s8, x4 : s8, x5 : s8) -> (s8) { bar(x1, x2, x3, x4, x5) + x5 }\n"
"bar :: fn(x1: s8, x2 : s8, x3 : s8, x4 : s8, x5 : s8) -> (s8) { x4 }"
);
check(spec_check_mass_result(test_context.result));
check(checker(1, 2, 3, 4, 5) == 9);
}
it("should correctly save volatile registers when calling other functions") {
s64(*checker)(s64) = (s64(*)(s64))test_program_inline_source_function(
"outer", &test_context,
"inner :: fn(x : s64) -> () { };"
"outer :: fn(x : s64) -> (s64) { inner(1); x }"
);
check(spec_check_mass_result(test_context.result));
s64 actual = checker(42);
check(actual == 42);
}
it("should correctly optimize call with a pointer coming from a stack") {
s64(*checker)(void *, void *, void *, void *, void *, void *) =
(s64(*)(void *, void *, void *, void *, void *, void *))test_program_inline_source_function(
"outer", &test_context,
"Payload :: c_struct [ lhs : i64 ];"
"inner :: fn(x1 : &Void, x2 : &Void, x3 : &Void, x4 : i64) -> _ { x4 };"
"outer :: fn(x1 : &Void, x2 : &Void, x3 : &Void, x4 : &Void, x5 : &Void, x6 : &Void) -> _ {\n"
" saved := cast(&Payload, x6)\n"
" inner(x1, x2, x3, saved.lhs)\n"
"}"
);
s64 expected = INT32_MAX;
check(spec_check_mass_result(test_context.result));
s64 actual = checker(0, 0, 0, 0, 0, &expected);
check(actual == expected);
}
it("should be able to define a local function") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn() -> (s64) { local :: fn() -> (s64) { 42 }; local() }"
);
check(spec_check_mass_result(test_context.result));
s64 answer = checker();
check(answer == 42);
}
it("should be able to make argument writable by reassigning it to a local variable") {
s64(*checker)(s64) = (s64(*)(s64))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn(x: s64) -> (s64) { x := x; x = x * 2; x }"
);
check(spec_check_mass_result(test_context.result));
s64 answer = checker(INT32_MAX);
check(answer == (s64)INT32_MAX * 2);
}
it("should be able to put a non-generic function literal into a typed local variable") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn() -> (s64) {"
"local : (fn() -> (s64)) = fn() -> (s64) { 42 };"
"local()"
"}"
);
check(spec_check_mass_result(test_context.result));
s64 answer = checker();
check(answer == 42);
}
it("should be able to put a generic function literal into a typed local variable") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn() -> (s64) {"
"local : (fn(x : s64) -> (s64)) = fn(x) -> (x) { x };"
"local(42)"
"}"
);
check(spec_check_mass_result(test_context.result));
s64 answer = checker();
check(answer == 42);
}
it("should be able to select from an overload set into a typed local variable") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn() -> (s64) {"
"foo :: fn(x : s64) -> (s64) { x }\n"
"foo :: fn(x : s32) -> (s32) { x }\n"
"local : (fn(x : s64) -> (s64)) = foo\n"
"local(42)"
"}"
);
check(spec_check_mass_result(test_context.result));
s64 answer = checker();
check(answer == 42);
}
it("should be able to put a function literal into an inferred local variable") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn() -> (s64) {"
"local := fn() -> (s64) { 42 };"
"local()"
"}"
);
check(spec_check_mass_result(test_context.result));
s64 answer = checker();
check(answer == 42);
}
it("should be able to select from an overload set into a function argument") {
s64(*checker)(void) = (s64(*)(void))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn() -> (s64) {"
"foo :: fn(x : s64) -> (s64) { x }\n"
"foo :: fn(x : s32) -> (s32) { x }\n"
"callback :: fn(f : fn(x : s64) -> (s64)) -> (s64) { 42 }\n"
"callback(foo)"
"}"
);
check(spec_check_mass_result(test_context.result));
s64 answer = checker();
check(answer == 42);
}
it("should be able to parse and run functions with overloads") {
s64(*checker)(s32) = (s64(*)(s32))test_program_inline_source_function(
"checker", &test_context,
"my_size_of :: fn(x : s32) -> (s64) { 4 }\n"
"my_size_of :: fn(x : s64) -> (s64) { 8 }\n"
"checker :: fn(x : s32) -> (s64) { my_size_of(x) }\n"
);
check(spec_check_mass_result(test_context.result));
s64 size = checker(0);
check(size == 4);
}
// TODO support either
// * lookup in parents and merging in of that overload set
// * some way to manually merge it in via smth like
// `my_size_of :: @scope.parent.my_size_of | fn(x : s64) -> (s64) { 8 }`
xit("should be able to parse and run functions with local overloads") {
s64(*checker)(s32) = (s64(*)(s32))test_program_inline_source_function(
"checker", &test_context,
"my_size_of :: fn(x : s32) -> (s64) { 4 }\n"
"checker :: fn(x : s32) -> (s64) {\n"
"my_size_of :: fn(x : s64) -> (s64) { 8 }\n"
"my_size_of(x)\n"
"}"
);
check(spec_check_mass_result(test_context.result));
s64 size = checker(0);
check(size == 4);
}
it("should prefer exact static value overload") {
s64(*checker)() = (s64(*)())test_program_inline_source_function(
"checker", &test_context,
"foo :: fn(type :: s32) -> (s64) { 42 }\n"
"foo :: fn(type : Type) -> (s64) { 21 }\n"
"checker :: fn() -> (s64) { foo(s32) }\n"
);
check(spec_check_mass_result(test_context.result));
s64 actual = checker();
check(actual == 42);
}
it("should report an overload overlap") {
test_program_inline_source_base(
"test", &test_context,
"overload :: fn(x : s64, y : s32) -> () { }\n"
"overload :: fn(x : s32, y : s64) -> () { }\n"
"test :: fn() -> () { overload(1, 2) }"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
Mass_Error *error = &test_context.result->Error.error;
check(error->tag == Mass_Error_Tag_Undecidable_Overload);
}
it("should support overload resolution based on fields in the tuple") {
s64(*checker)() = (s64(*)())test_program_inline_source_function(
"checker", &test_context,
"Bar :: c_struct[x:i64]\n"
"foo :: fn(x : [s64]) -> (s64) { 21 }\n"
"foo :: fn(x : [Bar]) -> (s64) { 42 }\n"
"checker :: fn() -> (s64) {\n"
"bar := Bar [1]\n"
"foo([bar])\n"
"}"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 42);
}
it("should report an error when trying to overload a non-function") {
test_program_inline_source_base(
"overload", &test_context,
"overload :: 42\n"
"overload :: 21\n"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
Mass_Error *error = &test_context.result->Error.error;
check(error->tag == Mass_Error_Tag_Non_Function_Overload);
}
it("should support default arguments") {
u64(*checker)(void) = (u64(*)(void))test_program_inline_source_function(
"test", &test_context,
"test_default :: fn(x : i64, y : i64 = 20) -> (i64) { x + y }\n"
"test :: fn() -> (i64) { (test_default(20) + \n test_default(0, 2)) }"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 42);
}
it("should support default arguments with inference") {
u64(*checker)(void) = (u64(*)(void))test_program_inline_source_function(
"test", &test_context,
"test_default :: fn(x : i64, y := 20) -> (i64) { x + y }\n"
"test :: fn() -> (i64) { (test_default(20) + \n test_default(0, 2)) }"
);
check(spec_check_mass_result(test_context.result));
check(checker() == 42);
}
it("should disallow default arguments coming after non-default ones") {
test_program_inline_source_function(
"test", &test_context,
"test :: fn(x : i64, y : i64 = 20, z : i64) -> () {}\n"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
Mass_Error *error = &test_context.result->Error.error;
check(error->tag == Mass_Error_Tag_Non_Trailing_Default_Argument);
}
it("should support capturing static arguments") {
u64(*checker)(void) = (u64(*)(void))test_program_inline_source_function(
"checker", &test_context,
"checker :: { ANSWER :: 42; fn() -> (i64) { ANSWER } }\n"
);
check(spec_check_mass_result(test_context.result));
u64 actual = checker();
check(actual == 42);
}
it("should be able to have an explicit return") {
u64(*checker)(u64) = (u64(*)(u64))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn(x : i64) -> (i64) { if x == 0 then { return 42 }; 0 }"
);
check(spec_check_mass_result(test_context.result));
u64 actual = checker(0);
check(actual == 42);
}
it("should be able to have an explicit return at the end of function") {
u64(*checker)(u64) = (u64(*)(u64))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn(x : i64) -> (i64) { return x }"
);
check(spec_check_mass_result(test_context.result));
u64 actual = checker(42);
check(actual == 42);
}
it("should report an error when unreacheable statements are found after a return") {
test_program_inline_source_function(
"checker", &test_context,
"checker :: fn(x : i64) -> (i64) { return x; x + 2 }"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
Mass_Error *error = &test_context.result->Error.error;
check(error->tag == Mass_Error_Tag_Unreachable_Statement);
}
it("should be able to have an explicit return without any expression following") {
void(*checker)(u64 *) = (void(*)(u64 *))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn(x : &i64) -> () { if x.* == 0 then { return }; x.* = 42 }"
);
check(spec_check_mass_result(test_context.result));
u64 number = 21;
checker(&number);
check(number == 42);
}
it("should be able to run fibonnacii") {
s64(*fibonnacci)(s64) = (s64(*)(s64))test_program_inline_source_function(
"fibonnacci", &test_context,
"fibonnacci :: fn(n : s64) -> (s64) {\n"
"if n < 2 then n else fibonnacci(n - 1) + fibonnacci(n - 2)"
"}"
);
check(spec_check_mass_result(test_context.result));
check(fibonnacci(0) == 0);
check(fibonnacci(1) == 1);
check(fibonnacci(10) == 55);
}
it("should support inferred return types for a non-recursive fn with no explicit returns") {
u64(*checker)(u64) = (u64(*)(u64))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn(x: i64) -> _ { x }"
);
check(spec_check_mass_result(test_context.result));
check(checker(42) == 42);
}
it("should support inferred return types for a non-recursive fn with an explicit return at the end") {
u64(*checker)(u64) = (u64(*)(u64))test_program_inline_source_function(
"checker", &test_context,
"checker :: fn(x: i64) -> _ { return x }"
);
check(spec_check_mass_result(test_context.result));
check(checker(42) == 42);
}
// :RecursiveInferredType
it("should report an error when trying to infer a type of a recursive fn") {
test_program_inline_source_base(
"checker", &test_context,
"checker :: fn(x: i64) -> _ { checker(1) }"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
}
it("should report an error when non-type id is being used as a type") {
test_program_inline_source_base(
"main", &test_context,
"foo :: fn() -> () {};"
"main :: fn(arg : foo) -> () {}"
);
check(test_context.result->tag == Mass_Result_Tag_Error);
Mass_Error *error = &test_context.result->Error.error;
check(error->tag == Mass_Error_Tag_Type_Mismatch);
check(error->Type_Mismatch.expected == &descriptor_type);