From ea661247b6e536022bc62129a1ef22a62f523b02 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Fri, 25 Oct 2019 16:12:54 -0700 Subject: [PATCH 1/3] 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 ; From cae0dfb1aa19e0fd92a1a8c73e6f610527cb3e06 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Sun, 27 Oct 2019 12:05:34 -0700 Subject: [PATCH 2/3] Change names to the standard library names for the functions in OCaml --- src/passes/operators/operators.ml | 6 +++--- src/test/contracts/bitwise_arithmetic.mligo | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 9ba53c1b5..e1ebfc2e5 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -177,9 +177,9 @@ module Simplify = struct ("Big_map.literal" , "BIG_MAP_LITERAL" ) ; ("Big_map.empty" , "BIG_MAP_EMPTY" ) ; - ("Bitwise.bor" , "OR") ; - ("Bitwise.band" , "AND") ; - ("Bitwise.xor" , "XOR") ; + ("Bitwise.lor" , "OR") ; + ("Bitwise.land" , "AND") ; + ("Bitwise.lxor" , "XOR") ; ("String.length", "SIZE") ; ("String.size", "SIZE") ; diff --git a/src/test/contracts/bitwise_arithmetic.mligo b/src/test/contracts/bitwise_arithmetic.mligo index 81671b800..831592c70 100644 --- a/src/test/contracts/bitwise_arithmetic.mligo +++ b/src/test/contracts/bitwise_arithmetic.mligo @@ -1,10 +1,10 @@ (* Test CameLIGO bitwise operators *) let or_op (n : nat) : nat = - Bitwise.bor n 4p + Bitwise.lor n 4p let and_op (n : nat) : nat = - Bitwise.band n 7p + Bitwise.land n 7p let xor_op (n : nat) : nat = - Bitwise.xor n 7p + Bitwise.lxor n 7p From c7e4f3f651d6da571936ce5225bfba4a0b09eb5f Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Mon, 28 Oct 2019 09:18:16 -0700 Subject: [PATCH 3/3] Remove lxor, land, and lor from reserved words --- src/passes/1-parser/ligodity/LexToken.mll | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/passes/1-parser/ligodity/LexToken.mll b/src/passes/1-parser/ligodity/LexToken.mll index 2c437d15c..d47651845 100644 --- a/src/passes/1-parser/ligodity/LexToken.mll +++ b/src/passes/1-parser/ligodity/LexToken.mll @@ -280,12 +280,9 @@ let reserved = |> add "functor" |> add "inherit" |> add "initializer" - |> add "land" |> add "lazy" - |> add "lor" |> add "lsl" |> add "lsr" - |> add "lxor" |> add "method" |> add "module" |> add "mutable"