easy to interpret expressions. evaluate-value does the job. CLI refactoring might be necessary

This commit is contained in:
Lesenechal Remi 2019-12-10 15:55:48 +01:00
parent 1c346ead28
commit 5ac25bfe21
8 changed files with 50 additions and 27 deletions

View File

@ -105,7 +105,7 @@ let compile_file =
let%bind simplified = Compile.Of_source.compile source_file (Syntax_name syntax) in
let%bind typed,_ = Compile.Of_simplified.compile simplified in
let%bind mini_c = Compile.Of_typed.compile typed in
let%bind michelson = Compile.Of_mini_c.aggregate_and_compile mini_c None entry_point in
let%bind michelson = Compile.Of_mini_c.aggregate_and_compile_contract mini_c entry_point in
let%bind contract = Compile.Of_mini_c.build_contract michelson in
ok @@ Format.asprintf "%a\n" (Main.Display.michelson_pp michelson_format) contract
in
@ -121,7 +121,7 @@ let measure_contract =
let%bind simplified = Compile.Of_source.compile source_file (Syntax_name syntax) in
let%bind typed,_ = Compile.Of_simplified.compile simplified in
let%bind mini_c = Compile.Of_typed.compile typed in
let%bind michelson = Compile.Of_mini_c.aggregate_and_compile mini_c None entry_point in
let%bind michelson = Compile.Of_mini_c.aggregate_and_compile_contract mini_c entry_point in
let%bind contract = Compile.Of_mini_c.build_contract michelson in
let open Tezos_utils in
ok @@ Format.asprintf "%d bytes\n" (Michelson.measure contract)
@ -143,7 +143,7 @@ let compile_parameter =
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%bind michelson_prg = Compile.Of_mini_c.aggregate_and_compile mini_c_prg None entry_point in
let%bind michelson_prg = Compile.Of_mini_c.aggregate_and_compile_contract mini_c_prg entry_point in
let env = Ast_typed.program_environment typed_prg in
let%bind (_contract: Tezos_utils.Michelson.michelson) =
(* fails if the given entry point is not a valid contract *)
@ -153,7 +153,7 @@ let compile_parameter =
let%bind simplified_param = Compile.Of_source.compile_expression v_syntax expression in
let%bind (typed_param,_) = Compile.Of_simplified.compile_expression ~env ~state simplified_param in
let%bind mini_c_param = Compile.Of_typed.compile_expression typed_param in
let%bind compiled_param = Compile.Of_mini_c.compile_expression mini_c_param in
let%bind compiled_param = Compile.Of_mini_c.aggregate_and_compile_expression mini_c_prg (Expression mini_c_param) [] in
let%bind value = Run.evaluate_expression compiled_param.expr compiled_param.expr_ty in
ok @@ Format.asprintf "%a\n" (Main.Display.michelson_pp michelson_format) value
in
@ -174,7 +174,7 @@ let compile_storage =
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%bind michelson_prg = Compile.Of_mini_c.aggregate_and_compile mini_c_prg None entry_point in
let%bind michelson_prg = Compile.Of_mini_c.aggregate_and_compile_contract mini_c_prg entry_point in
let env = Ast_typed.program_environment typed_prg in
let%bind (_contract: Tezos_utils.Michelson.michelson) =
(* fails if the given entry point is not a valid contract *)
@ -201,7 +201,7 @@ let dry_run =
let%bind typed_prg,state = Compile.Of_simplified.compile simplified in
let env = Ast_typed.program_environment typed_prg in
let%bind mini_c_prg = Compile.Of_typed.compile typed_prg in
let%bind michelson_prg = Compile.Of_mini_c.aggregate_and_compile mini_c_prg None entry_point in
let%bind michelson_prg = Compile.Of_mini_c.aggregate_and_compile_contract mini_c_prg entry_point in
let%bind (_contract: Tezos_utils.Michelson.michelson) =
(* fails if the given entry point is not a valid contract *)
Compile.Of_mini_c.build_contract michelson_prg in
@ -232,13 +232,13 @@ let run_function =
let%bind simplified_prg = Compile.Of_source.compile source_file (Syntax_name syntax) in
let%bind typed_prg,state = Compile.Of_simplified.compile simplified_prg in
let env = Ast_typed.program_environment typed_prg in
let%bind mini_c_prg = Compile.Of_typed.compile typed_prg in
let%bind mini_c_prg = Compile.Of_typed.compile typed_prg in
let%bind simplified_param = Compile.Of_source.compile_expression v_syntax parameter in
let%bind (typed_param,_) = Compile.Of_simplified.compile_expression ~env ~state simplified_param in
let%bind compiled_param = Compile.Of_typed.compile_expression typed_param in
let%bind michelson = Compile.Of_mini_c.aggregate_and_compile mini_c_prg (Some [compiled_param]) entry_point in
let%bind michelson = Compile.Of_mini_c.aggregate_and_compile_expression mini_c_prg (Entry_name entry_point) [compiled_param] in
let%bind options = Run.make_dry_run_options {amount ; sender ; source } in
let%bind michelson_output = Run.run ~options michelson.expr michelson.expr_ty in
let%bind simplified_output = Uncompile.uncompile_typed_program_entry_function_result typed_prg entry_point michelson_output in
@ -256,7 +256,8 @@ let evaluate_value =
let%bind simplified = Compile.Of_source.compile source_file (Syntax_name syntax) in
let%bind typed_prg,_ = Compile.Of_simplified.compile simplified in
let%bind mini_c = Compile.Of_typed.compile typed_prg in
let%bind compiled = Compile.Of_mini_c.aggregate_and_compile mini_c (Some []) entry_point in
let%bind (exp,_) = Mini_c.get_entry mini_c entry_point in
let%bind compiled = Compile.Of_mini_c.aggregate_and_compile_expression mini_c (Expression exp) [] in
let%bind options = Run.make_dry_run_options {amount ; sender ; source } in
let%bind michelson_output = Run.run ~options compiled.expr compiled.expr_ty in
let%bind simplified_output = Uncompile.uncompile_typed_program_entry_expression_result typed_prg entry_point michelson_output in

View File

@ -19,12 +19,28 @@ let compile_expression : expression -> Compiler.compiled_expression result = fun
let open! Compiler.Program in
ok { expr_ty ; expr }
let aggregate_and_compile = fun program arg_opt name ->
let%bind aggregated = aggregate_entry program name arg_opt in
let aggregated = Self_mini_c.all_expression aggregated in
match arg_opt with
| Some _ -> compile_expression aggregated
| None -> compile_contract aggregated
let aggregate_and_compile = fun program form ->
let%bind aggregated = aggregate_entry program form in
let aggregated' = Self_mini_c.all_expression aggregated in
match form with
| ContractForm _ -> compile_contract aggregated'
| ExpressionForm _ -> compile_expression aggregated'
let aggregate_and_compile_contract = fun program name ->
let%bind (exp, idx) = get_entry program name in
aggregate_and_compile program (ContractForm (exp, idx))
type compiled_expression_t =
| Expression of expression
| Entry_name of string
let aggregate_and_compile_expression = fun program exp args ->
match exp with
| Expression exp ->
aggregate_and_compile program (ExpressionForm ((exp,List.length program), args))
| Entry_name name ->
let%bind (exp, idx) = get_entry program name in
aggregate_and_compile program (ExpressionForm ((exp,idx), args))
let build_contract : Compiler.compiled_expression -> Michelson.michelson result =
fun compiled ->

View File

@ -157,9 +157,14 @@ let get_entry (lst : program) (name : string) : (expression * int) result =
if arg_lst is None, it means that the entry point is not an arbitrary expression
*)
type form_t =
| ContractForm of (expression * int)
| ExpressionForm of ((expression * int) * expression list)
let aggregate_entry (lst : program) (name : string) (arg_lst : expression list option) : expression result =
let%bind (entry_expression , entry_index) = get_entry lst name in
let aggregate_entry (lst : program) (form : form_t) : expression result =
let (entry_expression , entry_index, arg_lst) = match form with
| ContractForm (exp,i) -> (exp,i,[])
| ExpressionForm ((exp,i),argl) -> (exp,i,argl) in
let pre_declarations = List.until entry_index lst in
let wrapper =
let aux prec cur =
@ -169,7 +174,7 @@ let aggregate_entry (lst : program) (name : string) (arg_lst : expression list o
fun expr -> List.fold_right' aux expr pre_declarations
in
match (entry_expression.content , arg_lst) with
| (E_closure _ , Some (hd::tl)) -> (
| (E_closure _ , (hd::tl)) -> (
let%bind type_value' = match entry_expression.type_value with
| T_function (_,t) -> ok t
| _ -> simple_fail "Trying to aggregate closure which does not have function type" in
@ -189,6 +194,6 @@ let aggregate_entry (lst : program) (name : string) (arg_lst : expression list o
} tl in
ok @@ wrapper entry_expression'
)
| (_ , None) | (_, Some _) -> (
| (_ , _) -> (
ok @@ wrapper entry_expression
)
)

View File

@ -23,7 +23,7 @@ let compile_main () =
let%bind simplified = Ligo.Compile.Of_source.compile "./contracts/coase.ligo" (Syntax_name "pascaligo") in
let%bind typed_prg,_ = Ligo.Compile.Of_simplified.compile simplified in
let%bind mini_c_prg = Ligo.Compile.Of_typed.compile typed_prg in
let%bind michelson_prg = Ligo.Compile.Of_mini_c.aggregate_and_compile mini_c_prg None "main" in
let%bind michelson_prg = Ligo.Compile.Of_mini_c.aggregate_and_compile_contract mini_c_prg "main" in
let%bind (_contract: Tezos_utils.Michelson.michelson) =
(* fails if the given entry point is not a valid contract *)
Ligo.Compile.Of_mini_c.build_contract michelson_prg in

View File

@ -51,7 +51,8 @@ let dummy n =
let run_typed (entry_point:string) (program:Ast_typed.program) (input:Ast_typed.annotated_expression) =
let%bind input_mini_c = Compile.Of_typed.compile_expression input in
let%bind mini_c = Compile.Of_typed.compile program in
let%bind program_mich = Compile.Of_mini_c.aggregate_and_compile mini_c (Some [input_mini_c]) entry_point in
let%bind program_mich = Compile.Of_mini_c.aggregate_and_compile_expression
mini_c (Entry_name entry_point) [input_mini_c] in
let%bind res = Run.Of_michelson.run program_mich.expr program_mich.expr_ty in
let%bind output_type =
let%bind entry_expression = Ast_typed.get_entry program entry_point in

View File

@ -20,7 +20,7 @@ let compile_main () =
let%bind simplified = Ligo.Compile.Of_source.compile "./contracts/multisig.ligo" (Syntax_name "pascaligo") in
let%bind typed_prg,_ = Ligo.Compile.Of_simplified.compile simplified in
let%bind mini_c_prg = Ligo.Compile.Of_typed.compile typed_prg in
let%bind michelson_prg = Ligo.Compile.Of_mini_c.aggregate_and_compile mini_c_prg None "main" in
let%bind michelson_prg = Ligo.Compile.Of_mini_c.aggregate_and_compile_contract mini_c_prg "main" in
let%bind (_contract: Tezos_utils.Michelson.michelson) =
(* fails if the given entry point is not a valid contract *)
Ligo.Compile.Of_mini_c.build_contract michelson_prg in

View File

@ -20,7 +20,7 @@ let compile_main () =
let%bind simplified = Ligo.Compile.Of_source.compile "./contracts/multisig-v2.ligo" (Syntax_name "pascaligo") in
let%bind typed_prg,_ = Ligo.Compile.Of_simplified.compile simplified in
let%bind mini_c_prg = Ligo.Compile.Of_typed.compile typed_prg in
let%bind michelson_prg = Ligo.Compile.Of_mini_c.aggregate_and_compile mini_c_prg None "main" in
let%bind michelson_prg = Ligo.Compile.Of_mini_c.aggregate_and_compile_contract mini_c_prg "main" in
let%bind (_contract: Tezos_utils.Michelson.michelson) =
(* fails if the given entry point is not a valid contract *)
Ligo.Compile.Of_mini_c.build_contract michelson_prg in

View File

@ -85,8 +85,8 @@ let typed_program_with_simplified_input_to_michelson
let state = Typer.Solver.initial_state in
let%bind (typed_in,_) = Compile.Of_simplified.compile_expression ~env ~state input in
let%bind mini_c_in = Compile.Of_typed.compile_expression typed_in in
let%bind mini_c_prg = Compile.Of_typed.compile program in
Compile.Of_mini_c.aggregate_and_compile mini_c_prg (Some [mini_c_in]) entry_point
let%bind mini_c_prg = Compile.Of_typed.compile program in
Compile.Of_mini_c.aggregate_and_compile_expression mini_c_prg (Entry_name entry_point) [mini_c_in]
let run_typed_program_with_simplified_input ?options
(program: Ast_typed.program) (entry_point: string)
@ -144,7 +144,7 @@ let expect_evaluate program entry_point expecter =
error title content in
trace error @@
let%bind mini_c = Ligo.Compile.Of_typed.compile program in
let%bind michelson_value = Ligo.Compile.Of_mini_c.aggregate_and_compile mini_c (Some []) entry_point in
let%bind michelson_value = Ligo.Compile.Of_mini_c.aggregate_and_compile_expression mini_c (Entry_name entry_point) [] in
let%bind res_michelson = Ligo.Run.Of_michelson.run michelson_value.expr michelson_value.expr_ty in
let%bind res_simpl = Uncompile.uncompile_typed_program_entry_expression_result program entry_point res_michelson in
expecter res_simpl