Skip to content

Commit 1c17a46

Browse files
committed
tweaks
1 parent f674f51 commit 1c17a46

File tree

4 files changed

+298
-284
lines changed

4 files changed

+298
-284
lines changed

jscomp/core/js_output.ml

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,160 +17,160 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

2626

27-
module E = Js_exp_make
28-
module S = Js_stmt_make
27+
module E = Js_exp_make
28+
module S = Js_stmt_make
2929

30-
type finished =
31-
| True
32-
| False
30+
type finished =
31+
| True
32+
| False
3333
| Dummy (* Have no idea, so that when [++] is applied, always use the other *)
3434

35-
type t = {
35+
type t = {
3636
block : J.block ;
3737
value : J.expression option;
38-
finished : finished ;
38+
output_finished : finished ;
3939

4040
}
4141

4242
type continuation = Lam_compile_context.continuation
4343

44-
let make ?value ?(finished=False) block =
45-
{ block ; value ; finished }
44+
let make ?value ?(output_finished=False) block =
45+
{ block ; value ; output_finished }
4646

4747

48-
let dummy =
49-
{value = None; block = []; finished = Dummy }
48+
let dummy =
49+
{value = None; block = []; output_finished = Dummy }
5050

51-
let output_of_expression
51+
let output_of_expression
5252
(continuation : continuation)
5353
(should_return : Lam_compile_context.return_type)
5454
(lam : Lam.t) (exp : J.expression) : t =
55-
begin match continuation, should_return with
56-
| EffectCall, ReturnFalse ->
57-
if Lam_analysis.no_side_effects lam
55+
begin match continuation, should_return with
56+
| EffectCall, ReturnFalse ->
57+
if Lam_analysis.no_side_effects lam
5858
then dummy
59-
else {block = []; value = Some exp ; finished = False}
60-
| Declare (kind, n), ReturnFalse ->
59+
else {block = []; value = Some exp ; output_finished = False}
60+
| Declare (kind, n), ReturnFalse ->
6161
make [ S.define_variable ~kind n exp]
62-
| Assign n ,ReturnFalse ->
62+
| Assign n ,ReturnFalse ->
6363
make [S.assign n exp ]
6464
| EffectCall, ReturnTrue _ ->
65-
make [S.return_stmt exp] ~finished:True
66-
| (Declare _ | Assign _ ), ReturnTrue _ ->
67-
make [S.unknown_lambda lam] ~finished:True
68-
| NeedValue, _ ->
69-
{block = []; value = Some exp; finished = False }
65+
make [S.return_stmt exp] ~output_finished:True
66+
| (Declare _ | Assign _ ), ReturnTrue _ ->
67+
make [S.unknown_lambda lam] ~output_finished:True
68+
| NeedValue, _ ->
69+
{block = []; value = Some exp; output_finished = False }
7070
end
7171

72-
let output_of_block_and_expression
73-
(continuation : continuation)
72+
let output_of_block_and_expression
73+
(continuation : continuation)
7474
(should_return : Lam_compile_context.return_type)
75-
(lam : Lam.t) (block : J.block) exp : t =
76-
match continuation, should_return with
75+
(lam : Lam.t) (block : J.block) exp : t =
76+
match continuation, should_return with
7777
| EffectCall, ReturnFalse -> make block ~value:exp
78-
| Declare (kind,n), ReturnFalse ->
78+
| Declare (kind,n), ReturnFalse ->
7979
make (block @ [ S.define_variable ~kind n exp])
80-
| Assign n, ReturnFalse -> make (block @ [S.assign n exp])
81-
| EffectCall, ReturnTrue _ -> make (block @ [S.return_stmt exp]) ~finished:True
80+
| Assign n, ReturnFalse -> make (block @ [S.assign n exp])
81+
| EffectCall, ReturnTrue _ -> make (block @ [S.return_stmt exp]) ~output_finished:True
8282
| (Declare _ | Assign _), ReturnTrue _ ->
83-
make [S.unknown_lambda lam] ~finished:True
84-
| NeedValue, (ReturnTrue _ | ReturnFalse) ->
83+
make [S.unknown_lambda lam] ~output_finished:True
84+
| NeedValue, (ReturnTrue _ | ReturnFalse) ->
8585
make block ~value:exp
8686

8787

8888

89-
let block_with_opt_expr block (x : J.expression option) : J.block =
90-
match x with
89+
let block_with_opt_expr block (x : J.expression option) : J.block =
90+
match x with
9191
| None -> block
9292
| Some x when Js_analyzer.no_side_effect_expression x -> block
9393
| Some x -> block @ [S.exp x ]
9494

95-
let opt_expr_with_block (x : J.expression option) block : J.block =
96-
match x with
95+
let opt_expr_with_block (x : J.expression option) block : J.block =
96+
match x with
9797
| None -> block
9898
| Some x when Js_analyzer.no_side_effect_expression x -> block
9999
| Some x -> (S.exp x) :: block
100-
101100

102-
let rec unnest_block (block : J.block) : J.block =
103-
match block with
104-
| [{statement_desc = Block block}] -> unnest_block block
105-
| _ -> block
106101

107-
let output_as_block ( x : t) : J.block =
108-
match x with
109-
| {block; value = opt; finished} ->
102+
let rec unnest_block (block : J.block) : J.block =
103+
match block with
104+
| [{statement_desc = Block block}] -> unnest_block block
105+
| _ -> block
106+
107+
let output_as_block ( x : t) : J.block =
108+
match x with
109+
| {block; value = opt; output_finished} ->
110110
let block = unnest_block block in
111-
if finished = True then block
112-
else
111+
if output_finished = True then block
112+
else
113113
block_with_opt_expr block opt
114-
115114

116-
let to_break_block (x : t) : J.block * bool =
117-
let block = unnest_block x.block in
118-
match x with
119-
| {finished = True; _ } ->
115+
116+
let to_break_block (x : t) : J.block * bool =
117+
let block = unnest_block x.block in
118+
match x with
119+
| {output_finished = True; _ } ->
120120
block, false
121121
(* value does not matter when [finished] is true
122122
TODO: check if it has side efects
123123
*)
124-
| { value = None; finished } ->
125-
block,
126-
(match finished with | True -> false | (False | Dummy) -> true )
124+
| { value = None; output_finished } ->
125+
block,
126+
(match output_finished with | True -> false | (False | Dummy) -> true )
127127

128-
| {value = Some _ as opt; _} ->
128+
| {value = Some _ as opt; _} ->
129129
block_with_opt_expr block opt, true
130130

131131

132132
(** TODO: make everything expression make inlining hard, and code not readable?
133-
1. readability dpends on how we print the expression
133+
1. readability dpends on how we print the expression
134134
2. inlining needs generate symbols, which are statements, type mismatch
135135
we need capture [Exp e]
136136
137-
can we call them all [statement]? statement has no value
137+
can we call them all [statement]? statement has no value
138138
*)
139139
(* | {block = [{statement_desc = Exp e }]; value = None ; _}, _ *)
140140
(* -> *)
141141
(* append { x with block = []; value = Some e} y *)
142142
(* | _ , {block = [{statement_desc = Exp e }]; value = None ; _} *)
143143
(* -> *)
144144
(* append x { y with block = []; value = Some e} *)
145-
146-
let rec append_output (x : t ) (y : t ) : t =
145+
146+
let rec append_output (x : t ) (y : t ) : t =
147147
match x , y with (* ATTTENTION: should not optimize [opt_e2], it has to conform to [NeedValue]*)
148-
| {finished = True; _ }, _ -> x
149-
| _, {block = []; value= None; finished = Dummy } -> x
148+
| { output_finished = True; _ }, _ -> x
149+
| _, {block = []; value= None; output_finished = Dummy } -> x
150150
(* finished = true --> value = E.undefined otherwise would throw*)
151-
| {block = []; value= None; _ }, y -> y
152-
| {block = []; value= Some _; _}, {block = []; value= None; _ } -> x
153-
| {block = []; value = Some e1; _}, ({block = []; value = Some e2; finished } as z) ->
154-
if Js_analyzer.no_side_effect_expression e1
151+
| {block = []; value= None; _ }, y -> y
152+
| {block = []; value= Some _; _}, {block = []; value= None; _ } -> x
153+
| {block = []; value = Some e1; _}, ({block = []; value = Some e2; output_finished } as z) ->
154+
if Js_analyzer.no_side_effect_expression e1
155155
then z
156156
(* It would optimize cases like [module aliases]
157-
Bigarray, List
157+
Bigarray, List
158158
*)
159159
else
160-
{block = []; value = Some (E.seq e1 e2); finished}
160+
{block = []; value = Some (E.seq e1 e2); output_finished}
161161
(* {block = [S.exp e1]; value = Some e2(\* (E.seq e1 e2) *\); finished} *)
162162

163-
| {block = block1; value = opt_e1; _}, {block = block2; value = opt_e2; finished} ->
163+
| {block = block1; value = opt_e1; _}, {block = block2; value = opt_e2; output_finished} ->
164164
let block1 = unnest_block block1 in
165165
make (block1 @ (opt_expr_with_block opt_e1 @@ unnest_block block2))
166-
?value:opt_e2 ~finished
166+
?value:opt_e2 ~output_finished:output_finished
167167

168168

169169

170170

171171
(* Fold right is more efficient *)
172-
let concat (xs : t list) : t =
172+
let concat (xs : t list) : t =
173173
Ext_list.fold_right (fun x acc -> append_output x acc) xs dummy
174174

175-
let to_string x =
175+
let to_string x =
176176
Js_dump.string_of_block (output_as_block x)

jscomp/core/js_output.mli

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,7 +17,7 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
@@ -31,30 +31,30 @@
3131

3232
(** The intemediate output when compiling lambda into JS IR *)
3333

34-
(* Hongbo Should we rename this module js_of_lambda since it looks like it's
34+
(* Hongbo Should we rename this module js_of_lambda since it looks like it's
3535
containing that step
3636
*)
3737

3838

39-
type finished =
40-
| True
41-
| False
39+
type finished =
40+
| True
41+
| False
4242
| Dummy (* Have no idea, so that when [++] is applied, always use the other *)
4343

44-
type t = {
44+
type t = {
4545
block : J.block ;
4646
value : J.expression option;
47-
finished : finished
47+
output_finished : finished
4848
}
4949

50-
(** When [finished] is true the block is already terminated,
50+
(** When [finished] is true the block is already terminated,
5151
value does not make sense
5252
[finished] default to false, which is conservative
5353
*)
5454

5555
val make :
5656
?value: J.expression ->
57-
?finished:finished ->
57+
?output_finished:finished ->
5858
J.block ->
5959
t
6060

@@ -64,27 +64,27 @@ val output_as_block :
6464

6565
val to_break_block :
6666
t ->
67-
J.block * bool
68-
(* the second argument is
67+
J.block * bool
68+
(* the second argument is
6969
[true] means [break] needed
7070
7171
When we know the output is gonna finished true
72-
we can reduce
72+
we can reduce
7373
{[
74-
return xx ;
74+
return xx ;
7575
break
7676
]}
77-
into
77+
into
7878
{[
79-
return ;
79+
return ;
8080
]}
8181
8282
*)
8383

84-
val append_output: t -> t -> t
84+
val append_output: t -> t -> t
8585

8686

87-
val dummy : t
87+
val dummy : t
8888

8989

9090
val output_of_expression :
@@ -94,7 +94,7 @@ val output_of_expression :
9494
J.expression -> (* compiled expression *)
9595
t
9696

97-
val output_of_block_and_expression :
97+
val output_of_block_and_expression :
9898
Lam_compile_context.continuation ->
9999
Lam_compile_context.return_type ->
100100
Lam.t ->

0 commit comments

Comments
 (0)