Skip to content

Commit 1295561

Browse files
committed
Add support for unary uncurried pipe in uncurried mode
In normal mode, there is syntax for unary curried pipe (`x->f`) but not for unary uncurried. In uncurried mode, after this PR, unary uncurried pipe is supported in uncurried mode (still `x->f`).
1 parent a233b08 commit 1295561

File tree

10 files changed

+72
-13
lines changed

10 files changed

+72
-13
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- Introduce experimental uncurried by default mode. Can be turned on mid-file by adding standalone annotation `@@uncurried`. For experimentation only. https://github.com/rescript-lang/rescript-compiler/pull/5796
1818
- Adding `@@toUncurried` to the file and reformat will convert to uncurried syntax https://github.com/rescript-lang/rescript-compiler/pull/5800
19+
- Add support for unary uncurried pipe in uncurried mode
1920

2021
#### :boom: Breaking Change
2122

jscomp/frontend/ast_exp_apply.ml

+12-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
7070
| _ -> None
7171

7272
let inner_ops = [ "##"; "#@" ]
73-
let infix_ops = [ "|."; "#="; "##" ]
73+
let infix_ops = [ "|."; "|.u"; "#="; "##" ]
7474

7575
let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
7676
(args : Ast_compatible.args) : exp =
@@ -95,7 +95,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
9595
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
9696
| None -> (
9797
match view_as_app e infix_ops with
98-
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
98+
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
9999
(*
100100
a |. f
101101
a |. f b c [@bs] --> f a b c [@bs]
@@ -178,6 +178,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
178178
pexp_loc = e.pexp_loc;
179179
pexp_attributes = e.pexp_attributes @ other_attributes;
180180
}
181+
| _ when op = "|.u" ->
182+
(* a |.u f
183+
Uncurried unary application *)
184+
{
185+
pexp_desc =
186+
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
187+
[ (Nolabel, a) ];
188+
pexp_loc = e.pexp_loc;
189+
pexp_attributes = e.pexp_attributes;
190+
}
181191
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
182192
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
183193
(* - obj##property

jscomp/test/uncurried_pipe.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ var v27 = add(20, 7);
1717

1818
var v37 = 30 + add(3, 4) | 0;
1919

20+
function unary(x) {
21+
return x + 1 | 0;
22+
}
23+
2024
var StandardNotation = {
2125
add: add,
2226
addC: addC,
2327
v7: v7,
2428
v17: v17,
2529
v27: v27,
26-
v37: v37
30+
v37: v37,
31+
unary: unary
2732
};
2833

2934
var v7$1 = add(3, 4);
@@ -34,9 +39,12 @@ var v27$1 = add(20, 7);
3439

3540
var v37$1 = 30 + add(3, 4) | 0;
3641

42+
var v100 = unary(99);
43+
3744
exports.StandardNotation = StandardNotation;
3845
exports.v7 = v7$1;
3946
exports.v17 = v17$1;
4047
exports.v27 = v27$1;
4148
exports.v37 = v37$1;
49+
exports.v100 = v100;
4250
/* v7 Not a pure module */

jscomp/test/uncurried_pipe.res

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module StandardNotation = {
66
let v17 = 10->add(. 3->add(. 4))
77
let v27 = 20->add(. 3->addC(4))
88
let v37 = 30->addC(3->add(. 4))
9+
10+
let unary = (. x) => x + 1
911
}
1012

1113
@@uncurried
@@ -16,3 +18,5 @@ let v7 = 3->add(4)
1618
let v17 = 10->add(3->add(4))
1719
let v27 = 20->add(3->addC(. 4))
1820
let v37 = 30->addC(. 3->add(4))
21+
22+
let v100 = 99->unary

lib/4.06.1/unstable/js_compiler.ml

+12-2
Original file line numberDiff line numberDiff line change
@@ -150303,7 +150303,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
150303150303
| _ -> None
150304150304

150305150305
let inner_ops = [ "##"; "#@" ]
150306-
let infix_ops = [ "|."; "#="; "##" ]
150306+
let infix_ops = [ "|."; "|.u"; "#="; "##" ]
150307150307

150308150308
let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150309150309
(args : Ast_compatible.args) : exp =
@@ -150328,7 +150328,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150328150328
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
150329150329
| None -> (
150330150330
match view_as_app e infix_ops with
150331-
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
150331+
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
150332150332
(*
150333150333
a |. f
150334150334
a |. f b c [@bs] --> f a b c [@bs]
@@ -150411,6 +150411,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150411150411
pexp_loc = e.pexp_loc;
150412150412
pexp_attributes = e.pexp_attributes @ other_attributes;
150413150413
}
150414+
| _ when op = "|.u" ->
150415+
(* a |.u f
150416+
Uncurried unary application *)
150417+
{
150418+
pexp_desc =
150419+
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
150420+
[ (Nolabel, a) ];
150421+
pexp_loc = e.pexp_loc;
150422+
pexp_attributes = e.pexp_attributes;
150423+
}
150414150424
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
150415150425
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
150416150426
(* - obj##property

lib/4.06.1/unstable/js_playground_compiler.ml

+14-3
Original file line numberDiff line numberDiff line change
@@ -150303,7 +150303,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
150303150303
| _ -> None
150304150304

150305150305
let inner_ops = [ "##"; "#@" ]
150306-
let infix_ops = [ "|."; "#="; "##" ]
150306+
let infix_ops = [ "|."; "|.u"; "#="; "##" ]
150307150307

150308150308
let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150309150309
(args : Ast_compatible.args) : exp =
@@ -150328,7 +150328,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150328150328
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
150329150329
| None -> (
150330150330
match view_as_app e infix_ops with
150331-
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
150331+
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
150332150332
(*
150333150333
a |. f
150334150334
a |. f b c [@bs] --> f a b c [@bs]
@@ -150411,6 +150411,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150411150411
pexp_loc = e.pexp_loc;
150412150412
pexp_attributes = e.pexp_attributes @ other_attributes;
150413150413
}
150414+
| _ when op = "|.u" ->
150415+
(* a |.u f
150416+
Uncurried unary application *)
150417+
{
150418+
pexp_desc =
150419+
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
150420+
[ (Nolabel, a) ];
150421+
pexp_loc = e.pexp_loc;
150422+
pexp_attributes = e.pexp_attributes;
150423+
}
150414150424
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
150415150425
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
150416150426
(* - obj##property
@@ -162691,7 +162701,8 @@ let buildLongident words =
162691162701

162692162702
let makeInfixOperator p token startPos endPos =
162693162703
let stringifiedToken =
162694-
if token = Token.MinusGreater then "|."
162704+
if token = Token.MinusGreater then
162705+
if p.Parser.uncurried_by_default then "|.u" else "|."
162695162706
else if token = Token.PlusPlus then "^"
162696162707
else if token = Token.BangEqual then "<>"
162697162708
else if token = Token.BangEqualEqual then "!="

lib/4.06.1/whole_compiler.ml

+14-3
Original file line numberDiff line numberDiff line change
@@ -160587,7 +160587,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
160587160587
| _ -> None
160588160588

160589160589
let inner_ops = [ "##"; "#@" ]
160590-
let infix_ops = [ "|."; "#="; "##" ]
160590+
let infix_ops = [ "|."; "|.u"; "#="; "##" ]
160591160591

160592160592
let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
160593160593
(args : Ast_compatible.args) : exp =
@@ -160612,7 +160612,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
160612160612
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
160613160613
| None -> (
160614160614
match view_as_app e infix_ops with
160615-
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
160615+
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
160616160616
(*
160617160617
a |. f
160618160618
a |. f b c [@bs] --> f a b c [@bs]
@@ -160695,6 +160695,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
160695160695
pexp_loc = e.pexp_loc;
160696160696
pexp_attributes = e.pexp_attributes @ other_attributes;
160697160697
}
160698+
| _ when op = "|.u" ->
160699+
(* a |.u f
160700+
Uncurried unary application *)
160701+
{
160702+
pexp_desc =
160703+
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
160704+
[ (Nolabel, a) ];
160705+
pexp_loc = e.pexp_loc;
160706+
pexp_attributes = e.pexp_attributes;
160707+
}
160698160708
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
160699160709
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
160700160710
(* - obj##property
@@ -176123,7 +176133,8 @@ let buildLongident words =
176123176133

176124176134
let makeInfixOperator p token startPos endPos =
176125176135
let stringifiedToken =
176126-
if token = Token.MinusGreater then "|."
176136+
if token = Token.MinusGreater then
176137+
if p.Parser.uncurried_by_default then "|.u" else "|."
176127176138
else if token = Token.PlusPlus then "^"
176128176139
else if token = Token.BangEqual then "<>"
176129176140
else if token = Token.BangEqualEqual then "!="

res_syntax/src/res_core.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ let buildLongident words =
378378

379379
let makeInfixOperator p token startPos endPos =
380380
let stringifiedToken =
381-
if token = Token.MinusGreater then "|."
381+
if token = Token.MinusGreater then
382+
if p.Parser.uncurried_by_default then "|.u" else "|."
382383
else if token = Token.PlusPlus then "^"
383384
else if token = Token.BangEqual then "<>"
384385
else if token = Token.BangEqualEqual then "!="

res_syntax/tests/parsing/grammar/expressions/UncurriedByDefault.res

+2
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ type mixTyp = (.string) => (string, string) => (.string, string, string, string)
3434
type bTyp = string => (. string) => int
3535
type cTyp2 = (.string, string) => int
3636
type uTyp2 = (string, string) => int
37+
38+
let pipe1 = 3->f

res_syntax/tests/parsing/grammar/expressions/expected/UncurriedByDefault.res.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ type nonrec mixTyp =
5151
Js.Fn.arity2
5252
type nonrec bTyp = (string -> string -> int) Js.Fn.arity1
5353
type nonrec cTyp2 = string -> string -> int
54-
type nonrec uTyp2 = (string -> string -> int) Js.Fn.arity2
54+
type nonrec uTyp2 = (string -> string -> int) Js.Fn.arity2
55+
let pipe1 = 3 |.u f

0 commit comments

Comments
 (0)