Add is_nat operator to PascaLIGO

This commit is contained in:
John David Pressman 2019-11-13 00:54:32 -08:00 committed by John David Pressman
parent bdd7e5def2
commit 598fbf96ed
3 changed files with 24 additions and 0 deletions

View File

@ -64,6 +64,7 @@ module Simplify = struct
("size" , "SIZE") ; ("size" , "SIZE") ;
("int" , "INT") ; ("int" , "INT") ;
("abs" , "ABS") ; ("abs" , "ABS") ;
("is_nat", "ISNAT") ;
("amount" , "AMOUNT") ; ("amount" , "AMOUNT") ;
("balance", "BALANCE") ; ("balance", "BALANCE") ;
("now" , "NOW") ; ("now" , "NOW") ;
@ -511,6 +512,10 @@ module Typer = struct
let%bind () = assert_t_int t in let%bind () = assert_t_int t in
ok @@ t_nat () 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 neg = typer_1 "NEG" @@ fun t ->
let%bind () = Assert.assert_true (eq_1 t (t_nat ()) || eq_1 t (t_int ())) in let%bind () = Assert.assert_true (eq_1 t (t_nat ()) || eq_1 t (t_int ())) in
ok @@ t_int () ok @@ t_int ()
@ -775,6 +780,7 @@ module Typer = struct
get_entrypoint ; get_entrypoint ;
neg ; neg ;
abs ; abs ;
is_nat ;
cons ; cons ;
now ; now ;
slice ; slice ;
@ -844,6 +850,7 @@ module Compiler = struct
("ASSERT" , simple_unary @@ i_if (seq [i_push_unit]) (seq [i_push_unit ; i_failwith])) ; ("ASSERT" , simple_unary @@ i_if (seq [i_push_unit]) (seq [i_push_unit ; i_failwith])) ;
("INT" , simple_unary @@ prim I_INT) ; ("INT" , simple_unary @@ prim I_INT) ;
("ABS" , simple_unary @@ prim I_ABS) ; ("ABS" , simple_unary @@ prim I_ABS) ;
("ISNAT", simple_unary @@ prim I_ISNAT) ;
("CONS" , simple_binary @@ prim I_CONS) ; ("CONS" , simple_binary @@ prim I_CONS) ;
("UNIT" , simple_constant @@ prim I_UNIT) ; ("UNIT" , simple_constant @@ prim I_UNIT) ;
("BALANCE" , simple_constant @@ prim I_BALANCE) ; ("BALANCE" , simple_constant @@ prim I_BALANCE) ;

View File

@ -0,0 +1,2 @@
function main (const i: int) : option(nat) is
block {skip} with is_nat(i)

View File

@ -1211,6 +1211,20 @@ let balance_constant_mligo () : unit result =
let input = e_tuple [e_unit () ; e_mutez 0] in let input = e_tuple [e_unit () ; e_mutez 0] in
let expected = e_tuple [e_list []; e_mutez 4000000000000] in let expected = e_tuple [e_list []; e_mutez 4000000000000] in
expect_eq program "main" input expected 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 simple_access_ligo () : unit result =
let%bind program = type_file "./contracts/simple_access.ligo" in let%bind program = type_file "./contracts/simple_access.ligo" in
let make_input = e_tuple [e_int 0; e_int 1] 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 "let multiple (mligo)" mligo_let_multiple ;
test "balance constant" balance_constant ; test "balance constant" balance_constant ;
test "balance constant (mligo)" balance_constant_mligo ; test "balance constant (mligo)" balance_constant_mligo ;
test "is_nat" is_nat ;
test "simple_access (ligo)" simple_access_ligo; test "simple_access (ligo)" simple_access_ligo;
test "deep_access (ligo)" deep_access_ligo; test "deep_access (ligo)" deep_access_ligo;
test "entrypoints (ligo)" entrypoints_ligo ; test "entrypoints (ligo)" entrypoints_ligo ;