ligo/src/main/run/of_source.ml

130 lines
5.9 KiB
OCaml
Raw Normal View History

2019-09-15 13:12:19 +02:00
open Trace
include struct
open Ast_simplified
let assert_entry_point_defined : program -> string -> unit result =
fun program entry_point ->
let aux : declaration -> bool = fun declaration ->
match declaration with
| Declaration_type _ -> false
| Declaration_constant (name , _ , _) -> name = entry_point
in
trace_strong (simple_error "no entry-point with given name") @@
Assert.assert_true @@ List.exists aux @@ List.map Location.unwrap program
end
include struct
open Ast_typed
open Combinators
let get_entry_point_type : type_value -> (type_value * type_value) result = fun t ->
let%bind (arg , result) =
trace_strong (simple_error "entry-point doesn't have a function type") @@
get_t_function t in
let%bind (arg' , storage_param) =
trace_strong (simple_error "entry-point doesn't have 2 parameters") @@
get_t_pair arg in
let%bind (ops , storage_result) =
trace_strong (simple_error "entry-point doesn't have 2 results") @@
get_t_pair result in
let%bind () =
trace_strong (simple_error "entry-point doesn't have a list of operation as first result") @@
assert_t_list_operation ops in
let%bind () =
trace_strong (simple_error "entry-point doesn't identical type (storage) for second parameter and second result") @@
assert_type_value_eq (storage_param , storage_result) in
ok (arg' , storage_param)
let get_entry_point : program -> string -> (type_value * type_value) result = fun p e ->
let%bind declaration = get_declaration_by_name p e in
match declaration with
| Declaration_constant (d , _) -> get_entry_point_type d.annotated_expression.type_annotation
let assert_valid_entry_point = fun p e ->
let%bind _ = get_entry_point p e in
ok ()
end
2019-09-25 10:49:14 +02:00
(* 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
2019-09-22 23:39:15 +02:00
let run_contract ?amount ?storage_value source_filename entry_point storage parameter syntax =
2019-09-15 13:12:19 +02:00
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
2019-09-25 10:49:14 +02:00
let%bind args = compile_file_contract_args ?value:storage_value source_filename entry_point storage parameter syntax in
2019-09-15 13:12:19 +02:00
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 args
in
Compile.Of_simplified.uncompile_typed_program_entry_function_result program entry_point ex_value_ty
2019-09-25 10:49:14 +02:00
let run_function_entry ?amount source_filename entry_point input syntax =
2019-09-15 13:12:19 +02:00
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
2019-09-25 10:49:14 +02:00
let%bind args = compile_file_expression source_filename entry_point input syntax in
2019-09-15 13:12:19 +02:00
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 args
in
Compile.Of_simplified.uncompile_typed_program_entry_function_result program entry_point ex_value_ty
2019-09-25 10:49:14 +02:00
let evaluate_entry ?amount source_filename entry_point syntax =
2019-09-15 13:12:19 +02:00
let%bind program = Compile.Of_source.type_file syntax source_filename in
2019-09-19 01:34:37 +02:00
let%bind code = Compile.Of_typed.compile_expression_as_function_entry program entry_point in
2019-09-15 13:12:19 +02:00
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
2019-09-25 10:49:14 +02:00
Of_michelson.evaluate ~options code
2019-09-15 13:12:19 +02:00
in
Compile.Of_simplified.uncompile_typed_program_entry_expression_result program entry_point ex_value_ty
2019-09-25 10:49:14 +02:00
let evaluate_michelson expression syntax =
let%bind code = Compile.Of_source.compile_expression_as_function expression syntax in
Of_michelson.evaluate_michelson code