Skip to content

Commit 0ecd49e

Browse files
authored
Merge pull request #2620 from BuckleScript/pipe_syntax
tentative pipe syntax #2342
2 parents bcf4a24 + 1304b7c commit 0ecd49e

File tree

10 files changed

+309
-116
lines changed

10 files changed

+309
-116
lines changed

jscomp/syntax/ast_exp_apply.ml

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,50 @@ let handle_exp_apply
3131
(args : (Asttypes.label * Parsetree.expression) list)
3232
=
3333
let loc = e.pexp_loc in
34-
begin match fn with
35-
| {pexp_desc =
36-
Pexp_apply (
34+
begin match fn.pexp_desc with
35+
| Pexp_apply (
3736
{pexp_desc =
3837
Pexp_ident {txt = Lident "##" ; loc} ; _},
3938
[("", obj) ;
4039
("", {pexp_desc = Pexp_ident {txt = Lident name;_ } ; _} )
41-
]);
42-
_} -> (* f##paint 1 2 *)
40+
])
41+
-> (* f##paint 1 2 *)
4342
{e with pexp_desc = Ast_util.method_apply loc self obj name args }
44-
| {pexp_desc =
45-
Pexp_apply (
43+
| Pexp_apply (
4644
{pexp_desc =
4745
Pexp_ident {txt = Lident "#@" ; loc} ; _},
4846
[("", obj) ;
4947
("", {pexp_desc = Pexp_ident {txt = Lident name;_ } ; _} )
50-
]);
51-
_} -> (* f##paint 1 2 *)
48+
])
49+
-> (* f##paint 1 2 *)
5250
{e with pexp_desc = Ast_util.property_apply loc self obj name args }
51+
| Pexp_ident {txt = Lident "|."} ->
52+
(* a |. f b c [@bs] --> f a b c [@bs]
53+
*)
54+
begin match args with
55+
| [ "", obj_arg;
56+
"", ({pexp_desc = Pexp_apply (fn, args)} as app )
57+
] ->
58+
let obj_arg = self.expr self obj_arg in
59+
let fn = self.expr self fn in
60+
let args = Ext_list.map (fun (lab,exp) -> lab, self.expr self exp ) args in
61+
Bs_ast_invariant.warn_unused_attributes app.pexp_attributes;
62+
{ app
63+
with pexp_desc = Pexp_apply(fn, ("", obj_arg) :: args);
64+
pexp_attributes = []
65+
}
66+
(* a |. f [@bs]
67+
*)
68+
| [ "", obj_arg ;
69+
"", fn
70+
] ->
71+
Exp.apply ~loc (self.expr self fn) ["", self.expr self obj_arg]
72+
| _ ->
73+
Location.raise_errorf ~loc
74+
"invalid |. syntax "
75+
end
5376

54-
| {pexp_desc =
55-
Pexp_ident {txt = Lident "##" ; loc} ; _}
77+
| Pexp_ident {txt = Lident "##" ; loc}
5678
->
5779
begin match args with
5880
| [("", obj) ;
@@ -96,9 +118,7 @@ let handle_exp_apply
96118
end
97119
]}
98120
*)
99-
| {pexp_desc =
100-
Pexp_ident {txt = Lident ("#=" )}
101-
} ->
121+
| Pexp_ident {txt = Lident "#=" } ->
102122
begin match args with
103123
| ["",
104124
{pexp_desc =

jscomp/test/.depend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ offset.cmj : ../stdlib/string.cmj ../stdlib/set.cmj
481481
oo_js_test_date.cmj : mt.cmj ../runtime/js.cmj
482482
optional_ffi_test.cmj : mt.cmj
483483
pipe_send_readline.cmj : ../runtime/js.cmj
484+
pipe_syntax.cmj :
484485
poly_type.cmj : ../runtime/js.cmj
485486
poly_variant_test.cmj : mt.cmj ../runtime/js.cmj poly_variant_test.cmi
486487
polymorphism_test.cmj : polymorphism_test.cmi

jscomp/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_
242242
gpr_2503_test\
243243
block_alias_test\
244244
gpr_2608_test\
245+
pipe_syntax\
245246
# bs_uncurry_test
246247
# needs Lam to get rid of Uncurry arity first
247248
# simple_derive_test

jscomp/test/bs_list_test.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,21 @@ function makeTest(n) {
19821982
})));
19831983
}
19841984

1985-
b("File \"bs_list_test.ml\", line 295, characters 4-11", +(Belt_List.cmp(/* :: */[
1985+
eq("File \"bs_list_test.ml\", line 293, characters 5-12", /* :: */[
1986+
2,
1987+
/* :: */[
1988+
3,
1989+
/* [] */0
1990+
]
1991+
], /* :: */[
1992+
2,
1993+
/* :: */[
1994+
3,
1995+
/* [] */0
1996+
]
1997+
]);
1998+
1999+
b("File \"bs_list_test.ml\", line 299, characters 4-11", +(Belt_List.cmp(/* :: */[
19862000
1,
19872001
/* :: */[
19882002
2,
@@ -2005,7 +2019,7 @@ b("File \"bs_list_test.ml\", line 295, characters 4-11", +(Belt_List.cmp(/* :: *
20052019
]
20062020
], Caml_obj.caml_compare) > 0));
20072021

2008-
b("File \"bs_list_test.ml\", line 296, characters 4-11", +(Belt_List.cmp(/* :: */[
2022+
b("File \"bs_list_test.ml\", line 300, characters 4-11", +(Belt_List.cmp(/* :: */[
20092023
1,
20102024
/* :: */[
20112025
2,
@@ -2028,7 +2042,7 @@ b("File \"bs_list_test.ml\", line 296, characters 4-11", +(Belt_List.cmp(/* :: *
20282042
]
20292043
], Caml_obj.caml_compare) > 0));
20302044

2031-
b("File \"bs_list_test.ml\", line 297, characters 4-11", +(Belt_List.cmp(/* :: */[
2045+
b("File \"bs_list_test.ml\", line 301, characters 4-11", +(Belt_List.cmp(/* :: */[
20322046
1,
20332047
/* :: */[
20342048
2,
@@ -2051,7 +2065,7 @@ b("File \"bs_list_test.ml\", line 297, characters 4-11", +(Belt_List.cmp(/* :: *
20512065
]
20522066
], Caml_obj.caml_compare) < 0));
20532067

2054-
b("File \"bs_list_test.ml\", line 298, characters 4-11", +(Belt_List.cmp(/* :: */[
2068+
b("File \"bs_list_test.ml\", line 302, characters 4-11", +(Belt_List.cmp(/* :: */[
20552069
1,
20562070
/* :: */[
20572071
2,
@@ -2071,7 +2085,7 @@ b("File \"bs_list_test.ml\", line 298, characters 4-11", +(Belt_List.cmp(/* :: *
20712085
]
20722086
], Caml_primitive.caml_int_compare) > 0));
20732087

2074-
b("File \"bs_list_test.ml\", line 299, characters 4-11", +(Belt_List.cmp(/* :: */[
2088+
b("File \"bs_list_test.ml\", line 303, characters 4-11", +(Belt_List.cmp(/* :: */[
20752089
1,
20762090
/* :: */[
20772091
2,
@@ -2091,7 +2105,7 @@ b("File \"bs_list_test.ml\", line 299, characters 4-11", +(Belt_List.cmp(/* :: *
20912105
]
20922106
], Caml_primitive.caml_int_compare) === 0));
20932107

2094-
b("File \"bs_list_test.ml\", line 300, characters 4-11", +(Belt_List.cmp(/* :: */[
2108+
b("File \"bs_list_test.ml\", line 304, characters 4-11", +(Belt_List.cmp(/* :: */[
20952109
1,
20962110
/* :: */[
20972111
2,
@@ -2111,19 +2125,19 @@ b("File \"bs_list_test.ml\", line 300, characters 4-11", +(Belt_List.cmp(/* :: *
21112125
]
21122126
], Caml_primitive.caml_int_compare) > 0));
21132127

2114-
b("File \"bs_list_test.ml\", line 301, characters 4-11", +(Belt_List.cmpByLength(/* [] */0, /* [] */0) === 0));
2128+
b("File \"bs_list_test.ml\", line 305, characters 4-11", +(Belt_List.cmpByLength(/* [] */0, /* [] */0) === 0));
21152129

2116-
b("File \"bs_list_test.ml\", line 302, characters 4-11", +(Belt_List.cmpByLength(/* :: */[
2130+
b("File \"bs_list_test.ml\", line 306, characters 4-11", +(Belt_List.cmpByLength(/* :: */[
21172131
1,
21182132
/* [] */0
21192133
], /* [] */0) > 0));
21202134

2121-
b("File \"bs_list_test.ml\", line 303, characters 4-11", +(Belt_List.cmpByLength(/* [] */0, /* :: */[
2135+
b("File \"bs_list_test.ml\", line 307, characters 4-11", +(Belt_List.cmpByLength(/* [] */0, /* :: */[
21222136
1,
21232137
/* [] */0
21242138
]) < 0));
21252139

2126-
b("File \"bs_list_test.ml\", line 304, characters 4-11", +(Belt_List.cmpByLength(/* :: */[
2140+
b("File \"bs_list_test.ml\", line 308, characters 4-11", +(Belt_List.cmpByLength(/* :: */[
21272141
1,
21282142
/* :: */[
21292143
2,
@@ -2134,7 +2148,7 @@ b("File \"bs_list_test.ml\", line 304, characters 4-11", +(Belt_List.cmpByLength
21342148
/* [] */0
21352149
]) > 0));
21362150

2137-
b("File \"bs_list_test.ml\", line 305, characters 4-11", +(Belt_List.cmpByLength(/* :: */[
2151+
b("File \"bs_list_test.ml\", line 309, characters 4-11", +(Belt_List.cmpByLength(/* :: */[
21382152
1,
21392153
/* [] */0
21402154
], /* :: */[
@@ -2145,7 +2159,7 @@ b("File \"bs_list_test.ml\", line 305, characters 4-11", +(Belt_List.cmpByLength
21452159
]
21462160
]) < 0));
21472161

2148-
b("File \"bs_list_test.ml\", line 306, characters 4-11", +(Belt_List.cmpByLength(/* :: */[
2162+
b("File \"bs_list_test.ml\", line 310, characters 4-11", +(Belt_List.cmpByLength(/* :: */[
21492163
1,
21502164
/* :: */[
21512165
3,
@@ -2167,7 +2181,7 @@ makeTest(2);
21672181

21682182
makeTest(3);
21692183

2170-
b("File \"bs_list_test.ml\", line 316, characters 4-11", 1 - Belt_List.eq(/* :: */[
2184+
b("File \"bs_list_test.ml\", line 320, characters 4-11", 1 - Belt_List.eq(/* :: */[
21712185
1,
21722186
/* :: */[
21732187
2,
@@ -2186,7 +2200,7 @@ b("File \"bs_list_test.ml\", line 316, characters 4-11", 1 - Belt_List.eq(/* ::
21862200
return +(x === y);
21872201
})));
21882202

2189-
b("File \"bs_list_test.ml\", line 317, characters 4-11", Belt_List.eq(/* :: */[
2203+
b("File \"bs_list_test.ml\", line 321, characters 4-11", Belt_List.eq(/* :: */[
21902204
1,
21912205
/* :: */[
21922206
2,
@@ -2208,7 +2222,7 @@ b("File \"bs_list_test.ml\", line 317, characters 4-11", Belt_List.eq(/* :: */[
22082222
return +(x === y);
22092223
})));
22102224

2211-
b("File \"bs_list_test.ml\", line 318, characters 4-11", 1 - Belt_List.eq(/* :: */[
2225+
b("File \"bs_list_test.ml\", line 322, characters 4-11", 1 - Belt_List.eq(/* :: */[
22122226
1,
22132227
/* :: */[
22142228
2,
@@ -2230,7 +2244,7 @@ b("File \"bs_list_test.ml\", line 318, characters 4-11", 1 - Belt_List.eq(/* ::
22302244
return +(x === y);
22312245
})));
22322246

2233-
b("File \"bs_list_test.ml\", line 319, characters 4-11", 1 - Belt_List.eq(/* :: */[
2247+
b("File \"bs_list_test.ml\", line 323, characters 4-11", 1 - Belt_List.eq(/* :: */[
22342248
1,
22352249
/* :: */[
22362250
2,
@@ -2265,7 +2279,7 @@ var u1 = Belt_List.keepMap(u0, (function (x) {
22652279
}
22662280
}));
22672281

2268-
eq("File \"bs_list_test.ml\", line 324, characters 5-12", u1, /* :: */[
2282+
eq("File \"bs_list_test.ml\", line 327, characters 5-12", u1, /* :: */[
22692283
1,
22702284
/* :: */[
22712285
8,
@@ -2276,7 +2290,7 @@ eq("File \"bs_list_test.ml\", line 324, characters 5-12", u1, /* :: */[
22762290
]
22772291
]);
22782292

2279-
b("File \"bs_list_test.ml\", line 325, characters 4-11", Caml_obj.caml_equal(Belt_List.keepMap(/* :: */[
2293+
b("File \"bs_list_test.ml\", line 328, characters 4-11", Caml_obj.caml_equal(Belt_List.keepMap(/* :: */[
22802294
1,
22812295
/* :: */[
22822296
2,
@@ -2302,7 +2316,7 @@ b("File \"bs_list_test.ml\", line 325, characters 4-11", Caml_obj.caml_equal(Bel
23022316
]
23032317
]));
23042318

2305-
b("File \"bs_list_test.ml\", line 329, characters 4-11", +(Belt_List.keepMap(/* :: */[
2319+
b("File \"bs_list_test.ml\", line 332, characters 4-11", +(Belt_List.keepMap(/* :: */[
23062320
1,
23072321
/* :: */[
23082322
2,

jscomp/test/bs_list_test.ml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ let () =
289289
let makeTest n =
290290
eq __LOC__ (N.make n 3) (N.makeBy n (fun _ -> 3))
291291

292-
292+
let () =
293+
eq __LOC__
294+
([]
295+
|. N.add 3
296+
|. N.add 2 ) [2;3]
293297
let () =
294298

295299
b __LOC__ (N.cmp [1;2;3] [0;1;2;3] compare > 0) ;
@@ -319,13 +323,12 @@ let () =
319323
b __LOC__ (not @@ N.eq [1;2;3] [1;2;3;4] (=))
320324
let () =
321325
let u0 = N.makeBy 20 (fun x -> x) in
322-
let u1 = N.keepMap u0
323-
(fun x -> if x mod 7 = 0 then Some (x+1) else None) in
326+
let u1 = u0 |. N.keepMap (fun x -> if x mod 7 = 0 then Some (x+1) else None) in
324327
eq __LOC__ u1 [1;8;15];
325328
b __LOC__
326-
N.( keepMap [1;2;3;4] (fun x -> if x mod 2 = 0 then Some (-x ) else None)
327-
=
328-
[-2;-4]);
329+
N.(
330+
331+
[1;2;3;4] |. keepMap (fun x -> if x mod 2 = 0 then Some (-x ) else None) = [-2;-4]);
329332
b __LOC__ (
330333
N.keepMap [1;2;3;4] (fun x -> if x mod 5 = 0 then Some x else None) = []
331334
)

jscomp/test/pipe_syntax.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
var Curry = require("../../lib/js/curry.js");
4+
5+
function t0(x, f) {
6+
return Curry._1(f, Curry._1(f, Curry._1(f, x)));
7+
}
8+
9+
function t1(x, f) {
10+
return Curry._1(f, x);
11+
}
12+
13+
function t2(x, f, g) {
14+
return Curry._2(f, Curry._3(g, Curry._1(f, x), x, x), x);
15+
}
16+
17+
function t3(x, f) {
18+
return Curry._3(f, x, 1, 2);
19+
}
20+
21+
exports.t0 = t0;
22+
exports.t1 = t1;
23+
exports.t2 = t2;
24+
exports.t3 = t3;
25+
/* No side effect */

jscomp/test/pipe_syntax.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
include (
3+
struct
4+
5+
let t0 x f =
6+
x |. f |. f |. f
7+
8+
let t1 x f =
9+
x |. f
10+
11+
let t2 x f g =
12+
x |. f |. g x x |. f x
13+
14+
let t3 x f =
15+
x |. f ~h:1 ~x:2
16+
end : sig
17+
18+
val t0 : 'a -> ('a -> 'a) -> 'a
19+
val t1 : 'a -> ('a -> 'b) -> 'b
20+
val t2 : 'a -> ('a -> 'a -> 'b) -> (('a -> 'b) -> 'a -> 'a -> 'a) -> 'b
21+
val t3 : 'a -> ('a -> h:int -> x:int -> 'b) -> 'b
22+
end
23+
)

0 commit comments

Comments
 (0)