2019-09-10 15:19:15 +02:00
|
|
|
open Proto_alpha_utils
|
|
|
|
open Trace
|
|
|
|
open Compiler.Program
|
|
|
|
open Memory_proto_alpha.Protocol.Script_ir_translator
|
|
|
|
open Memory_proto_alpha.X
|
|
|
|
|
|
|
|
type options = Memory_proto_alpha.options
|
|
|
|
|
2019-09-19 08:29:31 +02:00
|
|
|
let run ?options ?(is_input_value = false) (program:compiled_program) (input_michelson:Michelson.t) : ex_typed_value result =
|
2019-09-10 15:19:15 +02:00
|
|
|
let Compiler.Program.{input;output;body} : compiled_program = program in
|
|
|
|
let (Ex_ty input_ty) = input in
|
|
|
|
let (Ex_ty output_ty) = output in
|
2019-09-19 01:34:37 +02:00
|
|
|
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 ;
|
2019-09-19 08:29:31 +02:00
|
|
|
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
|
|
|
|
let body = Michelson.(strip_nops @@ strip_annots body) in
|
2019-09-10 15:19:15 +02:00
|
|
|
let%bind descr =
|
|
|
|
Trace.trace_tzresult_lwt (simple_error "error parsing program code") @@
|
|
|
|
Memory_proto_alpha.parse_michelson body
|
|
|
|
(Item_t (input_ty, Empty_t, None)) (Item_t (output_ty, Empty_t, None)) in
|
|
|
|
let open! Memory_proto_alpha.Protocol.Script_interpreter in
|
|
|
|
let%bind (Item(output, Empty)) =
|
|
|
|
Trace.trace_tzresult_lwt (simple_error "error of execution") @@
|
|
|
|
Memory_proto_alpha.interpret ?options descr (Item(input, Empty)) in
|
|
|
|
ok (Ex_typed_value (output_ty, output))
|
2019-09-19 01:34:37 +02:00
|
|
|
|
2019-09-19 08:29:31 +02:00
|
|
|
let evaluate ?options program = run ?options ~is_input_value:true program Michelson.d_unit
|