From 496606c977a73d4fcc66e343dfcb7b6de418901f Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 17 Mar 2023 13:59:05 +0100 Subject: [PATCH] Add support for `|>` in uncurried mode by desugaring it Desugar the semantics of `|>` in uncurried mode so existing curried code still compiles. But the code will stop using `|>` after formatting. For example: `3 |> add3(1,2)` would not compile when `add3` has 3 arguments. But after desugaring, the third argument is added. --- CHANGELOG.md | 1 + res_syntax/src/res_core.ml | 13 ++++++++++--- res_syntax/tests/printer/expr/Uncurried.res | 7 +++++++ .../tests/printer/expr/expected/Uncurried.res.txt | 7 +++++++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 res_syntax/tests/printer/expr/Uncurried.res create mode 100644 res_syntax/tests/printer/expr/expected/Uncurried.res.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 321b36a47b..079fc2d513 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,7 @@ These are only breaking changes for unformatted code. - Treat uncurried application of primitives like curried application, which produces better output https://github.com/rescript-lang/rescript-compiler/pull/5851 - New internal representation for uncurried functions using built-in type `function$` this avoids having to declare all the possible arities ahead of time https://github.com/rescript-lang/rescript-compiler/pull/5870 - PPX V3: allow uncurried `make` function and treat it like a curried one https://github.com/rescript-lang/rescript-compiler/pull/6081 +- Add support for `|>` in uncurried mode by desugaring it https://github.com/rescript-lang/rescript-compiler/pull/6083 # 10.1.4 diff --git a/res_syntax/src/res_core.ml b/res_syntax/src/res_core.ml index a08180a9e6..7a0274e3b0 100644 --- a/res_syntax/src/res_core.ml +++ b/res_syntax/src/res_core.ml @@ -2204,9 +2204,16 @@ and parseBinaryExpr ?(context = OrdinaryExpr) ?a p prec = let b = parseBinaryExpr ~context p tokenPrec in let loc = mkLoc a.Parsetree.pexp_loc.loc_start b.pexp_loc.loc_end in let expr = - Ast_helper.Exp.apply ~loc - (makeInfixOperator p token startPos endPos) - [(Nolabel, a); (Nolabel, b)] + match (token, b.pexp_desc) with + | BarGreater, Pexp_apply (funExpr, args) + when p.uncurried_config = Uncurried -> + {b with pexp_desc = Pexp_apply (funExpr, args @ [(Nolabel, a)])} + | BarGreater, _ when p.uncurried_config = Uncurried -> + Ast_helper.Exp.apply ~loc b [(Nolabel, a)] + | _ -> + Ast_helper.Exp.apply ~loc + (makeInfixOperator p token startPos endPos) + [(Nolabel, a); (Nolabel, b)] in Parser.eatBreadcrumb p; loop expr) diff --git a/res_syntax/tests/printer/expr/Uncurried.res b/res_syntax/tests/printer/expr/Uncurried.res new file mode 100644 index 0000000000..3b05c23d9f --- /dev/null +++ b/res_syntax/tests/printer/expr/Uncurried.res @@ -0,0 +1,7 @@ +@@uncurried + +let add3 = (x,y,z) => x+y+z + +let triangle = 3 |> add3(1,2) |> add3(4,5) + +let () = 3 |> ignore \ No newline at end of file diff --git a/res_syntax/tests/printer/expr/expected/Uncurried.res.txt b/res_syntax/tests/printer/expr/expected/Uncurried.res.txt new file mode 100644 index 0000000000..758fa70ba2 --- /dev/null +++ b/res_syntax/tests/printer/expr/expected/Uncurried.res.txt @@ -0,0 +1,7 @@ +@@uncurried + +let add3 = (x, y, z) => x + y + z + +let triangle = add3(4, 5, add3(1, 2, 3)) + +let () = ignore(3)