From 05ecf8ec1a33716970ebe224464da4260fad9c9b Mon Sep 17 00:00:00 2001 From: MiryangJung Date: Tue, 24 Dec 2024 17:17:52 +0900 Subject: [PATCH] add add lxor (^) unified operator --- compiler/ml/unified_ops.ml | 13 +++++++++++++ runtime/Pervasives.res | 1 + runtime/Pervasives_mini.res | 1 + .../data/parsing/grammar/expressions/binary.res | 1 + tests/syntax_tests/data/printer/expr/binary.res | 1 + tests/tests/src/belt_int_test.res | 1 + tests/tests/src/unified_ops_test.res | 2 ++ 7 files changed, 20 insertions(+) diff --git a/compiler/ml/unified_ops.ml b/compiler/ml/unified_ops.ml index c57c14bce0..cb5c5f37f0 100644 --- a/compiler/ml/unified_ops.ml +++ b/compiler/ml/unified_ops.ml @@ -161,6 +161,19 @@ let entries = string = None; }; }; + { + path = builtin "^"; + name = "%xor"; + form = Binary; + specialization = + { + int = Pxorint; + bool = None; + float = None; + bigint = Some Pxorbigint; + string = None; + }; + }; |] let index_by_path = diff --git a/runtime/Pervasives.res b/runtime/Pervasives.res index a19348538d..ec9bf4bde1 100644 --- a/runtime/Pervasives.res +++ b/runtime/Pervasives.res @@ -51,6 +51,7 @@ external \"*": ('a, 'a) => 'a = "%mul" external \"/": ('a, 'a) => 'a = "%div" external \"%": ('a, 'a) => 'a = "%mod" external mod: ('a, 'a) => 'a = "%mod" +external \"^": ('a, 'a) => 'a = "%xor" /* Comparisons */ /* Note: Later comparisons will be converted to unified operations too */ diff --git a/runtime/Pervasives_mini.res b/runtime/Pervasives_mini.res index 9cf3cd90e4..e22f9adbfc 100644 --- a/runtime/Pervasives_mini.res +++ b/runtime/Pervasives_mini.res @@ -30,6 +30,7 @@ external \"*": (int, int) => int = "%mulint" external \"/": (int, int) => int = "%divint" external \"%": (int, int) => int = "%modint" external mod: (int, int) => int = "%modint" +external \"^": (int, int) => int = "%xorint" /* Comparisons */ /* Note: Later comparisons will be converted to unified operations too */ diff --git a/tests/syntax_tests/data/parsing/grammar/expressions/binary.res b/tests/syntax_tests/data/parsing/grammar/expressions/binary.res index 6c89739553..224eb2da39 100644 --- a/tests/syntax_tests/data/parsing/grammar/expressions/binary.res +++ b/tests/syntax_tests/data/parsing/grammar/expressions/binary.res @@ -28,6 +28,7 @@ let x = z |> @attr while condition { () } let x = a + -1 + -2 let x = a + @attr -1 + @attr -2 let x = a % a == 0 +let x = a ^ a == 0 // should be interpreted as binary expression not prefix op let x = a -b diff --git a/tests/syntax_tests/data/printer/expr/binary.res b/tests/syntax_tests/data/printer/expr/binary.res index 6e47a546b3..6f43115a00 100644 --- a/tests/syntax_tests/data/printer/expr/binary.res +++ b/tests/syntax_tests/data/printer/expr/binary.res @@ -54,6 +54,7 @@ let () = (x: int) |> (print_int: int => unit) x + y / z x / y + z x % y * z +x ^ y + z 100 * x / total 2 / 3 * 10 / 2 + 2 let rotateX = ((range / rect.height) * refY - range / 2) * getXMultiplication(rect.width) diff --git a/tests/tests/src/belt_int_test.res b/tests/tests/src/belt_int_test.res index a6c418b7ca..0c77875493 100644 --- a/tests/tests/src/belt_int_test.res +++ b/tests/tests/src/belt_int_test.res @@ -41,5 +41,6 @@ describe(__MODULE__, () => { eq(__LOC__, 2 * 3, 6) eq(__LOC__, 2 / 3, 0) eq(__LOC__, 2 % 2, 0) + eq(__LOC__, 2 ^ 3, 1) }) }) diff --git a/tests/tests/src/unified_ops_test.res b/tests/tests/src/unified_ops_test.res index c35588167f..c30d4fa0b3 100644 --- a/tests/tests/src/unified_ops_test.res +++ b/tests/tests/src/unified_ops_test.res @@ -20,3 +20,5 @@ let case2 = (a, b) => a + "test" + b let even = n => n % 2 == 0 let odd = n => n % 2 == 1 + +let lxor = (a, b: int) => a ^ b \ No newline at end of file