Skip to content

Commit c508d37

Browse files
committed
Mapping to/from AST v0
1 parent d388aee commit c508d37

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

compiler/ml/ast_mapper_from0.ml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ module E = struct
310310
| _ -> true)
311311
attrs
312312

313+
let extract_for_of_attribute attrs =
314+
List.find_map
315+
(function
316+
| {Location.txt = "res.for_of"}, Pt.PPat (_, Some expr) -> Some expr
317+
| _ -> None)
318+
attrs
319+
320+
let remove_for_of_attribute attrs =
321+
List.filter
322+
(function
323+
| {Location.txt = "res.for_of"}, _ -> false
324+
| _ -> true)
325+
attrs
326+
313327
let map_jsx_children sub (e : expression) : Pt.jsx_children =
314328
let rec visit (e : expression) : Pt.expression list =
315329
match e.pexp_desc with
@@ -524,9 +538,17 @@ module E = struct
524538
sequence ~loc ~attrs (sub.expr sub e1) (sub.expr sub e2)
525539
| Pexp_while (e1, e2) ->
526540
while_ ~loc ~attrs (sub.expr sub e1) (sub.expr sub e2)
527-
| Pexp_for (p, e1, e2, d, e3) ->
528-
for_ ~loc ~attrs (sub.pat sub p) (sub.expr sub e1) (sub.expr sub e2) d
529-
(sub.expr sub e3)
541+
| Pexp_for (p, e1, e2, d, e3) -> (
542+
let array_expr = extract_for_of_attribute attrs in
543+
let attrs = remove_for_of_attribute attrs in
544+
match array_expr with
545+
| Some array ->
546+
(* This is actually a for...of loop, decode it *)
547+
for_of ~loc ~attrs (sub.pat sub p) array (sub.expr sub e3)
548+
| None ->
549+
(* Regular for loop *)
550+
for_ ~loc ~attrs (sub.pat sub p) (sub.expr sub e1) (sub.expr sub e2) d
551+
(sub.expr sub e3))
530552
| Pexp_coerce (e, (), t2) ->
531553
coerce ~loc ~attrs (sub.expr sub e) (sub.typ sub t2)
532554
| Pexp_constraint (e, t) ->

compiler/ml/ast_mapper_to0.ml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,25 @@ module E = struct
450450
| Pexp_for (p, e1, e2, d, e3) ->
451451
for_ ~loc ~attrs (sub.pat sub p) (sub.expr sub e1) (sub.expr sub e2) d
452452
(sub.expr sub e3)
453-
| Pexp_for_of (_pat, _array_expr, _body_expr) ->
454-
(* Convert for...of to an extension since it doesn't exist in old AST *)
455-
extension ~loc ~attrs (Location.mkloc "rescript.for_of" loc, PStr [])
453+
| Pexp_for_of (pat, array_expr, body_expr) ->
454+
(* Encode for...of as a for loop with attributes *)
455+
let for_of_attr =
456+
sub.attribute sub
457+
( Location.mkloc "res.for_of" loc,
458+
PPat (Ast_helper.Pat.any (), Some array_expr) )
459+
in
460+
(* Use dummy values for start/end expressions since we store the array in attribute *)
461+
let start_expr =
462+
sub.expr sub
463+
(Ast_helper.Exp.constant (Parsetree.Pconst_integer ("0", None)))
464+
in
465+
let end_expr =
466+
sub.expr sub
467+
(Ast_helper.Exp.constant (Parsetree.Pconst_integer ("0", None)))
468+
in
469+
(* Use Upto direction flag (arbitrary choice) *)
470+
for_ ~loc ~attrs:(for_of_attr :: attrs) (sub.pat sub pat) start_expr
471+
end_expr Asttypes.Upto (sub.expr sub body_expr)
456472
| Pexp_coerce (e, (), t2) ->
457473
coerce ~loc ~attrs (sub.expr sub e) (sub.typ sub t2)
458474
| Pexp_constraint (e, t) ->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Test for..of AST mapping
2+
3+
let testForOf = () => {
4+
let arr = [1, 2, 3]
5+
6+
// Basic for..of
7+
for x of arr {
8+
Console.log(x)
9+
}
10+
11+
// For..of with complex expression
12+
for item of Array.map(arr, x => x + 1) {
13+
Console.log(item)
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Test for..of AST mapping
2+
3+
let testForOf = () => {
4+
let arr = [1, 2, 3]
5+
6+
// Basic for..of
7+
for x of arr {
8+
Console.log(x)
9+
}
10+
11+
// For..of with complex expression
12+
for item of Array.map(arr, x => x + 1) {
13+
Console.log(item)
14+
}
15+
}

0 commit comments

Comments
 (0)