Skip to content

Add support for unary uncurried pipe in uncurried mode #5804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- 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
- Adding `@@toUncurried` to the file and reformat will convert to uncurried syntax https://github.com/rescript-lang/rescript-compiler/pull/5800
- Add support for unary uncurried pipe in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/5804

#### :boom: Breaking Change

Expand Down
14 changes: 12 additions & 2 deletions jscomp/frontend/ast_exp_apply.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
| _ -> None

let inner_ops = [ "##"; "#@" ]
let infix_ops = [ "|."; "#="; "##" ]
let infix_ops = [ "|."; "|.u"; "#="; "##" ]

let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
(args : Ast_compatible.args) : exp =
Expand All @@ -95,7 +95,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
| None -> (
match view_as_app e infix_ops with
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
(*
a |. f
a |. f b c [@bs] --> f a b c [@bs]
Expand Down Expand Up @@ -178,6 +178,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
pexp_loc = e.pexp_loc;
pexp_attributes = e.pexp_attributes @ other_attributes;
}
| _ when op = "|.u" ->
(* a |.u f
Uncurried unary application *)
{
pexp_desc =
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
[ (Nolabel, a) ];
pexp_loc = e.pexp_loc;
pexp_attributes = e.pexp_attributes;
}
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
(* - obj##property
Expand Down
10 changes: 9 additions & 1 deletion jscomp/test/uncurried_pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ var v27 = add(20, 7);

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

function unary(x) {
return x + 1 | 0;
}

var StandardNotation = {
add: add,
addC: addC,
v7: v7,
v17: v17,
v27: v27,
v37: v37
v37: v37,
unary: unary
};

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

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

var v100 = unary(99);

exports.StandardNotation = StandardNotation;
exports.v7 = v7$1;
exports.v17 = v17$1;
exports.v27 = v27$1;
exports.v37 = v37$1;
exports.v100 = v100;
/* v7 Not a pure module */
4 changes: 4 additions & 0 deletions jscomp/test/uncurried_pipe.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module StandardNotation = {
let v17 = 10->add(. 3->add(. 4))
let v27 = 20->add(. 3->addC(4))
let v37 = 30->addC(3->add(. 4))

let unary = (. x) => x + 1
}

@@uncurried
Expand All @@ -16,3 +18,5 @@ let v7 = 3->add(4)
let v17 = 10->add(3->add(4))
let v27 = 20->add(3->addC(. 4))
let v37 = 30->addC(. 3->add(4))

let v100 = 99->unary
39 changes: 26 additions & 13 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49796,7 +49796,7 @@ let operatorPrecedence operator =
| "+" | "+." | "-" | "-." | "^" -> 5
| "*" | "*." | "/" | "/." -> 6
| "**" -> 7
| "#" | "##" | "|." -> 8
| "#" | "##" | "|." | "|.u" -> 8
| _ -> 0

let isUnaryOperator operator =
Expand All @@ -49818,7 +49818,7 @@ let isBinaryOperator operator =
match operator with
| ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">="
| "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "|."
| "<>" ->
| "|.u" | "<>" ->
true
| _ -> false

Expand Down Expand Up @@ -50184,14 +50184,14 @@ let isSinglePipeExpr expr =
let isPipeExpr expr =
match expr.pexp_desc with
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|>")}},
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")}},
[(Nolabel, _operand1); (Nolabel, _operand2)] ) ->
true
| _ -> false
in
match expr.pexp_desc with
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|>")}},
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")}},
[(Nolabel, operand1); (Nolabel, _operand2)] )
when not (isPipeExpr operand1) ->
true
Expand Down Expand Up @@ -51499,7 +51499,7 @@ and walkExpression expr t comments =
Longident.Lident
( ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!=="
| "<=" | ">=" | "|>" | "+" | "+." | "-" | "-." | "++" | "^"
| "*" | "*." | "/" | "/." | "**" | "|." | "<>" );
| "*" | "*." | "/" | "/." | "**" | "|." | "|.u" | "<>" );
};
},
[(Nolabel, operand1); (Nolabel, operand2)] ) ->
Expand Down Expand Up @@ -56548,7 +56548,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
let printBinaryOperator ~inlineRhs operator =
let operatorTxt =
match operator with
| "|." -> "->"
| "|." | "|.u" -> "->"
| "^" -> "++"
| "=" -> "=="
| "==" -> "==="
Expand All @@ -56557,12 +56557,12 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
| txt -> txt
in
let spacingBeforeOperator =
if operator = "|." then Doc.softLine
if operator = "|." || operator = "|.u" then Doc.softLine
else if operator = "|>" then Doc.line
else Doc.space
in
let spacingAfterOperator =
if operator = "|." then Doc.nil
if operator = "|." || operator = "|.u" then Doc.nil
else if operator = "|>" then Doc.space
else if inlineRhs then Doc.space
else Doc.line
Expand Down Expand Up @@ -56712,7 +56712,10 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
in
match expr.pexp_desc with
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Longident.Lident (("|." | "|>") as op)}},
( {
pexp_desc =
Pexp_ident {txt = Longident.Lident (("|." | "|.u" | "|>") as op)};
},
[(Nolabel, lhs); (Nolabel, rhs)] )
when not
(ParsetreeViewer.isBinaryExpression lhs
Expand All @@ -56727,8 +56730,8 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
printAttributes ~state expr.pexp_attributes cmtTbl;
lhsDoc;
(match (lhsHasCommentBelow, op) with
| true, "|." -> Doc.concat [Doc.softLine; Doc.text "->"]
| false, "|." -> Doc.text "->"
| true, ("|." | "|.u") -> Doc.concat [Doc.softLine; Doc.text "->"]
| false, ("|." | "|.u") -> Doc.text "->"
| true, "|>" -> Doc.concat [Doc.line; Doc.text "|> "]
| false, "|>" -> Doc.text " |> "
| _ -> Doc.nil);
Expand Down Expand Up @@ -150303,7 +150306,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
| _ -> None

let inner_ops = [ "##"; "#@" ]
let infix_ops = [ "|."; "#="; "##" ]
let infix_ops = [ "|."; "|.u"; "#="; "##" ]

let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
(args : Ast_compatible.args) : exp =
Expand All @@ -150328,7 +150331,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
| None -> (
match view_as_app e infix_ops with
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
(*
a |. f
a |. f b c [@bs] --> f a b c [@bs]
Expand Down Expand Up @@ -150411,6 +150414,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
pexp_loc = e.pexp_loc;
pexp_attributes = e.pexp_attributes @ other_attributes;
}
| _ when op = "|.u" ->
(* a |.u f
Uncurried unary application *)
{
pexp_desc =
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
[ (Nolabel, a) ];
pexp_loc = e.pexp_loc;
pexp_attributes = e.pexp_attributes;
}
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
(* - obj##property
Expand Down
42 changes: 28 additions & 14 deletions lib/4.06.1/unstable/js_playground_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49796,7 +49796,7 @@ let operatorPrecedence operator =
| "+" | "+." | "-" | "-." | "^" -> 5
| "*" | "*." | "/" | "/." -> 6
| "**" -> 7
| "#" | "##" | "|." -> 8
| "#" | "##" | "|." | "|.u" -> 8
| _ -> 0

let isUnaryOperator operator =
Expand All @@ -49818,7 +49818,7 @@ let isBinaryOperator operator =
match operator with
| ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">="
| "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "|."
| "<>" ->
| "|.u" | "<>" ->
true
| _ -> false

Expand Down Expand Up @@ -50184,14 +50184,14 @@ let isSinglePipeExpr expr =
let isPipeExpr expr =
match expr.pexp_desc with
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|>")}},
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")}},
[(Nolabel, _operand1); (Nolabel, _operand2)] ) ->
true
| _ -> false
in
match expr.pexp_desc with
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|>")}},
( {pexp_desc = Pexp_ident {txt = Longident.Lident ("|." | "|.u" | "|>")}},
[(Nolabel, operand1); (Nolabel, _operand2)] )
when not (isPipeExpr operand1) ->
true
Expand Down Expand Up @@ -51499,7 +51499,7 @@ and walkExpression expr t comments =
Longident.Lident
( ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!=="
| "<=" | ">=" | "|>" | "+" | "+." | "-" | "-." | "++" | "^"
| "*" | "*." | "/" | "/." | "**" | "|." | "<>" );
| "*" | "*." | "/" | "/." | "**" | "|." | "|.u" | "<>" );
};
},
[(Nolabel, operand1); (Nolabel, operand2)] ) ->
Expand Down Expand Up @@ -56548,7 +56548,7 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
let printBinaryOperator ~inlineRhs operator =
let operatorTxt =
match operator with
| "|." -> "->"
| "|." | "|.u" -> "->"
| "^" -> "++"
| "=" -> "=="
| "==" -> "==="
Expand All @@ -56557,12 +56557,12 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
| txt -> txt
in
let spacingBeforeOperator =
if operator = "|." then Doc.softLine
if operator = "|." || operator = "|.u" then Doc.softLine
else if operator = "|>" then Doc.line
else Doc.space
in
let spacingAfterOperator =
if operator = "|." then Doc.nil
if operator = "|." || operator = "|.u" then Doc.nil
else if operator = "|>" then Doc.space
else if inlineRhs then Doc.space
else Doc.line
Expand Down Expand Up @@ -56712,7 +56712,10 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
in
match expr.pexp_desc with
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Longident.Lident (("|." | "|>") as op)}},
( {
pexp_desc =
Pexp_ident {txt = Longident.Lident (("|." | "|.u" | "|>") as op)};
},
[(Nolabel, lhs); (Nolabel, rhs)] )
when not
(ParsetreeViewer.isBinaryExpression lhs
Expand All @@ -56727,8 +56730,8 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
printAttributes ~state expr.pexp_attributes cmtTbl;
lhsDoc;
(match (lhsHasCommentBelow, op) with
| true, "|." -> Doc.concat [Doc.softLine; Doc.text "->"]
| false, "|." -> Doc.text "->"
| true, ("|." | "|.u") -> Doc.concat [Doc.softLine; Doc.text "->"]
| false, ("|." | "|.u") -> Doc.text "->"
| true, "|>" -> Doc.concat [Doc.line; Doc.text "|> "]
| false, "|>" -> Doc.text " |> "
| _ -> Doc.nil);
Expand Down Expand Up @@ -150303,7 +150306,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
| _ -> None

let inner_ops = [ "##"; "#@" ]
let infix_ops = [ "|."; "#="; "##" ]
let infix_ops = [ "|."; "|.u"; "#="; "##" ]

let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
(args : Ast_compatible.args) : exp =
Expand All @@ -150328,7 +150331,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
| None -> (
match view_as_app e infix_ops with
| Some { op = "|."; args = [ a_; f_ ]; loc } -> (
| Some { op = ("|." | "|.u") as op; args = [ a_; f_ ]; loc } -> (
(*
a |. f
a |. f b c [@bs] --> f a b c [@bs]
Expand Down Expand Up @@ -150411,6 +150414,16 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) (fn : exp)
pexp_loc = e.pexp_loc;
pexp_attributes = e.pexp_attributes @ other_attributes;
}
| _ when op = "|.u" ->
(* a |.u f
Uncurried unary application *)
{
pexp_desc =
Ast_uncurry_apply.uncurry_fn_apply e.pexp_loc self f
[ (Nolabel, a) ];
pexp_loc = e.pexp_loc;
pexp_attributes = e.pexp_attributes;
}
| _ -> Ast_compatible.app1 ~loc ~attrs:e.pexp_attributes f a))
| Some { op = "##"; loc; args = [ obj; rest ] } -> (
(* - obj##property
Expand Down Expand Up @@ -162691,7 +162704,8 @@ let buildLongident words =

let makeInfixOperator p token startPos endPos =
let stringifiedToken =
if token = Token.MinusGreater then "|."
if token = Token.MinusGreater then
if p.Parser.uncurried_by_default then "|.u" else "|."
else if token = Token.PlusPlus then "^"
else if token = Token.BangEqual then "<>"
else if token = Token.BangEqualEqual then "!="
Expand Down
Loading