diff --git a/src/bin/cli.ml b/src/bin/cli.ml index 3ca3d2bf3..31e9261ab 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -89,7 +89,7 @@ let compile_parameter = toplevel ~display_format @@ let%bind value = trace (simple_error "compile-input") @@ - Ligo.Compile.Of_source.compile_file_contract_parameter source entry_point expression (Syntax_name syntax) in + Ligo.Run.Of_source.compile_file_contract_parameter source entry_point expression (Syntax_name syntax) in ok @@ Format.asprintf "%a\n" Tezos_utils.Michelson.pp value in let term = @@ -103,7 +103,7 @@ let compile_storage = toplevel ~display_format @@ let%bind value = trace (simple_error "compile-storage") @@ - Ligo.Compile.Of_source.compile_file_contract_storage ~value:bigmap source entry_point expression (Syntax_name syntax) in + Ligo.Run.Of_source.compile_file_contract_storage ~value:bigmap source entry_point expression (Syntax_name syntax) in ok @@ Format.asprintf "%a\n" Tezos_utils.Michelson.pp value in let term = @@ -129,7 +129,7 @@ let run_function = let f source entry_point parameter amount syntax display_format = toplevel ~display_format @@ let%bind output = - Ligo.Run.Of_source.run_function ~amount source entry_point parameter (Syntax_name syntax) in + Ligo.Run.Of_source.run_function_entry ~amount source entry_point parameter (Syntax_name syntax) in ok @@ Format.asprintf "%a\n" Ast_simplified.PP.expression output in let term = @@ -142,7 +142,7 @@ let evaluate_value = let f source entry_point amount syntax display_format = toplevel ~display_format @@ let%bind output = - Ligo.Run.Of_source.evaluate ~amount source entry_point (Syntax_name syntax) in + Ligo.Run.Of_source.evaluate_entry ~amount source entry_point (Syntax_name syntax) in ok @@ Format.asprintf "%a\n" Ast_simplified.PP.expression output in let term = @@ -151,11 +151,26 @@ let evaluate_value = let docs = "Subcommand: evaluate a given definition." in (term , Term.info ~docs cmdname) +let compile_expression = + let f expression syntax display_format = + toplevel ~display_format @@ + let%bind value = + trace (simple_error "compile-input") @@ + Ligo.Run.Of_source.compile_expression expression (Syntax_name syntax) in + ok @@ Format.asprintf "%a\n" Tezos_utils.Michelson.pp value + in + let term = + Term.(const f $ expression "" 0 $ syntax $ display_format) in + let cmdname = "compile-expression" in + let docs = "Subcommand: compile to a michelson value." in + (term , Term.info ~docs cmdname) + let () = Term.exit @@ Term.eval_choice main [ compile_file ; compile_parameter ; compile_storage ; + compile_expression ; dry_run ; run_function ; evaluate_value ; diff --git a/src/main/compile/of_mini_c.ml b/src/main/compile/of_mini_c.ml index 34d8cd753..fd8de3570 100644 --- a/src/main/compile/of_mini_c.ml +++ b/src/main/compile/of_mini_c.ml @@ -5,21 +5,15 @@ open Tezos_utils let compile_value : value -> type_value -> Michelson.t result = Compiler.Program.translate_value -let compile_expression ?(value = false) : expression -> _ result = fun e -> - if value then ( - let%bind value = expression_to_value e in - Format.printf "Compile to value\n" ; - let%bind result = compile_value value e.type_value in - Format.printf "Compiled to value\n" ; - ok result - ) else ( - Compiler.Program.translate_expression e Compiler.Environment.empty - ) +let compile_expression_as_value : expression -> _ result = fun e -> + let%bind value = expression_to_value e in + let%bind result = compile_value value e.type_value in + ok result let compile_expression_as_function : expression -> _ result = fun e -> let (input , output) = t_unit , e.type_value in - let%bind body = get_function e in - let%bind body = compile_value body (t_function input output) in + let%bind body = Compiler.Program.translate_expression e Compiler.Environment.empty in + let body = Michelson.(seq [ i_drop ; body ]) in let%bind (input , output) = bind_map_pair Compiler.Type.Ty.type_ (input , output) in let open! Compiler.Program in ok { input ; output ; body } diff --git a/src/main/compile/of_simplified.ml b/src/main/compile/of_simplified.ml index 215c908a5..cf8bc00fd 100644 --- a/src/main/compile/of_simplified.ml +++ b/src/main/compile/of_simplified.ml @@ -14,9 +14,13 @@ let compile_expression_as_function_entry (program : program) entry_point : _ res let%bind typed_program = Typer.type_program program in Of_typed.compile_expression_as_function_entry typed_program entry_point -let compile_expression ?(env = Ast_typed.Environment.full_empty) ?value ae : Michelson.t result = +let compile_expression_as_value ?(env = Ast_typed.Environment.full_empty) ae : Michelson.t result = let%bind typed = Typer.type_expression env ae in - Of_typed.compile_expression ?value typed + Of_typed.compile_expression_as_value typed + +let compile_expression_as_function ?(env = Ast_typed.Environment.full_empty) ae : _ result = + let%bind typed = Typer.type_expression env ae in + Of_typed.compile_expression_as_function typed let uncompile_typed_program_entry_expression_result program entry ex_ty_value = let%bind output_type = diff --git a/src/main/compile/of_source.ml b/src/main/compile/of_source.ml index 42c6adf91..f7576ec19 100644 --- a/src/main/compile/of_source.ml +++ b/src/main/compile/of_source.ml @@ -1,6 +1,5 @@ open Trace open Helpers -open Tezos_utils let parse_file_program source_filename syntax = let%bind syntax = syntax_to_variant syntax (Some source_filename) in @@ -18,31 +17,11 @@ let compile_file_contract_entry : string -> string -> s_syntax -> _ result = let%bind compiled_contract = Of_simplified.compile_contract_entry simplified entry_point in ok compiled_contract -let compile_file_contract_parameter : string -> string -> string -> s_syntax -> Michelson.t result = - fun source_filename _entry_point expression syntax -> - let%bind syntax = syntax_to_variant syntax (Some source_filename) in +let compile_expression_as_function : string -> s_syntax -> _ result = + fun expression syntax -> + let%bind syntax = syntax_to_variant syntax None in let%bind simplified = parsify_expression syntax expression in - Of_simplified.compile_expression simplified - -let compile_file_expression : string -> string -> string -> s_syntax -> Michelson.t result = - fun source_filename _entry_point expression syntax -> - let%bind syntax = syntax_to_variant syntax (Some source_filename) in - let%bind simplified = parsify_expression syntax expression in - Of_simplified.compile_expression simplified - -let compile_file_contract_storage ~value : string -> string -> string -> s_syntax -> Michelson.t result = - fun source_filename _entry_point expression syntax -> - let%bind syntax = syntax_to_variant syntax (Some source_filename) in - let%bind simplified = parsify_expression syntax expression in - Of_simplified.compile_expression ~value simplified - -let compile_file_contract_args = - fun ?value source_filename _entry_point storage parameter syntax -> - let%bind syntax = syntax_to_variant syntax (Some source_filename) in - let%bind storage_simplified = parsify_expression syntax storage in - let%bind parameter_simplified = parsify_expression syntax parameter in - let args = Ast_simplified.e_pair storage_simplified parameter_simplified in - Of_simplified.compile_expression ?value args + Of_simplified.compile_expression_as_function simplified let type_file ?(debug_simplify = false) ?(debug_typed = false) syntax (source_filename:string) : Ast_typed.program result = diff --git a/src/main/compile/of_typed.ml b/src/main/compile/of_typed.ml index e8ac1e8e7..79ca90040 100644 --- a/src/main/compile/of_typed.ml +++ b/src/main/compile/of_typed.ml @@ -3,9 +3,9 @@ open Ast_typed open Tezos_utils -let compile_expression ?(value = false) : annotated_expression -> Michelson.t result = fun e -> +let compile_expression_as_value : annotated_expression -> Michelson.t result = fun e -> let%bind mini_c_expression = Transpiler.transpile_annotated_expression e in - let%bind expr = Of_mini_c.compile_expression ~value mini_c_expression in + let%bind expr = Of_mini_c.compile_expression_as_value mini_c_expression in ok expr let compile_expression_as_function : annotated_expression -> _ result = fun e -> diff --git a/src/main/display.ml b/src/main/display.ml index 2d24e8008..93eebbfe9 100644 --- a/src/main/display.ml +++ b/src/main/display.ml @@ -66,6 +66,8 @@ let result_pp_dev f out (r : _ result) = let string_result_pp_dev = result_pp_hr (fun out s -> Format.fprintf out "%s" s) +let json_pp out x = Format.fprintf out "%s" (J.to_string x) + let string_result_pp_json out (r : string result) = let status_json status content : J.t = `Assoc ([ ("status" , `String status) ; @@ -73,10 +75,10 @@ let string_result_pp_json out (r : string result) = ]) in match r with | Ok (x , _) -> ( - Format.fprintf out "%a" J.pp (status_json "ok" (`String x)) + Format.fprintf out "%a" json_pp (status_json "ok" (`String x)) ) | Error e -> ( - Format.fprintf out "%a" J.pp (status_json "error" (e ())) + Format.fprintf out "%a" json_pp (status_json "error" (e ())) ) type display_format = [ diff --git a/src/main/run/of_michelson.ml b/src/main/run/of_michelson.ml index 307aa2274..220bc26c2 100644 --- a/src/main/run/of_michelson.ml +++ b/src/main/run/of_michelson.ml @@ -6,36 +6,24 @@ open Memory_proto_alpha.X type options = Memory_proto_alpha.options -let run ?options ?(is_input_value = false) (program:compiled_program) (input_michelson:Michelson.t) : ex_typed_value result = +let run ?options (* ?(is_input_value = false) *) (program:compiled_program) (input_michelson:Michelson.t) : ex_typed_value result = let Compiler.Program.{input;output;body} : compiled_program = program in let (Ex_ty input_ty) = input in let (Ex_ty output_ty) = output in - let%bind input_ty_mich = - Trace.trace_tzresult_lwt (simple_error "error unparsing input ty") @@ - Memory_proto_alpha.unparse_michelson_ty input_ty in - let%bind output_ty_mich = - Trace.trace_tzresult_lwt (simple_error "error unparsing output ty") @@ - Memory_proto_alpha.unparse_michelson_ty output_ty in - Format.printf "code: %a\n" Michelson.pp program.body ; - Format.printf "input_ty: %a\n" Michelson.pp input_ty_mich ; - Format.printf "output_ty: %a\n" Michelson.pp output_ty_mich ; - Format.printf "input: %a\n" Michelson.pp input_michelson ; + (* let%bind input_ty_mich = + * Trace.trace_tzresult_lwt (simple_error "error unparsing input ty") @@ + * Memory_proto_alpha.unparse_michelson_ty input_ty in + * let%bind output_ty_mich = + * Trace.trace_tzresult_lwt (simple_error "error unparsing output ty") @@ + * Memory_proto_alpha.unparse_michelson_ty output_ty in + * Format.printf "code: %a\n" Michelson.pp program.body ; + * Format.printf "input_ty: %a\n" Michelson.pp input_ty_mich ; + * Format.printf "output_ty: %a\n" Michelson.pp output_ty_mich ; + * Format.printf "input: %a\n" Michelson.pp input_michelson ; *) let%bind input = - if is_input_value then ( - Trace.trace_tzresult_lwt (simple_error "error parsing input") @@ - Memory_proto_alpha.parse_michelson_data input_michelson input_ty - ) else ( - let input_michelson = Michelson.(seq [ input_michelson ; dip i_drop ]) in - let body = Michelson.(strip_nops @@ strip_annots input_michelson) in - let%bind descr = - Trace.trace_tzresult_lwt (simple_error "error parsing input code") @@ - Memory_proto_alpha.parse_michelson body - (Item_t (Memory_proto_alpha.Protocol.Script_typed_ir.Unit_t None, Empty_t, None)) (Item_t (input_ty, Empty_t, None)) in - let%bind (Item(output, Empty)) = - Trace.trace_tzresult_lwt (simple_error "input error of execution") @@ - Memory_proto_alpha.interpret ?options descr (Item((), Empty)) in - ok output - ) in + Trace.trace_tzresult_lwt (simple_error "error parsing input") @@ + Memory_proto_alpha.parse_michelson_data input_michelson input_ty + in let body = Michelson.(strip_nops @@ strip_annots body) in let%bind descr = Trace.trace_tzresult_lwt (simple_error "error parsing program code") @@ @@ -47,4 +35,13 @@ let run ?options ?(is_input_value = false) (program:compiled_program) (input_mic Memory_proto_alpha.interpret ?options descr (Item(input, Empty)) in ok (Ex_typed_value (output_ty, output)) -let evaluate ?options program = run ?options ~is_input_value:true program Michelson.d_unit +let evaluate ?options program = run ?options program Michelson.d_unit + +let ex_value_ty_to_michelson (v : ex_typed_value) : Michelson.t result = + let (Ex_typed_value (value , ty)) = v in + Trace.trace_tzresult_lwt (simple_error "error unparsing michelson result") @@ + Memory_proto_alpha.unparse_michelson_data value ty + +let evaluate_michelson ?options program = + let%bind etv = evaluate ?options program in + ex_value_ty_to_michelson etv diff --git a/src/main/run/of_mini_c.ml b/src/main/run/of_mini_c.ml index dbe02bf08..131bf4ac5 100644 --- a/src/main/run/of_mini_c.ml +++ b/src/main/run/of_mini_c.ml @@ -38,7 +38,7 @@ let run_function ?options expression input ty = let run_function_value ?options expression input ty = let%bind code = Compile.Of_mini_c.compile_function expression in let%bind input = Compile.Of_mini_c.compile_value input ty in - let%bind ex_ty_value = Of_michelson.run ~is_input_value:true ?options code input in + let%bind ex_ty_value = Of_michelson.run ?options code input in Compile.Of_mini_c.uncompile_value ex_ty_value let run_function_entry ?options program entry input = diff --git a/src/main/run/of_simplified.ml b/src/main/run/of_simplified.ml index 9c5d830cc..523ceed23 100644 --- a/src/main/run/of_simplified.ml +++ b/src/main/run/of_simplified.ml @@ -6,6 +6,16 @@ let get_final_environment program = let (Ast_typed.Declaration_constant (_ , (_ , post_env))) = last_declaration in post_env +let compile_expression ?(value = false) ?env expr = + if value + then ( + Compile.Of_simplified.compile_expression_as_value ?env expr + ) + else ( + let%bind code = Compile.Of_simplified.compile_expression_as_function ?env expr in + Of_michelson.evaluate_michelson code + ) + let run_typed_program ?options ?input_to_value (program : Ast_typed.program) (entry : string) @@ -13,9 +23,9 @@ let run_typed_program let%bind code = Compile.Of_typed.compile_function_entry program entry in let%bind input = let env = get_final_environment program in - Compile.Of_simplified.compile_expression ~env ?value:input_to_value input + compile_expression ?value:input_to_value ~env input in - let%bind ex_ty_value = Of_michelson.run ?is_input_value:input_to_value ?options code input in + let%bind ex_ty_value = Of_michelson.run ?options code input in Compile.Of_simplified.uncompile_typed_program_entry_function_result program entry ex_ty_value let evaluate_typed_program_entry diff --git a/src/main/run/of_source.ml b/src/main/run/of_source.ml index 3014cbbb7..5fed28c09 100644 --- a/src/main/run/of_source.ml +++ b/src/main/run/of_source.ml @@ -46,10 +46,45 @@ include struct ok () end +(* open Tezos_utils *) + +let compile_file_contract_parameter : string -> string -> string -> Compile.Helpers.s_syntax -> Michelson.t result = + fun source_filename _entry_point expression syntax -> + let%bind syntax = Compile.Helpers.syntax_to_variant syntax (Some source_filename) in + let%bind simplified = Compile.Helpers.parsify_expression syntax expression in + Of_simplified.compile_expression simplified + +let compile_file_expression : string -> string -> string -> Compile.Helpers.s_syntax -> Michelson.t result = + fun source_filename _entry_point expression syntax -> + let%bind syntax = Compile.Helpers.syntax_to_variant syntax (Some source_filename) in + let%bind simplified = Compile.Helpers.parsify_expression syntax expression in + Of_simplified.compile_expression simplified + +let compile_expression : string -> Compile.Helpers.s_syntax -> Michelson.t result = + fun expression syntax -> + let%bind syntax = Compile.Helpers.syntax_to_variant syntax None in + let%bind simplified = Compile.Helpers.parsify_expression syntax expression in + Of_simplified.compile_expression simplified + +let compile_file_contract_storage ~value : string -> string -> string -> Compile.Helpers.s_syntax -> Michelson.t result = + fun source_filename _entry_point expression syntax -> + let%bind syntax = Compile.Helpers.syntax_to_variant syntax (Some source_filename) in + let%bind simplified = Compile.Helpers.parsify_expression syntax expression in + Of_simplified.compile_expression ~value simplified + +let compile_file_contract_args = + fun ?value source_filename _entry_point storage parameter syntax -> + let%bind syntax = Compile.Helpers.syntax_to_variant syntax (Some source_filename) in + let%bind storage_simplified = Compile.Helpers.parsify_expression syntax storage in + let%bind parameter_simplified = Compile.Helpers.parsify_expression syntax parameter in + let args = Ast_simplified.e_pair storage_simplified parameter_simplified in + Of_simplified.compile_expression ?value args + + let run_contract ?amount ?storage_value source_filename entry_point storage parameter syntax = let%bind program = Compile.Of_source.type_file syntax source_filename in let%bind code = Compile.Of_typed.compile_function_entry program entry_point in - let%bind args = Compile.Of_source.compile_file_contract_args ?value:storage_value source_filename entry_point storage parameter syntax in + let%bind args = compile_file_contract_args ?value:storage_value source_filename entry_point storage parameter syntax in let%bind ex_value_ty = let options = let open Proto_alpha_utils.Memory_proto_alpha in @@ -60,10 +95,10 @@ let run_contract ?amount ?storage_value source_filename entry_point storage para in Compile.Of_simplified.uncompile_typed_program_entry_function_result program entry_point ex_value_ty -let run_function ?amount source_filename entry_point input syntax = +let run_function_entry ?amount source_filename entry_point input syntax = let%bind program = Compile.Of_source.type_file syntax source_filename in let%bind code = Compile.Of_typed.compile_function_entry program entry_point in - let%bind args = Compile.Of_source.compile_file_expression source_filename entry_point input syntax in + let%bind args = compile_file_expression source_filename entry_point input syntax in let%bind ex_value_ty = let options = let open Proto_alpha_utils.Memory_proto_alpha in @@ -74,19 +109,21 @@ let run_function ?amount source_filename entry_point input syntax = in Compile.Of_simplified.uncompile_typed_program_entry_function_result program entry_point ex_value_ty -let evaluate ?amount source_filename entry_point syntax = +let evaluate_entry ?amount source_filename entry_point syntax = let%bind program = Compile.Of_source.type_file syntax source_filename in let%bind code = Compile.Of_typed.compile_expression_as_function_entry program entry_point in - let%bind input = - let fake_input = Ast_simplified.e_unit () in - Compile.Of_simplified.compile_expression fake_input - in let%bind ex_value_ty = let options = let open Proto_alpha_utils.Memory_proto_alpha in let amount = Option.bind (fun amount -> Protocol.Alpha_context.Tez.of_string amount) amount in (make_options ?amount ()) in - Of_michelson.run ~options code input + Of_michelson.evaluate ~options code in Compile.Of_simplified.uncompile_typed_program_entry_expression_result program entry_point ex_value_ty + +let evaluate_michelson expression syntax = + let%bind code = Compile.Of_source.compile_expression_as_function expression syntax in + Of_michelson.evaluate_michelson code + + diff --git a/src/main/run/of_typed.ml b/src/main/run/of_typed.ml index a645250cc..644e99d26 100644 --- a/src/main/run/of_typed.ml +++ b/src/main/run/of_typed.ml @@ -1,9 +1,19 @@ open Trace open Ast_typed +let compile_expression ?(value = false) expr = + if value + then ( + Compile.Of_typed.compile_expression_as_value expr + ) + else ( + let%bind code = Compile.Of_typed.compile_expression_as_function expr in + Of_michelson.evaluate_michelson code + ) + let run_function ?options f input = let%bind code = Compile.Of_typed.compile_function f in - let%bind input = Compile.Of_typed.compile_expression input in + let%bind input = compile_expression input in let%bind ex_ty_value = Of_michelson.run ?options code input in let%bind ty = let%bind (_ , output_ty) = get_t_function f.type_annotation in @@ -15,7 +25,9 @@ let run_entry ?options (entry : string) (program : Ast_typed.program) (input : Ast_typed.annotated_expression) : Ast_typed.annotated_expression result = let%bind code = Compile.Of_typed.compile_function_entry program entry in - let%bind input = Compile.Of_typed.compile_expression input in + let%bind input = + compile_expression input + in let%bind ex_ty_value = Of_michelson.run ?options code input in Compile.Of_typed.uncompile_entry_function_result program entry ex_ty_value