forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_dump.ml
1340 lines (1232 loc) · 39.3 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 *)
#if OCAML_VERSION =~ ">4.03.0" then
[@@@ocaml.warning "-57"] (* turn off such warning temporarily*)
#end
(*
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
(* There modules are dynamically inserted in the last stage
{Caml_curry}
{Caml_block}
{Caml_option}
They can appear anywhere so even if you have a module
{
let module Caml_block = ...
(* Later would insert the use of Caml_block here which should
point tto the runtime module
*)
}
There are no sane way to easy detect it ahead of time, we should be
conservative here.
(our call Js_fun_env.get_unbounded env) is not precise
*)
module Curry_gen = struct
let pp_curry_dot f =
P.string f Js_runtime_modules.curry;
P.string f L.dot
let pp_optimize_curry (f : P.t) (len : int) =
pp_curry_dot f;
P.string f "__";
P.string f (Printf.sprintf "%d" len)
let pp_app_any (f : P.t) =
pp_curry_dot f;
P.string f "app"
let pp_app (f : P.t) (len : int) =
pp_curry_dot f;
P.string f "_";
P.string f (Printf.sprintf "%d" len)
end
let block_dot f =
P.string f Js_runtime_modules.block;
P.string f L.dot
let pp_block_create f =
block_dot f ;
P.string f L.caml_block_create
let dbg_block_dot f =
P.string f Js_runtime_modules.caml_chrome_block;
P.string f L.dot
let dbg_block_create f =
dbg_block_dot f ;
P.string f L.caml_block_create
let dbg_poly_var f =
dbg_block_dot f;
P.string f L.block_poly_var
let dbg_simple_variant f =
dbg_block_dot f ;
P.string f L.block_simple_variant
let dbg_variant f =
dbg_block_dot f ;
P.string f L.block_variant
let return_indent = String.length L.return / Ext_pp.indent_length
let throw_indent = String.length L.throw / Ext_pp.indent_length
type cxt = Ext_pp_scope.t
let semi f = P.string f L.semi
let comma f = P.string f L.comma
let rec iter_lst cxt (f : P.t) ls element inter =
match ls with
| [] -> cxt
| [e] -> element cxt f e
| e::r ->
let acxt = element cxt f e in
inter f;
iter_lst acxt f r element inter
let raw_snippet_exp_simple_enough (s : string) =
Ext_string.for_all s (fun c ->
match c with
| 'a' .. 'z' | 'A' .. 'Z' | '_' -> true
| _ -> false
)
(* Parentheses are required when the expression
starts syntactically with "{" or "function"
TODO: be more conservative, since Google Closure will handle
the precedence correctly, we also need people read the code..
Here we force parens for some alien operators
If we move assign into a statement, will be less?
TODO: construct a test case that do need parenthesisze for expression
IIE does not apply (will be inlined?)
*)
(* e = function(x){...}(x); is good
*)
let exp_need_paren (e : J.expression) =
match e.expression_desc with
| Call ({expression_desc = Fun _ | Raw_js_function _; },_,_) -> true
(* | Caml_uninitialized_obj _ *)
| Raw_js_code (_, Exp)
| Fun _
| Raw_js_function _
| Caml_block (_,_,_, (Blk_record _ | Blk_module _))
| Object _ -> true
| Raw_js_code (_,Stmt)
| Length _
| Call _
| Caml_block_tag _
| Seq _
| Static_index _
| Cond _
| Bin _
| Is_null_or_undefined _
| String_index _
| Array_index _
| String_append _
| Char_of_int _
| Char_to_int _
| Var _
| Undefined
| Null
| Str _
| Unicode _
| Array _
| Optional_block _
| Caml_block _
| FlatCall _
| Typeof _
| Number _
| Js_not _
| Bool _
| New _
-> false
let comma_strings f ls =
iter_lst
() f
ls
(fun _ f a -> P.string f a )
comma
let comma_idents (cxt: cxt) f ls =
iter_lst cxt f ls
Ext_pp_scope.ident
comma
let pp_paren_params
(inner_cxt : cxt) (f : Ext_pp.t)
(lexical : Ident.t list) : unit =
P.string f L.lparen;
let _ : cxt = comma_idents inner_cxt f lexical in
P.string f L.rparen
(** Print as underscore for unused vars, may not be
needed in the future *)
let ipp_ident cxt f id (un_used : bool) =
Ext_pp_scope.ident cxt f (
if un_used then
Ext_ident.make_unused ()
else
id)
let pp_var_assign cxt f id =
P.string f L.var ;
P.space f ;
let acxt = Ext_pp_scope.ident cxt f id in
P.space f ;
P.string f L.eq ;
P.space f ;
acxt
let pp_js_function_params_body f s params =
P.paren_group f 1 (fun _ ->
comma_strings f params
);
P.brace f (fun _ -> P.string f s)
let pp_var_assign_this cxt f id =
let cxt = pp_var_assign cxt f id in
P.string f L.this;
P.space f ;
semi f ;
P.newline f ;
cxt
let pp_var_declare cxt f id =
P.string f L.var ;
P.space f ;
let acxt = Ext_pp_scope.ident cxt f id in
semi f ;
acxt
let pp_direction f (direction : J.for_direction) =
match direction with
| Upto -> P.string f L.plus_plus
| Downto -> P.string f L.minus_minus
let return_sp f =
P.string f L.return ; P.space f
let bool f b =
P.string f (if b then L.true_ else L.false_)
let comma_sp f =
comma f ; P.space f
let comma_nl f =
comma f ; P.newline f
let drop_comment (x : J.expression) =
if x.comment = None then x
else {x with comment = None}
let debugger_nl f =
P.newline f ;
P.string f L.debugger;
semi f ;
P.newline f
let break_nl f =
P.string f L.break;
P.space f ;
semi f;
P.newline f
let continue f s =
P.string f L.continue;
P.space f ;
P.string f s;
semi f
let formal_parameter_list cxt f offset l env =
iter_lst cxt f l Ext_pp_scope.ident comma_sp
(* 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
*)
let is_var (b : J.expression) a =
match b.expression_desc with
| Var (Id i) -> Ident.same i a
| _ -> false
(* 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 =
Curry_gen.pp_optimize_curry f len ;
P.paren_group f 1 (fun _ -> expression ~level:1 cxt f function_id )
and pp_function is_method
cxt (f : P.t) ?(name=No_name) (return : bool)
(l : Ident.t list) (b : J.block) (env : Js_fun_env.t ) : cxt =
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 is_method &&
Ext_list.for_all2_no_exn ls l is_var ->
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 *)
(match name with
| Name_top i | Name_non_top i ->
let cxt = pp_var_assign cxt f i in
let cxt = optimize len (arity = NA && len <= 8) cxt f v in
semi f ;
cxt
| No_name ->
if return then
return_sp f ;
optimize len (arity = NA && len <=8) cxt f v)
| _, _ ->
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 (Js_fun_env.get_unbounded env ) id in
(* the context will be continued after this function *)
let outer_cxt = Ext_pp_scope.merge cxt set_env 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
let param_body () : unit =
if is_method then
match l with
| [] -> assert false
| this::arguments ->
let cxt = P.paren_group f 1 (fun _ ->
formal_parameter_list inner_cxt f 1 arguments env )
in
P.space f ;
P.brace_vgroup f 1 (fun _ ->
let cxt =
if Js_fun_env.get_unused env 0 then cxt
else pp_var_assign_this cxt f this in
function_body cxt f b
);
else
let cxt =
P.paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f 0 l env ) in
P.space f ;
P.brace_vgroup f 1 (fun _ -> function_body cxt f b )
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
(if return then
return_sp f ;
match name with
| No_name ->
(* see # 1692, add a paren for annoymous function for safety *)
P.paren_group f 1 (fun _ ->
P.string f L.function_;
P.space f ;
param_body ())
| Name_non_top x ->
ignore (pp_var_assign inner_cxt f x : cxt );
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 : cxt);
param_body ())
else
(* print as
{[(function(x,y){...} (x,y))]}
*)
let lexical = Ident_set.elements lexical in
(if return then
return_sp f
else
match name with
| No_name -> ()
| Name_non_top name | Name_top name->
ignore (pp_var_assign inner_cxt f name : cxt)
)
;
P.string f L.lparen;
P.string f L.function_;
pp_paren_params inner_cxt f lexical;
P.brace_vgroup f 0 (fun _ ->
return_sp 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 ());
pp_paren_params inner_cxt f lexical;
P.string f L.rparen;
match name with
| No_name -> () (* expression *)
| _ -> semi f (* has binding, a statement *) in
handle
(match name with
| Name_top name | Name_non_top name when Ident_set.mem lexical name ->
(*TODO: when calculating lexical we should not include itself *)
Ident_set.remove lexical name
| _ -> lexical) 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_body ; should_break; comment; } : _ J.case_clause) ->
let cxt =
P.group f 1 (fun _ ->
P.group f 1 (fun _ ->
P.string f L.case;
P.space f ;
pp_comment_option f comment;
pp_cond f switch_case; (* could be integer or string *)
P.space f ;
P.string f L.colon );
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 . cxt ->
P.t -> (P.t -> 'a -> unit) -> 'a J.case_clause list -> cxt
= fun cxt f pp_cond cases ->
Ext_list.fold_left cases cxt (fun acc x -> pp_one_case_clause acc f pp_cond x)
and vident cxt f (v : J.vident) =
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
(* The higher the level, the more likely that inner has to add parens *)
and expression ~level:l cxt f (exp : J.expression) : cxt =
pp_comment_option f exp.comment ;
expression_desc cxt ~level:l f exp.expression_desc
and expression_desc cxt ~(level:int) f x : cxt =
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 ->
bool f b ; cxt
| Seq (e1, e2) ->
P.cond_paren_group f (level > 0) 1 (fun () ->
let cxt = expression ~level:0 cxt f e1 in
comma_sp f;
expression ~level:0 cxt f e2 )
| 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) ->
P.cond_paren_group f (level > 15) 1 (fun _ ->
P.group f 1 (fun _ ->
match info, el with
| {arity = Full }, _
| _, [] ->
let cxt = expression ~level:15 cxt f e in
P.paren_group f 1 (fun _ -> arguments cxt f el )
| _ , _ ->
let len = List.length el in
if 1 <= len && len <= 8 then
begin
Curry_gen.pp_app f len ;
P.paren_group f 1 (fun _ -> arguments cxt f (e::el))
end
else
begin
Curry_gen.pp_app_any f ;
P.paren_group f 1
(fun _ -> arguments cxt f [ e ; E.array Mutable el])
end))
| FlatCall(e,el) ->
P.group f 1 (fun _ ->
let cxt = expression ~level: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;
comma_sp f ;
expression ~level:1 cxt f el
)
)
| Char_to_int e ->
(match e.expression_desc with
| String_index (a,b) ->
P.group f 1 (fun _ ->
let cxt = expression ~level:15 cxt f a in
P.string f L.dot;
P.string f L.char_code_at;
P.paren_group f 1 (fun _ -> expression ~level:0 cxt f b);
)
| _ ->
P.group f 1 (fun _ ->
let cxt = expression ~level:15 cxt f e in
P.string f L.dot;
P.string f L.char_code_at;
P.string f "(0)";
cxt))
| 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])
)
| 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 ;
pp_js_function_params_body f s params;
cxt
| Raw_js_code (s,info) ->
(match info with
| Exp ->
if raw_snippet_exp_simple_enough s then
P.string f s
else begin
P.string f L.lparen;
P.string f s ;
P.string f L.rparen;
end;
cxt
| Stmt ->
P.newline f ;
P.string f s ;
P.newline f ;
cxt)
| Number v ->
let s =
match v with
| Float {f} ->
Js_number.caml_float_literal_to_js_string f
(* attach string here for float constant folding?*)
| Int { i; _}
-> Int32.to_string i (* 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 level > 13 (* Negative numbers may need to be parenthesized. *)
else level = 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 ->
P.cond_paren_group f (level > 0) 1 (fun _ ->
let cxt = expression ~level:1 cxt f e in
P.space f ;
P.string f "==";
P.space f ;
P.string f L.null;
cxt)
| Js_not e ->
P.cond_paren_group f (level > 13) 1 (fun _ ->
P.string f "!" ;
expression ~level:13 cxt f e
)
| Typeof e
->
P.string f "typeof";
P.space f;
expression ~level:13 cxt f e
| Bin (Eq, ({expression_desc = Array_index({expression_desc = Var i; _},
{expression_desc = Number (Int {i = k0 })}
) } as lhs),
{expression_desc =
(Bin((Plus as op),
{expression_desc = Array_index(
{expression_desc = Var j; _},
{expression_desc = Number (Int {i = k1; })}
); _}, delta)
| Bin((Plus as op), delta,
{expression_desc = Array_index(
{expression_desc = Var j; _},
{expression_desc = Number (Int {i = k1; })}
); _})
| Bin((Minus as op),
{expression_desc = Array_index(
{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..
*)
->
(** TODO: parenthesize when necessary *)
(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 ;
expression ~level:13 cxt f lhs (* Static index level is 15*)
| {expression_desc = Number (Int { i = -1l; _})}, Plus
| {expression_desc = Number (Int { i = 1l; _})}, Minus
->
P.string f L.minusminus;
P.space f ;
expression ~level:13 cxt f lhs
| _, _ ->
let cxt = expression ~level:13 cxt f lhs in
P.space f ;
P.string f (if op = Plus then "+=" else "-=");
P.space f ;
expression ~level:13 cxt f delta)
| 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 ]}
*)
->
P.cond_paren_group f (level > 13 ) 1 (fun _ ->
P.string f "-" ;
expression ~level:13 cxt f e
)
| Bin (op, e1, e2) ->
let (out, lft, rght) = Js_op_util.op_prec op in
let need_paren =
level > out || (match op with Lsl | Lsr | Asr -> true | _ -> false) in
(* We are more conservative here, to make the generated code more readable
to the user *)
P.cond_paren_group f need_paren 1 (fun _ ->
let cxt = expression ~level:lft cxt f e1 in
P.space f;
P.string f (Js_op_util.op_str op);
P.space f;
expression ~level:rght cxt f e2)
| String_append (e1, e2) ->
let op : Js_op.binop = Plus in
let (out, lft, rght) = Js_op_util.op_prec op in
let need_paren =
level > out || (match op with Lsl | Lsr | Asr -> true | _ -> false) in
P.cond_paren_group f need_paren 1 (fun _ ->
let cxt = expression ~level:lft cxt f e1 in
P.space f ;
P.string f "+";
P.space f;
expression ~level:rght cxt f e2)
| Array (el,_) ->
(** TODO: simplify for singleton list *)
(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))
| Optional_block (e,identity) ->
expression ~level cxt f
(if identity then e
else
E.runtime_call Js_runtime_modules.option "some" [e])
| Caml_block(el,_, _, Blk_module fields) ->
expression_desc cxt ~level f (Object (
(Ext_list.map_combine fields el Ext_ident.convert)))
| Caml_block(el,_, _, Blk_record fields) ->
expression_desc cxt ~level f (Object (
(List.combine (Array.to_list fields) el )))
(* name convention of Record is slight different from modules
*)
| 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
When it does not need block runtime, it means it will be compiled
as an array, note exception or open variant it is outer-most is
simply an array
*)
if not !Js_config.debug then begin
if not (Js_block_runtime.needBlockRuntime tag tag_info) then
expression_desc cxt ~level f (Array (el, mutable_flag))
else
begin
pp_block_create f;
P.paren_group f 1 (fun _ -> arguments cxt f [tag; E.array mutable_flag el])
end
end
else
if not (Js_block_runtime.needChromeRuntime tag tag_info) then
expression_desc cxt ~level f (Array (el, mutable_flag))
else
(
match tag_info with
| Blk_record _
| Blk_module _
| Blk_module_export ->
assert false
(*
This can not happen, see the pattern match on previous branch
TODO: we still need clean up local module compilation
to make it more obvious
*)
| Blk_variant name ->
dbg_poly_var f;
P.paren_group f 1 (fun _ -> arguments cxt f [
E.str name;
E.array mutable_flag el])
| Blk_constructor(name,number)
-> (* has to be debug mode *)
if number = 1 && Js_block_runtime.tag_is_zero tag then
begin
dbg_simple_variant f;
P.paren_group f 1 (fun _ ->
arguments cxt f
[E.str name; E.array mutable_flag el])
end
else begin
dbg_variant f;
P.paren_group f 1 (fun _ ->
arguments cxt f
[E.str name; tag; E.array mutable_flag el])
end
#if OCAML_VERSION =~ ">4.03.0" then
| Blk_record_inlined _ (* TODO: No support for debug mode yet *)
#end
| Blk_tuple
| Blk_extension
| Blk_class
| Blk_array
#if OCAML_VERSION =~ ">4.03.0" then
| Blk_record_ext _
#end
| Blk_extension_slot
| Blk_na _
->
dbg_block_create f;
P.paren_group f 1 (fun _ -> arguments cxt f [tag; E.array mutable_flag el])
)
| Caml_block_tag e ->
P.group f 1 (fun _ ->
let cxt = expression ~level:15 cxt f e in
P.string f L.dot ;
P.string f L.tag ;
cxt)
| Array_index (e, p)
| String_index (e,p)
->
P.cond_paren_group f (level > 15) 1 (fun _ ->
P.group f 1 (fun _ ->
let cxt = expression ~level:15 cxt f e in
P.bracket_group f 1 (fun _ ->
expression ~level:0 cxt f p )))
| Static_index (e, s,_) ->
P.cond_paren_group f (level > 15) 1 (fun _ ->
let cxt = expression ~level: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)
| Length (e, _) ->
(** Todo: check parens *)
P.cond_paren_group f (level > 15) 1 (fun _ ->
let cxt = expression ~level:15 cxt f e in
P.string f L.dot;
P.string f L.length;
cxt)
| New (e, el) ->
P.cond_paren_group f (level > 15) 1 (fun _ ->
P.group f 1 ( fun _ ->
P.string f L.new_;
P.space f;
let cxt = expression ~level:16 cxt f e in
P.paren_group f 1 (fun _ ->
match el with
| Some el -> arguments cxt f el
| None -> cxt)))
| Cond (e, e1, e2) ->
let action () =
let cxt = expression ~level: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 ~level:3 cxt f e1) in
P.space f;
P.string f L.colon;
P.space f ;
(* idem *)
P.group f 1 (fun _ -> expression ~level:3 cxt f e2)
in
if level > 2 then P.paren_vgroup f 1 action else action ()
| Object lst ->
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 level > 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 ()
and property_name_and_value_list cxt f (l : J.property_map) =
iter_lst cxt f l (fun cxt f (pn,e) ->
Js_dump_property.property_key f pn ;
P.string f L.colon;
P.space f;
expression ~level:1 cxt f e
) comma_nl
and array_element_list cxt f (el : E.t list) : cxt =
iter_lst cxt f el (expression ~level:1) comma_nl
and arguments cxt f (l : E.t list) : cxt =
iter_lst cxt f l (expression ~level:1) comma_sp
and variable_declaration top cxt f
(variable : J.variable_declaration) : cxt =
(* 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 pp_var_declare cxt f i
| { ident = name; value = Some e; ident_info = {used_stats; _}} ->
match used_stats with
| Dead_pure ->
cxt
| Dead_non_pure ->
(* Make sure parens are added correctly *)
statement_desc top cxt f (J.Exp e)