From ea661247b6e536022bc62129a1ef22a62f523b02 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Fri, 25 Oct 2019 16:12:54 -0700 Subject: [PATCH] Add bitwise operators to CameLIGO Right now they're defined as '.bor' and '.band' because of a glitch in CameLIGO's parser where 'X.Y' leads to a parse error if Y is a keyword or reserved word in CameLIGO. --- src/passes/operators/operators.ml | 4 ++++ src/test/contracts/bitwise_arithmetic.mligo | 10 ++++++++++ src/test/integration_tests.ml | 19 ++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/test/contracts/bitwise_arithmetic.mligo diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index b0b74fa9a..9ba53c1b5 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -177,6 +177,10 @@ module Simplify = struct ("Big_map.literal" , "BIG_MAP_LITERAL" ) ; ("Big_map.empty" , "BIG_MAP_EMPTY" ) ; + ("Bitwise.bor" , "OR") ; + ("Bitwise.band" , "AND") ; + ("Bitwise.xor" , "XOR") ; + ("String.length", "SIZE") ; ("String.size", "SIZE") ; ("String.slice", "SLICE") ; diff --git a/src/test/contracts/bitwise_arithmetic.mligo b/src/test/contracts/bitwise_arithmetic.mligo new file mode 100644 index 000000000..81671b800 --- /dev/null +++ b/src/test/contracts/bitwise_arithmetic.mligo @@ -0,0 +1,10 @@ +(* Test CameLIGO bitwise operators *) + +let or_op (n : nat) : nat = + Bitwise.bor n 4p + +let and_op (n : nat) : nat = + Bitwise.band n 7p + +let xor_op (n : nat) : nat = + Bitwise.xor n 7p diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 09e511322..ed60a0966 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -194,6 +194,22 @@ let bitwise_arithmetic () : unit result = let%bind () = expect_eq program "xor_op" (e_nat 7) (e_nat 0) in ok () +let bitwise_arithmetic_mligo () : unit result = + let%bind program = mtype_file "./contracts/bitwise_arithmetic.mligo" in + let%bind () = expect_eq program "or_op" (e_nat 7) (e_nat 7) in + let%bind () = expect_eq program "or_op" (e_nat 3) (e_nat 7) in + let%bind () = expect_eq program "or_op" (e_nat 2) (e_nat 6) in + let%bind () = expect_eq program "or_op" (e_nat 14) (e_nat 14) in + let%bind () = expect_eq program "or_op" (e_nat 10) (e_nat 14) in + let%bind () = expect_eq program "and_op" (e_nat 7) (e_nat 7) in + let%bind () = expect_eq program "and_op" (e_nat 3) (e_nat 3) in + let%bind () = expect_eq program "and_op" (e_nat 2) (e_nat 2) in + let%bind () = expect_eq program "and_op" (e_nat 14) (e_nat 6) in + let%bind () = expect_eq program "and_op" (e_nat 10) (e_nat 2) in + let%bind () = expect_eq program "xor_op" (e_nat 0) (e_nat 7) in + let%bind () = expect_eq program "xor_op" (e_nat 7) (e_nat 0) in + ok () + let string_arithmetic () : unit result = let%bind program = type_file "./contracts/string_arithmetic.ligo" in let%bind () = expect_eq program "concat_op" (e_string "foo") (e_string "foototo") in @@ -960,7 +976,8 @@ let main = test_suite "Integration (End to End)" [ test "multiple parameters" multiple_parameters ; test "bool" bool_expression ; test "arithmetic" arithmetic ; - test "bitiwse_arithmetic" bitwise_arithmetic ; + test "bitwise_arithmetic" bitwise_arithmetic ; + test "bitwise_arithmetic (mligo)" bitwise_arithmetic_mligo; test "string_arithmetic" string_arithmetic ; test "string_arithmetic (mligo)" string_arithmetic_mligo ; test "bytes_arithmetic" bytes_arithmetic ;