From a80ffae8971bcc0868e0a6a4d00a7fd6a566e70f Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Wed, 11 Dec 2019 20:42:52 +0100 Subject: [PATCH 1/4] add interpret command --- src/bin/cli.ml | 26 ++++++++++++++++++++++++++ src/bin/expect_tests/help_tests.ml | 8 ++++++++ src/main/uncompile/uncompile.ml | 7 ++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/bin/cli.ml b/src/bin/cli.ml index 181ffa43e..0ce3fea55 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -163,6 +163,31 @@ let compile_parameter = let doc = "Subcommand: compile parameters to a michelson expression. The resulting michelson expression can be passed as an argument in a transaction which calls a contract." in (term , Term.info ~doc cmdname) +let interpret = + let f expression source_file syntax amount sender source display_format = + toplevel ~display_format @@ + let%bind simplified = Compile.Of_source.compile source_file (Syntax_name syntax) in + let%bind typed_prg,state = Compile.Of_simplified.compile simplified in + let%bind mini_c_prg = Compile.Of_typed.compile typed_prg in + let env = Ast_typed.program_environment typed_prg in + + let%bind v_syntax = Helpers.syntax_to_variant (Syntax_name syntax) (Some source_file) in + let%bind simplified_exp = Compile.Of_source.compile_expression v_syntax expression in + let%bind (typed_exp,_) = Compile.Of_simplified.compile_expression ~env ~state simplified_exp in + let%bind mini_c_exp = Compile.Of_typed.compile_expression typed_exp in + let%bind compiled_exp = Compile.Of_mini_c.aggregate_and_compile_expression mini_c_prg mini_c_exp in + let%bind options = Run.make_dry_run_options {amount ; sender ; source } in + let%bind value = Run.run ~options compiled_exp.expr compiled_exp.expr_ty in + let%bind simplified_output = Uncompile.uncompile_expression typed_exp.type_annotation value in + ok @@ Format.asprintf "%a\n" Ast_simplified.PP.expression simplified_output + in + let term = + Term.(const f $ expression "EXPRESSION" 0 $ source_file 1 $ syntax $ amount $ sender $ source $ display_format ) in + let cmdname = "interpret" in + let doc = "Subcommand: interpret the expression in the context initialized by the provided source file." in + (term , Term.info ~doc cmdname) + + let compile_storage = let f source_file entry_point expression syntax display_format michelson_format = toplevel ~display_format @@ @@ -296,6 +321,7 @@ let run ?argv () = compile_parameter ; compile_storage ; compile_expression ; + interpret ; dry_run ; run_function ; evaluate_value ; diff --git a/src/bin/expect_tests/help_tests.ml b/src/bin/expect_tests/help_tests.ml index 75a574ab3..769df1144 100644 --- a/src/bin/expect_tests/help_tests.ml +++ b/src/bin/expect_tests/help_tests.ml @@ -37,6 +37,10 @@ let%expect_test _ = evaluate-value Subcommand: evaluate a given definition. + interpret + Subcommand: interpret the expression in the context initialized by + the provided source file. + measure-contract Subcommand: measure a contract's compiled size in bytes. @@ -84,6 +88,10 @@ let%expect_test _ = evaluate-value Subcommand: evaluate a given definition. + interpret + Subcommand: interpret the expression in the context initialized by + the provided source file. + measure-contract Subcommand: measure a contract's compiled size in bytes. diff --git a/src/main/uncompile/uncompile.ml b/src/main/uncompile/uncompile.ml index c020c2c4f..2fa1ee14d 100644 --- a/src/main/uncompile/uncompile.ml +++ b/src/main/uncompile/uncompile.ml @@ -16,4 +16,9 @@ let uncompile_typed_program_entry_expression_result program entry ex_ty_value = uncompile_value Expression program entry ex_ty_value let uncompile_typed_program_entry_function_result program entry ex_ty_value = - uncompile_value Function program entry ex_ty_value \ No newline at end of file + uncompile_value Function program entry ex_ty_value + +let uncompile_expression type_value ex_ty_value = + let%bind mini_c = Compiler.Uncompiler.translate_value ex_ty_value in + let%bind typed = Transpiler.untranspile mini_c type_value in + Typer.untype_expression typed \ No newline at end of file From 61fd91a0d4ff28834d26e55f2ed816f6330a1b72 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Thu, 12 Dec 2019 11:36:59 +0100 Subject: [PATCH 2/4] optional init file --- src/bin/cli.ml | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/bin/cli.ml b/src/bin/cli.ml index 0ce3fea55..a9376ca9d 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -47,6 +47,14 @@ let req_syntax n = info ~docv ~doc [] in required @@ pos n (some string) None info +let init_file = + let open Arg in + let info = + let docv = "INIT_FILE" in + let doc = "$(docv) is the path to the .ligo or .mligo file to be used for context initialization." in + info ~docv ~doc ["init-file"] in + value @@ opt (some string) None info + let amount = let open Arg in let info = @@ -164,25 +172,29 @@ let compile_parameter = (term , Term.info ~doc cmdname) let interpret = - let f expression source_file syntax amount sender source display_format = + let f expression init_file syntax amount sender source display_format = toplevel ~display_format @@ - let%bind simplified = Compile.Of_source.compile source_file (Syntax_name syntax) in - let%bind typed_prg,state = Compile.Of_simplified.compile simplified in - let%bind mini_c_prg = Compile.Of_typed.compile typed_prg in - let env = Ast_typed.program_environment typed_prg in - - let%bind v_syntax = Helpers.syntax_to_variant (Syntax_name syntax) (Some source_file) in + let%bind (decl_list,state,env) = match init_file with + | Some init_file -> + let%bind simplified = Compile.Of_source.compile init_file (Syntax_name syntax) in + let%bind typed_prg,state = Compile.Of_simplified.compile simplified in + let%bind mini_c_prg = Compile.Of_typed.compile typed_prg in + let env = Ast_typed.program_environment typed_prg in + ok (mini_c_prg,state,env) + | None -> ok ([],Typer.Solver.initial_state,Ast_typed.Environment.full_empty) in + + let%bind v_syntax = Helpers.syntax_to_variant (Syntax_name syntax) init_file in let%bind simplified_exp = Compile.Of_source.compile_expression v_syntax expression in let%bind (typed_exp,_) = Compile.Of_simplified.compile_expression ~env ~state simplified_exp in let%bind mini_c_exp = Compile.Of_typed.compile_expression typed_exp in - let%bind compiled_exp = Compile.Of_mini_c.aggregate_and_compile_expression mini_c_prg mini_c_exp in + let%bind compiled_exp = Compile.Of_mini_c.aggregate_and_compile_expression decl_list mini_c_exp in let%bind options = Run.make_dry_run_options {amount ; sender ; source } in let%bind value = Run.run ~options compiled_exp.expr compiled_exp.expr_ty in let%bind simplified_output = Uncompile.uncompile_expression typed_exp.type_annotation value in ok @@ Format.asprintf "%a\n" Ast_simplified.PP.expression simplified_output in let term = - Term.(const f $ expression "EXPRESSION" 0 $ source_file 1 $ syntax $ amount $ sender $ source $ display_format ) in + Term.(const f $ expression "EXPRESSION" 0 $ init_file $ syntax $ amount $ sender $ source $ display_format ) in let cmdname = "interpret" in let doc = "Subcommand: interpret the expression in the context initialized by the provided source file." in (term , Term.info ~doc cmdname) From 1842d50ef1aed1a87c7589ea2d789ed10a30b8bb Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Thu, 12 Dec 2019 12:13:43 +0100 Subject: [PATCH 3/4] some Ast_simplified PP improvements --- src/stages/ast_simplified/PP.ml | 15 +++++++++------ src/stages/common/PP.ml | 5 +++++ src/stages/common/PP.mli | 1 + 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/stages/ast_simplified/PP.ml b/src/stages/ast_simplified/PP.ml index 5f1998f95..ac7c4bafe 100644 --- a/src/stages/ast_simplified/PP.ml +++ b/src/stages/ast_simplified/PP.ml @@ -5,7 +5,10 @@ include Stage_common.PP let list_sep_d x ppf lst = match lst with | [] -> () - | _ -> fprintf ppf "@; @[%a@]@;" (list_sep x (tag "@;")) lst + | _ -> fprintf ppf " @[%a@] " (list_sep x (tag " ; ")) lst +let tuple_sep_d x ppf lst = match lst with + | [] -> () + | _ -> fprintf ppf " @[%a@] " (list_sep x (tag " , ")) lst let rec te' ppf (te : type_expression type_expression') : unit = type_expression' type_expression ppf te @@ -19,13 +22,13 @@ let rec expression ppf (e:expression) = match e.expression with | E_application (f, arg) -> fprintf ppf "(%a)@(%a)" expression f expression arg | E_constructor (c, ae) -> fprintf ppf "%a(%a)" constructor c expression ae | E_constant (b, lst) -> fprintf ppf "%a(%a)" constant b (list_sep_d expression) lst - | E_tuple lst -> fprintf ppf "tuple[%a]" (list_sep_d expression) lst + | E_tuple lst -> fprintf ppf "(%a)" (tuple_sep_d expression) lst | E_accessor (ae, p) -> fprintf ppf "%a.%a" expression ae access_path p - | E_record m -> fprintf ppf "record[%a]" (lmap_sep expression (const " , ")) m - | E_map m -> fprintf ppf "map[%a]" (list_sep_d assoc_expression) m + | E_record m -> fprintf ppf "{%a}" (lrecord_sep expression (const " , ")) m + | E_map m -> fprintf ppf "[%a]" (list_sep_d assoc_expression) m | E_big_map m -> fprintf ppf "big_map[%a]" (list_sep_d assoc_expression) m - | E_list lst -> fprintf ppf "list[%a]" (list_sep_d expression) lst - | E_set lst -> fprintf ppf "set[%a]" (list_sep_d expression) lst + | E_list lst -> fprintf ppf "[%a]" (list_sep_d expression) lst + | E_set lst -> fprintf ppf "{%a}" (list_sep_d expression) lst | E_look_up (ds, ind) -> fprintf ppf "(%a)[%a]" expression ds expression ind | E_lambda {binder;input_type;output_type;result} -> fprintf ppf "lambda (%a:%a) : %a return %a" diff --git a/src/stages/common/PP.ml b/src/stages/common/PP.ml index 7cc0fb122..e32e9dd52 100644 --- a/src/stages/common/PP.ml +++ b/src/stages/common/PP.ml @@ -128,6 +128,11 @@ let lmap_sep value sep ppf m = let new_pp ppf (k, v) = fprintf ppf "%a -> %a" label k value v in fprintf ppf "%a" (list_sep new_pp sep) lst +let lrecord_sep value sep ppf m = + let lst = Types.LMap.to_kv_list m in + let new_pp ppf (k, v) = fprintf ppf "%a = %a" label k value v in + fprintf ppf "%a" (list_sep new_pp sep) lst + let list_sep_d x = list_sep x (const " , ") let cmap_sep_d x = cmap_sep x (const " , ") let lmap_sep_d x = lmap_sep x (const " , ") diff --git a/src/stages/common/PP.mli b/src/stages/common/PP.mli index fa63bb418..b95fd3851 100644 --- a/src/stages/common/PP.mli +++ b/src/stages/common/PP.mli @@ -8,6 +8,7 @@ val label : formatter -> label -> unit val constant : formatter -> constant -> unit val cmap_sep : (formatter -> 'a -> unit) -> (formatter -> unit -> unit) -> formatter -> 'a CMap.t -> unit val lmap_sep : (formatter -> 'a -> unit) -> (formatter -> unit -> unit) -> formatter -> 'a LMap.t -> unit +val lrecord_sep : (formatter -> 'a -> unit) -> (formatter -> unit -> unit) -> formatter -> 'a LMap.t -> unit val type_expression' : (formatter -> 'a -> unit) -> formatter -> 'a type_expression' -> unit val type_operator : (formatter -> 'a -> unit) -> formatter -> 'a type_operator -> unit val type_constant : formatter -> type_constant -> unit From 0f6529fe94307b6900531325179060b06148a8e0 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Thu, 12 Dec 2019 13:18:45 +0100 Subject: [PATCH 4/4] fix test_cli.sh --- scripts/test_cli.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/test_cli.sh b/scripts/test_cli.sh index ad83f2e64..cc9170f5f 100755 --- a/scripts/test_cli.sh +++ b/scripts/test_cli.sh @@ -7,9 +7,7 @@ dry_run_output=$(./scripts/ligo_ci.sh dry-run src/test/contracts/website2.ligo m expected_compiled_parameter="(Right 1)"; expected_compiled_storage=1; -expected_dry_run_output="tuple[ list[] - 2 -]"; +expected_dry_run_output="( [] , 2 )"; if [ "$compiled_storage" != "$expected_compiled_storage" ]; then echo "Expected $expected_compiled_storage as compile-storage output, got $compiled_storage instead";