Skip to content

Commit 75871b0

Browse files
committedOct 9, 2018
1 parent 01275f2 commit 75871b0

6 files changed

+256
-257
lines changed
 

‎jscomp/core/js_exp_make.ml

+10-22
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
622622
| (Bin (Ge,
623623
({expression_desc = Length _ ;
624624
_}), {expression_desc = Number (Int { i = 0l ; _})})), _, _
625-
-> ifnot
625+
-> ifso
626626
| (Bin (Gt,
627627
({expression_desc = Length _;
628628
_} as pred ),
@@ -646,30 +646,18 @@ let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
646646
]}
647647
*)
648648
econd (and_ pred pred1) ifso1 ifnot
649-
| _, (Cond (p1, branch_code0, branch_code1)), _
650-
when Js_analyzer.eq_expression branch_code0 ifnot
649+
| _, (Cond (pred1, ifso1, ifnot1)), _
650+
when Js_analyzer.eq_expression ifso1 ifnot
651651
->
652-
(* the same as above except we revert the [cond] expression *)
653-
econd (and_ pred (not p1)) branch_code1 ifnot
654-
655-
| _, _, (Cond (p1', branch_code0, branch_code1))
656-
when Js_analyzer.eq_expression ifso branch_code0
657-
(*
658-
{[
659-
if b then branch_code0 else (if p1' then branch_code0 else branch_code1)
660-
]}
661-
is equivalent to
662-
{[
663-
if b or p1' then branch_code0 else branch_code1
664-
]}
665-
*)
652+
econd (and_ pred (not pred1)) ifnot1 ifnot
653+
| _, _, (Cond (pred1, ifso1, ifnot1))
654+
when Js_analyzer.eq_expression ifso ifso1
666655
->
667-
econd (or_ pred p1') ifso branch_code1
668-
| _, _, (Cond (p1', branch_code0, branch_code1))
669-
when Js_analyzer.eq_expression ifso branch_code1
656+
econd (or_ pred pred1) ifso ifnot1
657+
| _, _, (Cond (pred1, ifso1, ifnot1))
658+
when Js_analyzer.eq_expression ifso ifnot1
670659
->
671-
(* the same as above except we revert the [cond] expression *)
672-
econd (or_ pred (not p1')) ifso branch_code0
660+
econd (or_ pred (not pred1)) ifso ifso1
673661

674662
| Js_not e, _, _
675663
->

‎jscomp/core/js_stmt_make.ml

+87-106
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let return_unit : t list =
4040

4141
let empty_stmt : t =
4242
{ statement_desc = Block []; comment = None}
43-
43+
(* let empty_block : J.block = [] *)
4444
let throw_stmt ?comment v : t =
4545
{ statement_desc = J.Throw v; comment}
4646

@@ -63,14 +63,14 @@ let rec exp ?comment (e : E.t) : t =
6363
| _ ->
6464
{ statement_desc = Exp e; comment}
6565

66-
let declare_variable ?comment ?ident_info ~kind (v:Ident.t) : t=
66+
let declare_variable ?comment ?ident_info ~kind (ident:Ident.t) : t=
6767
let property : J.property = kind in
6868
let ident_info : J.ident_info =
6969
match ident_info with
7070
| None -> {used_stats = NA}
7171
| Some x -> x in
7272
{statement_desc =
73-
Variable { ident = v; value = None; property ;
73+
Variable { ident; value = None; property ;
7474
ident_info ;};
7575
comment}
7676

@@ -94,73 +94,66 @@ let alias_variable ?comment ~exp (v:Ident.t) : t=
9494
comment}
9595

9696

97-
let int_switch ?comment ?declaration ?default (e : J.expression) clauses : t =
97+
let int_switch ?comment ?declaration ?default
98+
(e : J.expression) (clauses : int J.case_clause list): t =
9899
match e.expression_desc with
99100
| Number (Int {i; _}) ->
100101
let continuation =
101-
begin match Ext_list.find_opt clauses
102-
(fun (x : _ J.case_clause) ->
103-
if x.switch_case = (Int32.to_int i) then
104-
Some x.switch_body else None )
105-
with
106-
| Some case -> case
107-
| None ->
108-
begin match default with
109-
| Some x -> x
110-
| None -> assert false
111-
end
112-
end in
113-
begin match declaration, continuation with
114-
| Some (kind, did),
115-
[ {statement_desc =
102+
match Ext_list.find_opt clauses
103+
(fun x ->
104+
if x.switch_case = Int32.to_int i then
105+
Some x.switch_body else None )
106+
with
107+
| Some case -> case
108+
| None ->
109+
match default with
110+
| Some x -> x
111+
| None -> assert false in
112+
(match declaration, continuation with
113+
| Some (kind, did),
114+
[ {statement_desc =
116115
Exp {
117-
expression_desc =
116+
expression_desc =
118117
Bin(Eq, {expression_desc = Var (Id id) ; _}, e0); _}; _}]
119-
when Ident.same did id
120-
->
121-
define_variable ?comment ~kind id e0
122-
| Some(kind,did), _
123-
->
124-
block (declare_variable ?comment ~kind did :: continuation)
125-
| None, _ -> block continuation
126-
end
118+
when Ident.same did id
119+
->
120+
define_variable ?comment ~kind id e0
121+
| Some(kind,did), _
122+
->
123+
block (declare_variable ?comment ~kind did :: continuation)
124+
| None, _ -> block continuation)
127125
| _ ->
128-
begin match declaration with
126+
match declaration with
129127
| Some (kind, did) ->
130128
block [declare_variable ?comment ~kind did ;
131129
{ statement_desc = J.Int_switch (e,clauses, default); comment}]
132-
| None -> { statement_desc = J.Int_switch (e,clauses, default); comment}
133-
end
130+
| None -> { statement_desc = J.Int_switch (e,clauses, default); comment}
134131

135-
let string_switch ?comment ?declaration ?default (e : J.expression) clauses : t=
132+
let string_switch ?comment ?declaration ?default
133+
(e : J.expression) (clauses : string J.case_clause list): t=
136134
match e.expression_desc with
137135
| Str (_,s) ->
138136
let continuation =
139-
begin match Ext_list.find_opt clauses
140-
(fun (x : string J.case_clause) ->
141-
if x.switch_case = s then
142-
Some x.switch_body
143-
else None
144-
)
145-
with
146-
| Some case -> case
147-
| None ->
148-
begin match default with
149-
| Some x -> x
150-
| None -> assert false
151-
end
152-
end in
153-
begin match declaration, continuation with
154-
| Some (kind, did),
155-
[ {statement_desc = Exp {expression_desc = Bin(Eq, {expression_desc = Var (Id id); _}, e0);_} ; _}]
156-
when Ident.same did id
157-
->
158-
define_variable ?comment ~kind id e0
159-
| Some(kind,did), _
160-
->
161-
block @@ declare_variable ?comment ~kind did :: continuation
162-
| None, _ -> block continuation
163-
end
137+
match Ext_list.find_opt clauses (fun x ->
138+
if x.switch_case = s then
139+
Some x.switch_body
140+
else None
141+
) with
142+
| Some case -> case
143+
| None ->
144+
match default with
145+
| Some x -> x
146+
| None -> assert false in
147+
(match declaration, continuation with
148+
| Some (kind, did),
149+
[ {statement_desc = Exp {expression_desc = Bin(Eq, {expression_desc = Var (Id id); _}, e0);_} ; _}]
150+
when Ident.same did id
151+
->
152+
define_variable ?comment ~kind id e0
153+
| Some(kind,did), _
154+
->
155+
block @@ declare_variable ?comment ~kind did :: continuation
156+
| None, _ -> block continuation)
164157
| _ ->
165158
match declaration with
166159
| Some (kind,did) ->
@@ -185,59 +178,52 @@ let string_switch ?comment ?declaration ?default (e : J.expression) clauses :
185178
bprint_pad_opt buf width_opt; bprint_char_set buf char_set;
186179
fmtiter rest false;
187180
]}
188-
*)
181+
182+
To hit this branch, we also need [declaration] passed down
183+
TODO: check how we compile [Lifthenelse]
184+
The declaration argument is introduced to merge assignment in both branches
185+
*)
189186
let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t =
190187
let declared = ref false in
191-
let rec aux ?comment (e : J.expression) (then_ : J.block) (else_ : J.block ) acc =
192-
match e.expression_desc, then_, (else_ : J.block ) with
188+
let rec aux ?comment (e : J.expression) (ifso : J.block) (ifnot : J.block ) acc : J.block =
189+
match e.expression_desc, ifso, ifnot with
193190
| _,
194191
[ {statement_desc = Return {return_value = b; _}; _}],
195-
[ {statement_desc = Return {return_value = a; _}; _}]
196-
->
192+
[ {statement_desc = Return {return_value = a; _}; _} as _ifnot_stmt]
193+
->
194+
(* ifnot_stmt :: { statement_desc = If(e, ifso,None); comment = None} :: acc *)
197195
return_stmt (E.econd e b a ) :: acc
198196
| _,
199197
[ {statement_desc =
200198
Exp
201-
{expression_desc = Bin(Eq, ({expression_desc = Var (Id id0); _} as l0), a0); _};
199+
{expression_desc = Bin(Eq, ({expression_desc = Var (Id var_ifso); _} as lhs_ifso), rhs_ifso); _};
202200
_}],
203201
[ {statement_desc =
204202
Exp (
205203
{ expression_desc =
206204
Bin(Eq,
207-
{expression_desc = Var (Id id1); _}, b0); _}); _}]
208-
when Ident.same id0 id1 ->
209-
begin match declaration with
210-
| Some (kind,did) when Ident.same did id0 ->
205+
{expression_desc = Var (Id var_ifnot); _}, lhs_ifnot); _}); _}]
206+
when Ident.same var_ifso var_ifnot ->
207+
(match declaration with
208+
| Some (kind,id) when Ident.same id var_ifso ->
211209
declared := true;
212-
define_variable ~kind id0 (E.econd e a0 b0) :: acc
213-
(* To hit this branch, we also need [declaration] passed down
214-
TODO: check how we compile [Lifthenelse]
215-
*)
210+
define_variable ~kind var_ifso (E.econd e rhs_ifso lhs_ifnot)
216211
| _ ->
217-
exp (E.assign l0 (E.econd e a0 b0)) :: acc
218-
end
219-
220-
| _, _,
221-
({statement_desc = Exp {expression_desc = Number _}; _}::more_else)
222-
->
223-
aux ?comment e then_ more_else acc
224-
| _, ({statement_desc = Exp {expression_desc = Number _}; _} :: more_then), _
225-
->
226-
aux ?comment e more_then else_ acc
227-
212+
exp (E.assign lhs_ifso (E.econd e rhs_ifso lhs_ifnot))) :: acc
213+
228214
| _, [ {statement_desc = Exp b; _}], [ {statement_desc = Exp a; _}]
229215
->
230216
exp (E.econd e b a) :: acc
231217
| _, [], []
232218
-> exp e :: acc
233219
| Js_not e, _ , _ :: _
234-
-> aux ?comment e else_ then_ acc
220+
-> aux ?comment e ifnot ifso acc
235221
| _, [], _
236222
->
237-
aux ?comment (E.not e) else_ [] acc
223+
aux ?comment (E.not e) ifnot [] acc
238224
(* Be careful that this re-write may result in non-terminating effect *)
239225
| _, (y::ys), (x::xs)
240-
when Js_analyzer.(eq_statement x y && no_side_effect_expression e)
226+
when Js_analyzer.eq_statement x y && Js_analyzer.no_side_effect_expression e
241227
->
242228
(** here we do agressive optimization, because it can help optimization later,
243229
move code outside of branch is generally helpful later
@@ -247,28 +233,23 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
247233

248234
| (Number ( Int { i = 0l; _}) | Bool false) , _, _
249235
->
250-
begin match else_ with
236+
(match ifnot with
251237
| [] -> acc
252-
| _ -> block else_ ::acc
253-
end
238+
| _ -> block ifnot ::acc)
254239
| Bool true, _, _ ->
255-
begin match then_ with
240+
(match ifso with
256241
| [] -> acc
257-
| _ -> block then_ :: acc
258-
end
259-
| (Number _ , _, _
260-
| (Bin (Ge,
261-
({expression_desc = Length _;
262-
_}), {expression_desc = Number (Int { i = 0l; _})})), _ , _)
242+
| _ -> block ifso :: acc)
243+
244+
| (Number _ , _, _)
245+
| ((Bin (Ge,
246+
({expression_desc = Length _;
247+
_}), {expression_desc = Number (Int { i = 0l; _})})), _ , _)
263248
(* TODO: always
264249
turn [Le] -> into [Ge]
265250
*)
266-
-> block then_ :: acc
267-
| Bin (Bor , a, {expression_desc = Number (Int { i = 0l ; _})}), _, _
268-
| Bin (Bor , {expression_desc = Number (Int { i = 0l ; _})}, a), _, _
269-
->
270-
aux ?comment a then_ else_ acc
271-
251+
-> block ifso :: acc
252+
272253

273254
| ((Bin (Gt,
274255
({expression_desc =
@@ -277,7 +258,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
277258
), _ , _
278259
->
279260
(** Add comment when simplified *)
280-
aux ?comment e then_ else_ acc
261+
aux ?comment e ifso ifnot acc
281262

282263
(*
283264
{[ if a then { if b then d else e} else e ]}
@@ -302,16 +283,16 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
302283

303284
| _,
304285
([ another_then] as cont),
305-
[ {statement_desc = If (pred, then_, Some [else_] ) }]
286+
[ {statement_desc = If (pred_ifnot, then_, Some [else_] ) }]
306287
when Js_analyzer.eq_statement else_ another_then
307288
->
308-
aux ?comment (E.or_ e (E.not pred)) cont then_ acc
289+
aux ?comment (E.or_ e (E.not pred_ifnot)) cont then_ acc
309290

310291
| _ ->
311292
{ statement_desc =
312293
If (e,
313-
then_,
314-
(match else_ with
294+
ifso,
295+
(match ifnot with
315296
| [] -> None
316297
| v -> Some v));
317298
comment } :: acc in

0 commit comments

Comments
 (0)
Please sign in to comment.