Skip to content

Commit

Permalink
refactor: use inline record for J.{While,ForRange} (#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Oct 27, 2024
1 parent 2922263 commit bb90af3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
15 changes: 8 additions & 7 deletions jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,15 @@ and statement_desc =
(* Function declaration and Variable declaration *)
| Exp of expression
| If of { cond : expression; then_ : block; else_ : block }
| While of label option * expression * block
| While of { label : label option; cond : expression; body : block }
(* check if it contains loop mutable values, happens in nested loop *)
| ForRange of
for_ident_expression option
* finish_ident_expression
* for_ident
* for_direction
* block
| ForRange of {
for_ident_expr : for_ident_expression option;
finish_expr : finish_ident_expression;
for_ident : for_ident;
direction : for_direction;
body : block;
}
| Continue of label
| Break (* only used when inline a fucntion *)
| Return of expression
Expand Down
11 changes: 9 additions & 2 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
string cxt L.else_;
space cxt;
brace_block cxt s2)
| While (label, e, s) ->
| While { label; cond = e; body = s } ->
(* FIXME: print scope as well *)
(match label with
| Some i ->
Expand All @@ -1086,7 +1086,14 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
let cxt = brace_block cxt s in
semi cxt;
cxt
| ForRange (for_ident_expression, finish, id, direction, s) ->
| ForRange
{
for_ident_expr = for_ident_expression;
finish_expr = finish;
for_ident = id;
direction;
body = s;
} ->
let action cxt =
vgroup cxt 0 (fun _ ->
let cxt =
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_pass_scope.ml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ let record_scope_pass =
statement =
(fun self state x ->
match x.statement_desc with
| ForRange (_, _, loop_id, _, stmts) ->
| ForRange { for_ident = loop_id; body = stmts; _ } ->
(* TODO: simplify definition of For *)
let {
defined_idents = defined_idents';
Expand Down
11 changes: 9 additions & 2 deletions jscomp/core/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,20 @@ let assign ?comment id e : t =
{ statement_desc = J.Exp (E.assign (E.var id) e); comment }

let while_ ?comment ?label (e : E.t) (st : J.block) : t =
{ statement_desc = While (label, e, st); comment }
{ statement_desc = While { label; cond = e; body = st }; comment }

let for_ ?comment for_ident_expression finish_ident_expression id direction
(b : J.block) : t =
{
statement_desc =
ForRange (for_ident_expression, finish_ident_expression, id, direction, b);
ForRange
{
for_ident_expr = for_ident_expression;
finish_expr = finish_ident_expression;
for_ident = id;
direction;
body = b;
};
comment;
}

Expand Down

0 comments on commit bb90af3

Please sign in to comment.