-
Notifications
You must be signed in to change notification settings - Fork 257
/
ASTBuilder.cs
1133 lines (930 loc) · 30.7 KB
/
ASTBuilder.cs
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
using System;
using System.Collections.Generic;
using Dafny;
using DAST;
using Microsoft.Dafny.Compilers;
namespace Microsoft.Dafny.Compilers {
class ProgramBuilder : ModuleContainer {
readonly List<Module> items = new();
public void AddModule(Module item) {
items.Add(item);
}
public List<Module> Finish() {
return items;
}
}
interface ModuleContainer {
void AddModule(Module item);
public ModuleBuilder Module(string name) {
return new ModuleBuilder(this, name);
}
}
class ModuleBuilder : ClassContainer, TraitContainer, NewtypeContainer, DatatypeContainer {
readonly ModuleContainer parent;
readonly string name;
readonly List<ModuleItem> body = new();
public ModuleBuilder(ModuleContainer parent, string name) {
this.parent = parent;
this.name = name;
}
public void AddModule(Module item) {
body.Add((ModuleItem)ModuleItem.create_Module(item));
}
public void AddClass(Class item) {
body.Add((ModuleItem)ModuleItem.create_Class(item));
}
public void AddTrait(Trait item) {
body.Add((ModuleItem)ModuleItem.create_Trait(item));
}
public void AddNewtype(Newtype item) {
body.Add((ModuleItem)ModuleItem.create_Newtype(item));
}
public void AddDatatype(Datatype item) {
body.Add((ModuleItem)ModuleItem.create_Datatype(item));
}
public object Finish() {
parent.AddModule((Module)Module.create(Sequence<Rune>.UnicodeFromString(this.name), Sequence<ModuleItem>.FromArray(body.ToArray())));
return parent;
}
}
interface ClassContainer {
void AddClass(Class item);
public ClassBuilder Class(string name, string enclosingModule, List<DAST.Type> typeParams, List<DAST.Type> superClasses) {
return new ClassBuilder(this, name, enclosingModule, typeParams, superClasses);
}
}
class ClassBuilder : ClassLike {
readonly ClassContainer parent;
readonly string name;
readonly string enclosingModule;
readonly List<DAST.Type> typeParams;
readonly List<DAST.Type> superClasses;
readonly List<DAST.Field> fields = new();
readonly List<DAST.Method> body = new();
public ClassBuilder(ClassContainer parent, string name, string enclosingModule, List<DAST.Type> typeParams, List<DAST.Type> superClasses) {
this.parent = parent;
this.name = name;
this.enclosingModule = enclosingModule;
this.typeParams = typeParams;
this.superClasses = superClasses;
}
public void AddMethod(DAST.Method item) {
body.Add(item);
}
public void AddField(DAST.Formal item, DAST.Expression defaultValue) {
fields.Add((DAST.Field)DAST.Field.create_Field(item, defaultValue != null ? Optional<DAST._IExpression>.create_Some(defaultValue) : Optional<DAST._IExpression>.create_None()));
}
public object Finish() {
parent.AddClass((Class)Class.create(
Sequence<Rune>.UnicodeFromString(this.name),
Sequence<Rune>.UnicodeFromString(this.enclosingModule),
Sequence<DAST.Type>.FromArray(this.typeParams.ToArray()),
Sequence<DAST.Type>.FromArray(this.superClasses.ToArray()),
Sequence<DAST.Field>.FromArray(this.fields.ToArray()),
Sequence<DAST.Method>.FromArray(body.ToArray())
));
return parent;
}
}
interface TraitContainer {
void AddTrait(Trait item);
public TraitBuilder Trait(string name, List<DAST.Type> typeParams) {
return new TraitBuilder(this, name, typeParams);
}
}
class TraitBuilder : ClassLike {
readonly TraitContainer parent;
readonly string name;
readonly List<DAST.Type> typeParams;
readonly List<DAST.Method> body = new();
public TraitBuilder(TraitContainer parent, string name, List<DAST.Type> typeParams) {
this.parent = parent;
this.name = name;
this.typeParams = typeParams;
}
public void AddMethod(DAST.Method item) {
// remove existing method with the same name, because we're going to define an implementation
for (int i = 0; i < body.Count; i++) {
if (body[i].is_Method && body[i].dtor_name.Equals(item.dtor_name)) {
body.RemoveAt(i);
break;
}
}
body.Add(item);
}
public void AddField(DAST.Formal item, DAST.Expression defaultValue) {
throw new NotImplementedException();
}
public object Finish() {
parent.AddTrait((Trait)Trait.create(
Sequence<Rune>.UnicodeFromString(this.name),
Sequence<DAST.Type>.FromArray(typeParams.ToArray()),
Sequence<DAST.Method>.FromArray(body.ToArray()))
);
return parent;
}
}
interface NewtypeContainer {
void AddNewtype(Newtype item);
public NewtypeBuilder Newtype(string name, List<DAST.Type> typeParams, DAST.Type baseType, List<DAST.Statement> witnessStmts, DAST.Expression witness) {
return new NewtypeBuilder(this, name, typeParams, baseType, witnessStmts, witness);
}
}
class NewtypeBuilder : ClassLike {
readonly NewtypeContainer parent;
readonly string name;
readonly List<DAST.Type> typeParams;
readonly DAST.Type baseType;
readonly List<DAST.Statement> witnessStmts;
readonly DAST.Expression witness;
public NewtypeBuilder(NewtypeContainer parent, string name, List<DAST.Type> typeParams, DAST.Type baseType, List<DAST.Statement> statements, DAST.Expression witness) {
this.parent = parent;
this.name = name;
this.typeParams = typeParams;
this.baseType = baseType;
this.witnessStmts = statements;
this.witness = witness;
}
public void AddMethod(DAST.Method item) {
throw new NotImplementedException();
}
public void AddField(DAST.Formal item, DAST.Expression defaultValue) {
throw new NotImplementedException();
}
public object Finish() {
parent.AddNewtype((Newtype)Newtype.create(
Sequence<Rune>.UnicodeFromString(this.name),
Sequence<DAST.Type>.FromArray(this.typeParams.ToArray()),
this.baseType,
Sequence<DAST.Statement>.FromArray(this.witnessStmts.ToArray()),
this.witness == null ? Optional<DAST._IExpression>.create_None() : Optional<DAST._IExpression>.create_Some(this.witness)
));
return parent;
}
}
interface DatatypeContainer {
void AddDatatype(Datatype item);
public DatatypeBuilder Datatype(string name, string enclosingModule, List<DAST.Type> typeParams, List<DAST.DatatypeCtor> ctors, bool isCo) {
return new DatatypeBuilder(this, name, enclosingModule, typeParams, ctors, isCo);
}
}
class DatatypeBuilder : ClassLike {
readonly DatatypeContainer parent;
readonly string name;
readonly string enclosingModule;
readonly List<DAST.Type> typeParams;
readonly List<DAST.DatatypeCtor> ctors;
readonly bool isCo;
readonly List<DAST.Method> body = new();
public DatatypeBuilder(DatatypeContainer parent, string name, string enclosingModule, List<DAST.Type> typeParams, List<DAST.DatatypeCtor> ctors, bool isCo) {
this.parent = parent;
this.name = name;
this.typeParams = typeParams;
this.enclosingModule = enclosingModule;
this.ctors = ctors;
this.isCo = isCo;
}
public void AddMethod(DAST.Method item) {
body.Add(item);
}
public void AddField(DAST.Formal item, DAST.Expression defaultValue) {
throw new NotImplementedException();
}
public object Finish() {
parent.AddDatatype((Datatype)Datatype.create(
Sequence<Rune>.UnicodeFromString(this.name),
Sequence<Rune>.UnicodeFromString(this.enclosingModule),
Sequence<DAST.Type>.FromArray(typeParams.ToArray()),
Sequence<DAST.DatatypeCtor>.FromArray(ctors.ToArray()),
Sequence<DAST.Method>.FromArray(body.ToArray()),
this.isCo
));
return parent;
}
}
interface ClassLike {
void AddMethod(DAST.Method item);
void AddField(DAST.Formal item, DAST.Expression defaultValue);
public MethodBuilder Method(
bool isStatic, bool hasBody,
ISequence<ISequence<Rune>> overridingPath,
string name,
List<DAST.Type> typeArgs,
List<DAST.Formal> params_,
List<DAST.Type> outTypes, List<ISequence<Rune>> outVars
) {
return new MethodBuilder(this, isStatic, hasBody, overridingPath, name, typeArgs, params_, outTypes, outVars);
}
public object Finish();
}
class MethodBuilder : StatementContainer {
readonly ClassLike parent;
readonly string name;
readonly bool isStatic;
readonly bool hasBody;
readonly ISequence<ISequence<Rune>> overridingPath;
readonly List<DAST.Type> typeArgs;
readonly List<DAST.Formal> params_;
readonly List<DAST.Type> outTypes;
readonly List<ISequence<Rune>> outVars;
readonly List<object> body = new();
public MethodBuilder(
ClassLike parent,
bool isStatic, bool hasBody,
ISequence<ISequence<Rune>> overridingPath,
string name,
List<DAST.Type> typeArgs,
List<DAST.Formal> params_,
List<DAST.Type> outTypes, List<ISequence<Rune>> outVars
) {
this.parent = parent;
this.isStatic = isStatic;
this.hasBody = hasBody;
this.overridingPath = overridingPath;
this.name = name;
this.typeArgs = typeArgs;
this.params_ = params_;
this.outTypes = outTypes;
this.outVars = outVars;
}
public List<object> ForkList() {
var ret = new List<object>();
body.Add(ret);
return ret;
}
public void AddStatement(DAST.Statement item) {
body.Add(item);
}
public void AddBuildable(BuildableStatement item) {
body.Add(item);
}
public DAST.Method Build() {
List<DAST.Statement> builtStatements = new();
StatementContainer.RecursivelyBuild(body, builtStatements);
return (DAST.Method)DAST.Method.create(
isStatic,
hasBody,
overridingPath != null ? Optional<ISequence<ISequence<Rune>>>.create_Some(overridingPath) : Optional<ISequence<ISequence<Rune>>>.create_None(),
Sequence<Rune>.UnicodeFromString(this.name),
Sequence<DAST.Type>.FromArray(typeArgs.ToArray()),
Sequence<DAST.Formal>.FromArray(params_.ToArray()),
Sequence<DAST.Statement>.FromArray(builtStatements.ToArray()),
Sequence<DAST.Type>.FromArray(outTypes.ToArray()),
outVars != null ? Optional<ISequence<ISequence<Rune>>>.create_Some(Sequence<ISequence<Rune>>.FromArray(outVars.ToArray())) : Optional<ISequence<ISequence<Rune>>>.create_None()
);
}
}
interface StatementContainer {
void AddStatement(DAST.Statement item);
void AddBuildable(BuildableStatement item);
List<object> ForkList();
public StatementContainer Fork() {
return new ForkedStatementContainer(ForkList());
}
protected static void RecursivelyBuild(List<object> body, List<DAST.Statement> builtStatements) {
foreach (var maybeBuilt in body) {
if (maybeBuilt is DAST.Statement built) {
builtStatements.Add(built);
} else if (maybeBuilt is BuildableStatement buildable) {
builtStatements.Add(buildable.Build());
} else if (maybeBuilt is List<object> rec) {
RecursivelyBuild(rec, builtStatements);
} else {
throw new InvalidOperationException("Unknown buildable type");
}
}
}
public void Print(DAST.Expression expr) {
AddStatement((DAST.Statement)DAST.Statement.create_Print(expr));
}
public AssignBuilder Assign() {
var ret = new AssignBuilder();
AddBuildable(ret);
return ret;
}
public DeclareBuilder DeclareAndAssign(DAST.Type type, string name) {
var ret = new DeclareBuilder(type, name);
AddBuildable(ret);
return ret;
}
public IfElseBuilder IfElse() {
var ret = new IfElseBuilder();
AddBuildable(ret);
return ret;
}
public WhileBuilder While() {
var ret = new WhileBuilder();
AddBuildable(ret);
return ret;
}
public TailRecursiveBuilder TailRecursive() {
var ret = new TailRecursiveBuilder();
AddBuildable(ret);
return ret;
}
public CallStmtBuilder Call() {
var ret = new CallStmtBuilder();
AddBuildable(ret);
return ret;
}
public ReturnBuilder Return() {
var ret = new ReturnBuilder();
AddBuildable(ret);
return ret;
}
}
interface BuildableStatement {
DAST.Statement Build();
}
class ForkedStatementContainer : StatementContainer {
readonly List<object> list;
public ForkedStatementContainer(List<object> list) {
this.list = list;
}
public void AddStatement(DAST.Statement item) {
list.Add(item);
}
public void AddBuildable(BuildableStatement item) {
list.Add(item);
}
public List<object> ForkList() {
var ret = new List<object>();
list.Add(ret);
return ret;
}
}
class DeclareBuilder : ExprContainer, BuildableStatement {
readonly DAST.Type type;
readonly string name;
public object value;
public DeclareBuilder(DAST.Type type, string name) {
this.type = type;
this.name = name;
}
public void AddExpr(DAST.Expression value) {
if (this.value != null) {
throw new InvalidOperationException();
} else {
this.value = value;
}
}
public void AddBuildable(BuildableExpr value) {
if (this.value != null) {
throw new InvalidOperationException();
} else {
this.value = value;
}
}
public DAST.Statement Build() {
if (this.value == null) {
return (DAST.Statement)DAST.Statement.create_DeclareVar(Sequence<Rune>.UnicodeFromString(name), type, DAST.Optional<DAST._IExpression>.create_None());
} else {
var builtValue = new List<DAST.Expression>();
ExprContainer.RecursivelyBuild(new List<object> { value }, builtValue);
return (DAST.Statement)DAST.Statement.create_DeclareVar(Sequence<Rune>.UnicodeFromString(name), type, DAST.Optional<DAST._IExpression>.create_Some(builtValue[0]));
}
}
}
class AssignBuilder : ExprContainer, BuildableStatement {
DAST.AssignLhs lhs = null;
public object value;
public void SetLhs(DAST.AssignLhs lhs) {
if (this.lhs != null && this.lhs != lhs) {
throw new InvalidOperationException("Cannot change name of variable in assignment: " + this.lhs + " -> " + lhs);
} else {
this.lhs = lhs;
}
}
public void AddExpr(DAST.Expression value) {
if (this.value != null) {
throw new InvalidOperationException();
} else {
this.value = value;
}
}
public void AddBuildable(BuildableExpr value) {
if (this.value != null) {
throw new InvalidOperationException();
} else {
this.value = value;
}
}
public DAST.Statement Build() {
if (this.value == null) {
throw new InvalidOperationException("Cannot assign null value to variable: " + lhs);
} else {
var builtValue = new List<DAST.Expression>();
ExprContainer.RecursivelyBuild(new List<object> { value }, builtValue);
return (DAST.Statement)DAST.Statement.create_Assign(lhs, builtValue[0]);
}
}
}
class IfElseBuilder : ExprContainer, StatementContainer, BuildableStatement {
object condition = null;
readonly List<object> ifBody = new();
readonly List<object> elseBody = new();
public IfElseBuilder() { }
public void AddExpr(DAST.Expression value) {
if (condition != null) {
throw new InvalidOperationException();
} else {
condition = value;
}
}
public void AddBuildable(BuildableExpr value) {
if (condition != null) {
throw new InvalidOperationException();
} else {
condition = value;
}
}
public void AddStatement(DAST.Statement item) {
ifBody.Add(item);
}
public void AddBuildable(BuildableStatement item) {
ifBody.Add(item);
}
public List<object> ForkList() {
var ret = new List<object>();
this.ifBody.Add(ret);
return ret;
}
public void AddElseStatement(DAST.Statement item) {
elseBody.Add(item);
}
public void AddElseBuildable(BuildableStatement item) {
elseBody.Add(item);
}
public List<object> ElseForkList() {
var ret = new List<object>();
this.elseBody.Add(ret);
return ret;
}
public ElseBuilder Else() {
return new ElseBuilder(this);
}
public DAST.Statement Build() {
List<DAST.Expression> builtCondition = new();
ExprContainer.RecursivelyBuild(new List<object> { condition }, builtCondition);
List<DAST.Statement> builtIfStatements = new();
StatementContainer.RecursivelyBuild(ifBody, builtIfStatements);
List<DAST.Statement> builtElseStatements = new();
StatementContainer.RecursivelyBuild(elseBody, builtElseStatements);
return (DAST.Statement)DAST.Statement.create_If(
builtCondition[0],
Sequence<DAST.Statement>.FromArray(builtIfStatements.ToArray()),
Sequence<DAST.Statement>.FromArray(builtElseStatements.ToArray())
);
}
}
class ElseBuilder : StatementContainer {
public readonly IfElseBuilder parent;
public ElseBuilder(IfElseBuilder parent) {
this.parent = parent;
}
public List<object> ForkList() {
return parent.ElseForkList();
}
public void AddStatement(DAST.Statement item) {
parent.AddElseStatement(item);
}
public void AddBuildable(BuildableStatement item) {
parent.AddElseBuildable(item);
}
}
class WhileBuilder : ExprContainer, StatementContainer, BuildableStatement {
object condition = null;
readonly List<object> body = new();
public WhileBuilder() { }
public void AddExpr(DAST.Expression value) {
if (condition != null) {
throw new InvalidOperationException();
} else {
condition = value;
}
}
public void AddBuildable(BuildableExpr value) {
if (condition != null) {
throw new InvalidOperationException();
} else {
condition = value;
}
}
public void AddStatement(DAST.Statement item) {
body.Add(item);
}
public void AddBuildable(BuildableStatement item) {
body.Add(item);
}
public List<object> ForkList() {
var ret = new List<object>();
this.body.Add(ret);
return ret;
}
public DAST.Statement Build() {
List<DAST.Expression> builtCondition = new();
ExprContainer.RecursivelyBuild(new List<object> { condition }, builtCondition);
List<DAST.Statement> builtStatements = new();
StatementContainer.RecursivelyBuild(body, builtStatements);
return (DAST.Statement)DAST.Statement.create_While(
builtCondition[0],
Sequence<DAST.Statement>.FromArray(builtStatements.ToArray())
);
}
}
class TailRecursiveBuilder : StatementContainer, BuildableStatement {
readonly List<object> body = new();
public TailRecursiveBuilder() { }
public void AddStatement(DAST.Statement item) {
body.Add(item);
}
public void AddBuildable(BuildableStatement item) {
body.Add(item);
}
public List<object> ForkList() {
var ret = new List<object>();
this.body.Add(ret);
return ret;
}
public DAST.Statement Build() {
List<DAST.Statement> builtStatements = new();
StatementContainer.RecursivelyBuild(body, builtStatements);
return (DAST.Statement)DAST.Statement.create_TailRecursive(
Sequence<DAST.Statement>.FromArray(builtStatements.ToArray())
);
}
}
class CallStmtBuilder : ExprContainer, BuildableStatement {
object on = null;
string name = null;
List<DAST.Type> typeArgs = null;
readonly List<object> args = new();
List<ISequence<Rune>> outs = null;
public CallStmtBuilder() { }
public void SetName(string name) {
if (this.name != null) {
throw new InvalidOperationException();
} else {
this.name = name;
}
}
public void SetTypeArgs(List<DAST.Type> typeArgs) {
if (this.typeArgs != null) {
throw new InvalidOperationException();
} else {
this.typeArgs = typeArgs;
}
}
public void AddExpr(DAST.Expression value) {
if (on == null) {
on = value;
} else {
args.Add(value);
}
}
public void AddBuildable(BuildableExpr value) {
if (on == null) {
on = value;
} else {
args.Add(value);
}
}
public void SetOuts(List<ISequence<Rune>> outs) {
if (this.outs != null) {
throw new InvalidOperationException();
} else {
this.outs = outs;
}
}
public DAST.Statement Build() {
List<DAST.Expression> builtOn = new();
ExprContainer.RecursivelyBuild(new List<object> { on }, builtOn);
List<DAST.Expression> builtArgs = new();
ExprContainer.RecursivelyBuild(args, builtArgs);
return (DAST.Statement)DAST.Statement.create_Call(
builtOn[0],
Sequence<Rune>.UnicodeFromString(name),
Sequence<DAST.Type>.FromArray(typeArgs.ToArray()),
Sequence<DAST.Expression>.FromArray(builtArgs.ToArray()),
outs == null ? DAST.Optional<ISequence<ISequence<Rune>>>.create_None() : DAST.Optional<ISequence<ISequence<Rune>>>.create_Some(Sequence<ISequence<Rune>>.FromArray(outs.ToArray()))
);
}
}
class ReturnBuilder : ExprContainer, BuildableStatement {
object value = null;
public ReturnBuilder() { }
public void AddExpr(DAST.Expression value) {
if (this.value != null) {
throw new InvalidOperationException();
} else {
this.value = value;
}
}
public void AddBuildable(BuildableExpr value) {
if (this.value != null) {
throw new InvalidOperationException();
} else {
this.value = value;
}
}
public DAST.Statement Build() {
var builtValue = new List<DAST.Expression>();
ExprContainer.RecursivelyBuild(new List<object> { value }, builtValue);
return (DAST.Statement)DAST.Statement.create_Return(builtValue[0]);
}
}
class StatementBuffer : StatementContainer {
readonly List<object> statements = new();
public void AddStatement(DAST.Statement item) {
statements.Add(item);
}
public void AddBuildable(BuildableStatement item) {
statements.Add(item);
}
public List<object> ForkList() {
var ret = new List<object>();
statements.Add(ret);
return ret;
}
public List<DAST.Statement> PopAll() {
var builtResult = new List<DAST.Statement>();
StatementContainer.RecursivelyBuild(statements, builtResult);
return builtResult;
}
}
class ExprBuffer : ExprContainer {
Stack<object> exprs = new();
public readonly object parent;
public ExprBuffer(object returnTo) {
this.parent = returnTo;
}
public void AddExpr(DAST.Expression item) {
exprs.Push(item);
}
public void AddBuildable(BuildableExpr item) {
exprs.Push(item);
}
public List<DAST.Expression> PopN(int n) {
if (exprs.Count < n) {
throw new InvalidOperationException();
} else {
var result = new List<object>();
for (int i = 0; i < n; i++) {
result.Insert(0, exprs.Pop());
}
var builtResult = new List<DAST.Expression>();
ExprContainer.RecursivelyBuild(result, builtResult);
return builtResult;
}
}
public List<DAST.Expression> PopAll() {
return PopN(exprs.Count);
}
public DAST.Expression Finish() {
if (exprs.Count != 1) {
throw new InvalidOperationException("Expected exactly one expression in buffer, got " + exprs.Comma(e => e.ToString()));
} else {
return PopN(1)[0];
}
}
}
interface ExprContainer {
void AddExpr(DAST.Expression item);
void AddBuildable(BuildableExpr item);
BinOpBuilder BinOp(string op) {
var ret = new BinOpBuilder(op);
AddBuildable(ret);
return ret;
}
CallExprBuilder Call() {
var ret = new CallExprBuilder();
AddBuildable(ret);
return ret;
}
LambdaExprBuilder Lambda(List<DAST.Formal> formals, DAST.Type retType) {
var ret = new LambdaExprBuilder(formals, retType);
AddBuildable(ret);
return ret;
}
IIFEExprBuilder IIFE(string name, DAST.Type tpe) {
var ret = new IIFEExprBuilder(name, tpe);
AddBuildable(ret);
return ret;
}
ConvertBuilder Convert(DAST.Type fromType, DAST.Type toType) {
var ret = new ConvertBuilder(fromType, toType);
AddBuildable(ret);
return ret;
}
protected static void RecursivelyBuild(List<object> body, List<DAST.Expression> builtExprs) {
foreach (var maybeBuilt in body) {
if (maybeBuilt is DAST.Expression built) {
builtExprs.Add(built);
} else if (maybeBuilt is BuildableExpr buildable) {
builtExprs.Add(buildable.Build());
} else {
throw new InvalidOperationException("Unknown buildable type: " + maybeBuilt.GetType());
}
}
}
}
interface BuildableExpr {
DAST.Expression Build();
}
class BinOpBuilder : ExprContainer, BuildableExpr {
readonly string op;
readonly List<object> operands = new();
public BinOpBuilder(string op) {
this.op = op;
}
public void AddExpr(DAST.Expression item) {
operands.Add(item);
}
public void AddBuildable(BuildableExpr item) {
operands.Add(item);
}
public DAST.Expression Build() {
if (operands.Count != 2) {
throw new InvalidOperationException("Expected exactly two operands, got " + operands.Comma(o => o.ToString()));
}
var builtOperands = new List<DAST.Expression>();
ExprContainer.RecursivelyBuild(operands, builtOperands);
return (DAST.Expression)DAST.Expression.create_BinOp(Sequence<Rune>.UnicodeFromString(op), builtOperands[0], builtOperands[1]);
}
}
class CallExprBuilder : ExprContainer, BuildableExpr {
object on = null;
string name = null;
List<DAST.Type> typeArgs = null;
readonly List<object> args = new();
List<ISequence<Rune>> outs = null;
public CallExprBuilder() { }
public void SetName(string name) {
if (this.name != null) {
throw new InvalidOperationException();
} else {
this.name = name;
}
}
public void SetTypeArgs(List<DAST.Type> typeArgs) {
if (this.typeArgs != null) {
throw new InvalidOperationException();
} else {
this.typeArgs = typeArgs;
}
}
public void AddExpr(DAST.Expression value) {
if (on == null) {
on = value;
} else {
args.Add(value);
}
}
public void AddBuildable(BuildableExpr value) {
if (on == null) {
on = value;
} else {
args.Add(value);
}
}
public void SetOuts(List<ISequence<Rune>> outs) {
if (this.outs != null) {
throw new InvalidOperationException();
} else {
this.outs = outs;
}
}
public DAST.Expression Build() {
var builtOn = new List<DAST.Expression>();
ExprContainer.RecursivelyBuild(new List<object> { on }, builtOn);
var builtArgs = new List<DAST.Expression>();
ExprContainer.RecursivelyBuild(args, builtArgs);
return (DAST.Expression)DAST.Expression.create_Call(
builtOn[0],
Sequence<Rune>.UnicodeFromString(name),
Sequence<DAST.Type>.FromArray(typeArgs.ToArray()),
Sequence<DAST.Expression>.FromArray(builtArgs.ToArray())
);
}
}
}
class LambdaExprBuilder : StatementContainer, BuildableExpr {
readonly List<DAST.Formal> formals;
readonly DAST.Type retType;
readonly List<object> body = new();
public LambdaExprBuilder(List<DAST.Formal> formals, DAST.Type retType) {
this.formals = formals;
this.retType = retType;
}
public void AddStatement(DAST.Statement item) {
body.Add(item);