Repository navigation
Expand file tree
/
Copy pathAnalysis.fs
More file actions
999 lines (743 loc) · 30.4 KB
/
Copy pathAnalysis.fs
File metadata and controls
999 lines (743 loc) · 30.4 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
module Semantic.Analysis
open System.Collections.Generic
// TODO: module system
// TODO: type alias
// TODO: operator overloading
open Common.Span
open Syntax.AST
open Semantic
open Env
type internal LetPat = { Static: bool }
type internal PatMode =
| ParamPat
| CondPat
| LetPat of LetPat
type internal Traverse(env: Environment) =
member this.Type ty =
let resolve (id: Id) =
match env.GetTy id.Sym with
| Some ty -> ty.Ty
| None ->
env.AddError(Undefined id)
TNever
match ty with
| NeverType _ -> TNever
| IdType i -> resolve i
| PathType { Prefix = Some Self; Seg = seg } ->
if seg.Length > 0 then
failwith "Not Implemented"
match env.GetSelfType() with
| Some ty -> ty
| None ->
env.AddError(UnboundSelfType ty.Span)
TNever
| PathType p ->
if p.Prefix <> None || p.Seg.Length > 1 then
failwith "Not Implemented"
let id, ty = p.Seg[0]
let instType = Array.map this.Type ty
let container = resolve id
match container with
| TStruct a when a.Generic.Length = instType.Length -> TStruct { a with Generic = instType }
| TEnum a when a.Generic.Length = instType.Length -> TEnum { a with Generic = instType }
| _ ->
env.AddError(GenericMismatch(container, instType, p.Span))
TNever
| TupleType t -> TTuple(Array.map this.Type t.Ele)
| RefType r -> TRef(this.Type r.Ty)
| InferedType span -> env.NewTVar span |> TVar
| ArrayType a ->
let ele = this.Type a.Ele
match a.Len with
| None -> TSlice ele
| Some(LitType { Value = Int len }) -> TArray(ele, len)
| Some _ -> failwith "Not Implemented"
| FnType f ->
let param = f.Param |> Array.map this.Type
let ret = this.Type f.Ret
TFn { Param = param; Ret = ret }
| LitType _ -> failwith "Not Implemented"
| NegType _ -> failwith "Not Implemented"
member _.Pat mode pat ty =
env.RegisterPat pat ty
let mayShadow, isCond, static_ =
match mode with
| LetPat { Static = static_ } -> not static_, false, static_
| ParamPat -> false, false, false
| CondPat -> true, true, false
let addSym sym (i: Id) (ty: Type) (mut: bool) =
if Map.containsKey i.Sym sym then
let (id, _, _) = sym[i.Sym]
env.AddError(DuplicateDefinition(i, id))
Map.add i.Sym (i, ty, mut) sym
let rec proc sym ty pat =
match pat with
| IdPat i -> addSym sym i.Id ty i.Mut
| LitPat l ->
if not isCond then
env.AddError(RefutablePat l.Span)
let litTy =
match l.Value with
| Int _ -> TInt(true, ISize)
| Bool _ -> TBool
| Char _ -> TChar
| Float _ -> TFloat F64
| String _ -> failwith "Not Implemented"
env.Unify litTy ty l.Span
sym
| RangePat { Span = span } -> failwith "Not Implemented"
| CatchAllPat _ -> sym
| TuplePat t ->
let addBinding (pat: Pat) = env.NewTVar pat.Span |> TVar
let patTy = t.Ele |> Array.map addBinding
env.Unify (TTuple patTy) ty t.Span
Array.fold2 proc sym patTy t.Ele
| AsPat a ->
let sym = addSym sym a.Id ty a.Mut
proc sym ty a.Pat
| OrPat { Pat = pat; Span = span } ->
if not isCond then
env.AddError(RefutablePat span)
let allSym = Array.map (proc Map.empty ty) pat
let first = allSym[0]
let firstKey = first |> Map.keys |> Array.ofSeq
for idx, sym in Array.indexed allSym do
if idx > 0 then
let currKey = sym |> Map.keys |> Array.ofSeq
if firstKey <> currKey then
env.AddError(OrPatDiff(pat[idx].Span, firstKey, currKey))
for key in firstKey do
let fid, firstTy, fmut = first[key]
let id, currTy, cmut = sym[key]
if fmut <> cmut then
env.AddError(OrPatMutDiff(fid, id))
env.Unify firstTy currTy id.Span
let mergeSym sym curr =
Map.fold (fun sym _ (id, ty, mut) -> addSym sym id ty mut) sym curr
Array.fold mergeSym sym allSym
| ArrayPat(_) -> failwith "Not Implemented"
| PathPat(_) -> failwith "Not Implemented"
| EnumPat e ->
if e.Variant.Prefix <> None || e.Variant.Seg.Length > 1 then
failwith "Not Implemented"
if not isCond then
env.AddError(RefutablePat e.Span)
let variantId = e.Variant.Seg[0]
let enumTy =
match env.GetVarTy variantId with
| Some ty ->
match ty with
| TFn({ Ret = TEnum _ } as variant) -> Some variant
| ty ->
env.AddError(ExpectEnum(variantId, ty))
None
| None ->
env.AddError(Undefined variantId)
None
let payloadTy =
match enumTy with
| Some fn ->
if fn.Param.Length <> e.Payload.Length then
env.AddError(ParamLenMismatch(e.Span, fn.Param.Length, e.Payload.Length))
env.Unify fn.Ret ty e.Span
Array.mapi (fun idx _ -> if idx < fn.Param.Length then fn.Param[idx] else TNever) e.Payload
| None -> Array.map (fun _ -> TNever) e.Payload
Array.fold2 proc sym payloadTy e.Payload
| StructPat(_) -> failwith "Not Implemented"
| SelfPat(_) -> failwith "Not Implemented"
| RefSelfPat(_) -> failwith "Not Implemented"
let map = proc Map.empty ty pat
for id, ty, mut in map.Values do
let info =
{ Def = id
Mut = mut
Static = static_ }
env.RegisterVar
mayShadow
info
{ Generic = [||]
Type = ty
Pred = [||] }
member this.ExprInner e =
match e with
| Id id ->
match env.GetVarTyWithCapture id with
| None ->
env.AddError(Undefined id)
TNever
| Some ty -> ty
| Binary b ->
let l = this.Expr b.Left
let r = this.Expr b.Right
match b.Op with
| Arith _ ->
env.Unify (TInt(true, ISize)) l b.Left.Span
env.Unify (TInt(true, ISize)) r b.Right.Span
TInt(true, ISize)
| Logic _ ->
env.Unify TBool l b.Left.Span
env.Unify TBool r b.Right.Span
TBool
| Cmp _ ->
env.Unify l r b.Span
TBool
| Pipe -> failwith "Not Implemented"
| SelfExpr(_) -> failwith "Not Implemented"
| LitExpr l ->
match l.Value with
| Int _ -> TInt(true, ISize)
| Bool _ -> TBool
| Char _ -> TChar
| Float _ -> TFloat F64
| String _ -> TString
| If i ->
let then_ = this.Cond i.Cond i.Then
for br in i.ElseIf do
let elseif = this.Cond br.Cond br.Block
env.Unify then_ elseif i.Span
match i.Else with
| Some else_ ->
let else_ = this.Block else_
env.Unify then_ else_ i.Span
| None -> env.Unify then_ UnitType i.Span
then_
| Match m ->
let value = this.Expr m.Value
let typeOfBranch (br: MatchBranch) =
env.EnterScope BlockScope
this.Pat CondPat br.Pat value
match br.Guard with
| Some g -> env.Unify (this.Expr g) TBool g.Span
| None -> ()
let brTy = this.Expr br.Expr
env.ExitScope()
env.NormalizeTy brTy
if Array.length m.Branch = 0 then
UnitType
else
let first = typeOfBranch m.Branch[0]
for br in m.Branch[1..] do
let brTy = typeOfBranch br
env.Unify first brTy br.Span
first
| Block b -> this.Block b
| Call c ->
match c.Callee with
// method
| Field f ->
let receiver = this.Expr f.Receiver
let arg = Array.map this.Expr c.Arg
env.FindMethod receiver arg f.Field.Sym f.Span
| _ ->
let callee = this.Expr c.Callee
let arg = Array.map this.Expr c.Arg
let ret = TVar(env.NewTVar c.Span)
env.Unify (TFn { Param = arg; Ret = ret }) callee c.Span
env.NormalizeTy ret
| As a ->
let ty = this.Type a.Ty
// As has three kinds of usage
// 1. cast to another kind of number
// 2. make trait object
// 3. make slice from array
failwith "Not Implemented"
| Unary u ->
let value = this.Expr u.Value
match u.Op with
| Not ->
env.Unify TBool value u.Span
TBool
| Neg ->
env.Unify (TInt(true, ISize)) value u.Span
TInt(true, ISize)
| Ref -> TRef value
| Deref ->
let ptr = TVar(env.NewTVar u.Value.Span)
env.Unify (TRef ptr) value u.Span
env.NormalizeTy ptr
| Assign a ->
let value = this.Expr a.Value
let place = this.Expr a.Place
env.Unify place value a.Span
let rec getVar expr =
match expr with
| Id i -> Some i
| Field { Receiver = receiver } -> getVar receiver
| Index { Container = container } -> getVar container
| Unary { Op = Deref; Value = expr } -> getVar expr
| _ -> None
match getVar a.Place with
| None -> ()
| Some id ->
match env.GetVarInfo id with
| None -> env.AddError(Undefined id)
| Some(info) ->
if not info.Mut then
env.AddError(AssignImmutable(info.Def, a.Span))
UnitType
| Field f ->
let receiver = (this.Expr f.Receiver).StripRef()
let key = f.Field.Sym
env.FindField receiver key f.Span
| TupleAccess(_) -> failwith "Not Implemented"
| Index idx ->
let container = this.Expr idx.Container
let index = this.Expr idx.Index
env.Unify (TInt(true, ISize)) index idx.Index.Span
let value = env.NewTVar idx.Span |> TVar
env.Unify (TSlice value) container idx.Span
env.NormalizeTy value
| Array a ->
if a.Ele.Length = 0 then
let ele = env.NewTVar a.Span
TSlice(TVar ele) // , 0UL)
else
let first = this.Expr a.Ele[0]
for ele in a.Ele[1..] do
let ty = this.Expr ele
env.Unify first ty ele.Span
TSlice(first) //, uint64 a.Ele.Length)
| ArrayRepeat a ->
// TODO: clone trait
let ty = this.Expr a.Ele
match a.Len with
| LitExpr { Value = Int len } -> TSlice ty // len)
| _ -> failwith "Not Implemented"
| StructLit s ->
if s.Struct.Prefix <> None || s.Struct.Seg.Length > 1 then
failwith "Not Implemented"
let stru, gen = s.Struct.Seg[0]
let span = stru.Span
match env.GetTy stru.Sym with
| None ->
env.AddError(Undefined stru)
TNever
| Some { Ty = TStruct sTy; Def = id } ->
let stru = env.GetStruct id
let gen =
if gen.Length > 0 then
Array.map this.Type gen
else
Array.map (fun _ -> TVar(env.NewTVar span)) stru.Generic
let map = Array.zip stru.Generic gen |> Map.ofArray
let sTy = TStruct { sTy with Generic = gen }
for field in s.Field do
match field with
| ShorthandField v ->
if stru.Field.ContainsKey v.Sym then
let field = stru.Field[v.Sym].InstantiateWithMap map
match env.GetVarTyWithCapture id with
| None -> env.AddError(Undefined id)
| Some ty -> env.Unify field ty v.Span
else
env.AddError(UndefinedField(v.Span, v.Sym))
| KeyValueField kv ->
if stru.Field.ContainsKey kv.Name.Sym then
let field = stru.Field[kv.Name.Sym].InstantiateWithMap map
let ty = this.Expr kv.Value
env.Unify field ty kv.Span
else
env.AddError(UndefinedField(kv.Name.Span, kv.Name.Sym))
| RestField r ->
let ty = this.Expr r.Value
env.Unify sTy ty r.Span
env.NormalizeTy sTy
| Some ty ->
env.AddError(ExpectStruct(stru, ty.Ty))
TNever
| Tuple t -> t.Ele |> Array.map this.Expr |> TTuple
| Closure c ->
let paramTy (p: Param) =
match p.Ty with
| Some ty -> this.Type ty
| None ->
let newTVar = env.NewTVar p.Span
TVar newTVar
let paramTy = Array.map paramTy c.Param
let retTy =
match c.Ret with
| Some ty -> this.Type ty
| None -> TVar(env.NewTVar c.Span)
let closureScope = ClosureScope { Closure = c; Ret = retTy }
env.EnterScope closureScope
for param, ty in Array.zip c.Param paramTy do
this.Pat ParamPat param.Pat ty
let ret = this.Expr c.Body
env.Unify retTy ret c.Span
env.ExitScope()
TFn
{ Param = Array.map env.NormalizeTy paramTy
Ret = env.NormalizeTy retTy }
| Path _ -> failwith "Not Implemented"
| Break _ -> TNever
| Continue _ -> TNever
| Return r ->
let retTy =
match env.GetReturn() with
| Some ty -> ty
| None -> failwith "Unreachable"
let ty =
match r.Value with
| Some v -> this.Expr v
| None -> UnitType
env.Unify retTy ty r.Span
TNever
| Range(_) -> failwith "Not Implemented"
| For(_) -> TNever
| While w ->
let _ = this.Cond w.Cond w.Body
TNever
| TryReturn(_) -> failwith "Not Implemented"
member this.Expr e =
let ty = this.ExprInner e
env.RegisterExpr e ty
ty
member this.Block(b: Block) =
env.EnterScope BlockScope
let chooseDecl s =
match s with
| DeclStmt d -> Some d
| ExprStmt _ -> None
let decl = Seq.choose chooseDecl b.Stmt
this.HoistDecl decl false
let typeof _ stmt =
match stmt with
| DeclStmt d ->
this.Decl d
UnitType
| ExprStmt e -> this.Expr e
let ty = Array.fold typeof UnitType b.Stmt
env.ExitScope()
env.NormalizeTy ty
member this.TyParam(p: TyParam[]) =
let group = env.NewGenGroup()
let gen = ResizeArray()
let pred = ResizeArray()
for idx, p in Array.indexed p do
let generic =
{ GroupId = group
Id = idx
Span = p.Id.Span
Sym = p.Id.Sym }
env.RegisterTy p.Id (TGen generic)
gen.Add generic
for b in p.Bound do
if b.Prefix <> None || b.Seg.Length > 1 then
failwith "Not Implemented"
let name, gen = b.Seg[0]
let gen = Array.map this.Type gen
let tr = env.GetTrait (fst b.Seg[0]).Sym
match tr with
| None -> env.AddError(Undefined name)
| Some tr ->
let p =
{ Trait = tr
Type = Array.append [| TGen generic |] gen }
let assoc = Array.skip tr.FreeVarLength p.Type
pred.Add p
env.AddPred p assoc
gen.ToArray(), pred.ToArray()
member this.HoistDecl (decl: seq<Decl>) topLevel =
let genMap = Dictionary()
let fnMap = Dictionary()
let makeGen group (idx, id: Id) =
{ GroupId = group
Id = idx
Span = id.Span
Sym = id.Sym }
// Process Type Decl Hoisted Name
for d in decl do
match d with
| Let _
| Const _
| FnDecl _ -> ()
| StructDecl s ->
let group = env.NewGenGroup()
let gen = s.TyParam |> Array.indexed |> Array.map (makeGen group)
genMap.Add(s.Name, gen)
env.RegisterTy
s.Name
(TStruct
{ Name = s.Name
Generic = Array.map TGen gen })
| EnumDecl e ->
let group = env.NewGenGroup()
let gen = e.TyParam |> Array.indexed |> Array.map (makeGen group)
genMap.Add(e.Name, gen)
env.RegisterTy
e.Name
(TEnum
{ Name = e.Name
Generic = Array.map TGen gen })
| TypeDecl _ -> failwith "Not Implemented"
| Use _ -> failwith "Not Implemented"
| Trait t ->
let group = env.NewGenGroup()
let makeGen (idx, p: TyParam) =
{ GroupId = group
Id = idx + 1
Span = p.Id.Span
Sym = p.Id.Sym }
let self =
{ GroupId = group
Id = 0
Span = Span.dummy
Sym = "self" }
let gen = t.TyParam |> Array.indexed |> Array.map makeGen
let mutable idx = gen.Length
let processAssoc item =
match item.Member with
| TraitType t ->
idx <- idx + 1
Some(
{ GroupId = group
Id = idx + 1
Span = t.Name.Span
Sym = t.Name.Sym }
)
| _ -> None
let assoc = t.Item |> Array.choose processAssoc
genMap.Add(t.Name, Array.concat [| [| self |]; gen; assoc |])
env.RegisterTy
t.Name
(TTrait
{ Name = t.Name
Generic = Array.map TGen gen })
| Impl _ -> ()
// Process Type Decl
for d in decl do
match d with
| Let l when topLevel ->
let ty: Type =
match l.Ty with
| Some ty -> this.Type ty
| None -> env.NewTVar l.Span |> TVar
this.Pat (LetPat { Static = true }) l.Pat ty
| Let _ -> ()
| Const _ -> ()
| FnDecl f ->
env.EnterScope TypeScope
let gen, pred = this.TyParam f.TyParam
let paramTy (p: Param) =
match p.Ty with
| Some ty -> this.Type ty
| None ->
let newTVar = env.NewTVar p.Span
TVar newTVar
let param = Array.map paramTy f.Param
let ret =
match f.Ret with
| Some ty -> this.Type ty
| None -> TVar(env.NewTVar f.Span)
env.ExitScope()
let info =
{ Def = f.Name
Mut = false
Static = true }
let fnTy = { Param = param; Ret = ret }
let fnScope =
{ Param = param
Ret = ret
Fixed = f.TyParam.Length > 0
Name = f.Name }
fnMap.Add(f.Name, fnScope)
env.RegisterVar
false
info
{ Generic = gen
Type = TFn fnTy
Pred = pred }
| StructDecl s ->
let gen = genMap[s.Name]
env.EnterScope(
AdtScope
{ Self =
TStruct
{ Name = s.Name
Generic = Array.map TGen gen } }
)
for idx, p in Array.indexed s.TyParam do
env.RegisterTy p (TGen gen[idx])
let processField m (field: StructFieldDef) =
let name = field.Name.Sym
if Map.containsKey name m then
env.AddError(DuplicateField field.Name)
Map.add name (this.Type field.Ty) m
let field = Array.fold processField Map.empty s.Field
env.ExitScope()
let stru =
{ Name = s.Name
Field = field
Generic = gen }
env.RegisterStruct s stru
| EnumDecl e ->
let gen = genMap[e.Name]
env.EnterScope(
AdtScope
{ Self =
TStruct
{ Name = e.Name
Generic = Array.map TGen gen } }
)
for idx, p in Array.indexed e.TyParam do
env.RegisterTy p (TGen gen[idx])
let processVariant m (variant: EnumVariantDef) =
let name = variant.Name.Sym
if Map.containsKey name m then
env.AddError(DuplicateVariant variant.Name)
let payload = Array.map this.Type variant.Payload
Map.add name payload m
let variant = Array.fold processVariant Map.empty e.Variant
env.ExitScope()
let enum =
{ Name = e.Name
Variant = variant
Generic = gen }
env.RegisterEnum e enum
| TypeDecl _ -> failwith "Not Implemented"
| Use _ -> failwith "Not Implemented"
| Trait t ->
let gen = genMap[t.Name]
env.EnterScope(TraitScope { Self = TGen gen[0] })
for idx in 1 .. gen.Length - 1 do
let g = gen[idx]
env.RegisterTy { Sym = g.Sym; Span = g.Span } (TGen g)
let super = ResizeArray()
for path in t.Super do
if path.Prefix <> None || path.Seg.Length > 1 then
failwith "Not Implemented"
let id = (fst path.Seg[0])
let tr = env.GetTrait id.Sym
match tr with
| Some s -> super.Add s
| None -> env.AddError(Undefined id)
let super = super.ToArray()
let processParam (p: Param) = p.Ty |> Option.get |> this.Type
let processMember (method: Map<string, Function>, tyName: Id[]) (m: TraitMember) =
match m with
| TraitMethod m ->
match m.Default with
| Some _ -> failwith "Not Implemented"
| None -> ()
let param = Array.map processParam m.Param[1..]
let ret =
match m.Ret with
| Some r -> this.Type r
| None -> UnitType
let f = { Param = param; Ret = ret }
Map.add m.Name.Sym f method, tyName
| TraitType t -> method, Array.append tyName [| t.Name |]
| TraitValue _ -> method, tyName
let method, tyName =
t.Item |> Array.map _.Member |> Array.fold processMember (Map.empty, [||])
let methodSafe f =
(TFn f).FindTGen() |> Seq.forall (fun g -> g.Id <> 0)
let safe = method |> Map.values |> Seq.forall methodSafe
env.ExitScope()
env.RegisterTrait
{ Name = t.Name
Method = method
Generic = gen
ObjectSafe = safe
Super = super
DepName = tyName }
| Impl i ->
env.EnterScope TypeScope
let gen, pred = this.TyParam i.TyParam
let genGroup = if gen.Length > 0 then gen[0].GroupId else env.NewGenGroup()
let trait_, trGen =
match i.Trait with
| None -> failwith "Not Implemented"
| Some t ->
if t.Prefix <> None || t.Seg.Length > 1 then
failwith "Not Implemented"
t.Seg[0]
let trGen = Array.map this.Type trGen
let ty = this.Type i.Ty
match env.GetTrait trait_.Sym with
| None -> env.AddError(Undefined trait_)
| Some t ->
let processTy assoc (mem: ImplItem) =
match mem.Item with
| AssocType a ->
let ty = this.Type a.Ty
if t.HasDep a.Name.Sym then
Map.add a.Name.Sym ty assoc
else
env.AddError(UndefinedAssocType(i.Span, a.Name))
assoc
| Method _ -> assoc
| AssocValue _ -> assoc
let assocTy = Array.fold processTy Map.empty i.Item
let processTy gen (name: Id) =
match Map.tryFind name.Sym assocTy with
| Some ty -> Array.append gen [| ty |]
| None ->
env.AddError(TraitMemberMissing(t.Name, name, i.Span))
gen
let trGen = Array.fold processTy trGen t.DepName
env.ImplTrait gen pred t trGen ty i.Span
env.ExitScope()
for item in decl do
match item with
| FnDecl f ->
let fnScope = fnMap[f.Name]
env.EnterScope(FnScope fnScope)
for param, ty in Array.zip f.Param fnScope.Param do
this.Pat ParamPat param.Pat ty
let ret = this.Block f.Body
env.Unify fnScope.Ret ret f.Name.Span
env.ExitScope()
| Let l when topLevel ->
let value = this.Expr l.Value
env.Unify (env.GetPatTy l.Pat) value l.Span
| _ -> ()
for name in fnMap.Keys do
env.ToNever name
member this.Decl d =
match d with
| Let l ->
let value = this.Expr l.Value
let ty =
match l.Ty with
| Some ty ->
let ty = this.Type ty
env.Unify ty value l.Span
ty
| None -> value
this.Pat (LetPat { Static = false }) l.Pat ty
| Const _ -> failwith "Not Implemented"
| Use _ -> failwith "Not Implemented"
| FnDecl _
| StructDecl _
| EnumDecl _
| TypeDecl _
| Trait _
| Impl _ -> ()
member this.Cond cond block =
match cond with
| BoolCond b ->
env.Unify TBool (this.Expr b) b.Span
this.Block block
| LetCond c ->
let value = this.Expr c.Value
env.EnterScope BlockScope
this.Pat CondPat c.Pat value
let ty = this.Block block
env.ExitScope()
env.NormalizeTy ty
member this.Module(m: Module) =
env.EnterScope TopLevelScope
let decl = m.Item |> Array.map _.Decl
this.HoistDecl decl true
env.ExitScope()
let analysis (sema: SemanticInfo) (m: Module) =
let error = ResizeArray()
let env = Environment(sema, error)
let traverse = Traverse env
traverse.Module m
for id in sema.DeclTy.Keys do
let scm = sema.DeclTy[id]
let ty = env.NormalizeTy scm.Type
sema.DeclTy[id] <- { scm with Type = ty }
if error.Count = 0 then Ok sema else Error(error.ToArray())