From 598fbf96edde5c06426912b48ce9643f599b3f23 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Wed, 13 Nov 2019 00:54:32 -0800 Subject: [PATCH 1/2] Add is_nat operator to PascaLIGO --- src/passes/operators/operators.ml | 7 +++++++ src/test/contracts/isnat.ligo | 2 ++ src/test/integration_tests.ml | 15 +++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 src/test/contracts/isnat.ligo diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 256c23bf3..f7378aefd 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -64,6 +64,7 @@ module Simplify = struct ("size" , "SIZE") ; ("int" , "INT") ; ("abs" , "ABS") ; + ("is_nat", "ISNAT") ; ("amount" , "AMOUNT") ; ("balance", "BALANCE") ; ("now" , "NOW") ; @@ -511,6 +512,10 @@ module Typer = struct let%bind () = assert_t_int t in ok @@ t_nat () + let is_nat = typer_1 "ISNAT" @@ fun t -> + let%bind () = assert_t_int t in + ok @@ t_option (t_nat ()) () + let neg = typer_1 "NEG" @@ fun t -> let%bind () = Assert.assert_true (eq_1 t (t_nat ()) || eq_1 t (t_int ())) in ok @@ t_int () @@ -775,6 +780,7 @@ module Typer = struct get_entrypoint ; neg ; abs ; + is_nat ; cons ; now ; slice ; @@ -844,6 +850,7 @@ module Compiler = struct ("ASSERT" , simple_unary @@ i_if (seq [i_push_unit]) (seq [i_push_unit ; i_failwith])) ; ("INT" , simple_unary @@ prim I_INT) ; ("ABS" , simple_unary @@ prim I_ABS) ; + ("ISNAT", simple_unary @@ prim I_ISNAT) ; ("CONS" , simple_binary @@ prim I_CONS) ; ("UNIT" , simple_constant @@ prim I_UNIT) ; ("BALANCE" , simple_constant @@ prim I_BALANCE) ; diff --git a/src/test/contracts/isnat.ligo b/src/test/contracts/isnat.ligo new file mode 100644 index 000000000..75215f28a --- /dev/null +++ b/src/test/contracts/isnat.ligo @@ -0,0 +1,2 @@ +function main (const i: int) : option(nat) is + block {skip} with is_nat(i) diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 106475b03..def453160 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -1211,6 +1211,20 @@ let balance_constant_mligo () : unit result = let input = e_tuple [e_unit () ; e_mutez 0] in let expected = e_tuple [e_list []; e_mutez 4000000000000] in expect_eq program "main" input expected + +let is_nat () : unit result = + let%bind program = type_file "./contracts/isnat.ligo" in + let%bind () = + let input = e_int 10 in + let expected = e_some (e_nat 10) in + expect_eq program "main" input expected + in + let%bind () = + let input = e_int (-10) in + let expected = e_none () in + expect_eq program "main" input expected + in ok () + let simple_access_ligo () : unit result = let%bind program = type_file "./contracts/simple_access.ligo" in let make_input = e_tuple [e_int 0; e_int 1] in @@ -1313,6 +1327,7 @@ let main = test_suite "Integration (End to End)" [ test "let multiple (mligo)" mligo_let_multiple ; test "balance constant" balance_constant ; test "balance constant (mligo)" balance_constant_mligo ; + test "is_nat" is_nat ; test "simple_access (ligo)" simple_access_ligo; test "deep_access (ligo)" deep_access_ligo; test "entrypoints (ligo)" entrypoints_ligo ; From eb855958184eec4ae63110b141d58726521611fe Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Wed, 20 Nov 2019 04:48:04 -0800 Subject: [PATCH 2/2] Add is_nat operator to CameLIGO --- src/passes/operators/operators.ml | 2 ++ src/test/contracts/isnat.mligo | 1 + src/test/integration_tests.ml | 14 ++++++++++++++ 3 files changed, 17 insertions(+) create mode 100644 src/test/contracts/isnat.mligo diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index f7378aefd..e1bfb0417 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -208,6 +208,8 @@ module Simplify = struct ("abs" , "ABS") ; ("unit" , "UNIT") ; ("source" , "SOURCE") ; + + ("Michelson.is_nat" , "ISNAT") ; ] let type_constants = type_constants diff --git a/src/test/contracts/isnat.mligo b/src/test/contracts/isnat.mligo new file mode 100644 index 000000000..c5bea0294 --- /dev/null +++ b/src/test/contracts/isnat.mligo @@ -0,0 +1 @@ +let main (i: int) : nat option = Michelson.is_nat i diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index def453160..b509a845f 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -1225,6 +1225,19 @@ let is_nat () : unit result = expect_eq program "main" input expected in ok () +let is_nat_mligo () : unit result = + let%bind program = mtype_file "./contracts/isnat.mligo" in + let%bind () = + let input = e_int 10 in + let expected = e_some (e_nat 10) in + expect_eq program "main" input expected + in + let%bind () = + let input = e_int (-10) in + let expected = e_none () in + expect_eq program "main" input expected + in ok () + let simple_access_ligo () : unit result = let%bind program = type_file "./contracts/simple_access.ligo" in let make_input = e_tuple [e_int 0; e_int 1] in @@ -1328,6 +1341,7 @@ let main = test_suite "Integration (End to End)" [ test "balance constant" balance_constant ; test "balance constant (mligo)" balance_constant_mligo ; test "is_nat" is_nat ; + test "is_not (mligo)" is_nat_mligo ; test "simple_access (ligo)" simple_access_ligo; test "deep_access (ligo)" deep_access_ligo; test "entrypoints (ligo)" entrypoints_ligo ;