-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.fs
650 lines (546 loc) · 13.7 KB
/
Library.fs
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
module Syntax.AST
open Common.Span
type Lit =
| String of string
| Char of char
| Int of uint64
| Float of double
| Bool of bool
type Id = { Sym: string; Span: Span }
type Literal = { Value: Lit; Span: Span }
type UnaryOp =
| Neg
| Not
| Ref
| Deref
member this.ToString =
match this with
| Neg -> "-"
| Not -> "!"
| Ref -> "&"
| Deref -> "*"
type ArithOp =
| Add
| Sub
| Mul
| Div
| Rem
| BitOr
| BitAnd
| BitXor
| Shl
| Shr
member this.ToString =
match this with
| Add -> "+"
| Sub -> "-"
| Mul -> "*"
| Div -> "/"
| Rem -> "%"
| BitOr -> "|"
| BitAnd -> "&"
| BitXor -> "^"
| Shl -> "<<"
| Shr -> ">>"
type LogicOp =
| And
| Or
member this.ToString =
match this with
| And -> "&&"
| Or -> "||"
type CmpOp =
| EqEq
| NotEq
| Lt
| Gt
| LtEq
| GtEq
member this.ToString =
match this with
| EqEq -> "=="
| NotEq -> "!="
| Lt -> "<"
| Gt -> ">"
| LtEq -> "<="
| GtEq -> ">="
type BinaryOp =
| Arith of ArithOp
| Logic of LogicOp
| Cmp of CmpOp
| Pipe
member this.ToString =
match this with
| Arith a -> a.ToString
| Logic l -> l.ToString
| Cmp c -> c.ToString
| Pipe -> "|>"
type Visibility =
| Public
| Private
| Internal
member this.ToString =
match this with
| Public -> "public"
| Private -> "private"
| Internal -> "internal"
type PathPrefix =
| Self
| LowSelf
| Package
member this.ToString =
match this with
| Self -> "Self"
| LowSelf -> "self"
| Package -> "package"
type PathPat =
{ Prefix: Option<PathPrefix>
Seg: Id[]
Span: Span }
type Path =
{ Prefix: Option<PathPrefix>
Seg: (Id * Type[])[]
Span: Span }
and FnType =
{ Param: Type[]; Ret: Type; Span: Span }
and ExtraType = { Ty: Type; Span: Span }
and ArrayType =
{ Ele: Type
Len: Option<Type>
Span: Span }
and TupleType = { Ele: Type[]; Span: Span }
and TyParam =
{ Id: Id
Const: bool
Bound: Path[]
Span: Span }
and Type =
| IdType of Id
| PathType of Path
| LitType of Literal
| NeverType of Span
| NegType of ExtraType
| RefType of ExtraType
| TupleType of TupleType
| ArrayType of ArrayType
| InferedType of Span
| FnType of FnType
member this.Span =
match this with
| IdType i -> i.Span
| PathType p -> p.Span
| TupleType t -> t.Span
| LitType l -> l.Span
| NeverType s -> s
| NegType r -> r.Span
| RefType r -> r.Span
| ArrayType a -> a.Span
| InferedType s -> s
| FnType f -> f.Span
member this.WithSpan span =
match this with
| IdType i -> IdType { i with Span = span }
| PathType p -> PathType { p with Span = span }
| TupleType t -> TupleType { t with Span = span }
| LitType l -> LitType { l with Span = span }
| NeverType _ -> NeverType span
| NegType r -> NegType { r with Span = span }
| RefType r -> RefType { r with Span = span }
| ArrayType a -> ArrayType { a with Span = span }
| InferedType _ -> InferedType span
| FnType f -> FnType { f with Span = span }
type IdPat = { Id: Id; Mut: bool; Span: Span }
and SeqPat = { Ele: Pat[]; Span: Span }
and AsPat =
{ Pat: Pat
Mut: bool
Id: Id
Span: Span }
and EnumPat =
{ Variant: PathPat
Payload: Pat[]
Span: Span }
and OrPat = { Pat: Pat[]; Span: Span }
and KeyValuePat = { Id: Id; Pat: Pat; Span: Span }
and FieldPat =
| ShorthandPat of Id
| KeyValuePat of KeyValuePat
member this.Span =
match this with
| ShorthandPat i -> i.Span
| KeyValuePat k -> k.Span
and RangePat =
{ From: Option<Pat>
To: Option<Pat>
Span: Span }
and StructPat =
{ Struct: PathPat
Field: FieldPat[]
Span: Span }
and Pat =
| IdPat of IdPat
| LitPat of Literal
| TuplePat of SeqPat
| ArrayPat of SeqPat
| AsPat of AsPat
| PathPat of PathPat
| EnumPat of EnumPat
| StructPat of StructPat
| OrPat of OrPat
| CatchAllPat of Span
| RangePat of RangePat
| SelfPat of Span
| RefSelfPat of Span
member this.Span =
match this with
| LitPat l -> l.Span
| IdPat i -> i.Span
| TuplePat t -> t.Span
| ArrayPat a -> a.Span
| AsPat a -> a.Span
| PathPat p -> p.Span
| EnumPat e -> e.Span
| StructPat s -> s.Span
| OrPat o -> o.Span
| CatchAllPat s -> s
| RangePat r -> r.Span
| SelfPat s -> s
| RefSelfPat s -> s
member this.WithSpan span =
match this with
| LitPat l -> LitPat { l with Span = span }
| IdPat i -> IdPat { i with Span = span }
| TuplePat t -> TuplePat { t with Span = span }
| ArrayPat a -> ArrayPat { a with Span = span }
| AsPat a -> AsPat { a with Span = span }
| PathPat p -> PathPat { p with Span = span }
| EnumPat e -> EnumPat { e with Span = span }
| StructPat s -> StructPat { s with Span = span }
| OrPat o -> OrPat { o with Span = span }
| RangePat r -> RangePat { r with Span = span }
| CatchAllPat _ -> CatchAllPat span
| SelfPat _ -> SelfPat span
| RefSelfPat _ -> RefSelfPat span
type Param =
{ Pat: Pat
Ty: Option<Type>
Span: Span }
type Call<'a> = { Callee: 'a; Arg: 'a[]; Span: Span }
and LetCond = { Pat: Pat; Value: Expr; Span: Span }
and Cond =
| BoolCond of Expr
| LetCond of LetCond
member this.Span =
match this with
| BoolCond b -> b.Span
| LetCond l -> l.Span
and Elseif =
{ Cond: Cond; Block: Block; Span: Span }
and If =
{ Cond: Cond
Then: Block
ElseIf: Elseif[]
Else: Option<Block>
Span: Span }
and Unary =
{ Op: UnaryOp; Value: Expr; Span: Span }
and Assign =
{ Place: Expr
Op: Option<ArithOp>
Value: Expr
Span: Span }
and Binary =
{ Left: Expr
Op: BinaryOp
Right: Expr
Span: Span }
and As = { Value: Expr; Ty: Type; Span: Span }
and Field =
{ Receiver: Expr
Field: Id
Span: Span }
and TupleAccess =
{ Receiver: Expr
Index: uint64 * Span
Span: Span }
and Index =
{ Container: Expr
Index: Expr
Span: Span }
and Block = { Stmt: Stmt[]; Span: Span }
and ArrayRepeat = { Ele: Expr; Len: Expr; Span: Span }
and Seq = { Ele: Expr[]; Span: Span }
and RangeExpr =
{ From: Option<Expr>
To: Option<Expr>
Exclusive: bool
Span: Span }
and KeyValueField = { Name: Id; Value: Expr; Span: Span }
and RestField = { Value: Expr; Span: Span }
and StructField =
| ShorthandField of Id
| KeyValueField of KeyValueField
| RestField of RestField
member this.Span =
match this with
| ShorthandField i -> i.Span
| KeyValueField k -> k.Span
| RestField s -> s.Span
and StructLit =
{ Struct: Path
Field: StructField[]
Span: Span }
and For =
{ Pat: Pat
Iter: Expr
Body: Block
Span: Span }
and While = { Cond: Cond; Body: Block; Span: Span }
and TryReturn = { Base: Expr; Span: Span }
and Closure =
{ Param: Param[]
Ret: Option<Type>
Body: Expr
Span: Span }
and MatchBranch =
{ Pat: Pat
Guard: Option<Expr>
Expr: Expr
Span: Span }
and Match =
{ Value: Expr
Branch: MatchBranch[]
Span: Span }
and Return = { Value: Option<Expr>; Span: Span }
and Expr =
| Id of Id
| SelfExpr of Span
| LitExpr of Literal
| If of If
| Block of Block
| Call of Call<Expr>
| As of As
| Unary of Unary
| Assign of Assign
| Binary of Binary
| Field of Field
| TupleAccess of TupleAccess
| Index of Index
| Array of Seq
| ArrayRepeat of ArrayRepeat
| StructLit of StructLit
| Tuple of Seq
| Closure of Closure
| Path of Path
| Break of Span
| Continue of Span
| Return of Return
| Range of RangeExpr
| For of For
| While of While
/// question mark expression
| TryReturn of TryReturn
| Match of Match
member this.Span =
match this with
| Id i -> i.Span
| LitExpr l -> l.Span
| SelfExpr s -> s
| If i -> i.Span
| Block b -> b.Span
| Call c -> c.Span
| As a -> a.Span
| Unary u -> u.Span
| Assign a -> a.Span
| Binary b -> b.Span
| Field f -> f.Span
| TupleAccess t -> t.Span
| Index i -> i.Span
| Array t
| Tuple t -> t.Span
| StructLit s -> s.Span
| Closure c -> c.Span
| ArrayRepeat a -> a.Span
| Path p -> p.Span
| Break b -> b
| Continue c -> c
| Return r -> r.Span
| Range r -> r.Span
| For f -> f.Span
| While w -> w.Span
| TryReturn t -> t.Span
| Match m -> m.Span
member this.WithSpan span =
match this with
| Id i -> Id { i with Span = span }
| LitExpr l -> LitExpr { l with Span = span }
| SelfExpr _ -> SelfExpr span
| If i -> If { i with Span = span }
| Block b -> Block { b with Span = span }
| Call c -> Call { c with Span = span }
| As a -> As { a with Span = span }
| Unary u -> Unary { u with Span = span }
| Assign a -> Assign { a with Span = span }
| Binary b -> Binary { b with Span = span }
| Field f -> Field { f with Span = span }
| TupleAccess t -> TupleAccess { t with Span = t.Span }
| Index i -> Index { i with Span = span }
| Array t -> Array { t with Span = span }
| Tuple t -> Tuple { t with Span = span }
| StructLit s -> StructLit { s with Span = span }
| Closure c -> Closure { c with Span = span }
| ArrayRepeat a -> ArrayRepeat { a with Span = span }
| Path p -> Path { p with Span = span }
| Break _ -> Break span
| Continue _ -> Continue span
| Return r -> Return { r with Span = span }
| Range r -> Range { r with Span = span }
| For f -> For { f with Span = span }
| While w -> While { w with Span = span }
| TryReturn t -> TryReturn { t with Span = span }
| Match m -> Match { m with Span = span }
and Let =
{ Pat: Pat
Ty: Option<Type>
Value: Expr
Span: Span }
and Const =
{ Name: Id
Ty: Option<Type>
Value: Expr
Span: Span }
and Fn =
{ Name: Id
TyParam: TyParam[]
Param: Param[]
Ret: Option<Type>
Body: Block
Span: Span }
and UsePath =
{ Span: Span; Seg: Id[]; Item: UseItem }
and UseItem =
| UseAll of Span
| UseSelf of Span
| UseItem of Id
| UseMany of Span * UsePath[]
member this.Span =
match this with
| UseAll a -> a
| UseSelf s -> s
| UseItem i -> i.Span
| UseMany(s, _) -> s
and Use =
{ Span: Span
Prefix: Option<PathPrefix>
Seg: Id[]
Item: UseItem }
and TypeDecl = { Name: Id; Ty: Type; Span: Span }
and StructFieldDef =
{ Vis: Visibility
Name: Id
Ty: Type
Span: Span }
and StructDecl =
{ Name: Id
TyParam: Id[]
Field: StructFieldDef[]
Span: Span }
and EnumVariantDef =
{ Name: Id
Payload: Type[]
Tag: Option<Expr>
Span: Span }
and EnumDecl =
{ Name: Id
TyParam: Id[]
Variant: EnumVariantDef[]
Span: Span }
and TraitMethod =
{ Name: Id
Param: Param[]
Ret: Option<Type>
Default: Option<Block>
Span: Span }
and TraitType = { Name: Id; Bound: Path[]; Span: Span }
and TraitValue =
{ Id: Id
Ty: Type
DefaultValue: Option<Expr>
Span: Span }
and TraitMember =
| TraitMethod of TraitMethod
| TraitType of TraitType
| TraitValue of TraitValue
member this.Span =
match this with
| TraitMethod t -> t.Span
| TraitType t -> t.Span
| TraitValue t -> t.Span
and TraitItem =
{ Attr: Attr[]
Member: TraitMember
Span: Span }
and Trait =
{ Name: Id
TyParam: TyParam[]
Super: Path[]
Item: TraitItem[]
Span: Span }
and ImplDecl =
| AssocType of TypeDecl
| AssocValue of Const
| Method of Fn
member this.Span =
match this with
| AssocType t -> t.Span
| AssocValue c -> c.Span
| Method m -> m.Span
and ImplItem =
{ Vis: Visibility
Attr: Attr[]
Item: ImplDecl
Span: Span }
and Impl =
{ Trait: Option<Path>
TyParam: TyParam[]
Ty: Type
Item: ImplItem[]
Span: Span }
and Attr =
| IdAttr of Id
| LitAttr of Literal
| CallAttr of Call<Attr>
and Decl =
| Let of Let
| Const of Const
| FnDecl of Fn
| StructDecl of StructDecl
| EnumDecl of EnumDecl
| TypeDecl of TypeDecl
| Use of Use
| Trait of Trait
| Impl of Impl
member this.Span =
match this with
| Let l -> l.Span
| Const c -> c.Span
| FnDecl f -> f.Span
| StructDecl s -> s.Span
| EnumDecl e -> e.Span
| TypeDecl t -> t.Span
| Use u -> u.Span
| Trait t -> t.Span
| Impl i -> i.Span
and Stmt =
| ExprStmt of Expr
| DeclStmt of Decl
member this.Span =
match this with
| ExprStmt e -> e.Span
| DeclStmt d -> d.Span
type ModuleItem =
{ Vis: Visibility
Attr: Attr[]
Decl: Decl
Span: Span }
type Module = { Item: ModuleItem[]; Span: Span }