Skip to content

Commit a874831

Browse files
committed
See how far an agent gets when asked to add module await ast.
1 parent 5cd7de5 commit a874831

8 files changed

Lines changed: 97 additions & 35 deletions

File tree

‎compiler/ml/ast_helper.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ module Mod = struct
262262
let apply ?loc ?attrs m1 m2 = mk ?loc ?attrs (Pmod_apply (m1, m2))
263263
let constraint_ ?loc ?attrs m mty = mk ?loc ?attrs (Pmod_constraint (m, mty))
264264
let unpack ?loc ?attrs e = mk ?loc ?attrs (Pmod_unpack e)
265+
let await ?loc ?attrs m = mk ?loc ?attrs (Pmod_await m)
265266
let extension ?loc ?attrs a = mk ?loc ?attrs (Pmod_extension a)
266267
end
267268

‎compiler/ml/ast_mapper.ml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ module M = struct
239239
| Pmod_constraint (m, mty) ->
240240
constraint_ ~loc ~attrs (sub.module_expr sub m) (sub.module_type sub mty)
241241
| Pmod_unpack e -> unpack ~loc ~attrs (sub.expr sub e)
242+
| Pmod_await m ->
243+
{ pmod_desc = Pmod_await (sub.module_expr sub m);
244+
pmod_loc = sub.location sub loc;
245+
pmod_attributes = sub.attributes sub attrs }
242246
| Pmod_extension x -> extension ~loc ~attrs (sub.extension sub x)
243247

244248
let map_structure_item sub {pstr_loc = loc; pstr_desc = desc} =

‎compiler/ml/ast_mapper_from0.ml‎

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,82 @@ end
250250
module M = struct
251251
(* Value expressions for the module language *)
252252

253-
let map sub {pmod_loc = loc; pmod_desc = desc; pmod_attributes = attrs} =
254-
let open Mod in
253+
let rec map sub ({Parsetree0.pmod_loc = loc; pmod_desc = desc; pmod_attributes = attrs} : Parsetree0.module_expr) =
255254
let loc = sub.location sub loc in
256255
let attrs = sub.attributes sub attrs in
256+
let has_await_attribute =
257+
List.exists (fun (attr : Parsetree.attribute) ->
258+
match attr with
259+
| ({Location.txt = "res.await"}, _) -> true
260+
| _ -> false
261+
) attrs
262+
in
263+
let attrs_without_await =
264+
List.filter (fun (attr : Parsetree.attribute) ->
265+
match attr with
266+
| ({Location.txt = "res.await"}, _) -> false
267+
| _ -> true
268+
) attrs
269+
in
257270
match desc with
258-
| Pmod_ident x -> ident ~loc ~attrs (map_loc sub x)
259-
| Pmod_structure str -> structure ~loc ~attrs (sub.structure sub str)
260-
| Pmod_functor (arg, arg_ty, body) ->
261-
functor_ ~loc ~attrs (map_loc sub arg)
262-
(Misc.may_map (sub.module_type sub) arg_ty)
263-
(sub.module_expr sub body)
264-
| Pmod_apply (m1, m2) ->
265-
apply ~loc ~attrs (sub.module_expr sub m1) (sub.module_expr sub m2)
266-
| Pmod_constraint (m, mty) ->
267-
constraint_ ~loc ~attrs (sub.module_expr sub m) (sub.module_type sub mty)
268-
| Pmod_unpack e -> unpack ~loc ~attrs (sub.expr sub e)
269-
| Pmod_extension x -> extension ~loc ~attrs (sub.extension sub x)
271+
| Parsetree0.Pmod_ident x when has_await_attribute ->
272+
let inner_mod = {
273+
Parsetree.pmod_loc = loc;
274+
pmod_desc = Parsetree.Pmod_ident (map_loc sub x);
275+
pmod_attributes = []
276+
} in
277+
{
278+
Parsetree.pmod_loc = loc;
279+
pmod_desc = Parsetree.Pmod_await inner_mod;
280+
pmod_attributes = attrs_without_await
281+
}
282+
| Parsetree0.Pmod_ident x ->
283+
{
284+
Parsetree.pmod_loc = loc;
285+
pmod_desc = Parsetree.Pmod_ident (map_loc sub x);
286+
pmod_attributes = attrs
287+
}
288+
| Parsetree0.Pmod_structure s ->
289+
{
290+
Parsetree.pmod_loc = loc;
291+
pmod_desc = Parsetree.Pmod_structure (sub.structure sub s);
292+
pmod_attributes = attrs
293+
}
294+
| Parsetree0.Pmod_functor (arg, arg_type, body) ->
295+
{
296+
Parsetree.pmod_loc = loc;
297+
pmod_desc = Parsetree.Pmod_functor (
298+
map_loc sub arg,
299+
Option.map (sub.module_type sub) arg_type,
300+
map sub body
301+
);
302+
pmod_attributes = attrs
303+
}
304+
| Parsetree0.Pmod_apply (me1, me2) ->
305+
{
306+
Parsetree.pmod_loc = loc;
307+
pmod_desc = Parsetree.Pmod_apply (map sub me1, map sub me2);
308+
pmod_attributes = attrs
309+
}
310+
| Parsetree0.Pmod_constraint (me, mt) ->
311+
{
312+
Parsetree.pmod_loc = loc;
313+
pmod_desc = Parsetree.Pmod_constraint (map sub me, sub.module_type sub mt);
314+
pmod_attributes = attrs
315+
}
316+
| Parsetree0.Pmod_unpack e ->
317+
{
318+
Parsetree.pmod_loc = loc;
319+
pmod_desc = Parsetree.Pmod_unpack (sub.expr sub e);
320+
pmod_attributes = attrs
321+
}
322+
| Parsetree0.Pmod_extension x ->
323+
{
324+
Parsetree.pmod_loc = loc;
325+
pmod_desc = Parsetree.Pmod_extension (sub.extension sub x);
326+
pmod_attributes = attrs
327+
}
328+
| _ -> assert false
270329

271330
let map_structure_item sub {pstr_loc = loc; pstr_desc = desc} =
272331
let open Str in

‎compiler/ml/ast_mapper_to0.ml‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ module M = struct
257257
| Pmod_constraint (m, mty) ->
258258
constraint_ ~loc ~attrs (sub.module_expr sub m) (sub.module_type sub mty)
259259
| Pmod_unpack e -> unpack ~loc ~attrs (sub.expr sub e)
260+
| Pmod_await m ->
261+
let m1 = sub.module_expr sub m in
262+
let attrs = (Location.mknoloc "res.await", PStr []) :: attrs in
263+
ident ~loc ~attrs (Location.mknoloc (Longident.Lident "Await"))
260264
| Pmod_extension x -> extension ~loc ~attrs (sub.extension sub x)
261265

262266
let map_structure_item sub {pstr_loc = loc; pstr_desc = desc} =
@@ -517,22 +521,6 @@ module E = struct
517521
(Asttypes.Noloc.Labelled "children", children_expr);
518522
(Asttypes.Noloc.Nolabel, unit_expr);
519523
])
520-
| Pexp_jsx_element
521-
(Jsx_container_element
522-
{
523-
jsx_container_element_tag_name_start = tag_name;
524-
jsx_container_element_props = props;
525-
jsx_container_element_children = children;
526-
}) ->
527-
let tag_ident = map_loc sub tag_name in
528-
let props = map_jsx_props sub props in
529-
let children_expr = map_jsx_children sub loc children in
530-
apply ~loc ~attrs:(jsx_attr sub :: attrs) (ident tag_ident)
531-
(props
532-
@ [
533-
(Asttypes.Noloc.Labelled "children", children_expr);
534-
(Asttypes.Noloc.Nolabel, jsx_unit_expr);
535-
])
536524
end
537525

538526
module P = struct

‎compiler/ml/parsetree.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ and module_expr_desc =
610610
| Pmod_apply of module_expr * module_expr (* ME1(ME2) *)
611611
| Pmod_constraint of module_expr * module_type (* (ME : MT) *)
612612
| Pmod_unpack of expression (* (val E) *)
613+
| Pmod_await of module_expr (* @await M *)
613614
| Pmod_extension of extension
614615
(* [%id] *)
615616

‎compiler/ml/parsetree0.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ and module_expr_desc =
546546
| Pmod_apply of module_expr * module_expr (* ME1(ME2) *)
547547
| Pmod_constraint of module_expr * module_type (* (ME : MT) *)
548548
| Pmod_unpack of expression (* (val E) *)
549+
| Pmod_await of module_expr (* @await ME *)
549550
| Pmod_extension of extension
550551
(* [%id] *)
551552

‎compiler/ml/printast.ml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,9 @@ and module_expr i ppf x =
587587
| Pmod_unpack e ->
588588
line i ppf "Pmod_unpack\n";
589589
expression i ppf e
590+
| Pmod_await me ->
591+
line i ppf "Pmod_await\n";
592+
module_expr i ppf me
590593
| Pmod_extension (s, arg) ->
591594
line i ppf "Pmod_extension \"%s\"\n" s.txt;
592595
payload i ppf arg

‎compiler/syntax/src/res_core.ml‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ end
157157

158158
let ternary_attr = (Location.mknoloc "res.ternary", Parsetree.PStr [])
159159
let if_let_attr = (Location.mknoloc "res.iflet", Parsetree.PStr [])
160-
let make_await_attr loc = (Location.mkloc "res.await" loc, Parsetree.PStr [])
161160
let suppress_fragile_match_warning_attr =
162161
( Location.mknoloc "warning",
163162
Parsetree.PStr
@@ -6028,10 +6027,16 @@ and parse_module_expr p =
60286027
if is_es6_arrow_functor p then parse_functor_module_expr p
60296028
else parse_primary_mod_expr p
60306029
in
6031-
{
6032-
mod_expr with
6033-
pmod_attributes = List.concat [mod_expr.pmod_attributes; attrs];
6034-
}
6030+
let mod_expr =
6031+
if has_await then
6032+
{ pmod_desc = Pmod_await mod_expr;
6033+
pmod_loc = loc_await;
6034+
pmod_attributes = attrs }
6035+
else
6036+
{ mod_expr with
6037+
pmod_attributes = List.concat [mod_expr.pmod_attributes; attrs] }
6038+
in
6039+
mod_expr
60356040

60366041
and parse_constrained_mod_expr p =
60376042
let mod_expr = parse_module_expr p in

0 commit comments

Comments
 (0)