@@ -40,7 +40,7 @@ let return_unit : t list =
40
40
41
41
let empty_stmt : t =
42
42
{ statement_desc = Block [] ; comment = None }
43
-
43
+ (* let empty_block : J.block = [] *)
44
44
let throw_stmt ?comment v : t =
45
45
{ statement_desc = J. Throw v; comment}
46
46
@@ -63,14 +63,14 @@ let rec exp ?comment (e : E.t) : t =
63
63
| _ ->
64
64
{ statement_desc = Exp e; comment}
65
65
66
- let declare_variable ?comment ?ident_info ~kind (v :Ident.t ) : t =
66
+ let declare_variable ?comment ?ident_info ~kind (ident :Ident.t ) : t =
67
67
let property : J.property = kind in
68
68
let ident_info : J.ident_info =
69
69
match ident_info with
70
70
| None -> {used_stats = NA }
71
71
| Some x -> x in
72
72
{statement_desc =
73
- Variable { ident = v ; value = None ; property ;
73
+ Variable { ident; value = None ; property ;
74
74
ident_info ;};
75
75
comment}
76
76
@@ -94,73 +94,66 @@ let alias_variable ?comment ~exp (v:Ident.t) : t=
94
94
comment}
95
95
96
96
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 =
98
99
match e.expression_desc with
99
100
| Number (Int {i; _} ) ->
100
101
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 =
116
115
Exp {
117
- expression_desc =
116
+ expression_desc =
118
117
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)
127
125
| _ ->
128
- begin match declaration with
126
+ match declaration with
129
127
| Some (kind , did ) ->
130
128
block [declare_variable ?comment ~kind did ;
131
129
{ 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}
134
131
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 =
136
134
match e.expression_desc with
137
135
| Str (_ ,s ) ->
138
136
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)
164
157
| _ ->
165
158
match declaration with
166
159
| Some (kind ,did ) ->
@@ -185,59 +178,52 @@ let string_switch ?comment ?declaration ?default (e : J.expression) clauses :
185
178
bprint_pad_opt buf width_opt; bprint_char_set buf char_set;
186
179
fmtiter rest false;
187
180
]}
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
+ *)
189
186
let rec if_ ?comment ?declaration ?else_ (e : J.expression ) (then_ : J.block ) : t =
190
187
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
193
190
| _,
194
191
[ {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 *)
197
195
return_stmt (E. econd e b a ) :: acc
198
196
| _,
199
197
[ {statement_desc =
200
198
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 ); _};
202
200
_}],
203
201
[ {statement_desc =
204
202
Exp (
205
203
{ expression_desc =
206
204
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 ->
211
209
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)
216
211
| _ ->
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
+
228
214
| _, [ {statement_desc = Exp b; _}], [ {statement_desc = Exp a; _}]
229
215
->
230
216
exp (E. econd e b a) :: acc
231
217
| _, [] , []
232
218
-> exp e :: acc
233
219
| Js_not e, _ , _ :: _
234
- -> aux ?comment e else_ then_ acc
220
+ -> aux ?comment e ifnot ifso acc
235
221
| _, [] , _
236
222
->
237
- aux ?comment (E. not e) else_ [] acc
223
+ aux ?comment (E. not e) ifnot [] acc
238
224
(* Be careful that this re-write may result in non-terminating effect *)
239
225
| _, (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
241
227
->
242
228
(* * here we do agressive optimization, because it can help optimization later,
243
229
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)
247
233
248
234
| (Number ( Int { i = 0l ; _}) | Bool false ) , _, _
249
235
->
250
- begin match else_ with
236
+ ( match ifnot with
251
237
| [] -> acc
252
- | _ -> block else_ ::acc
253
- end
238
+ | _ -> block ifnot ::acc)
254
239
| Bool true , _ , _ ->
255
- begin match then_ with
240
+ ( match ifso with
256
241
| [] -> 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 ; _})})), _ , _)
263
248
(* TODO: always
264
249
turn [Le] -> into [Ge]
265
250
*)
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
+
272
253
273
254
| ((Bin (Gt ,
274
255
({expression_desc =
@@ -277,7 +258,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
277
258
), _ , _
278
259
->
279
260
(* * Add comment when simplified *)
280
- aux ?comment e then_ else_ acc
261
+ aux ?comment e ifso ifnot acc
281
262
282
263
(*
283
264
{[ 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)
302
283
303
284
| _,
304
285
([ another_then] as cont),
305
- [ {statement_desc = If (pred , then_, Some [else_] ) }]
286
+ [ {statement_desc = If (pred_ifnot , then_, Some [else_] ) }]
306
287
when Js_analyzer. eq_statement else_ another_then
307
288
->
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
309
290
310
291
| _ ->
311
292
{ statement_desc =
312
293
If (e,
313
- then_ ,
314
- (match else_ with
294
+ ifso ,
295
+ (match ifnot with
315
296
| [] -> None
316
297
| v -> Some v));
317
298
comment } :: acc in
0 commit comments