-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_and_vars.c
2299 lines (2152 loc) · 80 KB
/
types_and_vars.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
/*
* Here we store variable declarations
* and type check our abstract syntax tree
*/
#include "types_and_vars.h"
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#ifndef __pure
# define __pure __attribute__((pure))
#endif
#define EPFX "typecheck: error: "
#define LEPFX "typecheck:%d: error: "
#define PFX "typecheck: "
// for where our code should never reach if case statements are complete
#define FAIL_MISSED_CASE() do { \
fprintf(stderr, "function %s, file %s, line %d\n", \
__FUNCTION__, __FILE__, __LINE__); \
abort(); \
} while(0)
/* See header file */
int debug_type_checker = 0;
/*
* Keep a table of global type names
*/
#define TYPE_NAMES_MAX 1024
static Type* type_names[TYPE_NAMES_MAX];
static int type_name_count = 0;
/* A numbering to use for type contraints */
#define TYPE_CONTRAINTS_MAX 1024
static int next_constraint_id = 0;
static TypeExpr* constraint_theories[TYPE_CONTRAINTS_MAX];
/*
* A stack of variable names that may help us type expressions
*/
#define VAR_MAX 1024
static struct Var {
Symbol name;
TypeExpr* type;
} variable_stack[VAR_MAX];
/* ptr to the end of the stack */
static struct Var* var_stack_ptr = variable_stack;
/* make life easier printing debug messaged */
#define dbgprint(M, ...) do { \
if (debug_type_checker) { fprintf(stderr, PFX M, ##__VA_ARGS__); } \
} while(0)
#define dbgtprintf(M, ...) do { \
if (debug_type_checker) { tprintf(stderr, PFX M, ##__VA_ARGS__); } \
} while(0)
static TypeExpr* lookup_ornull_typexpr(Symbol name)
{
for (int i = 0; i < type_name_count; i++) {
if (type_names[i]->name == name) {
return type_names[i]->definition;
}
}
return NULL;
}
static TypeExpr* lookup_typexpr(Symbol name)
{
TypeExpr* res = lookup_ornull_typexpr(name);
if (res)
return res;
fprintf(stderr, "%s\n", name);
assert(0 && "type not in type_names table");
FAIL_MISSED_CASE();
}
static TypeExpr* deref_typexpr(Symbol name)
{
for (;;) {
TypeExpr* dereffed = lookup_typexpr(name);
if (dereffed->tag != TYPE_NAME || dereffed->name == name) {
return dereffed;
}
name = dereffed->name;
}
}
__pure static _Bool typexpr_equals(TypeExpr* left, TypeExpr* right)
{
// deref and names as far as possible, then compare trees
// normalize input cases
if (left->tag > right->tag) {
return typexpr_equals(right, left);
}
assert(left->tag <= right-> tag);
switch (left->tag)
{
case TYPE_NAME:
{
switch (right->tag)
{
case TYPE_NAME:
{
// compare names, otherwise compare dereffed
if (left->name == right->name)
return 1;
TypeExpr* dleft = deref_typexpr(left->name);
TypeExpr* dright = deref_typexpr(right->name);
if (dleft->name == left->name && dright->name == right->name) {
return 0;
}
return typexpr_equals(dleft, dright);
}
case TYPE_ARROW:
case TYPE_CONSTRAINT:
case TYPE_TUPLE:
case TYPE_CONSTRUCTOR:
{
TypeExpr* dleft = deref_typexpr(left->name);
if (dleft->name == left->name)
return 0;
return typexpr_equals(dleft, right);
}
}
}
case TYPE_ARROW: // both type_arrows or right is constraint
{
if (right->tag != TYPE_ARROW) {
return 0;
}
return typexpr_equals(left->left, right->left)
&& typexpr_equals(left->right, right->right);
}
case TYPE_CONSTRAINT:
{
if (right->tag != TYPE_CONSTRAINT) {
return 0; // could conform but not equal
}
return left->constraint_id == right->constraint_id;
}
case TYPE_TUPLE:
{
if (right->tag != TYPE_TUPLE) {
return 0;
}
for (TypeExprList* l = left->type_list, * r = right->type_list;
l || r; l = l->next, r = r->next)
{
if (!(l && r)) {
return 0; // not same length
}
if (!typexpr_equals(l->type, r->type)) {
return 0;
}
}
return 1;
}
case TYPE_CONSTRUCTOR:
{
// until we add some more type expressions
assert(right->tag == TYPE_CONSTRUCTOR);
return left->constructor == right->constructor
&& typexpr_equals(left->param, right->param);
}
}
FAIL_MISSED_CASE();
}
static TypeExpr* deref_if_needed(TypeExpr* t)
{
if (t->tag == TYPE_NAME)
return deref_typexpr(t->name);
return t;
}
__pure static _Bool typexpr_conforms_to(TypeExpr* left, TypeExpr* right)
{
/*
* probably instead of NULL we should be using a special
* type tag... or assigning special names
*/
if (left->tag == TYPE_CONSTRAINT || right->tag == TYPE_CONSTRAINT)
return 1;
if (typexpr_equals(left, right))
return 1;
// fully expand
TypeExpr* dleft = deref_if_needed(left);
TypeExpr* dright = deref_if_needed(right);
// if either were name then they don't match
if (dleft->tag == TYPE_NAME || dright->tag == TYPE_NAME) {
return 0;
}
if (dleft->tag != dright->tag) {
return 0;
}
if (dleft->tag == TYPE_CONSTRUCTOR) {
return left->constructor == right->constructor
&& typexpr_conforms_to(left->param, right->param);
}
if (dleft->tag == TYPE_TUPLE) {
for (TypeExprList* l = left->type_list, * r = right->type_list;
l || r; l = l->next, r = r->next)
{
if (!(l && r)) {
return 0; // not same length
}
if (!typexpr_conforms_to(l->type, r->type)) {
return 0;
}
}
return 1;
}
// Must be both both arrow
return typexpr_conforms_to(left->left, right->left)
&& typexpr_conforms_to(left->right, right->right);
}
static TypeExpr* new_type_constraint()
{
if (next_constraint_id >= TYPE_CONTRAINTS_MAX) {
// TODO: attempt to reduce in here
fprintf(stderr, EPFX"more than %d unconstrained type variables\n",
TYPE_CONTRAINTS_MAX);
exit(EXIT_FAILURE);
}
return typeconstraint(next_constraint_id++);
}
static void add_type_name(Type* node)
{
if (type_name_count >= TYPE_NAMES_MAX) {
fprintf(stderr, EPFX"more than %d typenames defined\n", TYPE_NAMES_MAX);
exit(EXIT_FAILURE);
}
if (lookup_ornull_typexpr(node->name) != NULL) {
fprintf(stderr, EPFX"type %s has already been defined\n",
node->name);
exit(EXIT_FAILURE);
}
dbgprint("adding named type: %s\n", node->name);
type_names[type_name_count++] = node;
}
static void add_builtin_type(const char* name_of_type)
{
Type* type_node = malloc(sizeof *type_node);
type_node->name = symbol(name_of_type);
type_node->definition = typename(type_node->name);
add_type_name(type_node);
}
static void push_var(Symbol name, TypeExpr* type)
{
if (var_stack_ptr >= variable_stack + VAR_MAX) {
fprintf(stderr, EPFX"more than %d variable bindings in single scope\n",
VAR_MAX);
exit(EXIT_FAILURE);
}
if (debug_type_checker) {
if (type) {
tprintf(stderr, PFX"pushing var %s : %T\n", name, type);
} else {
fprintf(stderr, PFX"pushing var %s\n", name);
}
}
*var_stack_ptr++ = (struct Var) { name, type };
}
/*
* Lookup @name in the symbol table
* Giving @lineno of the ast node
*/
static struct Var* lookup_var(Symbol name, int lineno)
{
struct Var* end = var_stack_ptr;
while (--end >= variable_stack) {
if (end->name == name) {
return end;
}
}
fprintf(stderr, LEPFX"value %s not in scope\n", lineno, name);
exit(EXIT_FAILURE);
}
static void print_type_error(TypeExpr* expected, TypeExpr* actual)
{
tprintf(stderr, " expected: %T\n actual: %T\n", expected, actual);
}
__attribute__((warn_unused_result))
__pure static _Bool solid_type(TypeExpr* type)
{
if (!type)
return 0;
switch (type->tag) {
case TYPE_NAME:
return 1;
case TYPE_ARROW:
return solid_type(type->left) && solid_type(type->right);
case TYPE_CONSTRAINT:
return 0;
case TYPE_CONSTRUCTOR:
return solid_type(type->param);
case TYPE_TUPLE:
for (TypeExprList* l = type->type_list; l; l = l->next) {
if (!solid_type(l->type))
return 0;
}
return 1;
}
FAIL_MISSED_CASE();
}
static _Bool type_uses_constraint(TypeExpr* type, int constraint_id)
{
if (!type)
return 0;
switch (type->tag)
{
// the dereffed might, but dereffing will get the new value
case TYPE_NAME: return 0;
case TYPE_ARROW: return type_uses_constraint(type->left, constraint_id)
|| type_uses_constraint(type->right, constraint_id);
case TYPE_CONSTRAINT: return type->constraint_id == constraint_id;
case TYPE_CONSTRUCTOR: return type_uses_constraint(type->param, constraint_id);
case TYPE_TUPLE:
{
for (TypeExprList* l = type->type_list; l; l = l->next) {
if (type_uses_constraint(l->type, constraint_id))
return 1;
}
return 0;
}
}
FAIL_MISSED_CASE();
}
static TypeExpr* replaced_constraint(TypeExpr*, int, TypeExpr* );
static TypeExprList* replaced_constraint_list(
TypeExprList* toreplace, int constraint_id, TypeExpr* theory)
{
TypeExprList* tmp = NULL;
for (TypeExprList* l = toreplace; l; l = l->next) {
tmp = type_add(tmp, replaced_constraint(l->type, constraint_id, theory));
}
__auto_type result = reversed_types(tmp);
while (tmp) {
__auto_type tmp2 = tmp->next;
free((void*)tmp); // cast away const
tmp = tmp2;
}
return result;
}
__attribute__((warn_unused_result))
static TypeExpr* replaced_constraint(
TypeExpr* toreplace, int constraint_id, TypeExpr* theory)
{
switch (toreplace->tag)
{
case TYPE_NAME:
return toreplace;
case TYPE_ARROW:
return typearrow(
replaced_constraint(toreplace->left, constraint_id, theory),
replaced_constraint(toreplace->right, constraint_id, theory));
case TYPE_CONSTRAINT:
return toreplace->constraint_id == constraint_id ?
theory : toreplace;
case TYPE_TUPLE:
return typetuple(
replaced_constraint_list(
toreplace->type_list, constraint_id, theory));
case TYPE_CONSTRUCTOR:
return typeconstructor(
replaced_constraint(toreplace->param, constraint_id, theory),
toreplace->constructor);
}
FAIL_MISSED_CASE();
}
static void replace_constraint_if_needed(
TypeExpr** ptoreplace, int constraint_id, TypeExpr* theory)
{
if (type_uses_constraint(*ptoreplace, constraint_id)) {
*ptoreplace = replaced_constraint(*ptoreplace, constraint_id, theory);
}
}
// pre-declare because recursive calls
static void apply_theory_expr(Expr* expr, int constraint_id, TypeExpr* theory);
/*
* Replace the uses of type variables with given constraint_id with theory
*/
static void apply_theory_params(
ParamList* params, int constraint_id, TypeExpr* theory)
{
for (; params; params = params->next) {
Param* p = params->param;
replace_constraint_if_needed(&p->type, constraint_id, theory);
}
}
static void apply_theory_pat(Pattern* pat, int constraint_id, TypeExpr* theory)
{
replace_constraint_if_needed(&pat->type, constraint_id, theory);
switch (pat->tag)
{
case PAT_VAR:
case PAT_DISCARD:
break;
case PAT_CONS:
apply_theory_pat(pat->left, constraint_id, theory);
apply_theory_pat(pat->right, constraint_id, theory);
break;
case PAT_TUPLE:
for (PatternList* l = pat->pat_list; l; l = l->next) {
apply_theory_pat(l->pattern, constraint_id, theory);
}
break;
case PAT_CTOR_NOARG:
break;
case PAT_CTOR_WARG:
apply_theory_pat(pat->ctor_arg, constraint_id, theory);
break;
case PAT_INT:
case PAT_STR:
case PAT_NIL:
break;
}
}
static void apply_theory_cases(CaseList* cases, int constraint_id, TypeExpr* theory)
{
for (; cases; cases = cases->next) {
apply_theory_pat(cases->kase->pattern, constraint_id, theory);
apply_theory_expr(cases->kase->expr, constraint_id, theory);
}
}
/*
* Replace the uses of type variables with given constraint_id with theory
*/
static void apply_theory_expr(Expr* expr, int constraint_id, TypeExpr* theory)
{
replace_constraint_if_needed(&expr->type, constraint_id, theory);
switch (expr->tag)
{
case PLUS:
case MINUS:
case MULTIPLY:
case DIVIDE:
case EQUAL:
case LESSTHAN:
case LESSEQUAL:
case APPLY:
apply_theory_expr(expr->left, constraint_id, theory);
apply_theory_expr(expr->right, constraint_id, theory);
break;
case VAR:
case UNITVAL:
case INTVAL: // Nothing to do here
case STRVAL:
break;
case RECFUNC_EXPR:
case FUNC_EXPR:
apply_theory_params(expr->func.params, constraint_id, theory);
apply_theory_expr(expr->func.body, constraint_id, theory);
apply_theory_expr(expr->func.subexpr, constraint_id, theory);
replace_constraint_if_needed(&expr->func.functype, constraint_id, theory);
replace_constraint_if_needed(&expr->func.resulttype, constraint_id, theory);
break;
case BIND_EXPR:
apply_theory_pat(expr->binding.pattern, constraint_id, theory);
apply_theory_expr(expr->binding.init, constraint_id, theory);
apply_theory_expr(expr->binding.subexpr, constraint_id, theory);
break;
case IF_EXPR:
apply_theory_expr(expr->condition, constraint_id, theory);
apply_theory_expr(expr->btrue, constraint_id, theory);
apply_theory_expr(expr->bfalse, constraint_id, theory);
break;
case LIST:
case VECTOR:
case TUPLE:
for (ExprList* l = expr->expr_list; l; l = l->next) {
apply_theory_expr(l->expr, constraint_id, theory);
}
break;
case EXTERN_EXPR:
assert(expr->tag != EXTERN_EXPR && "not expected");
break;
case MATCH_EXPR:
apply_theory_expr(expr->matchexpr, constraint_id, theory);
apply_theory_cases(expr->cases, constraint_id, theory);
break;
case DIRECT_CALL:
assert(expr->tag != DIRECT_CALL && "not expected");
break;
}
}
/*
* Replace the uses of type variables with given constraint_id with theory
*/
static void apply_theory(
DeclarationList* root, int constraint_id, TypeExpr* theory)
{
for (DeclarationList* c = root; c; c = c->next) {
Declaration* decl = c->declaration;
switch (decl->tag)
{
case DECL_EXTERN:
case DECL_TYPE:
case DECL_TYPECTOR:
break;
case DECL_BIND:
{
replace_constraint_if_needed(
&decl->binding.type, constraint_id, theory);
apply_theory_pat(decl->binding.pattern, constraint_id, theory);
apply_theory_expr(decl->binding.init, constraint_id, theory);
break;
}
case DECL_RECFUNC:
case DECL_FUNC:
{
replace_constraint_if_needed(&decl->func.type, constraint_id, theory);
replace_constraint_if_needed(&decl->func.resulttype, constraint_id, theory);
apply_theory_params(decl->func.params, constraint_id, theory);
apply_theory_expr(decl->func.body, constraint_id, theory);
break;
}
}
}
}
static void check_type_names_are_declared(TypeExpr* typexpr)
{
switch (typexpr->tag)
{
case TYPE_NAME:
{
if (!lookup_ornull_typexpr(typexpr->name)) {
fprintf(stderr, "type name %s was not declared\n",
typexpr->name);
exit(EXIT_FAILURE);
}
return;
}
case TYPE_ARROW:
{
check_type_names_are_declared(typexpr->left);
check_type_names_are_declared(typexpr->right);
return;
}
case TYPE_CONSTRAINT:
{
return;
}
case TYPE_TUPLE:
{
for (TypeExprList* l = typexpr->type_list; l; l = l->next) {
check_type_names_are_declared(l->type);
}
return;
}
case TYPE_CONSTRUCTOR:
{
// TODO: need to check the constructor name has been declared
check_type_names_are_declared(typexpr->param);
return;
}
}
}
static int len_typelist(int acc, TypeExprList* list)
{
if (!list) {
return acc;
}
return len_typelist(1 + acc, list->next);
}
#define len_typelist(x) len_typelist(0, x)
/*
* Attempt to add a theory that these types are equal, which we will later
* try to unify.
* @param etype
* Existing type - usually the type annotation that is already
* attached to the tree. The non-solid type.
* @param newtype
* The more solid type we have worked out applies for the expression also
*/
static int theorise_equal(TypeExpr* etype, TypeExpr* newtype)
{
switch (etype->tag) {
case TYPE_CONSTRAINT:
{
const int eid = etype->constraint_id;
// Check whether the theories on this constraint have been updated
if (constraint_theories[eid]) {
if (debug_type_checker
&& (newtype->tag != TYPE_CONSTRAINT
|| newtype->constraint_id != eid)) {
dbgtprintf("constraint %d already has theory %T, ignore "
"theory %T\n", eid, constraint_theories[eid], newtype);
}
// ignore newtype
return 0;
} else if (newtype->tag != TYPE_CONSTRAINT) {
dbgtprintf("'%d <- %T\n", eid, newtype);
constraint_theories[eid] = newtype;
return 1;
} else {
assert(newtype->tag == TYPE_CONSTRAINT);
const int nid = newtype->constraint_id;
if (nid == eid) {
return 0;
} else if (nid < eid) {
constraint_theories[eid] = newtype;
return 1;
} else {
assert(nid > eid);
// repoint highest ID to lowest
if (constraint_theories[nid]) {
// Check their theory isn't us
if (constraint_theories[nid] == etype) {
return 0;
}
// steal their theory since we don't have one
dbgtprintf("'%d <- %T\n", eid, constraint_theories[nid]);
constraint_theories[eid] = constraint_theories[nid];
}
// Make us their theory
dbgtprintf("'%d <- %T\n", nid, etype);
constraint_theories[nid] = etype;
return 1;
}
}
}
case TYPE_ARROW:
{
if (newtype->tag != TYPE_ARROW) {
return 0;
}
return theorise_equal(etype->left, newtype->left)
+ theorise_equal(etype->right, newtype->right);
}
case TYPE_NAME:
return 0;
case TYPE_CONSTRUCTOR:
if (newtype->tag != TYPE_CONSTRUCTOR)
return 0;
if (etype->constructor == newtype->constructor) {
return theorise_equal(etype->param, newtype->param);
}
return 0;
case TYPE_TUPLE:
{
if (newtype->tag != TYPE_TUPLE) { return 0; }
if (len_typelist(etype->type_list) != len_typelist(newtype->type_list)) {
// Not sure whether it should make it this far
fprintf(stderr,
"warn: theorising equal different sized tuple types");
return 0;
}
int types_added = 0;
for (TypeExprList* l = etype->type_list, * r = newtype->type_list;
l; l = l->next, r = r->next)
{
types_added += theorise_equal(l->type, r->type);
}
return types_added;
}
}
FAIL_MISSED_CASE();
}
__attribute__((const))
static const char* expr_name(enum ExprTag tag)
{
switch (tag) {
case PLUS: return "plus";
case MINUS: return "minus";
case MULTIPLY: return "multiply";
case DIVIDE: return "divide";
case EQUAL: return "equal";
case LESSTHAN: return "less than";
case LESSEQUAL: return "less than or equal";
case APPLY: return "apply";
case VAR: return "var";
case UNITVAL: return "unit";
case INTVAL: return "int";
case STRVAL: return "string";
case FUNC_EXPR: return "func";
case RECFUNC_EXPR: return "recfunc";
case BIND_EXPR: return "let";
case IF_EXPR: return "if";
case LIST: return "list";
case VECTOR: return "vector";
case TUPLE: return "tuple";
case EXTERN_EXPR: return "extern";
case MATCH_EXPR: return "match";
case DIRECT_CALL: return "direct-call";
}
FAIL_MISSED_CASE();
}
#define typexpr_equals_or_exit(LN,ET,AT,M,...) do { \
int __line = LN; \
TypeExpr* __et = (ET); \
TypeExpr* __at = (AT); \
if (!typexpr_equals(__et, __at)) { \
fprintf(stderr, LEPFX M "\n", __line, ##__VA_ARGS__); \
print_type_error(__et, __at); \
exit(EXIT_FAILURE); \
} \
} while (0)
#define typexpr_conforms_or_exit(LN,ET,AT,M,...) do { \
int __line = LN; \
TypeExpr* __et = (ET); \
TypeExpr* __at = (AT); \
if (!typexpr_conforms_to(__et, __at)) { \
tprintf(stderr, LEPFX M "\n", __line, ##__VA_ARGS__); \
print_type_error(__et, __at); \
exit(EXIT_FAILURE); \
} \
} while (0)
static int count_names_pat(Pattern* pat);
static int count_names_pat_list(PatternList* l, int acc)
{
if (!l) return acc;
return count_names_pat_list(l->next, acc + count_names_pat(l->pattern));
}
static int count_names_pat(Pattern* pat)
{
switch (pat->tag)
{
case PAT_VAR: return 1;
case PAT_DISCARD: return 0;
case PAT_CONS: return count_names_pat(pat->left)
+ count_names_pat(pat->right);
case PAT_TUPLE: return count_names_pat_list(pat->pat_list, 0);
case PAT_CTOR_WARG: return count_names_pat(pat->ctor_arg);
case PAT_CTOR_NOARG: /* fall through */
case PAT_INT:
case PAT_STR:
case PAT_NIL: return 0;
}
FAIL_MISSED_CASE();
}
static void add_names_to_array_pat(Pattern* pat, Symbol** ptr_next)
{
switch (pat->tag)
{
case PAT_VAR:
*(*ptr_next)++ = pat->name;
return;
case PAT_DISCARD: return;
case PAT_CONS:
add_names_to_array_pat(pat->left, ptr_next);
add_names_to_array_pat(pat->right, ptr_next);
return;
case PAT_TUPLE:
for (PatternList* l = pat->pat_list; l; l = l->next) {
add_names_to_array_pat(l->pattern, ptr_next);
}
return;
case PAT_CTOR_WARG:
add_names_to_array_pat(pat->ctor_arg, ptr_next);
return;
case PAT_CTOR_NOARG: return;
case PAT_INT: return;
case PAT_STR: return;
case PAT_NIL: return;
}
}
static int pat_list_len(PatternList* list)
{
int n = 0;
for (; list; list = list->next)
n++;
return n;
}
static void pattern_match_and_push_vars0(Pattern* pat, TypeExpr* init_type)
{
switch (pat->tag)
{
case PAT_VAR:
{
if (pat->type) {
typexpr_conforms_or_exit(pat->an_line, pat->type, init_type,
"name %s in let binding", pat->name);
theorise_equal(pat->type, init_type);
theorise_equal(init_type, pat->type);
} else {
pat->type = init_type; // types_added++?
}
push_var(pat->name, init_type);
break;
}
case PAT_DISCARD:
{
if (pat->type) {
typexpr_conforms_or_exit(pat->an_line, pat->type, init_type,
"_ in let binding");
theorise_equal(pat->type, init_type);
theorise_equal(init_type, pat->type);
} else {
pat->type = init_type; //types_added++?
}
break;
}
case PAT_CONS:
{
init_type = deref_if_needed(init_type);
// Now we need to have that either init_type is a list type or a
// constraint type we can suggest is an
if (init_type->tag == TYPE_CONSTRAINT) {
if (!pat->left->type) {
pat->left->type = new_type_constraint();
}
if (!pat->type) {
pat->type = typeconstructor(pat->left->type, symbol("list"));
}
theorise_equal(init_type, pat->type);
if (!pat->right->type) {
pat->right->type = pat->type;
}
pattern_match_and_push_vars0(pat->left, pat->left->type);
pattern_match_and_push_vars0(pat->right, pat->right->type);
} else if (init_type->tag == TYPE_CONSTRUCTOR
&& init_type->constructor == symbol("list")) {
pattern_match_and_push_vars0(pat->left, init_type->param);
pattern_match_and_push_vars0(pat->right, init_type);
} else {
tprintf(stderr,
LEPFX"expected list type in init for pattern %P\n",
pat->an_line, pat);
TypeExpr* expected = typeconstructor(
typename(symbol("'a")), symbol("list"));
print_type_error(expected, init_type);
free((void*)expected->param); // cast away const :'(
free((void*)expected);
exit(EXIT_FAILURE);
}
if (pat->type) {
typexpr_conforms_or_exit(pat->an_line, pat->type, init_type,
"pattern %P in let binding", pat);
theorise_equal(pat->type, init_type);
theorise_equal(init_type, pat->type);
} else {
pat->type = init_type;
}
break;
}
case PAT_TUPLE:
{
init_type = deref_if_needed(init_type);
if (init_type->tag == TYPE_CONSTRAINT) {
int ntypes = pat_list_len(pat->pat_list);
TypeExpr** typstack = malloc(ntypes * sizeof *typstack);
if (!typstack) { perror("out of memory"); abort(); }
int typstack_idx = 0;
for (PatternList* l = pat->pat_list; l; l = l->next) {
if (!l->pattern->type) {
typstack[typstack_idx++] =
l->pattern->type = new_type_constraint();
} else {
typstack[typstack_idx++] = l->pattern->type;
}
}
if (!pat->type) {
TypeExprList* pat_types = NULL;
for (int i = ntypes - 1; i >= 0; --i) {
pat_types = type_add(pat_types, typstack[i]);
}
pat->type = typetuple(pat_types);
}
theorise_equal(init_type, pat->type);
for (PatternList* l = pat->pat_list; l; l = l->next) {
pattern_match_and_push_vars0(l->pattern, l->pattern->type);
}
} else if (init_type->tag == TYPE_TUPLE
&& len_typelist(init_type->type_list) == pat_list_len(pat->pat_list)) {
TypeExprList* tl = init_type->type_list;
for (PatternList* l = pat->pat_list; l; l = l->next, tl = tl->next) {
pattern_match_and_push_vars0(l->pattern, tl->type);
}
} else {
tprintf(stderr, LEPFX"expected %d-tuple type init for pattern %P\n",
pat->an_line, pat_list_len(pat->pat_list), pat);
tprintf(stderr, " actual: %T\n", init_type);
exit(EXIT_FAILURE);
}
if (pat->type) {
typexpr_conforms_or_exit(pat->an_line, pat->type, init_type,
"pattern %P in let binding", pat);
theorise_equal(pat->type, init_type);
theorise_equal(init_type, pat->type);
} else {
pat->type = init_type;
}
break;
}
case PAT_CTOR_NOARG:
{
// Get the type that the constructor constructs:
struct Var* found_var = lookup_var(pat->ctor_name, pat->an_line);
TypeExpr* found_type = found_var->type;
assert(found_type);
assert(solid_type(found_type));
// Check init type matches the constructors type directly
typexpr_conforms_or_exit(pat->an_line, init_type, found_type,
"type of pattern '%s' does not match the type the value "
"being matched against constructs ", pat->ctor_name);
theorise_equal(init_type, found_type);
if (pat->type) {
typexpr_conforms_or_exit(pat->an_line, found_type, pat->type,
"type of pattern does not match the type which "
"constructor '%s' constructs ", pat->ctor_name);
theorise_equal(pat->type, found_type);
} else {
pat->type = found_type;
}
break;
}
case PAT_CTOR_WARG:
{
// Do we need to check that the sub-pattern can match the type the
// constuctor constructs?
struct Var* found_var = lookup_var(pat->ctor_name, pat->an_line);
TypeExpr* found_type = found_var->type;
assert(found_type->tag == TYPE_ARROW); // The constructor must be a fn
typexpr_conforms_or_exit(pat->an_line, init_type, found_type->right,
"type of pattern '%s _' does not match the type the value "
"being matched against constructs ", pat->ctor_name);
theorise_equal(init_type, found_type->right);
// recurse - the ctor_arg has ctor fn param type
pattern_match_and_push_vars0(pat->ctor_arg, found_type->left);
if (pat->type) {
typexpr_conforms_or_exit(pat->an_line, found_type->right, pat->type,
"type of pattern does not match the type which "
"constructor '%s' constructs ", pat->ctor_name);
theorise_equal(pat->type, found_type->right);
} else {
pat->type = found_type->right;
}
break;
}
case PAT_INT:
{
TypeExpr* int_type = lookup_typexpr(symbol("int"));
typexpr_conforms_or_exit(pat->an_line, init_type, int_type,
"int literal pattern %d", pat->intval);
if (!pat->type) {
pat->type = int_type;
}
assert(typexpr_equals(int_type, pat->type));
break;
}
case PAT_STR:
{
TypeExpr* str_type = lookup_typexpr(symbol("string"));
typexpr_conforms_or_exit(pat->an_line, init_type, str_type,
"string literal pattern \"%s\"", pat->strval);
if (!pat->type) {
pat->type = str_type;
}