Skip to content

Commit e07496a

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 e07496a

15 files changed

+136
-57
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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 https://github.com/rescript-lang/rescript-compiler/pull/5804
1920

2021
#### :boom: Breaking Change
2122

jscomp/frontend/ast_exp_apply.ml

Lines changed: 12 additions & 2 deletions
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

Lines changed: 9 additions & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49796,7 +49796,7 @@ let operatorPrecedence operator =
4979649796
| "+" | "+." | "-" | "-." | "^" -> 5
4979749797
| "*" | "*." | "/" | "/." -> 6
4979849798
| "**" -> 7
49799-
| "#" | "##" | "|." -> 8
49799+
| "#" | "##" | "|." | "|.u" -> 8
4980049800
| _ -> 0
4980149801

4980249802
let isUnaryOperator operator =
@@ -49818,7 +49818,7 @@ let isBinaryOperator operator =
4981849818
match operator with
4981949819
| ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">="
4982049820
| "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "|."
49821-
| "<>" ->
49821+
| "|.u" | "<>" ->
4982249822
true
4982349823
| _ -> false
4982449824

@@ -50184,14 +50184,14 @@ let isSinglePipeExpr expr =
5018450184
let isPipeExpr expr =
5018550185
match expr.pexp_desc with
5018650186
| Pexp_apply
50187-
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|>")}},
50187+
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")}},
5018850188
[(Nolabel, _operand1); (Nolabel, _operand2)] ) ->
5018950189
true
5019050190
| _ -> false
5019150191
in
5019250192
match expr.pexp_desc with
5019350193
| Pexp_apply
50194-
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|>")}},
50194+
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")}},
5019550195
[(Nolabel, operand1); (Nolabel, _operand2)] )
5019650196
when not (isPipeExpr operand1) ->
5019750197
true
@@ -51499,7 +51499,7 @@ and walkExpression expr t comments =
5149951499
Longident.Lident
5150051500
( ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!=="
5150151501
| "<=" | ">=" | "|>" | "+" | "+." | "-" | "-." | "++" | "^"
51502-
| "*" | "*." | "/" | "/." | "**" | "|." | "<>" );
51502+
| "*" | "*." | "/" | "/." | "**" | "|." | "|.u" | "<>" );
5150351503
};
5150451504
},
5150551505
[(Nolabel, operand1); (Nolabel, operand2)] ) ->
@@ -56548,7 +56548,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
5654856548
let printBinaryOperator ~inlineRhs operator =
5654956549
let operatorTxt =
5655056550
match operator with
56551-
| "|." -> "->"
56551+
| "|." | "|.u" -> "->"
5655256552
| "^" -> "++"
5655356553
| "=" -> "=="
5655456554
| "==" -> "==="
@@ -56557,12 +56557,12 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
5655756557
| txt -> txt
5655856558
in
5655956559
let spacingBeforeOperator =
56560-
if operator = "|." then Doc.softLine
56560+
if operator = "|." || operator = "|.u" then Doc.softLine
5656156561
else if operator = "|>" then Doc.line
5656256562
else Doc.space
5656356563
in
5656456564
let spacingAfterOperator =
56565-
if operator = "|." then Doc.nil
56565+
if operator = "|." || operator = "|.u" then Doc.nil
5656656566
else if operator = "|>" then Doc.space
5656756567
else if inlineRhs then Doc.space
5656856568
else Doc.line
@@ -56712,7 +56712,10 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
5671256712
in
5671356713
match expr.pexp_desc with
5671456714
| Pexp_apply
56715-
( {pexp_desc = Pexp_ident {txt = Longident.Lident (("|." | "|>") as op)}},
56715+
( {
56716+
pexp_desc =
56717+
Pexp_ident {txt = Longident.Lident (("|." | "|.u" | "|>") as op)};
56718+
},
5671656719
[(Nolabel, lhs); (Nolabel, rhs)] )
5671756720
when not
5671856721
(ParsetreeViewer.isBinaryExpression lhs
@@ -56727,8 +56730,8 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
5672756730
printAttributes ~state expr.pexp_attributes cmtTbl;
5672856731
lhsDoc;
5672956732
(match (lhsHasCommentBelow, op) with
56730-
| true, "|." -> Doc.concat [Doc.softLine; Doc.text "->"]
56731-
| false, "|." -> Doc.text "->"
56733+
| true, ("|." | "|.u") -> Doc.concat [Doc.softLine; Doc.text "->"]
56734+
| false, ("|." | "|.u") -> Doc.text "->"
5673256735
| true, "|>" -> Doc.concat [Doc.line; Doc.text "|> "]
5673356736
| false, "|>" -> Doc.text " |> "
5673456737
| _ -> Doc.nil);
@@ -150303,7 +150306,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
150303150306
| _ -> None
150304150307

150305150308
let inner_ops = [ "##"; "#@" ]
150306-
let infix_ops = [ "|."; "#="; "##" ]
150309+
let infix_ops = [ "|."; "|.u"; "#="; "##" ]
150307150310

150308150311
let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150309150312
(args : Ast_compatible.args) : exp =
@@ -150328,7 +150331,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150328150331
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
150329150332
| None -> (
150330150333
match view_as_app e infix_ops with
150331-
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
150334+
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
150332150335
(*
150333150336
a |. f
150334150337
a |. f b c [@bs] --> f a b c [@bs]
@@ -150411,6 +150414,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150411150414
pexp_loc = e.pexp_loc;
150412150415
pexp_attributes = e.pexp_attributes @ other_attributes;
150413150416
}
150417+
| _ when op = "|.u" ->
150418+
(* a |.u f
150419+
Uncurried unary application *)
150420+
{
150421+
pexp_desc =
150422+
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
150423+
[ (Nolabel, a) ];
150424+
pexp_loc = e.pexp_loc;
150425+
pexp_attributes = e.pexp_attributes;
150426+
}
150414150427
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
150415150428
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
150416150429
(* - obj##property

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49796,7 +49796,7 @@ let operatorPrecedence operator =
4979649796
| "+" | "+." | "-" | "-." | "^" -> 5
4979749797
| "*" | "*." | "/" | "/." -> 6
4979849798
| "**" -> 7
49799-
| "#" | "##" | "|." -> 8
49799+
| "#" | "##" | "|." | "|.u" -> 8
4980049800
| _ -> 0
4980149801

4980249802
let isUnaryOperator operator =
@@ -49818,7 +49818,7 @@ let isBinaryOperator operator =
4981849818
match operator with
4981949819
| ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">="
4982049820
| "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "|."
49821-
| "<>" ->
49821+
| "|.u" | "<>" ->
4982249822
true
4982349823
| _ -> false
4982449824

@@ -50184,14 +50184,14 @@ let isSinglePipeExpr expr =
5018450184
let isPipeExpr expr =
5018550185
match expr.pexp_desc with
5018650186
| Pexp_apply
50187-
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|>")}},
50187+
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")}},
5018850188
[(Nolabel, _operand1); (Nolabel, _operand2)] ) ->
5018950189
true
5019050190
| _ -> false
5019150191
in
5019250192
match expr.pexp_desc with
5019350193
| Pexp_apply
50194-
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|>")}},
50194+
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")}},
5019550195
[(Nolabel, operand1); (Nolabel, _operand2)] )
5019650196
when not (isPipeExpr operand1) ->
5019750197
true
@@ -51499,7 +51499,7 @@ and walkExpression expr t comments =
5149951499
Longident.Lident
5150051500
( ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!=="
5150151501
| "<=" | ">=" | "|>" | "+" | "+." | "-" | "-." | "++" | "^"
51502-
| "*" | "*." | "/" | "/." | "**" | "|." | "<>" );
51502+
| "*" | "*." | "/" | "/." | "**" | "|." | "|.u" | "<>" );
5150351503
};
5150451504
},
5150551505
[(Nolabel, operand1); (Nolabel, operand2)] ) ->
@@ -56548,7 +56548,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
5654856548
let printBinaryOperator ~inlineRhs operator =
5654956549
let operatorTxt =
5655056550
match operator with
56551-
| "|." -> "->"
56551+
| "|." | "|.u" -> "->"
5655256552
| "^" -> "++"
5655356553
| "=" -> "=="
5655456554
| "==" -> "==="
@@ -56557,12 +56557,12 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
5655756557
| txt -> txt
5655856558
in
5655956559
let spacingBeforeOperator =
56560-
if operator = "|." then Doc.softLine
56560+
if operator = "|." || operator = "|.u" then Doc.softLine
5656156561
else if operator = "|>" then Doc.line
5656256562
else Doc.space
5656356563
in
5656456564
let spacingAfterOperator =
56565-
if operator = "|." then Doc.nil
56565+
if operator = "|." || operator = "|.u" then Doc.nil
5656656566
else if operator = "|>" then Doc.space
5656756567
else if inlineRhs then Doc.space
5656856568
else Doc.line
@@ -56712,7 +56712,10 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
5671256712
in
5671356713
match expr.pexp_desc with
5671456714
| Pexp_apply
56715-
( {pexp_desc = Pexp_ident {txt = Longident.Lident (("|." | "|>") as op)}},
56715+
( {
56716+
pexp_desc =
56717+
Pexp_ident {txt = Longident.Lident (("|." | "|.u" | "|>") as op)};
56718+
},
5671656719
[(Nolabel, lhs); (Nolabel, rhs)] )
5671756720
when not
5671856721
(ParsetreeViewer.isBinaryExpression lhs
@@ -56727,8 +56730,8 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
5672756730
printAttributes ~state expr.pexp_attributes cmtTbl;
5672856731
lhsDoc;
5672956732
(match (lhsHasCommentBelow, op) with
56730-
| true, "|." -> Doc.concat [Doc.softLine; Doc.text "->"]
56731-
| false, "|." -> Doc.text "->"
56733+
| true, ("|." | "|.u") -> Doc.concat [Doc.softLine; Doc.text "->"]
56734+
| false, ("|." | "|.u") -> Doc.text "->"
5673256735
| true, "|>" -> Doc.concat [Doc.line; Doc.text "|> "]
5673356736
| false, "|>" -> Doc.text " |> "
5673456737
| _ -> Doc.nil);
@@ -150303,7 +150306,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
150303150306
| _ -> None
150304150307

150305150308
let inner_ops = [ "##"; "#@" ]
150306-
let infix_ops = [ "|."; "#="; "##" ]
150309+
let infix_ops = [ "|."; "|.u"; "#="; "##" ]
150307150310

150308150311
let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150309150312
(args : Ast_compatible.args) : exp =
@@ -150328,7 +150331,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150328150331
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
150329150332
| None -> (
150330150333
match view_as_app e infix_ops with
150331-
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
150334+
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
150332150335
(*
150333150336
a |. f
150334150337
a |. f b c [@bs] --> f a b c [@bs]
@@ -150411,6 +150414,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
150411150414
pexp_loc = e.pexp_loc;
150412150415
pexp_attributes = e.pexp_attributes @ other_attributes;
150413150416
}
150417+
| _ when op = "|.u" ->
150418+
(* a |.u f
150419+
Uncurried unary application *)
150420+
{
150421+
pexp_desc =
150422+
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
150423+
[ (Nolabel, a) ];
150424+
pexp_loc = e.pexp_loc;
150425+
pexp_attributes = e.pexp_attributes;
150426+
}
150414150427
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
150415150428
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
150416150429
(* - obj##property
@@ -162691,7 +162704,8 @@ let buildLongident words =
162691162704

162692162705
let makeInfixOperator p token startPos endPos =
162693162706
let stringifiedToken =
162694-
if token = Token.MinusGreater then "|."
162707+
if token = Token.MinusGreater then
162708+
if p.Parser.uncurried_by_default then "|.u" else "|."
162695162709
else if token = Token.PlusPlus then "^"
162696162710
else if token = Token.BangEqual then "<>"
162697162711
else if token = Token.BangEqualEqual then "!="

0 commit comments

Comments
 (0)