Skip to content

Commit d388aee

Browse files
committed
Implement for..of loops for arrays
1 parent a631b38 commit d388aee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+554
-43
lines changed

analysis/reanalyze/src/Arnold.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,9 @@ module Compile = struct
979979
| Texp_for _ ->
980980
notImplemented "Texp_for";
981981
assert false
982+
| Texp_for_of _ ->
983+
notImplemented "Texp_for_of";
984+
assert false
982985
| Texp_send _ ->
983986
notImplemented "Texp_send";
984987
assert false

analysis/reanalyze/src/SideEffects.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ let rec exprNoSideEffects (expr : Typedtree.expression) =
5959
| Texp_for (_id, _pat, e1, e2, _dir, e3) ->
6060
e1 |> exprNoSideEffects && e2 |> exprNoSideEffects
6161
&& e3 |> exprNoSideEffects
62+
| Texp_for_of (_id, _pat, e1, e2) ->
63+
e1 |> exprNoSideEffects && e2 |> exprNoSideEffects
6264
| Texp_send _ -> false
6365
| Texp_letexception (_ec, e) -> e |> exprNoSideEffects
6466
| Texp_pack _ -> false

analysis/src/Utils.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ let identifyPexp pexp =
100100
| Pexp_sequence _ -> "Pexp_sequence"
101101
| Pexp_while _ -> "Pexp_while"
102102
| Pexp_for _ -> "Pexp_for"
103+
| Pexp_for_of _ -> "Pexp_for_of"
103104
| Pexp_constraint _ -> "Pexp_constraint"
104105
| Pexp_coerce _ -> "Pexp_coerce"
105106
| Pexp_send _ -> "Pexp_send"

compiler/core/j.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ and statement_desc =
264264
* for_ident
265265
* for_direction
266266
* block
267+
| ForOf of for_ident * expression * block
267268
| Continue
268269
| Break (* only used when inline a fucntion *)
269270
| Return of expression

compiler/core/js_analyzer.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ let no_side_effect_obj =
135135
| Throw _ | Debugger | Break | Variable _ | Continue ->
136136
raise_notrace Not_found
137137
| Exp e -> self.expression self e
138-
| Int_switch _ | String_switch _ | ForRange _ | If _ | While _ | Block _
139-
| Return _ | Try _ ->
138+
| Int_switch _ | String_switch _ | ForRange _ | ForOf _ | If _ | While _
139+
| Block _ | Return _ | Try _ ->
140140
super.statement self s);
141141
expression =
142142
(fun _ s ->
@@ -255,7 +255,7 @@ and eq_statement ({statement_desc = x0} : J.statement)
255255
match y0 with
256256
| Block ys0 -> eq_block xs0 ys0
257257
| _ -> false)
258-
| Variable _ | If _ | While _ | ForRange _ | Continue | Int_switch _
258+
| Variable _ | If _ | While _ | ForRange _ | ForOf _ | Continue | Int_switch _
259259
| String_switch _ | Throw _ | Try _ ->
260260
false
261261

compiler/core/js_dump.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,21 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
14781478
brace_block cxt f s)
14791479
in
14801480
action cxt
1481+
| ForOf (id, iterable, s) ->
1482+
P.vgroup f 0 (fun _ ->
1483+
P.group f 0 (fun _ ->
1484+
P.string f L.for_;
1485+
P.space f;
1486+
P.paren_group f 1 (fun _ ->
1487+
P.string f L.let_;
1488+
P.space f;
1489+
ignore (Ext_pp_scope.ident cxt f id);
1490+
P.space f;
1491+
P.string f L.of_;
1492+
P.space f;
1493+
ignore (expression ~level:0 cxt f iterable)));
1494+
P.space f;
1495+
brace_block cxt f s)
14811496
| Continue ->
14821497
continue f;
14831498
cxt

compiler/core/js_dump_lit.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ let if_ = "if"
9090

9191
let for_ = "for"
9292

93+
let of_ = "of"
94+
9395
let try_ = "try"
9496

9597
let finally = "finally"

compiler/core/js_fold.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ class fold =
241241
let _self = _self#for_direction _x3 in
242242
let _self = _self#block _x4 in
243243
_self
244+
| ForOf (_x0, _x1, _x2) ->
245+
let _self = _self#for_ident _x0 in
246+
let _self = _self#expression _x1 in
247+
let _self = _self#block _x2 in
248+
_self
244249
| Continue -> _self
245250
| Break -> _self
246251
| Return _x0 ->

compiler/core/js_pass_scope.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ let record_scope_pass =
238238
statement =
239239
(fun self state x ->
240240
match x.statement_desc with
241-
| ForRange (_, _, loop_id, _, _) ->
241+
| ForRange (_, _, loop_id, _, _) | ForOf (loop_id, _, _) ->
242242
(* TODO: simplify definition of For *)
243243
let {
244244
defined_idents = defined_idents';

compiler/core/js_record_fold.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ let statement_desc : 'a. ('a, statement_desc) fn =
245245
let st = for_direction _self st _x3 in
246246
let st = _self.block _self st _x4 in
247247
st
248+
| ForOf (_x0, _x1, _x2) ->
249+
let st = _self.for_ident _self st _x0 in
250+
let st = _self.expression _self st _x1 in
251+
let st = _self.block _self st _x2 in
252+
st
248253
| Continue -> st
249254
| Break -> st
250255
| Return _x0 ->

0 commit comments

Comments
 (0)