-
Notifications
You must be signed in to change notification settings - Fork 2
/
translate.ml
982 lines (900 loc) · 28.5 KB
/
translate.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
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
open Core_kernel
module OldStr = Str
open Elm
open Migrate_parsetree.Ast_404
open Ast_helper
let (>>) a b = (fun x -> a (b x))
type ('a, 'b) tree =
| Leaf of 'a
| Node of (('a, 'b) tree) * ('b) * (('a, 'b) tree)
(* ------------------------ *)
(* config *)
(* ------------------------ *)
let config_function_patterns =
[ "DontPort.fromInt", "string_of_int"
; "DontPort.fromFloat", "string_of_float"
; "DontPort.replace", "Regex.replace"
]
let config_module_patterns =
[ "^LE$", "List"
; "List.Extra", "List"
; "Maybe.Extra", "Option"
; "Maybe", "Option"
; "^ME$", "Option"
; "^RE$", "Result"
]
let config_post_process_patterns =
[ "module LE = List.Extra", ""
; "module ME = Maybe.Extra", ""
; "module RE = Result.Extra", ""
; "module JSE = Json.Encode", ""
; "module JSD = Json.Decode", ""
; "module JSDP = Json.Decode.Pipeline", ""
; "module Events = Html.Events", ""
; "open DontPort", ""
; "\\(varName, dval\\) dict", "dval Belt.Map.String.t"
; "deMaybe", "deOption"
; "^let tipe2str", "let rec tipe2str"
; "^let toRepr_", "let rec toRepr_"
; "^let toRepr ", "and toRepr "
; "^let listThreadBlanks", "let rec listThreadBlanks"
; "^let closeThreads", "let rec closeThreads"
; "^let closeObjectLiterals", "let rec closeObjectLiterals"
; "^let closeListLiterals", "let rec closeListLiterals"
; "^let maybeExtendThreadAt", "let rec maybeExtendThreadAt"
; "^let wrapInThread", "let rec wrapInThread"
; "^let childrenOf", "let rec childrenOf"
; "^let uses", "let rec uses"
; "^let parentOf_", "let rec parentOf_"
; "^let allData \\(", "let rec allData ("
; "^let replace_", "let rec replace_"
; "^let clone \\(", "let rec clone ("
; "^let addThreadBlank \\(", "let rec addThreadBlank ("
; "^let isFunctionInExpr \\(", "let rec isFunctionInExpr ("
; "let rec_ancestors ", "let rec rec_ancestors "
; "Attrs.href", "Html.href"
; "Attrs.class_", "Html.class'"
; "Events.onWithOptions", "Html.onWithOptions"
; "Html.attribute", "Vdom.property"
; "floatAdd", "(+.)"
; "floatSubtract", "(-.)"
; "floatMultiply", "(*.)"
; "floatDivide", "(/.)"
; "StrSet.set", "StrSet.t"
; "action: \\(\\(model -> toplevel\\) -> pointerData\\) -> modification"
, "action: model -> toplevel -> pointerData -> modification"
]
let config_type_patterns =
[ "^tLID$", "tlid"
; "^iD$", "id"
; "^lVDict$", "lvDict"
; "^aVDict$", "avDict"
; "^fFIsExpanded$", "ffIsExpanded"
; "^rPCParams$", "rpcParams"
; "^rPCResult$", "rpcResult"
]
(* ------------------------ *)
(* post processing *)
(* ------------------------ *)
let rewrite (patterns: (string * string) list) str : string =
List.fold ~init:str patterns
~f:(fun prev (pattern,template) ->
Re2.rewrite_exn
~template
(Re2.create_exn ~options:[`Posix_syntax true; `One_line false] pattern)
prev
)
let post_process =
let patterns =
[ "\\(string, (.*)\\) dict", "\\1 Belt.Map.String.t"
; "\\(int, (.*)\\) dict", "\\1 Belt.Map.Int.t"
; "Http\\.error", "string Http.error"
]
in
rewrite (config_post_process_patterns @ patterns)
(* ------------------------ *)
(* Rename appropriately *)
(* ------------------------ *)
let keywords =
[ "and"
; "as"
; "asr"
; "assert"
; "begin"
; "class"
; "constraint"
; "do"
; "done"
; "downto"
; "else"
; "end"
; "exception"
; "external"
; "false"
; "for"
; "fun"
; "function"
; "functor"
; "if"
; "in"
; "include"
; "inherit"
; "initializer"
; "land"
; "lazy"
; "let"
; "lor"
; "lsl"
; "lsr"
; "lxor"
; "match"
; "method"
; "mod"
; "module"
; "open"
; "mutable"
; "new"
; "nonrec"
; "object"
; "of"
; "open"
; "open!"
; "or"
; "private"
; "rec"
; "sig"
; "struct"
; "then"
; "to"
; "true"
; "try"
; "type"
; "val"
; "virtual"
; "when"
; "while"
; "with"
]
let avoid_keyword (n: string) : string =
if List.mem ~equal:(=) keywords n
then n ^ "_"
else n
(* ------------------------ *)
(* Convert to OCaml AST types *)
(* ------------------------ *)
let name2string ?(kw_ok=false) ?(fix=ident) n : string =
n
|> (fun x -> if kw_ok then x else avoid_keyword x)
|> fix
let name2str ?(kw_ok=false) ?(fix=ident) (str: string) : str =
str
|> name2string ~kw_ok ~fix
|> Location.mknoloc
let name2lid ?(kw_ok=false) ?(fix=ident) n : lid =
n
|> name2string ~kw_ok ~fix
|> Longident.parse
|> Location.mknoloc
let names2something
?(fix_each : string -> string = ident)
?(fix_all : string -> string = ident)
?(fix_init : string -> string = ident)
?(fix_last : string -> string = ident)
?(fix_head : string -> string = ident)
?(fix_tail : string -> string = ident)
(names: string list)
: string =
names
|> List.map ~f:fix_each
|> (function | [] -> []
| head :: tail -> (fix_head head) :: (List.map ~f:fix_tail tail))
|> List.rev
|> (function | [] -> []
| last :: init -> (fix_last last) :: (List.map ~f:fix_init init))
|> List.rev
|> List.map ~f:avoid_keyword
|> String.concat ~sep:"."
|> fix_all
|> avoid_keyword
let names2lid
?(fix_each : string -> string = ident)
?(fix_all : string -> string = ident)
?(fix_init : string -> string = ident)
?(fix_last : string -> string = ident)
?(fix_head : string -> string = ident)
?(fix_tail : string -> string = ident)
(names: string list)
: lid =
names
|> names2something ~fix_each ~fix_all ~fix_init ~fix_last ~fix_head ~fix_tail
|> Longident.parse
|> Location.mknoloc
let names2str
?(fix_each : string -> string = ident)
?(fix_all : string -> string = ident)
?(fix_init : string -> string = ident)
?(fix_last : string -> string = ident)
?(fix_head : string -> string = ident)
?(fix_tail : string -> string = ident)
(names: string list)
: str =
names
|> names2something ~fix_each ~fix_all ~fix_init ~fix_last ~fix_head ~fix_tail
|> Location.mknoloc
(* ------------------------ *)
(* Fix specific strings and convert *)
(* ------------------------ *)
let fix_fqtype n : string =
let patterns =
[ "maybe", "option"
; "Dom.error", "Dom.errorEvent"
; "Keyboard.Event.keyboardEvent", "Dom.keyboardEvent"
; "Keyboard.Event.KeyboardEvent", "Dom.keyboardEvent"
; "Navigation.location", "Web.Location.location"
; "^time$", "Time.t"
; "^cmd$", "Cmd.t"
]
in
rewrite patterns n
let fix_type n : string =
n
|> String.uncapitalize
|> rewrite config_type_patterns
let fix_module n : string =
let patterns =
[]
in
rewrite (patterns @ config_module_patterns) n
let fix_constructor n : string =
let patterns =
[ "^Just$", "Some"
; "^Nothing$", "None"
; "^Err$", "Error"
]
in
rewrite patterns n
let fix_function name : string =
let patterns =
[ "==", "="
; "/=", "<>"
; "\\+\\+", "^"
; "%", "mod"
; "Navigation.programWithFlags", "Navigation.navigationProgram"
; "Result.toMaybe", "Result.toOption"
]
in
rewrite (patterns @ config_function_patterns) name
(* ------------------------ *)
(* TODOs *)
(* ------------------------ *)
let todosRemaining = ref []
let todo name data =
let desc = "(" ^ name ^ "): " ^ data in
todosRemaining := !todosRemaining @ [desc];
"todo " ^ (if String.length desc >= 30
then String.slice desc 0 30
else desc)
(* ------------------------ *)
(* AST conversions *)
(* ------------------------ *)
let skip_preCommented (a: 'a preCommented) : 'a = Tuple.T2.get2 a
let skip_postCommented (a: 'a postCommented) : 'a = Tuple.T2.get1 a
let skip_commented (a: 'a commented) : 'a = Tuple.T3.get2 a
let skip_located (a: 'a located) : 'a = Tuple.T2.get2 a
let skip_withEol (a: 'a withEol) : 'a = Tuple.T2.get1 a
let seq2list (s: 'a sequence) : 'a list =
List.map s
~f:(fun (_c, (_c2, (a, _s))) -> a)
let openCommentedList2list (l: 'a openCommentedList) : 'a list =
let (init, last) = l in
let newInit = List.map init
~f:(fun i -> i
|> skip_commented
|> skip_withEol)
in
let newLast = last
|> skip_preCommented
|> skip_withEol
in
newInit @ [newLast]
let litExpO lit : Parsetree.expression =
match lit with
| Str (str, _l) -> Exp.constant (Const.string str)
| Boolean true -> Exp.construct (name2lid ~kw_ok:true"true") None
| Boolean false -> Exp.construct (name2lid ~kw_ok:true "false") None
| IntNum (i, repr) -> Exp.constant (Const.int i)
| FloatNum (f, repr) -> Exp.constant (Const.float (string_of_float f))
| Chr c -> Exp.constant (Const.char c)
let litPatO lit : Parsetree.pattern =
match lit with
| Str (str, _l) -> Pat.constant (Const.string str)
| Boolean true -> Pat.construct (name2lid ~kw_ok:true"true") None
| Boolean false -> Pat.construct (name2lid ~kw_ok:true"false") None
| IntNum (i, repr) -> Pat.constant (Const.int i)
| FloatNum (f, repr) -> Pat.constant (Const.float (string_of_float f))
| Chr c -> Pat.constant (Const.char c)
let rec patpO (patp: patternp) : Parsetree.pattern =
let pats2list pats =
List.fold pats
~init:(Pat.construct (name2lid "[]") None)
~f:(fun prev arg ->
Pat.construct
(name2lid "::")
(Some (Pat.tuple [patO arg; prev])))
in
let pats2listWithTail pats last =
List.fold pats
~init:(patO last)
~f:(fun prev arg ->
Pat.construct
(name2lid "::")
(Some (Pat.tuple [patO arg; prev])))
in
match patp with
| Anything -> Pat.any ()
| VarPattern name -> Pat.var (name2str name)
| PLiteral lit ->
litPatO lit
| UnitPattern _cs ->
Pat.construct (name2lid "()") None
| TuplePattern ps ->
let ps = List.map ~f:skip_commented ps in
Pat.tuple (List.map ~f:patO ps)
| EmptyListPattern _cs ->
pats2list []
| ListPattern pats ->
pats
|> List.map ~f:skip_commented
|> pats2list
| ConsPattern { cpFirst; cpRest } ->
let pats =
Tuple.T2.get1 cpFirst
:: (List.map cpRest ~f:(fun (_cs, _cs2, pat, _wtf) -> pat))
in
(match List.rev pats with
| last :: init -> pats2listWithTail (List.rev init) last
| _ -> failwith "impossible")
| RecordPattern fields ->
let fields = List.map fields
~f:(fun (_c, name, _c2) ->
(name2lid name, Pat.var (name2str name)))
in
Pat.record fields Closed
| Data (names, args) ->
let tuple =
args
|> List.map ~f:(fun (_c, pat) -> patO pat)
|> Pat.tuple
in
let n = names2lid ~fix_last:fix_constructor names in
if args = []
then
Pat.construct n None
else
Pat.construct n (Some tuple)
| Alias ((pat, _cs), (_cs2, id)) ->
Pat.alias (patO pat) (name2str id)
| _ ->
Pat.constant
(Const.string (todo "pattern" (show_patternp patp)))
and patO (pat: pattern) : Parsetree.pattern =
pat |> skip_located |> patpO
let rec createLambda (pats: pattern list) (body: expr) : Parsetree.expression =
List.fold (List.rev pats) ~init:(exprO body)
~f:(fun (prev: Parsetree.expression) (pat: pattern) ->
Exp.fun_ Asttypes.Nolabel None (patO pat) prev)
and wrapTagInLambda (tagexpr: expr) : expr =
let arg = (Elm.fakeRegion, VarPattern "x") in
let body =
( Elm.fakeRegion
, App
( tagexpr
, [([], (fakeRegion, VarExpr (VarRef ([], "x"))))], FASplitFirst))
in
(fakeRegion, Lambda ([([], arg)], [], body, false))
and exprpO (exprp) : Parsetree.expression =
let firstArgIsFn expr =
match skip_located expr with
| VarExpr (VarRef (["List"], "map"))
| VarExpr (VarRef (["Maybe"], "map")) ->
true
| _ -> false
in
match exprp with
| Case (((_c, clause, _c2), _unknown_bool), pats) ->
let patterns =
List.map pats
~f:(fun ((_c, pat, _c2), (_c3, rhs)) ->
Exp.case (patO pat) (exprO rhs))
in
Exp.match_ (exprO clause) patterns
(* Constructors with 1 arg *)
| App ((_r, VarExpr (TagRef (path, var))), [_c, arg], _line) ->
Exp.construct
(names2lid
~fix_init:fix_module
~fix_last:fix_constructor
(path @ [var]))
(Some (exprO arg))
(* Constructors with multiple args *)
| App ((_r, VarExpr (TagRef (path, var))), args, _line) ->
Exp.construct
(names2lid
~fix_init:fix_module
~fix_last:fix_constructor
(path @ [var]))
(Some
(Exp.tuple
(List.map args ~f:(fun (_c, a) -> exprO a))))
| App ((_r, VarExpr (OpRef "::")), args, _line) ->
(* OCaml has no cons operator *)
exprpO (App ((_r, VarExpr (OpRef "List.cons")), args, _line))
| App (fn, ((_cs, (_r, VarExpr (TagRef _))) as arg) :: rest, _line) when firstArgIsFn fn ->
(* List.map Expr ... and similar *)
let arg = arg |> skip_preCommented |> wrapTagInLambda in
let rest = List.map ~f:skip_preCommented rest in
Exp.apply
(exprO fn)
(List.map ~f:as_arg (arg :: rest))
| App (fn, args, _line) ->
Exp.apply
(exprO fn)
(List.map args ~f:(fun a -> a |> skip_preCommented |> as_arg ))
| ELiteral lit -> litExpO lit
| VarExpr (VarRef (path, var)) ->
Exp.ident
(names2lid
~fix_all:fix_function
~fix_init:fix_module
(path @ [var]))
| VarExpr (TagRef (path, var)) ->
Exp.construct
(names2lid
~fix_init:fix_module
~fix_last:fix_constructor
(path @ [var]))
None
| VarExpr (OpRef name) ->
Exp.construct
(name2lid ~fix:fix_function name)
None
| RecordExpr { base; fields } ->
let base = Option.map base
~f:(fun (_c, var, _c2) -> Exp.ident (name2lid var))
in
let fields = seq2list fields in
let fields =
List.map fields
~f:(fun field ->
( skip_postCommented field._key |> name2lid
, skip_preCommented field._value |> exprO))
in
Exp.record fields base
| TupleExpr (exprs, _l) ->
Exp.tuple
(List.map exprs
~f:(fun expr -> exprO (skip_commented expr)))
| TupleFunction count ->
Exp.ident (names2lid ["Tuple" ^ (string_of_int count); "create"])
| Parens (_c, expr, _c2) ->
exprO expr
| Unit _cs ->
Exp.construct (name2lid "()") None
| Access (expr, field) ->
Exp.field (exprO expr) (name2lid field)
| Let (declarations, _c, body) ->
(* TODO: add the type definitions *)
List.fold ~init:(exprO body) (List.rev declarations)
~f:(fun prev decl ->
match decl with
| LetDefinition (namePat, args, _cs, rhs) ->
let let_ : Parsetree.expression =
List.fold (List.rev args) ~init:(exprO rhs)
~f:(fun prev (_c, arg) ->
(Exp.fun_ Asttypes.Nolabel None (patO arg) prev))
in
let vb = Vb.mk (patO namePat) let_ in
(Exp.let_ Nonrecursive [vb] prev)
| LetAnnotation _annot ->
let rhs = Exp.constant (Const.string "type annotation") in
let vb = Vb.mk (Pat.var (name2str "_")) rhs in
(Exp.let_ Nonrecursive [vb] prev)
| LetComment _c ->
let rhs = Exp.constant (Const.string "comment") in
let vb = Vb.mk (Pat.var (name2str "_")) rhs in
(Exp.let_ Nonrecursive [vb] prev))
| ExplicitList { terms } ->
let terms = List.map (seq2list terms) ~f:exprO in
List.fold (List.rev terms)
~init:(Exp.construct (name2lid "[]") None)
~f:(fun prev arg ->
Exp.construct (name2lid "::") (Some (Exp.tuple [arg; prev])))
| Lambda (pats, _cs, body, _l) ->
createLambda (List.map ~f:skip_preCommented pats) body
| Binops (lhs, rest, _l) ->
let is_left_associative op =
op = "|>"
|| op = ">="
|| op = "<="
|| op = "/="
|| op = "=="
|| op = ">>"
|| op = "+"
|| op = "*"
in
(* extract pairs *)
let op_pairs = List.map ~f:(fun (_cs, op, _cs2, rhs) -> op, rhs) rest in
(* find pipes into tags and wrap them *)
let op_pairs = List.map op_pairs
~f:(fun (op, rhs) ->
match op, skip_located rhs with
| OpRef "|>", VarExpr (TagRef _) ->
(op, wrapTagInLambda rhs)
| _ -> (op, rhs))
in
(* create a right-associative tree for this binops *)
let result : (ref_ * (expr, ref_) tree) option =
List.fold_right
op_pairs
~init:None
~f:(fun ((op, lhs) : ref_ * expr) (prev: (ref_ * (expr, ref_) tree) option) ->
match prev with
| None -> Some (op, Leaf lhs)
| Some (prev_op, prev_tree) ->
Some (op, Node (Leaf lhs, prev_op, prev_tree)))
in
let tree =
match result with
| None -> failwith "what?"
| Some (op, rhs) -> Node (Leaf lhs, op, rhs)
in
let rec translate_tree t =
(match t with
| Leaf e -> exprO e
| Node (a, OpRef op1, Node (b, OpRef op2, c))
when is_left_associative op1 ->
translate_tree (Node ((Node (a, OpRef op1, b)), OpRef op2, c))
| Node (a, op, b) ->
Exp.apply
(ref_O op)
[ (Asttypes.Nolabel, translate_tree a)
; (Asttypes.Nolabel, translate_tree b)
])
in
translate_tree tree
| Unary (_op, expr) ->
Exp.apply
(Exp.ident (name2lid "~-"))
[(Asttypes.Nolabel, exprO expr)]
| If ((ifclause), rest, elsebody) ->
let rest = List.map rest ~f:skip_preCommented in
List.fold (ifclause :: rest)
~init:(elsebody |> skip_preCommented |> exprO)
~f:(fun prev (ifcond, ifbody) ->
Exp.ifthenelse
(ifcond |> skip_commented |> exprO)
(ifbody |> skip_commented |> exprO)
(Some prev))
| AccessFunction field ->
Exp.fun_ Asttypes.Nolabel None
(Pat.var (name2str "x"))
(Exp.field (Exp.ident (name2lid "x")) (name2lid field))
and exprO (expr: expr) : Parsetree.expression =
expr |> skip_located |> exprpO
and as_arg (expr: expr) : (Asttypes.arg_label * Parsetree.expression) =
(Asttypes.Nolabel, exprO expr)
and ref_O r =
match r with
| VarRef (path, var) ->
Exp.ident
(names2lid
~fix_all:fix_function
~fix_init:fix_module
(path @ [var]))
| OpRef op ->
Exp.ident
(name2lid
~fix:fix_function
op)
| TagRef (path, var) ->
Exp.construct
(names2lid
~fix_all:fix_function
~fix_init:fix_module
(path @ [var]))
None
(* let x (a:int) b c = *)
let toplevelLet name (args: pattern list) (expr: expr) : Parsetree.structure_item =
let args =
List.fold (List.rev args) ~init:(exprO expr)
~f:(fun prev arg ->
(Exp.fun_ Asttypes.Nolabel None (patO arg) prev))
in
let let_ =
Vb.mk
(Pat.var (name2str name))
args
in
Str.value Asttypes.Nonrecursive [let_]
let to_list a = [a]
let importsO ((_c, i): Elm.imports) : Parsetree.structure =
i
|> List.map ~f:(fun (fqn,(_comments, importMethod)) ->
let modName = fqn
|> Longident.unflatten
|> fun x -> Option.value_exn x
|> Location.mknoloc
in
let alias =
match importMethod.alias with
| Some (_c, (_c2, alias)) ->
(* import X as Y -> module Y = X *)
modName
|> Mod.ident
|> Mb.mk (Location.mknoloc alias)
|> Str.module_
|> to_list
| _ -> []
in
let listings =
match importMethod.exposedVars with
(* import X -> Nothing, it's implicit *)
| (_c, (_c2, ClosedListing)) -> []
(* import X exposing (..) -> open X *)
| (_c, (_c2, OpenListing (_c3, (), _c4))) ->
modName
|> Opn.mk
|> Str.open_
|> to_list
| (_c, (_c2, ExplicitListing (detailed, _line))) ->
(* import X exposing (a, b) -> type a = X.a; let b = X.b *)
(* let vs = *)
(* (* TODO: this doesn't work yet *) *)
(* detailed.values *)
(* |> List.map ~f:Tuple.T2.get1 *)
(* |> List.map *)
(* ~f:(fun name -> *)
(* let fqn = fqn @ [name] *)
(* |> Longident.unflatten *)
(* |> fun x -> Option.value_exn x *)
(* |> Location.mknoloc *)
(* in *)
(* let binding = *)
(* Vb.mk *)
(* (Pat.var (as_var name)) *)
(* (Exp.ident fqn) *)
(* in *)
(* Exp.ident fqn *)
(* |> Exp.let_ Asttypes.Nonrecursive [] *)
(* |> Str.eval *)
(* ) *)
(* in *)
(* let ops = detailed.operators *)
(* |> List.map ~f:Tuple.T2.get1 *)
(* in *)
(* (* You can import nested constructors here, but we don't *) *)
(* let types = detailed.types *)
(* |> List.map ~f:Tuple.T2.get1 *)
(* in *)
(* vs *)
(* (* TODO: add types and ops *) *)
(* @ types @ ops *)
[]
in
alias @ listings
)
|> List.concat
let rec type_O (t: type_) : Parsetree.core_type =
(match skip_located t with
| FunctionType ft ->
let (first, _eol) = ft.first in
let rest =
List.map ~f:(fun (_c1, _c2, type_, _eol) -> type_)
ft.rest
in
List.fold ~init:(type_O first) rest
~f:(fun prev t ->
Typ.arrow Asttypes.Nolabel prev (t |> type_O))
| UnitType _cs ->
Typ.constr (name2lid "unit") []
| TupleType ts ->
ts
|> List.map ~f:skip_commented
|> List.map ~f:skip_withEol
|> List.map ~f:type_O
|> Typ.tuple
| TypeConstruction (tc, ts) ->
(match tc with
| TupleConstructor i -> failwith "tupleconstructor"
| NamedConstructor names ->
Typ.constr
(names2lid
~fix_init:fix_module
~fix_last:fix_type
~fix_all:fix_fqtype
names)
(List.map ts ~f:(fun t -> t |> skip_preCommented |> type_O)))
| TypeVariable name ->
Typ.var (name2string ~fix:fix_type name)
| _ -> Typ.var (failwith (show_type_ t))
)
type typeSignature = string * (Elm.type_ list) [@@deriving show]
let extractTypeSignature (s: Elm.declaration Elm.topLevelStructure) : typeSignature option =
match s with
| Entry (_r, TypeAnnotation ((ref_, _c1), (_c2, type_))) ->
let ts =
match skip_located type_ with
| TypeConstruction _ -> [type_]
| UnitType _ -> [type_]
| TupleType _ -> [type_]
| FunctionType {rest; first} ->
let first = skip_withEol first in
let rest = List.map ~f:(fun (_cs, _cs2, t, _) -> t) rest in
first :: rest
| _ -> failwith "unexpected type signature"
in
(match ref_ with
| VarRef ([], name) -> Some (name, ts)
| OpRef op -> Some (op, ts)
| _ -> failwith ("no type sig for:" ^ (show_ref_ ref_)))
| _ -> None
let topLevelStructureO (sigs: typeSignature list) (s: Elm.declaration Elm.topLevelStructure) : Parsetree.structure =
let getType (name: string) : Elm.type_ list option =
List.Assoc.find ~equal:(=) sigs name
in
match s with
| BodyComment _c -> []
| DocComment _c -> []
| Entry (_r, decl) ->
(* TODO: when you have a definition, find the associated type annotation for it. *)
(* A Definition needs a let *)
(match decl with
| TypeAnnotation ((ref_, _c1), (_c2, type_)) -> []
| PortAnnotation _ -> [] (* skip for now *)
| Definition ((_, VarPattern name), args, _c, expr) ->
(match getType name with
| Some types ->
if List.length args <> List.length types - 1
then
failwith ("wrong type signature length\n" ^ (show_typeSignature (name, types)));
let returnType = List.last_exn types in
let argTypes = types |> List.rev |> List.tl_exn |> List.rev in
let args = List.map args ~f:skip_preCommented in
let args = List.zip_exn args argTypes in
let expr = Exp.constraint_ (exprO expr) (type_O returnType) in
let args =
List.fold (List.rev args) ~init:expr
~f:(fun prev (arg, argType) ->
let arg = Pat.constraint_ (patO arg) (type_O argType) in
(Exp.fun_ Asttypes.Nolabel None arg prev))
in
let let_ =
Vb.mk
(Pat.var (name2str name))
args
in
[Str.value Asttypes.Nonrecursive [let_]]
| None ->
let args = List.map args ~f:skip_preCommented in
let args =
List.fold (List.rev args) ~init:(exprO expr)
~f:(fun prev arg ->
(Exp.fun_ Asttypes.Nolabel None (patO arg) prev))
in
let let_ =
Vb.mk
(Pat.var (name2str name))
args
in
[Str.value Asttypes.Nonrecursive [let_]])
| TypeAlias (_cs, nameWithArgs, (_c, type_)) ->
let (name, args) = skip_commented nameWithArgs in
let name = name2string ~fix:fix_type name in
let params =
List.map args
~f:(fun arg -> (arg
|> skip_preCommented
|> Typ.var
, Asttypes.Invariant))
in
let t =
(match skip_located type_ with
| RecordType { rtFields } ->
(* TODO: extensible types use rtBase here *)
let fields =
List.map (seq2list rtFields)
~f:(fun field ->
Type.field
(skip_postCommented field._key |> name2str)
(skip_preCommented field._value |> type_O))
in
let kind = Parsetree.Ptype_record fields in
Type.mk ~params ~kind (name2str ~fix:fix_type name)
| _ ->
Type.mk ~manifest:(type_O type_) (name2str ~fix:fix_type name))
in
[Str.type_ Recursive [t]]
| Datatype { nameWithArgs; tags } ->
let (name, args) = skip_commented nameWithArgs in
let params =
List.map args
~f:(fun arg -> (arg
|> skip_preCommented
|> Typ.var
, Asttypes.Invariant))
in
let constructors =
tags
|> openCommentedList2list
|> List.map ~f:(fun (name, types) ->
let args = List.map types
~f:(fun t -> t |> skip_preCommented |> type_O)
in
Type.constructor (name2str ~fix:fix_constructor name)
~args:(Parsetree.Pcstr_tuple args))
in
let kind = Parsetree.Ptype_variant constructors in
[Str.type_ Recursive
[(Type.mk ~params ~kind (name2str ~fix:fix_type name))]]
| _ -> failwith (show_declaration decl)
)
let foldTypesTogether (body : Parsetree.structure) =
let open Parsetree in
List.fold body ~init:[]
~f:(fun prev current ->
match prev, current with
| [], current -> [current]
| head :: tail, current ->
begin match head.pstr_desc, current.pstr_desc with
| ( Parsetree.Pstr_type (flag, l1)
, Parsetree.Pstr_type (_, l2)) ->
{ head with pstr_desc = Parsetree.Pstr_type (flag, l1 @ l2)
} :: tail
| _ -> current :: head :: tail
end)
|> List.rev
let moduleO (m: Elm.module_) : Parsetree.structure =
(* TODO: comments, docs *)
(* ignore header *)
let imports = m.imports |> importsO in
let standardImports =
[ Str.open_ (Opn.mk (name2lid "Tea"))
; Str.open_ (Opn.mk ~override:Override (name2lid "Porting"))
]
in
let typeSignatures = List.filter_map ~f:extractTypeSignature m.body in
let body =
m.body
|> List.map ~f:(topLevelStructureO typeSignatures)
|> List.concat
|> foldTypesTogether
in
standardImports @ imports @ body
let to_ocaml (m: Elm.module_) : (Parsetree.structure * Reason_comment.t list) =
let file = moduleO m in
(file, [])
let debug_ocaml channel : string =
let migration =
let module Versions = Migrate_parsetree_versions in
Versions.migrate Versions.ocaml_404 Versions.ocaml_current
in
Lexing.from_channel channel
|> Reason_toolchain.ML.implementation
|> migration.copy_structure
|> Printast.structure 0 Format.str_formatter;
Format.flush_str_formatter ()
let translate_elm channel : string =
channel
|> Yojson.Basic.from_channel
|> Elm.moduleJ
|> to_ocaml
|> Reason_toolchain.ML.print_implementation_with_comments
Format.str_formatter;
Format.flush_str_formatter ()
|> post_process
let parse_elm channel : string =
channel
|> Yojson.Basic.from_channel
|> Elm.moduleJ
|> Elm.show_module_
|> OldStr.global_replace (OldStr.regexp "Translate\\.") ""