fix stuff

This commit is contained in:
galfour 2019-09-25 10:49:14 +02:00
parent e6ac10f0ce
commit 87bbdad553
11 changed files with 138 additions and 88 deletions

View File

@ -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 ;

View File

@ -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 compile_expression_as_value : expression -> _ result = fun e ->
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_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 }

View File

@ -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 =

View File

@ -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 =

View File

@ -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 ->

View File

@ -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 = [

View File

@ -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
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

View File

@ -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 =

View File

@ -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

View File

@ -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

View File

@ -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