From ada8a2b70360619ef0844d75d7aa8edf5c030819 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Tue, 24 Sep 2019 15:47:04 -0700 Subject: [PATCH] Add (commented out) for loop test & working procedure test Also clean up typo in unsupported for loop error message --- src/contracts/for_fail.ligo | 11 +++++++++++ src/contracts/loop.ligo | 9 ++++++++- src/contracts/procedure.ligo | 6 ++++++ src/simplify/pascaligo.ml | 2 +- src/test/integration_tests.ml | 22 ++++++++++++++++++++-- 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/contracts/for_fail.ligo create mode 100644 src/contracts/procedure.ligo diff --git a/src/contracts/for_fail.ligo b/src/contracts/for_fail.ligo new file mode 100644 index 000000000..0a177bca1 --- /dev/null +++ b/src/contracts/for_fail.ligo @@ -0,0 +1,11 @@ +// This was meant to test the for loop in PascaLIGO +// But for whatever reason, the LIGO compiler currently thinks this is a 'complex loop' +// even though it isn't. +// See this error: +// $ ligo dry-run for.ligo main 0 0 +// bounded iterators: only simple for loops are supported yet +// {"loop_loc":"in file \"for.ligo\", line 4, characters 10-42"} + + +function main (const a: int) : int is + block { for i := 0 to 100 block { skip } } with i; diff --git a/src/contracts/loop.ligo b/src/contracts/loop.ligo index fcba9fda7..13c47983c 100644 --- a/src/contracts/loop.ligo +++ b/src/contracts/loop.ligo @@ -7,7 +7,7 @@ function counter (var n : nat) : nat is block { } } with i -function sum (var n : nat) : nat is block { +function while_sum (var n : nat) : nat is block { var i : nat := 0n ; var r : nat := 0n ; while (i < n) block { @@ -16,6 +16,13 @@ function sum (var n : nat) : nat is block { } } with r +function for_sum (var n : nat) : nat is block { + for i := 1 to 100 + begin + n := n + 1; + end } + with n + function dummy (const n : nat) : nat is block { while (False) block { skip } } with n diff --git a/src/contracts/procedure.ligo b/src/contracts/procedure.ligo new file mode 100644 index 000000000..fe63d191b --- /dev/null +++ b/src/contracts/procedure.ligo @@ -0,0 +1,6 @@ +// Test a trivial PascaLIGO procedure + +procedure main (const i : int) : int is + begin + skip + end with i diff --git a/src/simplify/pascaligo.ml b/src/simplify/pascaligo.ml index 4aeab4d2a..4b736e068 100644 --- a/src/simplify/pascaligo.ml +++ b/src/simplify/pascaligo.ml @@ -145,7 +145,7 @@ module Errors = struct let unsupported_for_loops region = let title () = "bounded iterators" in let message () = - Format.asprintf "only simple for loops are supported yet" in + Format.asprintf "only simple for loops are supported for now" in let data = [ ("loop_loc", fun () -> Format.asprintf "%a" Location.pp_lift @@ region) diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 9582122d4..2382b8719 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -16,6 +16,11 @@ let function_ () : unit result = let make_expect = fun n -> n in expect_eq_n_int program "main" make_expect +let procedure () : unit result = + let%bind program = type_file "./contracts/procedure.ligo" in + let make_expect = fun n -> n in + expect_eq_n_int program "main" make_expect + let assign () : unit result = let%bind program = type_file "./contracts/assign.ligo" in let make_expect = fun n -> n + 1 in @@ -459,10 +464,22 @@ let loop () : unit result = let%bind () = let make_input = e_nat in let make_expected = fun n -> e_nat (n * (n + 1) / 2) in - expect_eq_n_pos_mid program "sum" make_input make_expected - in + expect_eq_n_pos_mid program "while_sum" make_input make_expected + in(* For loop is currently unsupported + + let%bind () = + let make_input = e_nat in + let make_expected = fun n -> e_nat (n * (n + 1) / 2) in + expect_eq_n_pos_mid program "for_sum" make_input make_expected + in *) ok () +(* Don't know how to assert parse error happens in this test framework +let for_fail () : unit result = + let%bind program = type_file "./contracts/for_fail.ligo" in + let%bind () = expect_fail program "main" (e_nat 0) + in ok () *) + let matching () : unit result = let%bind program = type_file "./contracts/match.ligo" in let%bind () = @@ -666,6 +683,7 @@ let website2_ligo () : unit result = let main = test_suite "Integration (End to End)" [ test "type alias" type_alias ; test "function" function_ ; + test "procedure" procedure ; test "assign" assign ; test "declaration local" declaration_local ; test "complex function" complex_function ;