-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenSesterl.ml
783 lines (670 loc) · 25.6 KB
/
genSesterl.ml
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
open Types
module Constant : GenMlScheme.CONSTANT = struct
let fun_decode_field = "Json.Decode.field"
let fun_decode_and_then = ("Json.Decode.bind", false)
let fun_decode_map = "Json.Decode.map"
let fun_decode_succeed = "Json.Decode.pure"
let fun_decode_fail = "Json.Decode.fail"
let fun_decode_string = "Json.Decode.string"
let fun_encode_object = "Json.Encode.object_from_list"
let fun_encode_string = "Json.Encode.string"
let type_decoder = "Json.Decode.t"
let type_value = "Json.Encode.t"
let global_decoder name =
"decode_" ^ (Name.to_snake_case name)
let global_encoder name =
"encode_" ^ (Name.to_snake_case name)
let local_for_key key =
"local_key_" ^ (Key.to_snake_case key)
let local_for_parameter x =
"local_param_" ^ (Variable.to_snake_case x)
module ReservedWordSet = Set.Make(String)
let reserved_words =
ReservedWordSet.of_list [
"let"; "rec"; "and"; "in"; "fun";
"if"; "then"; "else"; "true"; "false";
"act"; "do"; "receive"; "end";
"case"; "of";
"val"; "type"; "module"; "struct"; "signature"; "sig"; "with";
"external"; "include"; "import"; "freeze"; "pack"; "assert"; "open";
]
let key key =
let s = Key.to_snake_case key in
if reserved_words |> ReservedWordSet.mem s then
s ^ "_"
else
s
let constructor =
Constructor.to_upper_camel_case
let builtin_type_candidates =
let ( ==> ) x y = (x, y) in
[
Name.bool ==> "bool";
Name.int ==> "int";
Name.string ==> "binary";
Name.list ==> "list";
Name.option ==> "option";
]
let type_identifier name =
match
builtin_type_candidates |> List.find_map (fun (namex, s) ->
if Name.compare namex name = 0 then Some(s) else None
)
with
| Some(s) -> s
| None -> Name.to_snake_case name
let type_parameter x =
"$" ^ (Variable.to_snake_case x)
end
module Output : sig
type identifier
val global_decoder : Name.t -> identifier
val global_encoder : Name.t -> identifier
val local_for_key : Key.t -> identifier
val local_for_parameter : Variable.t -> identifier
type tree
val identifier : identifier -> tree
val application : identifier -> tree list -> tree
val general_application : tree -> tree -> tree
val abstraction : identifier -> tree -> tree
val string_literal : string -> tree
val constructor : Types.constructor -> tree
val tuple : tree list -> tree
val list : tree list -> tree
val record : tree RecordMap.t -> tree
val record_field_access : tree -> Key.t -> tree
val branching : tree VariantMap.t -> tree
val decode_field_access : Key.t -> tree -> tree
val access_argument : tree -> tree
val and_then : tree -> tree -> tree
val map : tree -> tree -> tree
val succeed : tree -> tree
val encode_record : tree -> tree
val encode_branching : (tree option) VariantMap.t -> tree
type type_identifier
val type_identifier : Name.t -> type_identifier
type type_parameter
val type_parameter : Variable.t -> type_parameter
type typ
val type_name : type_identifier -> typ list -> typ
val type_variable : type_parameter -> typ
val record_type : (Key.t * typ) list -> typ
val function_type : typ list -> typ -> typ
val decoder_type : typ -> typ
val encoder_type : typ -> typ
type declaration
val stringify_declaration : declaration -> string
val define_value : identifier -> type_parameter list -> (identifier * typ) list -> typ -> tree -> declaration
val define_value_by_text : identifier -> typ -> string -> declaration
val built_in_decoder : built_in -> declaration
val built_in_encoder : built_in -> declaration
val define_type_alias : type_identifier -> type_parameter list -> typ -> declaration
val define_data_type : type_identifier -> type_parameter list -> (Types.constructor * typ option) list -> declaration
val define_type_by_text : type_identifier -> string -> declaration
end = struct
include GenMlScheme.Make(Constant)
let rec stringify_pattern (pat : pattern) : string =
match pat with
| StringPattern(s) ->
Format.sprintf "\"%s\"" s
| ConstructorPattern(sctor, patopt) ->
begin
match patopt with
| None -> sctor
| Some(patsub) -> Format.sprintf "%s(%s)" sctor (stringify_pattern patsub)
end
| IdentifierPattern(Var(s)) ->
s
let rec stringify_tree (otree : tree) : string =
match otree with
| Identifier(Var(s)) ->
s
| Application{ applied = applied; arguments = args; } ->
let s = stringify_tree applied in
let sargs = args |> List.map stringify_tree in
Format.sprintf "%s(%s)" s (String.concat ", " sargs)
| Abstract{ variable = Var(s); body = body; } ->
Format.sprintf "(fun(%s) -> %s end)" s (stringify_tree body)
| StringLiteral(s) ->
Format.sprintf "\"%s\"" s
| Constructor(ctor) ->
ctor
| Tuple(otrees) ->
let ss = otrees |> List.map stringify_tree in
Format.sprintf "{%s}" (String.concat ", " ss)
| List(otrees) ->
let ss = otrees |> List.map stringify_tree in
Format.sprintf "[ %s ]" (String.concat ", " ss)
| Record(orcd) ->
let ss =
RecordMap.fold (fun key otree acc ->
let s = Format.sprintf "%s = %s" (Constant.key key) (stringify_tree otree) in
Alist.extend acc s
) orcd Alist.empty |> Alist.to_list
in
Format.sprintf "{ %s }" (String.concat ", " ss)
| FieldAccess{ record = otree_record; key = key; } ->
let skey = Constant.key key in
let s = stringify_tree otree_record in
Format.sprintf "%s.%s" s skey
| Case{ subject = otree_subject; branches = branches } ->
let ss =
branches |> List.map (fun (pat, otree) ->
Format.sprintf " | %s -> %s" (stringify_pattern pat) (stringify_tree otree)
)
in
Format.sprintf "(case %s of %s end)" (stringify_tree otree_subject) (String.concat "" ss)
let rec stringify_type (ty : typ) : string =
match ty with
| TypeName(TypeIdentifier(s), tyargs) ->
begin
match tyargs with
| [] ->
s
| _ :: _ ->
let ss = tyargs |> List.map stringify_type in
Format.sprintf "%s<%s>" s (String.concat ", " ss)
end
| TypeVariable(TypeParameter(a)) ->
a
| FuncType(tys1, ty2) ->
let sdom = tys1 |> List.map stringify_type |> String.concat ", " in
Format.sprintf "fun(%s) -> %s"
sdom
(stringify_type ty2)
| RecordType(tyrcd) ->
begin
match tyrcd with
| [] ->
"unit"
| _ :: _ ->
let sr =
tyrcd |> List.map (fun (key, ty) ->
Format.sprintf "%s : %s" (Constant.key key) (stringify_type ty)
) |> String.concat ", "
in
Format.sprintf "{ %s }" sr
end
let stringify_declaration (odecl : declaration) : string =
match odecl with
| DefVal{
val_name = Var(s);
universal = otyparams;
parameters = oparams;
return_type = tyret;
body = otree;
} ->
let styparam =
match otyparams with
| [] -> ""
| _ :: _ -> "<" ^ (String.concat ", " (otyparams |> List.map (fun (TypeParameter(s)) -> s))) ^ ">"
in
let ss = oparams |> List.map (fun (Var(s), ty) -> s ^ " : " ^ (stringify_type ty)) in
Format.sprintf "%s%s(%s) : %s = %s"
s
styparam
(String.concat ", " ss)
(stringify_type tyret)
(stringify_tree otree)
| DefValByText{
val_name = Var(s);
typ = typ;
text = text;
} ->
Printf.sprintf "%s() : %s = %s"
s
(stringify_type typ)
text
| DefTypeAlias{
type_name = TypeIdentifier(s);
parameters = otyparams;
body = ty;
} ->
let sparam =
match otyparams with
| [] -> ""
| _ :: _ -> "<" ^ (String.concat ", " (otyparams |> List.map (fun (TypeParameter(a)) -> a))) ^ ">"
in
Format.sprintf "%s%s = %s"
s
sparam
(stringify_type ty)
| DefDataType{
type_name = TypeIdentifier(s);
parameters = otyparams;
patterns = defs;
} ->
let ss =
defs |> List.mapi (fun index (ctor, tyopt) ->
let sarg =
match tyopt with
| None -> ""
| Some(ty) -> "(" ^ (stringify_type ty) ^ ")"
in
Format.sprintf " | %s%s" (Constant.constructor ctor) sarg
)
in
let sparam =
match otyparams with
| [] -> ""
| _ :: _ -> "<" ^ (String.concat ", " (otyparams |> List.map (fun (TypeParameter(a)) -> a))) ^ ">"
in
Format.sprintf "%s%s = %s"
s
sparam
(String.concat "" ss)
| DefTypeByText{
type_name = TypeIdentifier(s);
text = text;
} ->
Printf.sprintf "%s = %s"
s
text
let built_in_decoder (builtin : built_in) : declaration =
let dec = decoder_type in
let ( !$ ) tp = TypeVariable(tp) in
match builtin with
| BBool ->
DefVal{
val_name = global_decoder Name.bool;
universal = [];
parameters = [];
return_type = dec (base Name.bool);
body = application (Var("Json.Decode.bool")) [];
}
| BInt ->
DefVal{
val_name = global_decoder Name.int;
universal = [];
parameters = [];
return_type = dec (base Name.int);
body = application (Var("Json.Decode.int")) [];
}
| BString ->
DefVal{
val_name = global_decoder Name.string;
universal = [];
parameters = [];
return_type = dec (base Name.string);
body = application (Var("Json.Decode.string")) [];
}
| BList(_) ->
let ovar = Var("x") in
let typaram = TypeParameter("$a") in
DefVal{
val_name = global_decoder Name.list;
universal = [ typaram ];
parameters = [ (ovar, dec (!$ typaram)) ];
return_type = dec (TypeName(type_identifier Name.list, [!$ typaram]));
body = application (Var("Json.Decode.list")) [ Identifier(ovar) ];
}
| BOption(_) ->
let otree_abs =
let ovar = Var("temp") in
abstraction ovar (general_application (Constructor("Some")) (identifier ovar))
in
let ovar = Var("x") in
let omap =
VariantMap.empty
|> VariantMap.add Constructor.none (succeed (Constructor("None")))
|> VariantMap.add Constructor.some (access_argument (map otree_abs (Identifier(ovar))))
in
let typaram = TypeParameter("$a") in
DefVal{
val_name = global_decoder Name.option;
universal = [ typaram ];
parameters = [ (ovar, dec (!$ typaram)) ];
return_type = dec (TypeName(type_identifier Name.option, [!$ typaram]));
body = branching omap;
}
let built_in_encoder (builtin : built_in) : declaration =
let enc = encoder_type in
let ( !$ ) tp = TypeVariable(tp) in
match builtin with
| BBool ->
let ovar = Var("v") in
DefVal{
val_name = global_encoder Name.bool;
universal = [];
parameters = [];
return_type = enc (base Name.bool);
body = abstraction ovar (application (Var("Json.Encode.bool")) [ identifier ovar ]);
}
| BInt ->
let ovar = Var("v") in
DefVal{
val_name = global_encoder Name.int;
universal = [];
parameters = [];
return_type = enc (base Name.int);
body = abstraction ovar (application (Var("Json.Encode.int")) [ identifier ovar ]);
}
| BString ->
let ovar = Var("v") in
DefVal{
val_name = global_encoder Name.string;
universal = [];
parameters = [];
return_type = enc (base Name.string);
body = abstraction ovar (application (Var("Json.Encode.string")) [ identifier ovar ]);
}
| BList(_) ->
let ovar_param = Var("enc") in
let ovar = Var("v") in
let typaram = TypeParameter("$a") in
let body =
abstraction ovar
(application (Var("Json.Encode.list"))
[ application (Var("Stdlib.List.map")) [ identifier ovar_param; identifier ovar; ] ])
in
DefVal{
val_name = global_encoder Name.list;
universal = [ typaram ];
parameters = [ (ovar_param, enc (!$ typaram)) ];
return_type = enc (TypeName(type_identifier Name.list, [!$ typaram]));
body = body;
}
| BOption(_) ->
let ovar_param = Var("enc") in
let ovar = Var("v") in
let ovar_sub = Var("sub") in
let typaram = TypeParameter("$a") in
let body =
abstraction ovar
(Case{
subject = Identifier(ovar);
branches = [
(ConstructorPattern("None", None),
encoded_none);
(ConstructorPattern("Some", Some(IdentifierPattern(ovar_sub))),
encoded_some (general_application (Identifier(ovar_param)) (Identifier(ovar_sub))));
];
})
in
DefVal{
val_name = global_encoder Name.option;
universal = [ typaram ];
parameters = [ (ovar_param, enc (!$ typaram)) ];
return_type = enc (TypeName(type_identifier Name.option, [!$ typaram]));
body = body;
}
end
let decoder_of_variable (x : Variable.t) : Output.tree =
Output.identifier (Output.local_for_parameter x)
let rec decoder_of_name (name : Name.t) (args : message list) : Output.tree =
let otrees = args |> List.map generate_message_decoder in
Output.application (Output.global_decoder name) otrees
and decoder_of_record (rcd : message RecordMap.t) : Output.tree =
let otree_ans =
let orcd =
rcd |> RecordMap.mapi (fun key _ ->
Output.identifier (Output.local_for_key key)
)
in
Output.succeed (Output.record orcd)
in
let acc =
RecordMap.fold (fun key vmsg acc ->
let otree_decv = generate_message_decoder vmsg in
let otree_accessk = Output.decode_field_access key otree_decv in
Alist.extend acc (key, otree_accessk)
) rcd Alist.empty
in
List.fold_right (fun (key, otree_accessk) otree_acc ->
Output.and_then (Output.abstraction (Output.local_for_key key) otree_acc) otree_accessk
) (acc |> Alist.to_list) otree_ans
and decoder_of_variant (variant : (message option) VariantMap.t) : Output.tree =
let ocases =
variant |> VariantMap.mapi (fun ctor argmsgopt ->
match argmsgopt with
| None ->
Output.succeed (Output.constructor ctor)
| Some(argmsg) ->
let otree_decarg = generate_message_decoder argmsg in
let otree_abs =
let ovar =
match Variable.from_snake_case "temp" with
| Some(s) -> Output.local_for_parameter s
| None -> assert false
(* TODO: make less ad-hoc *)
in
let open Output in
abstraction ovar (general_application (constructor ctor) (identifier ovar))
in
Output.access_argument (Output.map otree_abs otree_decarg)
)
in
Output.branching ocases
and generate_message_decoder (msg : message) : Output.tree =
match msg with
| Variable((_, x)) -> decoder_of_variable x
| Name((_, name), args) -> decoder_of_name name args
let rec generate_message_type (msg : message) : Output.typ =
match msg with
| Variable((_, x)) ->
Output.type_variable (Output.type_parameter x)
| Name((_, name), args) ->
let tys = args |> List.map generate_message_type in
Output.type_name (Output.type_identifier name) tys
let generate_record_type (record : record) : Output.typ =
let tyrcd =
RecordMap.fold (fun key vmsg acc ->
let ty = generate_message_type vmsg in
Alist.extend acc (key, ty)
) record Alist.empty |> Alist.to_list
in
Output.record_type tyrcd
let make_decoder_parameters (params : (Variable.t ranged) list) =
params |> List.map (fun (_, x) ->
let ovar = Output.local_for_parameter x in
let ty = Output.decoder_type (Output.type_variable (Output.type_parameter x)) in
(ovar, ty)
)
let make_encoder_parameters (params : (Variable.t ranged) list) =
params |> List.map (fun (_, x) ->
let ovar = Output.local_for_parameter x in
let ty = Output.encoder_type (Output.type_variable (Output.type_parameter x)) in
(ovar, ty)
)
let encoder_of_variable (x : Variable.t) : Output.tree =
Output.identifier (Output.local_for_parameter x)
let rec encoder_of_name (name : Name.t) (args : message list) : Output.tree =
let otrees = args |> List.map generate_message_encoder in
Output.application (Output.global_encoder name) otrees
and encoder_of_record (rcd : message RecordMap.t) : Output.tree =
let x_record =
match Variable.from_snake_case "temp" with
| Some(s) -> Output.local_for_parameter s
| None -> assert false
(* TODO: make less ad-hoc *)
in
let otrees =
RecordMap.fold (fun key vmsg acc ->
let otree_encoder = generate_message_encoder vmsg in
let otree_encoded =
Output.general_application otree_encoder (Output.record_field_access (Output.identifier(x_record)) key)
in
let otree_pair = Output.tuple [ Output.string_literal (CommonConstant.key_for_json key); otree_encoded ] in
Alist.extend acc otree_pair
) rcd Alist.empty |> Alist.to_list
in
Output.abstraction x_record (Output.encode_record (Output.list otrees))
and encoder_of_variant (variant : (message option) VariantMap.t) : Output.tree =
let encmap =
variant |> VariantMap.map (fun msgopt ->
match msgopt with
| None -> None
| Some(msg) -> Some(generate_message_encoder msg)
)
in
Output.encode_branching encmap
(** Given a message [msg] of type [T], [generate_message_encoder msg] generates
the code representation of encoder of type [T].
The return value is an representation of Elm code of type [T -> Value].
*)
and generate_message_encoder (msg : message) : Output.tree =
match msg with
| Variable((_, x)) -> encoder_of_variable x
| Name((_, name), args) -> encoder_of_name name args
let generate (module_name : string) (imports : string list) (decls : declarations) : (string, error) result =
let open ResultMonad in
DeclMap.fold (fun name def res ->
res >>= fun (tyacc, valacc) ->
let ovar_decoder = Output.global_decoder name in
let ovar_encoder = Output.global_encoder name in
match def.def_main with
| BuiltIn(builtin) ->
let odecl_decoder = Output.built_in_decoder builtin in
let odecl_encoder = Output.built_in_encoder builtin in
let valacc = Alist.append valacc [ odecl_decoder; odecl_encoder; ] in
return (tyacc, valacc)
| GivenNormal(msg) ->
let otyname = Output.type_identifier name in
let otyparams = def.def_params |> List.map (fun (_, x) -> Output.type_parameter x) in
let odecl_type =
Output.define_type_alias otyname otyparams (generate_message_type msg)
in
let tyaliasmsg =
let tyargs = otyparams |> List.map Output.type_variable in
Output.type_name otyname tyargs
in
let odecl_decoder =
let oparams = make_decoder_parameters def.def_params in
let tyret = Output.decoder_type tyaliasmsg in
let otree_decoder = generate_message_decoder msg in
Output.define_value ovar_decoder otyparams oparams tyret otree_decoder
in
let odecl_encoder =
let oparams = make_encoder_parameters def.def_params in
let tyret = Output.encoder_type tyaliasmsg in
let otree_encoder = generate_message_encoder msg in
Output.define_value ovar_encoder otyparams oparams tyret otree_encoder
in
let tyacc = Alist.extend tyacc odecl_type in
let valacc = Alist.append valacc [ odecl_decoder; odecl_encoder; ] in
return (tyacc, valacc)
| GivenRecord(record) ->
let otyname = Output.type_identifier name in
let otyparams = def.def_params |> List.map (fun (_, x) -> Output.type_parameter x) in
let odecl_type =
Output.define_type_alias otyname otyparams (generate_record_type record)
in
let tyaliasmsg =
let tyargs = otyparams |> List.map Output.type_variable in
Output.type_name otyname tyargs
in
let odecl_decoder =
let oparams = make_decoder_parameters def.def_params in
let tyret = Output.decoder_type tyaliasmsg in
let otree_decoder = decoder_of_record record in
Output.define_value ovar_decoder otyparams oparams tyret otree_decoder
in
let odecl_encoder =
let oparams = make_encoder_parameters def.def_params in
let tyret = Output.encoder_type tyaliasmsg in
let otree_encoder = encoder_of_record record in
Output.define_value ovar_encoder otyparams oparams tyret otree_encoder
in
let tyacc = Alist.extend tyacc odecl_type in
let valacc = Alist.append valacc [ odecl_decoder; odecl_encoder; ] in
return (tyacc, valacc)
| GivenVariant(variant) ->
let otyname = Output.type_identifier name in
let otyparams = def.def_params |> List.map (fun (_, x) -> Output.type_parameter x) in
let odecl_type =
let otymain =
VariantMap.fold (fun ctor argmsgopt acc ->
let oargtyopt =
match argmsgopt with
| None -> None
| Some(argmsg) -> Some(generate_message_type argmsg)
in
Alist.extend acc (ctor, oargtyopt)
) variant Alist.empty |> Alist.to_list
in
Output.define_data_type otyname otyparams otymain
in
let tyaliasmsg =
let tyargs = otyparams |> List.map Output.type_variable in
Output.type_name otyname tyargs
in
let odecl_decoder =
let oparams = make_decoder_parameters def.def_params in
let tyret = Output.decoder_type tyaliasmsg in
let otree_decoder = decoder_of_variant variant in
Output.define_value ovar_decoder otyparams oparams tyret otree_decoder
in
let odecl_encoder =
let oparams = make_encoder_parameters def.def_params in
let tyret = Output.encoder_type tyaliasmsg in
let otree_encoder = encoder_of_variant variant in
Output.define_value ovar_encoder otyparams oparams tyret otree_encoder
in
let tyacc = Alist.extend tyacc odecl_type in
let valacc = Alist.append valacc [ odecl_decoder; odecl_encoder; ] in
return (tyacc, valacc)
| GivenExternal(extern) ->
begin
match extern |> ExternalMap.find_opt "sesterl" with
| None ->
error (NoExternal{ format = "sesterl"; name = name })
| Some(dict) ->
begin
match def.def_params with
| _ :: _ ->
error NoParameterAllowedForExternal
| [] ->
get_mandatory_string dict "type" >>= fun type_text ->
get_mandatory_string dict "decoder" >>= fun decoder_text ->
get_mandatory_string dict "encoder" >>= fun encoder_text ->
let otyname = Output.type_identifier name in
let odecl_type =
Output.define_type_by_text otyname type_text
in
let tyaliasmsg =
Output.type_name otyname []
in
let odecl_decoder =
let tyannot = Output.decoder_type tyaliasmsg in
Output.define_value_by_text ovar_decoder tyannot decoder_text
in
let odecl_encoder =
let tyannot = Output.encoder_type tyaliasmsg in
Output.define_value_by_text ovar_encoder tyannot encoder_text
in
let tyacc = Alist.extend tyacc odecl_type in
let valacc = Alist.append valacc [ odecl_decoder; odecl_encoder; ] in
return (tyacc, valacc)
end
end
) decls (return (Alist.empty, Alist.empty)) >>= fun (tyacc, valacc) ->
let stydecls =
tyacc |> Alist.to_list |> List.mapi (fun i odecl ->
let token = if i == 0 then "type" else "and" in
Printf.sprintf " %s %s\n\n" token (Output.stringify_declaration odecl)
)
in
let svaldecls =
valacc |> Alist.to_list |> List.mapi (fun i odecl ->
let token = if i == 0 then "val rec" else "and" in
Printf.sprintf " %s %s\n\n" token (Output.stringify_declaration odecl)
)
in
let (n1, n2, n3) = language_version in
let s =
List.concat [
[
Printf.sprintf "/* Auto-generated by APBuf %d.%d.%d */\n" n1 n2 n3;
];
(imports |> List.map (fun s -> Printf.sprintf "import %s\n" s));
[
Printf.sprintf "module %s = struct\n" module_name;
];
stydecls;
svaldecls;
[
"end\n";
];
] |> String.concat ""
in
return s