forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_dump.ml
1461 lines (1361 loc) · 40.9 KB
/
js_dump.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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* BuckleScript compiler
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2010 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)
(* Authors: Jérôme Vouillon, Hongbo Zhang *)
(*
http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi
ASI catch up
{[
a=b
++c
---
a=b ++c
====================
a ++
---
a
++
====================
a --
---
a
--
====================
(continue/break/return/throw) a
---
(continue/break/return/throw)
a
====================
]}
*)
module P = Ext_pp
module E = Js_exp_make
module S = Js_stmt_make
module L = Js_dump_lit
let return_indent = (String.length L.return / Ext_pp.indent_length)
let throw_indent = (String.length L.throw / Ext_pp.indent_length)
let semi f = P.string f L.semi
let op_prec, op_str =
Js_op_util.(op_prec, op_str)
let rec comma_strings f (ls : string list) =
match ls with
| [] -> ()
| [x] -> P.string f x
| y :: ys ->
P.string f y;
P.string f L.comma;
comma_strings f ys
let rec comma_idents cxt f (ls : Ident.t list) =
match ls with
| [] -> cxt
| [x] -> Ext_pp_scope.ident cxt f x
| y :: ys ->
let cxt = Ext_pp_scope.ident cxt f y in
P.string f L.comma;
comma_idents cxt f ys
let ipp_ident cxt f id un_used =
if un_used then
Ext_pp_scope.ident cxt f (Ext_ident.make_unused ())
else
Ext_pp_scope.ident cxt f id
let rec formal_parameter_list cxt (f : P.t) method_ l env =
let offset = if method_ then 1 else 0 in
let rec aux i cxt l =
match l with
| [] -> cxt
| [id] -> ipp_ident cxt f id (Js_fun_env.get_unused env i)
| id :: r ->
let cxt = ipp_ident cxt f id (Js_fun_env.get_unused env i) in
P.string f L.comma; P.space f;
aux (i + 1) cxt r
in
match l with
| [] -> cxt
| [i] ->
(** necessary, since some js libraries like [mocha]...*)
if Js_fun_env.get_unused env offset then cxt
else
Ext_pp_scope.ident cxt f i
| _ ->
aux offset cxt l
(* IdentMap *)
(*
f/122 -->
f/122 is in the map
if in, use the old mapping
else
check f,
if in last bumped id
else
use "f", register it
check "f"
if not , use "f", register stamp -> 0
else
check stamp
if in use it
else check last bumped id, increase it and register
*)
type name =
| No_name
| Name_top of Ident.t
| Name_non_top of Ident.t
(**
Turn [function f (x,y) { return a (x,y)} ] into [Curry.__2(a)],
The idea is that [Curry.__2] will guess the arity of [a], if it does
hit, then there is no cost when passed
*)
(* TODO: refactoring
Note that {!pp_function} could print both statement and expression when [No_name] is given
*)
let rec
try_optimize_curry cxt f len function_id =
begin
P.string f Js_runtime_modules.curry;
P.string f L.dot;
P.string f "__";
P.string f (Printf.sprintf "%d" len);
P.paren_group f 1 (fun _ -> expression 1 cxt f function_id )
end
and pp_function method_
cxt (f : P.t) ?(name=No_name) return
(l : Ident.t list) (b : J.block) (env : Js_fun_env.t ) =
match b, (name, return) with
| [ {statement_desc =
Return {return_value =
{expression_desc =
Call(({expression_desc = Var v ; _} as function_id),
ls ,
{arity = ( Full | NA as arity(* see #234*));
(* TODO: need a case to justify it*)
call_info =
(Call_builtin_runtime | Call_ml )})}}}],
((_, false) | (No_name, true))
when
(* match such case:
{[ function(x,y){ return u(x,y) } ]}
it can be optimized in to either [u] or [Curry.__n(u)]
*)
not method_ &&
Ext_list.for_all2_no_exn (fun a (b : J.expression) ->
match b.expression_desc with
| Var (Id i) -> Ident.same a i
| _ -> false) l ls ->
let optimize len p cxt f v =
if p then try_optimize_curry cxt f len function_id
else
vident cxt f v
in
let len = List.length l in (* length *)
begin match name with
| Name_top i | Name_non_top i ->
P.string f L.var;
P.space f ;
let cxt = Ext_pp_scope.ident cxt f i in
P.space f ;
P.string f L.eq;
P.space f ;
let cxt = optimize len (arity = NA && len <= 8) cxt f v in
semi f ;
cxt
| No_name ->
if return then
begin
P.string f L.return ;
P.space f
end;
optimize len (arity = NA && len <=8) cxt f v
end
| _, _ ->
let set_env : Ident_set.t = (** identifiers will be printed following*)
match name with
| No_name ->
Js_fun_env.get_unbounded env
| Name_top id | Name_non_top id -> Ident_set.add id (Js_fun_env.get_unbounded env )
in
(* the context will be continued after this function *)
let outer_cxt = Ext_pp_scope.merge set_env cxt in
(* the context used to be printed inside this function
when printing a function,
only the enclosed variables and function name matters,
if the function does not capture any variable, then the context is empty
*)
let inner_cxt = Ext_pp_scope.sub_scope outer_cxt set_env in
(* (if not @@ Js_fun_env.is_empty env then *)
(* pp_comment f (Some (Js_fun_env.to_string env))) ; *)
let param_body () =
if method_ then begin
let cxt = P.paren_group f 1 (fun _ ->
formal_parameter_list inner_cxt f method_ (List.tl l) env )
in
P.space f ;
ignore @@ P.brace_vgroup f 1 (fun _ ->
let cxt =
if not (Js_fun_env.get_unused env 0) then
begin
P.string f L.var ;
P.space f;
let cxt = Ext_pp_scope.ident cxt f (List.hd l) in
P.space f ;
P.string f L.eq ;
P.space f ;
P.string f L.this;
P.space f ;
semi f ;
P.newline f ;
cxt ;
end
else
cxt
in
statement_list false cxt f b
);
end
else begin
let cxt = P.paren_group f 1 (fun _ ->
formal_parameter_list inner_cxt f method_ l env )
in
P.space f ;
ignore @@ P.brace_vgroup f 1 (fun _ -> statement_list false cxt f b );
end
in
let lexical : Ident_set.t = Js_fun_env.get_lexical_scope env in
let enclose lexical return =
let handle lexical =
if Ident_set.is_empty lexical
then
begin
if return then
begin
P.string f L.return ;
P.space f
end ;
begin match name with
| No_name ->
(* see # 1692, add a paren for annoymous function for safety *)
P.paren_group f 1 begin fun _ ->
P.string f L.function_;
P.space f ;
param_body ()
end
| Name_non_top x ->
P.string f L.var ;
P.space f ;
ignore @@ Ext_pp_scope.ident inner_cxt f x ;
P.space f ;
P.string f L.eq ;
P.space f ;
P.string f L.function_;
P.space f ;
param_body ();
semi f ;
| Name_top x ->
P.string f L.function_;
P.space f ;
ignore (Ext_pp_scope.ident inner_cxt f x);
param_body ();
end;
end
else
(* print as
{[(function(x,y){...} (x,y))]}
*)
let lexical = Ident_set.elements lexical in
(if return then
begin
P.string f L.return ;
P.space f
end
else
begin match name with
| No_name -> ()
| Name_non_top name | Name_top name->
P.string f L.var;
P.space f;
ignore @@ Ext_pp_scope.ident inner_cxt f name ;
P.space f ;
P.string f L.eq;
P.space f ;
end
)
;
P.string f L.lparen;
P.string f L.function_;
P.string f L.lparen;
ignore @@ comma_idents inner_cxt f lexical;
P.string f L.rparen;
P.brace_vgroup f 0 (fun _ ->
begin
P.string f L.return ;
P.space f;
P.string f L.function_;
P.space f ;
(match name with
| No_name -> ()
| Name_non_top x | Name_top x -> ignore (Ext_pp_scope.ident inner_cxt f x));
param_body ()
end);
P.string f L.lparen;
ignore @@ comma_idents inner_cxt f lexical;
P.string f L.rparen;
P.string f L.rparen;
begin match name with
| No_name -> () (* expression *)
| _ -> semi f (* has binding, a statement *)
end
in
begin match name with
| Name_top name | Name_non_top name when Ident_set.mem name lexical ->
(*TODO: when calculating lexical we should not include itself *)
let lexical = (Ident_set.remove name lexical) in
handle lexical
| _ -> handle lexical
end
in
enclose lexical return
;
outer_cxt
(* Assume the cond would not change the context,
since it can be either [int] or [string]
*)
and pp_one_case_clause : 'a .
_ -> P.t -> (P.t -> 'a -> unit) -> 'a J.case_clause -> _
= fun cxt f pp_cond
({switch_case = switch_case; switch_body = (switch_body,should_break)} : _ J.case_clause) ->
let cxt =
P.group f 1 (fun _ ->
P.group f 1 (fun _ ->
P.string f L.case;
P.space f ;
pp_cond f switch_case; (* could be integer or string *)
P.space f ;
P.string f L.colon );
P.space f;
P.group f 1 (fun _ ->
let cxt =
match switch_body with
| [] -> cxt
| _ ->
P.newline f ;
statement_list false cxt f switch_body
in
(if should_break then
begin
P.newline f ;
P.string f L.break;
semi f;
end) ;
cxt))
in
P.newline f;
cxt
and loop_case_clauses : 'a . Ext_pp_scope.t ->
P.t -> (P.t -> 'a -> unit) -> 'a J.case_clause list -> Ext_pp_scope.t
= fun cxt f pp_cond cases ->
match cases with
| [] -> cxt
| [x] -> pp_one_case_clause cxt f pp_cond x
| x::xs ->
let cxt = pp_one_case_clause cxt f pp_cond x
in loop_case_clauses cxt f pp_cond xs
and vident cxt f (v : J.vident) =
begin match v with
| Id v | Qualified(v, _, None) ->
Ext_pp_scope.ident cxt f v
| Qualified (id, (Ml | Runtime), Some name) ->
let cxt = Ext_pp_scope.ident cxt f id in
P.string f L.dot;
P.string f (Ext_ident.convert name);
cxt
| Qualified (id, External _, Some name) ->
let cxt = Ext_pp_scope.ident cxt f id in
Js_dump_property.property_access f name ;
cxt
end
and expression l cxt f (exp : J.expression) : Ext_pp_scope.t =
pp_comment_option f exp.comment ;
expression_desc cxt l f exp.expression_desc
and
expression_desc cxt (l:int) f x : Ext_pp_scope.t =
match x with
| Null ->
P.string f L.null; cxt
| Undefined
->
P.string f L.undefined; cxt
| Var v ->
vident cxt f v
| Bool b ->
(if b then P.string f L.true_ else P.string f L.false_ ) ; cxt
| Seq (e1, e2) ->
let action () =
let cxt = expression 0 cxt f e1 in
P.string f L.comma ;
P.space f ;
expression 0 cxt f e2 in
if l > 0 then
P.paren_group f 1 action
else action ()
| Fun (method_, l, b, env) -> (* TODO: dump for comments *)
pp_function method_ cxt f false l b env
(* TODO:
when [e] is [Js_raw_code] with arity
print it in a more precise way
It seems the optimizer already did work to make sure
{[
Call (Raw_js_code (s, Exp i), el, {Full})
when Ext_list.length_equal el i
]}
*)
| Call (e, el, info) ->
let action () =
P.group f 1 (fun _ ->
match info, el with
| {arity = Full }, _
| _, [] ->
let cxt = expression 15 cxt f e in
P.paren_group f 1 (fun _ -> arguments cxt f el )
| _ , _ ->
(* ipp_comment f (Some "!") *)
P.string f Js_runtime_modules.curry;
P.string f L.dot;
let len = List.length el in
if 1 <= len && len <= 8 then
begin
P.string f L.app;
P.string f (Printf.sprintf "%d" len);
P.paren_group f 1 (fun _ -> arguments cxt f (e::el))
end
else
begin
P.string f L.app_array;
P.paren_group f 1
(fun _ -> arguments cxt f [ e ; E.array Mutable el])
end)
in
if l > 15 then P.paren_group f 1 action
else action ()
| FlatCall(e,el) ->
P.group f 1 (fun _ ->
let cxt = expression 15 cxt f e in
P.string f L.dot;
P.string f L.apply;
P.paren_group f 1 (fun _ ->
P.string f L.null;
P.string f L.comma;
P.space f ;
expression 1 cxt f el
)
)
| Char_to_int e ->
begin match e.expression_desc with
| String_access (a,b) ->
P.group f 1 (fun _ ->
let cxt = expression 15 cxt f a in
P.string f L.dot;
P.string f L.char_code_at;
P.paren_group f 1 (fun _ -> expression 0 cxt f b);
)
| _ ->
P.group f 1 (fun _ ->
let cxt = expression 15 cxt f e in
P.string f L.dot;
P.string f L.char_code_at;
P.string f "(0)";
cxt)
end
| Char_of_int e ->
P.group f 1 (fun _ ->
P.string f L.string_cap;
P.string f L.dot;
P.string f L.fromCharcode;
P.paren_group f 1 (fun _ -> arguments cxt f [e])
)
| Math (name, el) ->
P.group f 1 (fun _ ->
P.string f L.math;
P.string f L.dot;
P.string f name;
P.paren_group f 1 (fun _ -> arguments cxt f el)
)
| Unicode s ->
P.string f "\"";
P.string f s ;
P.string f "\"";
cxt
| Str (_, s) ->
(*TODO --
when utf8-> it will not escape '\\' which is definitely not we want
*)
Js_dump_string.pp_string f s;
cxt
| Raw_js_function (s,params) ->
P.string f L.function_;
P.space f ;
P.paren_group f 1 (fun _ ->
comma_strings f params
);
P.brace f (fun _ ->
P.string f s);
cxt
| Raw_js_code (s,info) ->
begin match info with
| Exp ->
P.string f "(";
P.string f s ;
P.string f ")";
cxt
| Stmt ->
P.newline f ;
P.string f s ;
P.newline f ;
cxt
end
| Number v ->
let s =
match v with
| Float {f = v} ->
Js_number.caml_float_literal_to_js_string v
(* attach string here for float constant folding?*)
| Int { i = v; _}
-> Int32.to_string v (* check , js convention with ocaml lexical convention *)
| Uint i
-> Format.asprintf "%lu" i
| Nint i -> Nativeint.to_string i
in
let need_paren =
if s.[0] = '-'
then l > 13 (* Negative numbers may need to be parenthesized. *)
else l = 15 (* Parenthesize as well when followed by a dot. *)
&& s.[0] <> 'I' (* Infinity *)
&& s.[0] <> 'N' (* NaN *)
in
let action = fun _ -> P.string f s in
(
if need_paren
then P.paren f action
else action ()
);
cxt
| Is_null_or_undefined e ->
let action = (fun _ ->
let cxt = expression 1 cxt f e in
P.space f ;
P.string f "==";
P.space f ;
P.string f L.null;
cxt) in
if l > 0 then
P.paren_group f 1 action
else action ()
| Js_not e ->
let action () =
P.string f "!" ;
expression 13 cxt f e
in
if l > 13
then P.paren_group f 1 action
else action ()
| Typeof e
->
P.string f "typeof";
P.space f;
expression 13 cxt f e
| Caml_block_set_tag(a,b) ->
expression_desc cxt l f
(Bin(Eq,
{expression_desc = Caml_block_tag a; comment = None},
b
))
| Caml_block_set_length(a,b) ->
expression_desc cxt l f
(Bin(Eq,
{expression_desc = Length (a,Caml_block); comment = None},
b
))
| Bin (Eq, {expression_desc = Var i },
{expression_desc =
(
Bin(
(Plus as op), {expression_desc = Var j}, delta)
| Bin(
(Plus as op), delta, {expression_desc = Var j})
| Bin(
(Minus as op), {expression_desc = Var j}, delta)
)
})
when Js_op_util.same_vident i j ->
(* TODO: parenthesize when necessary *)
begin match delta, op with
| {expression_desc = Number (Int { i = 1l; _})}, Plus
(* TODO: float 1. instead,
since in JS, ++ is a float operation
*)
| {expression_desc = Number (Int { i = -1l; _})}, Minus
->
P.string f L.plusplus;
P.space f ;
vident cxt f i
| {expression_desc = Number (Int { i = -1l; _})}, Plus
| {expression_desc = Number (Int { i = 1l; _})}, Minus
->
P.string f L.minusminus;
P.space f ;
vident cxt f i;
| _, _ ->
let cxt = vident cxt f i in
P.space f ;
if op = Plus then P.string f "+="
else P.string f "-=";
P.space f ;
expression 13 cxt f delta
end
| Bin (Eq, {expression_desc = Access({expression_desc = Var i; _},
{expression_desc = Number (Int {i = k0 })}
) },
{expression_desc =
(Bin((Plus as op),
{expression_desc = Access(
{expression_desc = Var j; _},
{expression_desc = Number (Int {i = k1; })}
); _}, delta)
| Bin((Plus as op), delta,
{expression_desc = Access(
{expression_desc = Var j; _},
{expression_desc = Number (Int {i = k1; })}
); _})
| Bin((Minus as op),
{expression_desc = Access(
{expression_desc = Var j; _},
{expression_desc = Number (Int {i = k1; })}
); _}, delta)
)})
when k0 = k1 && Js_op_util.same_vident i j
(* Note that
{[x = x + 1]}
is exactly the same (side effect, and return value)
as {[ ++ x]}
same to
{[ x = x + a]}
{[ x += a ]}
they both return the modified value too
*)
(* TODO:
handle parens..
*)
->
let aux cxt f vid i =
let cxt = vident cxt f vid in
P.string f "[";
P.string f (Int32.to_string i);
P.string f"]";
cxt in
(** TODO: parenthesize when necessary *)
begin match delta, op with
| {expression_desc = Number (Int { i = 1l; _})}, Plus
| {expression_desc = Number (Int { i = -1l; _})}, Minus
->
P.string f L.plusplus;
P.space f ;
aux cxt f i k0
| {expression_desc = Number (Int { i = -1l; _})}, Plus
| {expression_desc = Number (Int { i = 1l; _})}, Minus
->
P.string f L.minusminus;
P.space f ;
aux cxt f i k0
| _, _ ->
let cxt = aux cxt f i k0 in
P.space f ;
if op = Plus then P.string f "+="
else P.string f "-=";
P.space f ;
expression 13 cxt f delta
end
| Bin (Minus, {expression_desc = Number (Int {i=0l;_} | Float {f = "0."})}, e)
(* TODO:
Handle multiple cases like
{[ 0. - x ]}
{[ 0.00 - x ]}
{[ 0.000 - x ]}
*)
->
let action () =
P.string f "-" ;
expression 13 cxt f e
in
if l > 13 then P.paren_group f 1 action
else action ()
| Bin (op, e1, e2) ->
let (out, lft, rght) = op_prec op in
let need_paren =
l > out || (match op with Lsl | Lsr | Asr -> true | _ -> false) in
let action () =
(* We are more conservative here, to make the generated code more readable
to the user
*)
let cxt = expression lft cxt f e1 in
P.space f;
P.string f (op_str op);
P.space f;
expression rght cxt f e2
in
if need_paren
then P.paren_group f 1 action
else action ()
| String_append (e1, e2) ->
let op : Js_op.binop = Plus in
let (out, lft, rght) = op_prec op in
let need_paren =
l > out || (match op with Lsl | Lsr | Asr -> true | _ -> false) in
let action () =
let cxt = expression lft cxt f e1 in
P.space f ;
P.string f "+";
P.space f;
expression rght cxt f e2
in
if need_paren then P.paren_group f 1 action else action ()
| Array (el,_) ->
(** TODO: simplify for singleton list *)
begin match el with
| []| [ _ ] -> P.bracket_group f 1 @@ fun _ -> array_element_list cxt f el
| _ -> P.bracket_vgroup f 1 @@ fun _ -> array_element_list cxt f el
end
(* | Caml_uninitialized_obj (tag, size)
-> (* FIXME *)
expression_desc cxt l f (Object [Length, size ; Tag, tag]) *)
| Caml_block( el, mutable_flag, tag, tag_info)
->
(* Note that, if we ignore more than tag [0] we loose some information
with regard tag *)
(* TODO: for numbers like 248, 255 we can reverse engineer to make it
[Obj.xx_flag], but we can not do this in runtime libraries
*)
if Js_fold_basic.needBlockRuntime tag tag_info then begin
match tag_info with
| Blk_record labels ->
P.string f L.caml_block;
P.string f L.dot ;
P.string f L.block_record;
P.paren_group f 1
(fun _ -> arguments cxt f
[E.array Immutable
(Ext_array.to_list_f E.str labels);
E.array mutable_flag
(List.map (fun (x : J.expression) -> {x with comment = None}) el) ]
)
| Blk_module (Some labels) ->
P.string f L.caml_block;
P.string f L.dot ;
P.string f L.block_local_module;
P.paren_group f 1
(fun _ -> arguments cxt f
[E.array Immutable
(Ext_list.map E.str labels);
E.array mutable_flag
(List.map (fun (x :J.expression) -> {x with comment = None}) el)
]
)
| Blk_variant name ->
P.string f L.caml_block;
P.string f L.dot ;
P.string f L.block_poly_var;
P.paren_group f 1
(fun _ -> arguments cxt f
[
E.str name;
E.array mutable_flag el]
)
| Blk_constructor(name,_) when !Js_config.debug ->
P.string f L.caml_block;
P.string f L.dot ;
P.string f L.block_variant;
P.paren_group f 1
(fun _ -> arguments cxt f
[E.str name; E.array mutable_flag el])
| _ ->
begin
P.string f L.caml_block;
P.string f L.dot ;
P.string f L.caml_block_create;
P.paren_group f 1
(fun _ -> arguments cxt f [tag; E.array mutable_flag el])
end
end
else
expression_desc cxt l f (Array (el, mutable_flag))
| Caml_block_tag e ->
P.group f 1 (fun _ ->
let cxt = expression 15 cxt f e in
P.string f L.dot ;
P.string f L.tag ;
cxt)
| Access (e, e')
| String_access (e,e')
->
let action () =
P.group f 1 @@ fun _ ->
let cxt = expression 15 cxt f e in
P.bracket_group f 1 @@ fun _ ->
expression 0 cxt f e'
in
if l > 15 then P.paren_group f 1 action else action ()
| Length (e, _) ->
let action () = (** Todo: check parens *)
let cxt = expression 15 cxt f e in
P.string f L.dot;
P.string f L.length;
cxt in
if l > 15 then P.paren_group f 1 action else action ()
| Dot (e, s,normal) ->
let action () =
let cxt = expression 15 cxt f e in
Js_dump_property.property_access f s ;
(* See [ .obj_of_exports]
maybe in the ast level we should have
refer and export
*)
cxt in
if l > 15 then P.paren_group f 1 action else action ()
| New (e, el) ->
let action () =
P.group f 1 @@ fun _ ->
P.string f L.new_;
P.space f;
let cxt = expression 16 cxt f e in
P.paren_group f 1 @@ fun _ ->
match el with
| Some el -> arguments cxt f el
| None -> cxt
in
if l > 15 then P.paren_group f 1 action else action ()
| Cond (e, e1, e2) ->
let action () =
(* P.group f 1 @@ fun _ -> *)
let cxt = expression 3 cxt f e in
P.space f;
P.string f L.question;
P.space f;
(*
[level 1] is correct, however
to make nice indentation , force nested conditional to be parenthesized
*)
let cxt = (P.group f 1 @@ fun _ -> expression 3 cxt f e1) in
(* let cxt = (P.group f 1 @@ fun _ -> expression 1 cxt f e1) in *)
P.space f;
P.string f L.colon;
P.space f ;
(* idem *)
P.group f 1 @@ fun _ -> expression 3 cxt f e2
(* P.group f 1 @@ fun _ -> expression 1 cxt f e2 *)
in
if l > 2 then P.paren_vgroup f 1 action else action ()
| Object lst ->
begin
match lst with
| [] -> P.string f "{ }" ; cxt
| _ ->
let action () =
P.brace_vgroup f 1 @@ fun _ ->
property_name_and_value_list cxt f lst in
if l > 1 then
(* #1946 object literal is easy to be
interpreted as block statement
here we avoid parens in such case
{[
var f = { x : 2 , y : 2}
]}
*)
P.paren_group f 1 action
else action ()
end
and property_name cxt f (s : J.property_name) : unit =
Js_dump_property.property_key f s
and property_name_and_value_list cxt f l : Ext_pp_scope.t =
match l with
| [] -> cxt
| [(pn, e)] ->
property_name cxt f pn ;
P.string f L.colon;
P.space f;
expression 1 cxt f e
| (pn, e) :: r ->
property_name cxt f pn ;
P.string f L.colon;
P.space f;
let cxt = expression 1 cxt f e in
P.string f L.comma;
P.newline f;
property_name_and_value_list cxt f r
and array_element_list cxt f el : Ext_pp_scope.t =
match el with
| [] -> cxt
| [e] -> expression 1 cxt f e
| e :: r ->
let cxt = expression 1 cxt f e
in
P.string f L.comma; P.newline f; array_element_list cxt f r
and arguments cxt f l : Ext_pp_scope.t =
match l with
| [] -> cxt
| [e] -> expression 1 cxt f e
| e :: r ->
let cxt = expression 1 cxt f e in
P.string f L.comma; P.space f; arguments cxt f r
and variable_declaration top cxt f
(variable : J.variable_declaration) : Ext_pp_scope.t =
(* TODO: print [const/var] for different backends *)
match variable with
| {ident = i; value = None; ident_info ; _} ->
if ident_info.used_stats = Dead_pure
then cxt
else