Skip to content

Commit 02ecdce

Browse files
committed
Proof of Concept: explore per-file custom infix operators.
Add support for `@@infix.add` and `@@infix.remove`.
1 parent f66a7c1 commit 02ecdce

File tree

9 files changed

+60
-8
lines changed

9 files changed

+60
-8
lines changed

res_syntax/src/res_core.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6420,7 +6420,7 @@ and parseStandaloneAttribute p =
64206420
p.uncurried_config <- Res_uncurried.Default;
64216421
attrId
64226422
| "toUncurried" -> {attrId with txt = "uncurried"}
6423-
| "infix" -> (
6423+
| "infix" | "infix.add" | "infix.remove" -> (
64246424
match payload with
64256425
| PStr
64266426
[
@@ -6441,9 +6441,12 @@ and parseStandaloneAttribute p =
64416441
_ );
64426442
};
64436443
] ->
6444-
p.scanner.customInfix <-
6445-
p.scanner.customInfix |> Res_custom_infix.addSymbol ~name ~alias;
6446-
attrId
6444+
if attrId.txt <> "infix.add" then
6445+
p.scanner.customInfix <-
6446+
p.scanner.customInfix |> Res_custom_infix.addSymbol ~name ~alias;
6447+
6448+
if attrId.txt = "infix.remove" then attrId
6449+
else {attrId with txt = "infix"}
64476450
| _ ->
64486451
Parser.err ~startPos ~endPos:p.startPos p
64496452
(Diagnostics.message

res_syntax/src/res_custom_infix.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ let addSymbol ~name ~alias x = (name, alias) :: x
44

55
let findAlias ~alias x = x |> List.find_opt (fun (_, a) -> alias = a)
66

7+
let removeName ~name x = x |> List.filter (fun (n, _) -> n <> name)
8+
79
let lookupName ~name ~src ~srcLen ~offset =
810
let nameLen = String.length name in
911
let restLen = srcLen - offset in

res_syntax/src/res_printer.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5279,7 +5279,7 @@ and printAttribute ?(standalone = false) ~state
52795279
| "toUncurried" ->
52805280
state.uncurried_config <- Res_uncurried.Default;
52815281
{id with txt = "uncurried"}
5282-
| "infix" -> (
5282+
| "infix" | "infix.add" | "infix.remove" -> (
52835283
match payload with
52845284
| PStr
52855285
[
@@ -5303,9 +5303,13 @@ and printAttribute ?(standalone = false) ~state
53035303
_ );
53045304
};
53055305
] ->
5306-
state.customInfix <-
5307-
state.customInfix |> Res_custom_infix.addSymbol ~name ~alias;
5308-
id
5306+
if id.txt = "infix.remove" then
5307+
state.customInfix <-
5308+
state.customInfix |> Res_custom_infix.removeName ~name
5309+
else
5310+
state.customInfix <-
5311+
state.customInfix |> Res_custom_infix.addSymbol ~name ~alias;
5312+
if id.txt = "infix.add" then {id with txt = "infix"} else id
53095313
| _ -> (* should never happpen *) id)
53105314
| _ -> id
53115315
in
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let plus = (x, y) => x + y
2+
let minus = (x, y) => x - y
3+
4+
@@infix(("😀", "plus"))
5+
@@infix(("💩💩", "minus"))
6+
7+
let q = 3 😀 4 💩💩 5
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let plus = (x, y) => x + y
2+
let minus = (x, y) => x - y
3+
4+
@@infix.remove(("😀", "plus"))
5+
@@infix.remove(("💩💩", "minus"))
6+
7+
let q = minus(plus(3, 4), 5)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module M = {
2+
@@infix(("+++", "+"))
3+
4+
let z = 3 +++ 4
5+
}
6+
7+
let z = 3 + 4
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let plus = (x, y) => x + y
2+
let minus = (x, y) => x - y
3+
4+
@@infix.add(("😀", "plus"))
5+
@@infix.add(("💩💩", "minus"))
6+
7+
let q = minus(plus(3, 4), 5)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let plus = (x, y) => x + y
2+
let minus = (x, y) => x - y
3+
4+
@@infix.remove(("😀", "plus"))
5+
@@infix.remove(("💩💩", "minus"))
6+
7+
let q = 3 😀 4 💩💩 5
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
module M = {
3+
@@infix.add(("+++", "+"))
4+
5+
let z = 3 + 4
6+
}
7+
8+
let z = 3 + 4

0 commit comments

Comments
 (0)