From 7e05b7d276e4620cfd6a881b5fa893432e948280 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Mon, 6 Jan 2020 17:46:00 +0100 Subject: [PATCH 1/3] now removes the entry-point declaration from the program before aggregation --- src/main/compile/of_mini_c.ml | 7 ++++--- src/stages/mini_c/misc.ml | 4 ++-- vendors/ligo-utils/simple-utils/x_list.ml | 5 +++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/compile/of_mini_c.ml b/src/main/compile/of_mini_c.ml index 4387ca133..fb2945265 100644 --- a/src/main/compile/of_mini_c.ml +++ b/src/main/compile/of_mini_c.ml @@ -25,9 +25,10 @@ let aggregate_and_compile = fun program form -> | ContractForm _ -> compile_contract aggregated' | ExpressionForm _ -> compile_expression aggregated' -let aggregate_and_compile_contract = fun program name -> - let%bind (exp, _) = get_entry program name in - aggregate_and_compile program (ContractForm exp) +let aggregate_and_compile_contract = fun (program : Types.program) name -> + let%bind (exp, idx) = get_entry program name in + let program' = List.remove_from idx program in + aggregate_and_compile program' (ContractForm exp) let aggregate_and_compile_expression = fun program exp -> aggregate_and_compile program (ExpressionForm exp) diff --git a/src/stages/mini_c/misc.ml b/src/stages/mini_c/misc.ml index 5cae24799..2dae579d3 100644 --- a/src/stages/mini_c/misc.ml +++ b/src/stages/mini_c/misc.ml @@ -129,14 +129,14 @@ let get_entry (lst : program) (name : string) : (expression * int) result = then Some decl_expr else None in - List.find_map aux lst + List.find_map aux (List.rev lst) in let entry_index = let aux x = let (((decl_name , _) , _)) = x in Var.equal decl_name (Var.of_name name) in - List.find_index aux lst + (List.length lst) - (List.find_index aux (List.rev lst)) - 1 in ok (entry_expression , entry_index) diff --git a/vendors/ligo-utils/simple-utils/x_list.ml b/vendors/ligo-utils/simple-utils/x_list.ml index 19bf881a5..8541c4614 100644 --- a/vendors/ligo-utils/simple-utils/x_list.ml +++ b/vendors/ligo-utils/simple-utils/x_list.ml @@ -5,6 +5,11 @@ let rec remove n = function | _ :: tl when n = 0 -> tl | hd :: tl -> hd :: remove (n - 1) tl +let rec remove_from n = function + | [] -> raise (Failure "List.remove_from") + | _ when n = 0 -> [] + | hd :: tl -> hd :: remove_from (n - 1) tl + let map ?(acc = []) f lst = let rec aux acc f = function | [] -> acc From 8ce4772ae4f202f177726eb4a21757a37ae7654f Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Mon, 6 Jan 2020 18:51:43 +0100 Subject: [PATCH 2/3] add tests --- src/bin/expect_tests/contract_tests.ml | 10 +++++++++- src/test/contracts/double_main.ligo | 8 ++++++++ src/test/contracts/redeclaration.ligo | 6 ++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/test/contracts/double_main.ligo create mode 100644 src/test/contracts/redeclaration.ligo diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index b9b34c076..97516018e 100644 --- a/src/bin/expect_tests/contract_tests.ml +++ b/src/bin/expect_tests/contract_tests.ml @@ -943,4 +943,12 @@ let%expect_test _ = let%expect_test _ = run_ligo_bad [ "compile-contract" ; contract "bad_timestamp.ligo" ; "main" ] ; - [%expect {| ligo: in file "bad_timestamp.ligo", line 5, characters 29-43. Badly formatted timestamp "badtimestamp": {"location":"in file \"bad_timestamp.ligo\", line 5, characters 29-43"} |}] \ No newline at end of file + [%expect {| ligo: in file "bad_timestamp.ligo", line 5, characters 29-43. Badly formatted timestamp "badtimestamp": {"location":"in file \"bad_timestamp.ligo\", line 5, characters 29-43"} |}] + +let%expect_test _ = + run_ligo_good [ "dry-run" ; contract "redeclaration.ligo" ; "main" ; "unit" ; "0" ] ; + [%expect {|( [] , 0 ) |}] + +let%expect_test _ = + run_ligo_good [ "dry-run" ; contract "double_main.ligo" ; "main" ; "unit" ; "0" ] ; + [%expect {|( [] , 2 ) |}] \ No newline at end of file diff --git a/src/test/contracts/double_main.ligo b/src/test/contracts/double_main.ligo new file mode 100644 index 000000000..6ad75dd80 --- /dev/null +++ b/src/test/contracts/double_main.ligo @@ -0,0 +1,8 @@ +function main(const p : unit; const s : int) : list(operation) * int is + ((list end : list(operation)), s + 1) + +function main(const p : unit; const s : int) : list(operation) * int is + begin + const ret : list(operation) * int = main(p, s) + end + with (ret.0, ret.1 + 1) \ No newline at end of file diff --git a/src/test/contracts/redeclaration.ligo b/src/test/contracts/redeclaration.ligo new file mode 100644 index 000000000..c74594ad3 --- /dev/null +++ b/src/test/contracts/redeclaration.ligo @@ -0,0 +1,6 @@ +function foo(const p : unit) : int is 0 + +function main(const p : unit; const s : int) : list(operation) * int is + ((list end : list(operation)), foo(unit)) + +function foo(const p : unit) : int is 1 \ No newline at end of file From a0a8f114c0ed599fbed8aa169e623dc1d23e40e3 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Mon, 6 Jan 2020 19:24:23 +0100 Subject: [PATCH 3/3] replaces remove_from by take which does not raise any exceptions --- src/main/compile/of_mini_c.ml | 2 +- vendors/ligo-utils/simple-utils/x_list.ml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/compile/of_mini_c.ml b/src/main/compile/of_mini_c.ml index fb2945265..be27f0f6b 100644 --- a/src/main/compile/of_mini_c.ml +++ b/src/main/compile/of_mini_c.ml @@ -27,7 +27,7 @@ let aggregate_and_compile = fun program form -> let aggregate_and_compile_contract = fun (program : Types.program) name -> let%bind (exp, idx) = get_entry program name in - let program' = List.remove_from idx program in + let program' = List.take idx program in aggregate_and_compile program' (ContractForm exp) let aggregate_and_compile_expression = fun program exp -> diff --git a/vendors/ligo-utils/simple-utils/x_list.ml b/vendors/ligo-utils/simple-utils/x_list.ml index 8541c4614..4b74c0261 100644 --- a/vendors/ligo-utils/simple-utils/x_list.ml +++ b/vendors/ligo-utils/simple-utils/x_list.ml @@ -5,10 +5,10 @@ let rec remove n = function | _ :: tl when n = 0 -> tl | hd :: tl -> hd :: remove (n - 1) tl -let rec remove_from n = function - | [] -> raise (Failure "List.remove_from") +let rec take n = function + | [] -> [] | _ when n = 0 -> [] - | hd :: tl -> hd :: remove_from (n - 1) tl + | hd :: tl -> hd :: take (n - 1) tl let map ?(acc = []) f lst = let rec aux acc f = function