Skip to content

Commit e1f4308

Browse files
committed
clean up statement handling
1 parent fddddfe commit e1f4308

11 files changed

+312
-149
lines changed

jscomp/all.depend

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ core/lam_pass_deep_flatten.cmx : core/lam_util.cmx core/lam_group.cmx \
509509
core/lam.cmx core/js_number.cmx ext/ident_set.cmx ext/ext_list.cmx \
510510
core/lam_pass_deep_flatten.cmi
511511
core/js_stmt_make.cmx : core/lam_print.cmx core/lam.cmx core/js_exp_make.cmx \
512-
core/js_closure.cmx core/js_analyzer.cmx core/j.cmx core/js_stmt_make.cmi
512+
core/js_closure.cmx core/js_analyzer.cmx core/j.cmx ext/ext_list.cmx \
513+
core/js_stmt_make.cmi
513514
core/js_pass_flatten.cmx : core/js_stmt_make.cmx core/js_map.cmx \
514515
core/js_exp_make.cmx core/js_analyzer.cmx core/j.cmx ext/ext_list.cmx \
515516
core/js_pass_flatten.cmi

jscomp/core/js_of_lam_variant.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ let eval (arg : J.expression) (dispatches : (int * string) list ) : E.t =
4141
[(S.int_switch arg
4242
(Ext_list.map (fun (i,r) ->
4343
{J.case = i ;
44-
body = [S.return (E.str r)],
44+
body = [S.return_stmt (E.str r)],
4545
false (* FIXME: if true, still print break*)
4646
}) dispatches))]
4747

@@ -60,7 +60,7 @@ let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
6060
[(S.int_switch arg
6161
(Ext_list.map (fun (i,r) ->
6262
{J.case = i ;
63-
body = [S.return (E.index (E.var event) 0l)],
63+
body = [S.return_stmt (E.index (E.var event) 0l)],
6464
false (* FIXME: if true, still print break*)
6565
}) dispatches))]
6666
, (* TODO: improve, one dispatch later,
@@ -71,7 +71,7 @@ let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
7171
[(S.int_switch arg
7272
(Ext_list.map (fun (i,r) ->
7373
{J.case = i ;
74-
body = [S.return (E.index (E.var event) 1l)],
74+
body = [S.return_stmt (E.index (E.var event) 1l)],
7575
false (* FIXME: if true, still print break*)
7676
}) dispatches))]
7777
)
@@ -87,7 +87,7 @@ let eval_as_int (arg : J.expression) (dispatches : (int * int) list ) : E.t =
8787
[(S.int_switch arg
8888
(Ext_list.map (fun (i,r) ->
8989
{J.case = i ;
90-
body = [S.return (E.int (Int32.of_int r))],
90+
body = [S.return_stmt (E.int (Int32.of_int r))],
9191
false (* FIXME: if true, still print break*)
9292
}) dispatches))]
9393

jscomp/core/js_output.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let handle_name_tail
6767
then dummy
6868
else {block = []; value = Some exp ; finished = False}
6969
| EffectCall, ReturnTrue _ ->
70-
make [S.return exp] ~finished:True
70+
make [S.return_stmt exp] ~finished:True
7171
| Declare (kind, n), ReturnFalse ->
7272
make [ S.define ~kind n exp]
7373
| Assign n ,ReturnFalse ->
@@ -87,13 +87,14 @@ let handle_block_return
8787
| Assign n, ReturnFalse -> make (block @ [S.assign n exp])
8888
| (Declare _ | Assign _), ReturnTrue _ -> make [S.unknown_lambda lam] ~finished:True
8989
| EffectCall, ReturnFalse -> make block ~value:exp
90-
| EffectCall, ReturnTrue _ -> make (block @ [S.return exp]) ~finished:True
90+
| EffectCall, ReturnTrue _ -> make (block @ [S.return_stmt exp]) ~finished:True
9191
| NeedValue, _ -> make block ~value:exp
9292

9393
let statement_of_opt_expr (x : J.expression option) : J.statement =
9494
match x with
95-
| None -> S.empty ()
96-
| Some x when Js_analyzer.no_side_effect_expression x -> S.empty ()
95+
| None -> S.empty_stmt
96+
| Some x when Js_analyzer.no_side_effect_expression x ->
97+
S.empty_stmt
9798
(* TODO, pure analysis in lambda instead *)
9899
| Some x -> S.exp x
99100

jscomp/core/js_pass_flatten.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ let flatten_map =
6464
end
6565
| Return ( {return_value = {expression_desc = Cond (a,b,c); comment}})
6666
->
67-
{ statement_desc = If (a, [self#statement (S.return b)],
68-
Some [ self#statement (S.return c)]); comment}
67+
{ statement_desc = If (a, [self#statement (S.return_stmt b)],
68+
Some [ self#statement (S.return_stmt c)]); comment}
6969

7070
| Return ({return_value = {expression_desc = Seq _; _} as v}) ->
7171
let block = Js_analyzer.rev_flatten_seq v in
7272
begin match block with
7373
| {statement_desc = Exp last_one ; _} :: rest_rev
7474
->
7575
super#statement
76-
(S.block (Ext_list.rev_map_append (self#statement) rest_rev [S.return last_one]))
76+
(S.block (Ext_list.rev_map_append (self#statement) rest_rev [S.return_stmt last_one]))
7777
| _ -> assert false
7878
end
7979
| Block [x]

jscomp/core/js_stmt_make.ml

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,25 @@ module E = Js_exp_make
3131

3232
type t = J.statement
3333

34-
let return ?comment e : t =
34+
let return_stmt ?comment e : t =
3535
{statement_desc = Return {return_value = e; } ; comment}
3636

37-
let return_unit ?comment () : t =
38-
return ?comment E.unit
37+
let return_unit : t list =
38+
[{ statement_desc = Return {return_value = E.unit; } ;
39+
comment = None}]
40+
41+
let empty_stmt : t =
42+
{ statement_desc = Block []; comment = None}
3943

40-
let break ?comment () : t =
41-
{comment ; statement_desc = Break }
42-
43-
let mk ?comment statement_desc : t =
44-
{statement_desc; comment}
45-
46-
let empty ?comment () : t = { statement_desc = Block []; comment}
47-
48-
let throw ?comment v : t = { statement_desc = J.Throw v; comment}
44+
let throw_stmt ?comment v : t =
45+
{ statement_desc = J.Throw v; comment}
4946

5047
(* avoid nested block *)
5148
let rec block ?comment (b : J.block) : t =
5249
match b with
5350
| [{statement_desc = Block bs } ] -> block bs
5451
| [b] -> b
55-
| [] -> empty ?comment ()
52+
| [] -> empty_stmt
5653
| _ -> {statement_desc = Block b ; comment}
5754

5855
(* It's a statement, we can discard some values *)
@@ -92,10 +89,12 @@ let int_switch ?comment ?declaration ?default (e : J.expression) clauses : t
9289
match e.expression_desc with
9390
| Number (Int {i; _}) ->
9491
let continuation =
95-
begin match List.find (fun (x : _ J.case_clause) -> x.case = (Int32.to_int i)) clauses
92+
begin match Ext_list.find_opt
93+
(fun (x : _ J.case_clause) ->
94+
if x.case = (Int32.to_int i) then Some (fst x.body) else None ) clauses
9695
with
97-
| case -> fst case.body
98-
| exception Not_found ->
96+
| Some case -> case
97+
| None ->
9998
begin match default with
10099
| Some x -> x
101100
| None -> assert false
@@ -124,11 +123,15 @@ let string_switch ?comment ?declaration ?default (e : J.expression) clauses :
124123
match e.expression_desc with
125124
| Str (_,s) ->
126125
let continuation =
127-
begin match List.find
128-
(fun (x : string J.case_clause) -> x.case = s) clauses
126+
begin match Ext_list.find_opt
127+
(fun (x : string J.case_clause) ->
128+
if x.case = s then
129+
Some (fst x.body)
130+
else None
131+
) clauses
129132
with
130-
| case -> (fst case.body)
131-
| exception Not_found ->
133+
| Some case -> case
134+
| None ->
132135
begin match default with
133136
| Some x -> x
134137
| None -> assert false
@@ -177,7 +180,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
177180
| _, [ {statement_desc = Return {return_value = b; _}; _}],
178181
[ {statement_desc = Return {return_value = a; _}; _}]
179182
->
180-
return (E.econd e b a ) :: acc
183+
return_stmt (E.econd e b a ) :: acc
181184
| _, [ {statement_desc =
182185
Exp {expression_desc = Bin(Eq, ({expression_desc = Var (Id id0); _} as l0), a0); _}; _}],
183186
[ {statement_desc =
@@ -345,7 +348,7 @@ let unknown_lambda ?(comment="unknown") (lam : Lam.t ) : t =
345348
(* TODO:
346349
actually, only loops can be labelled
347350
*)
348-
let continue ?comment ?(label="") unit : t =
351+
let continue_stmt ?comment ?(label="") unit : t =
349352
{
350353
statement_desc = J.Continue label;
351354
comment;

jscomp/core/js_stmt_make.mli

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434

3535
type t = J.statement
3636

37-
val mk : ?comment:string -> J.statement_desc -> t
3837

39-
val empty : ?comment:string -> unit -> t
38+
(** empty statement, block of length 0 *)
39+
val empty_stmt :
40+
t
4041

41-
val throw : ?comment:string -> J.expression -> t
42+
val throw_stmt :
43+
?comment:string ->
44+
J.expression ->
45+
t
4246

4347
val if_ :
4448
?comment:string ->
@@ -51,59 +55,132 @@ val if_ :
5155
J.block ->
5256
t
5357

54-
val block : ?comment:string -> J.block -> t
58+
(**
59+
turn a block into a single statement,
60+
avoid nested block
61+
*)
62+
val block :
63+
?comment:string ->
64+
J.block ->
65+
t
5566

67+
(** [int_switch ~declaration e clauses]
68+
69+
The [declaration] is attached to peepwhole
70+
such pattern
71+
72+
{[
73+
var x ;
74+
x = yy
75+
]}
76+
77+
into
78+
{[
79+
var x = yy;
80+
]}
81+
*)
5682
val int_switch :
57-
?comment:string -> ?declaration:Lam.let_kind * Ident.t ->
58-
?default:J.block -> J.expression -> int J.case_clause list -> t
83+
?comment:string ->
84+
?declaration:Lam.let_kind * Ident.t ->
85+
?default:J.block ->
86+
J.expression ->
87+
int J.case_clause list ->
88+
t
5989

60-
val string_switch : ?comment:string -> ?declaration:Lam.let_kind * Ident.t ->
61-
?default:J.block -> J.expression -> string J.case_clause list -> t
90+
val string_switch :
91+
?comment:string ->
92+
?declaration:Lam.let_kind * Ident.t ->
93+
?default:J.block ->
94+
J.expression ->
95+
string J.case_clause list ->
96+
t
6297

63-
val declare_variable : ?comment:string ->
64-
?ident_info:J.ident_info
65-
-> kind:Lam.let_kind -> Ident.t -> t
98+
val declare_variable :
99+
?comment:string ->
100+
?ident_info:J.ident_info ->
101+
kind:Lam.let_kind ->
102+
Ident.t ->
103+
t
66104

67105
val define :
68106
?comment:string ->
69107
?ident_info:J.ident_info ->
70-
kind:Lam.let_kind -> Ident.t -> J.expression -> t
108+
kind:Lam.let_kind ->
109+
Ident.t ->
110+
J.expression ->
111+
t
71112

72113
val alias_variable :
73-
?comment:string -> ?exp:J.expression -> Ident.t -> t
74-
val assign : ?comment:string -> J.ident -> J.expression -> t
114+
?comment:string ->
115+
?exp:J.expression ->
116+
Ident.t ->
117+
t
75118

76-
val assign_unit : ?comment:string -> J.ident -> t
119+
val assign :
120+
?comment:string ->
121+
J.ident ->
122+
J.expression ->
123+
t
124+
125+
val assign_unit :
126+
?comment:string ->
127+
J.ident ->
128+
t
77129

78-
val declare_unit : ?comment:string -> J.ident -> t
130+
val declare_unit :
131+
?comment:string ->
132+
J.ident ->
133+
t
79134

80-
val while_ : ?comment:string ->
81-
?label:J.label -> ?env:Js_closure.t -> J.expression -> J.block -> t
135+
val while_ :
136+
?comment:string ->
137+
?label:J.label ->
138+
?env:Js_closure.t ->
139+
J.expression ->
140+
J.block ->
141+
t
82142

83143
val for_ :
84144
?comment:string ->
85145
?env:Js_closure.t ->
86146
J.for_ident_expression option ->
87147
J.finish_ident_expression ->
88-
J.for_ident -> J.for_direction -> J.block -> t
148+
J.for_ident ->
149+
J.for_direction ->
150+
J.block ->
151+
t
89152

90153
val try_ :
91154
?comment:string ->
92-
?with_:J.ident * J.block -> ?finally:J.block -> J.block -> t
155+
?with_:J.ident * J.block ->
156+
?finally:J.block ->
157+
J.block ->
158+
t
93159

94-
val exp : ?comment:string -> J.expression -> t
160+
val exp :
161+
?comment:string ->
162+
J.expression ->
163+
t
95164

96-
val return : ?comment:string -> J.expression -> t
165+
val return_stmt :
166+
?comment:string ->
167+
J.expression ->
168+
t
97169

98-
val unknown_lambda : ?comment:string -> Lam.t -> t
170+
val unknown_lambda :
171+
?comment:string ->
172+
Lam.t ->
173+
t
99174

100-
val return_unit : ?comment:string -> unit -> t
175+
val return_unit : t list
101176
(** for ocaml function which returns unit
102177
it will be compiled into [return 0] in js *)
103178

104-
val break : ?comment:string -> unit -> t
105-
106179
(** if [label] is not set, it will default to empty *)
107-
val continue : ?comment:string -> ?label:J.label -> unit -> t
180+
val continue_stmt :
181+
?comment:string ->
182+
?label:J.label ->
183+
unit ->
184+
t
108185

109186
val debugger : t

0 commit comments

Comments
 (0)