diff --git a/src/bin/cli.ml b/src/bin/cli.ml index 42cecfd4f..24cc13f77 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -410,6 +410,19 @@ let dump_changelog = let doc = "Dump the LIGO changelog to stdout." in (Term.ret term , Term.info ~doc cmdname) +let list_declarations = + let f source_file syntax = + toplevel ~display_format:(`Human_readable) @@ + let%bind simplified_prg = Compile.Of_source.compile source_file (Syntax_name syntax) in + let json_decl = List.map (fun decl -> `String decl) @@ Compile.Of_simplified.list_declarations simplified_prg in + ok @@ J.to_string @@ `Assoc [ ("source_file", `String source_file) ; ("declarations", `List json_decl) ] + in + let term = + Term.(const f $ source_file 0 $ syntax ) in + let cmdname = "list-declarations" in + let doc = "Subcommand: list all the top-level decalarations." in + (Term.ret term , Term.info ~doc cmdname) + let run ?argv () = Term.eval_choice ?argv main [ compile_file ; @@ -425,5 +438,6 @@ let run ?argv () = print_cst ; print_ast ; print_typed_ast ; - print_mini_c + print_mini_c ; + list_declarations ; ] diff --git a/src/bin/expect_tests/eval_value_tests.ml b/src/bin/expect_tests/eval_value_tests.ml deleted file mode 100644 index ef4ccd1bd..000000000 --- a/src/bin/expect_tests/eval_value_tests.ml +++ /dev/null @@ -1,10 +0,0 @@ -open Cli_expect - -let%expect_test _ = - run_ligo_good [ "evaluate-value" ; "../../test/contracts/evaluation_tests.ligo" ; "a" ] ; - [%expect {| - {foo = +0 , bar = "bar"} |} ]; - - run_ligo_good [ "evaluate-value" ; "../../test/contracts/evaluation_tests.ligo" ; "b" ] ; - [%expect {| - 2 |} ] \ No newline at end of file diff --git a/src/bin/expect_tests/help_tests.ml b/src/bin/expect_tests/help_tests.ml index b385abd14..716837074 100644 --- a/src/bin/expect_tests/help_tests.ml +++ b/src/bin/expect_tests/help_tests.ml @@ -44,6 +44,9 @@ let%expect_test _ = Subcommand: interpret the expression in the context initialized by the provided source file. + list-declarations + Subcommand: list all the top-level decalarations. + measure-contract Subcommand: measure a contract's compiled size in bytes. @@ -117,6 +120,9 @@ let%expect_test _ = Subcommand: interpret the expression in the context initialized by the provided source file. + list-declarations + Subcommand: list all the top-level decalarations. + measure-contract Subcommand: measure a contract's compiled size in bytes. diff --git a/src/bin/expect_tests/misc_cli_commands.ml b/src/bin/expect_tests/misc_cli_commands.ml new file mode 100644 index 000000000..b18de4873 --- /dev/null +++ b/src/bin/expect_tests/misc_cli_commands.ml @@ -0,0 +1,22 @@ +open Cli_expect + +(* evaluate-value *) +let%expect_test _ = + run_ligo_good [ "evaluate-value" ; "../../test/contracts/evaluation_tests.ligo" ; "a" ] ; + [%expect {| + {foo = +0 , bar = "bar"} |} ]; + + run_ligo_good [ "evaluate-value" ; "../../test/contracts/evaluation_tests.ligo" ; "b" ] ; + [%expect {| + 2 |} ] + +(* list-declarations *) +let%expect_test _ = + run_ligo_good [ "list-declarations" ; "../../test/contracts/loop.ligo" ] ; + [%expect {| {"source_file":"../../test/contracts/loop.ligo","declarations":["inner_capture_in_conditional_block","dummy","nested_for_collection_local_var","nested_for_collection","for_collection_map_k","for_collection_map_kv","for_collection_empty","for_collection_with_patches","for_collection_comp_with_acc","for_collection_proc_call","for_collection_rhs_capture","for_collection_if_and_local_var","for_collection_set","for_collection_list","for_sum","while_sum","counter"]} |} ]; + + run_ligo_good [ "list-declarations" ; "../../test/contracts/loop.mligo" ] ; + [%expect {| {"source_file":"../../test/contracts/loop.mligo","declarations":["counter_nest","aux_nest","counter","counter_simple","aux_simple"]} |} ]; + + run_ligo_good [ "list-declarations" ; "../../test/contracts/loop.religo" ] ; + [%expect {| {"source_file":"../../test/contracts/loop.religo","declarations":["counter_nest","aux_nest","counter","counter_simple","aux_simple"]} |} ]; \ No newline at end of file diff --git a/src/main/compile/of_simplified.ml b/src/main/compile/of_simplified.ml index 072243a9c..243ecd586 100644 --- a/src/main/compile/of_simplified.ml +++ b/src/main/compile/of_simplified.ml @@ -20,4 +20,14 @@ let apply (entry_point : string) (param : Ast_simplified.expression) : Ast_simpl ok applied let pretty_print formatter (program : Ast_simplified.program) = - Ast_simplified.PP.program formatter program \ No newline at end of file + Ast_simplified.PP.program formatter program + +let list_declarations (program : Ast_simplified.program) : string list = + List.fold_left + (fun prev el -> + let open Location in + let open Ast_simplified in + match el.wrap_content with + | Declaration_constant (var,_,_,_) -> (Var.to_name var)::prev + | _ -> prev) + [] program