Merge branch 'feature/hex' into 'dev'

Add hex michelson output, use enums for format options

See merge request ligolang/ligo!162
This commit is contained in:
Tom Jack 2019-10-31 15:39:03 +00:00
commit 58638c6a05
6 changed files with 41 additions and 48 deletions

View File

@ -75,20 +75,26 @@ let display_format =
let docv = "DISPLAY_FORMAT" in
let doc = "$(docv) is the format that will be used by the CLI. Available formats are 'dev', 'json', and 'human-readable' (default). When human-readable lacks details (we are still tweaking it), please contact us and use another format in the meanwhile." in
info ~docv ~doc ["format" ; "display-format"] in
value @@ opt string "human-readable" info
value @@
opt
(enum [("human-readable", `Human_readable); ("dev", `Dev); ("json", `Json)])
`Human_readable
info
let michelson_code_format =
let open Arg in
let info =
let docv = "MICHELSON_FORMAT" in
let doc = "$(docv) is the format that will be used by compile-contract for the resulting Michelson. Available formats are 'micheline', and 'michelson' (default). Micheline is the format used by [XXX]." in
let doc = "$(docv) is the format that will be used by compile-contract for the resulting Michelson. Available formats are 'text' (default), 'json' and 'hex'." in
info ~docv ~doc ["michelson-format"] in
value @@ opt string "michelson" info
value @@
opt
(enum [("text", `Text); ("json", `Json); ("hex", `Hex)])
`Text info
let compile_file =
let f source_file entry_point syntax display_format michelson_format =
toplevel ~display_format @@
let%bind michelson_format = Main.Display.michelson_format_of_string michelson_format in
let%bind contract =
trace (simple_info "compiling contract to michelson") @@
Ligo.Compile.Of_source.compile_file_contract_entry source_file entry_point (Syntax_name syntax) in
@ -101,29 +107,29 @@ let compile_file =
(term , Term.info ~docs cmdname)
let compile_parameter =
let f source_file entry_point expression syntax display_format =
let f source_file entry_point expression syntax display_format michelson_format =
toplevel ~display_format @@
let%bind value =
trace (simple_error "compile-input") @@
Ligo.Run.Of_source.compile_file_contract_parameter source_file entry_point expression (Syntax_name syntax) in
ok @@ Format.asprintf "%a\n" Tezos_utils.Michelson.pp value
ok @@ Format.asprintf "%a\n" (Main.Display.michelson_pp michelson_format) value
in
let term =
Term.(const f $ source_file 0 $ entry_point 1 $ expression "PARAMETER" 2 $ syntax $ display_format) in
Term.(const f $ source_file 0 $ entry_point 1 $ expression "PARAMETER" 2 $ syntax $ display_format $ michelson_code_format) in
let cmdname = "compile-parameter" in
let docs = "Subcommand: compile parameters to a michelson expression. The resulting michelson expression can be passed as an argument in a transaction which calls a contract. See `ligo " ^ cmdname ^ " --help' for a list of options specific to this subcommand." in
(term , Term.info ~docs cmdname)
let compile_storage =
let f source_file entry_point expression syntax display_format bigmap =
let f source_file entry_point expression syntax display_format michelson_format bigmap =
toplevel ~display_format @@
let%bind value =
trace (simple_error "compile-storage") @@
Ligo.Run.Of_source.compile_file_contract_storage ~value:bigmap source_file entry_point expression (Syntax_name syntax) in
ok @@ Format.asprintf "%a\n" Tezos_utils.Michelson.pp value
ok @@ Format.asprintf "%a\n" (Main.Display.michelson_pp michelson_format) value
in
let term =
Term.(const f $ source_file 0 $ entry_point 1 $ expression "STORAGE" 2 $ syntax $ display_format $ bigmap) in
Term.(const f $ source_file 0 $ entry_point 1 $ expression "STORAGE" 2 $ syntax $ display_format $ michelson_code_format $ bigmap) in
let cmdname = "compile-storage" in
let docs = "Subcommand: compile an initial storage in ligo syntax to a michelson expression. The resulting michelson expression can be passed as an argument in a transaction which originates a contract. See `ligo " ^ cmdname ^ " --help' for a list of options specific to this subcommand." in
(term , Term.info ~docs cmdname)
@ -175,15 +181,15 @@ let evaluate_value =
(term , Term.info ~docs cmdname)
let compile_expression =
let f expression syntax display_format =
let f expression syntax display_format michelson_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
ok @@ Format.asprintf "%a\n" (Main.Display.michelson_pp michelson_format) value
in
let term =
Term.(const f $ expression "" 0 $ syntax $ display_format) in
Term.(const f $ expression "" 0 $ syntax $ display_format $ michelson_code_format) in
let cmdname = "compile-expression" in
let docs = "Subcommand: compile to a michelson value." in
(term , Term.info ~docs cmdname)

View File

@ -1,16 +1,9 @@
open Trace
open Main.Display
let toplevel ~(display_format : string) (x : string result) =
let display_format =
try display_format_of_string display_format
with _ -> (
Format.printf "bad display format %s, try looking at DISPLAY_FORMAT in the man (--help)." display_format ;
failwith "Display format"
)
in
let toplevel ~(display_format : display_format) (x : string result) =
match x with
| Ok _ -> Format.printf "%a\n%!" (formatted_string_result_pp display_format) x
| Ok _ -> Format.printf "%a%!" (formatted_string_result_pp display_format) x
| Error _ ->
Format.eprintf "%a\n%!" (formatted_string_result_pp display_format) x ;
Format.eprintf "%a%!" (formatted_string_result_pp display_format) x ;
exit 1

View File

@ -1,3 +1,3 @@
open Trace
val toplevel : display_format : string -> string result -> unit
val toplevel : display_format : Main.Display.display_format -> string result -> unit

View File

@ -87,13 +87,6 @@ type display_format = [
| `Dev
]
let display_format_of_string = fun s : display_format ->
match s with
| "dev" -> `Dev
| "json" -> `Json
| "human-readable" -> `Human_readable
| _ -> failwith "bad display_format"
let formatted_string_result_pp (display_format : display_format) =
match display_format with
| `Human_readable -> string_result_pp_hr
@ -101,16 +94,12 @@ let formatted_string_result_pp (display_format : display_format) =
| `Json -> string_result_pp_json
type michelson_format = [
| `Michelson
| `Micheline
| `Text
| `Json
| `Hex
]
let michelson_format_of_string = fun s : michelson_format result ->
match s with
| "michelson" -> ok `Michelson
| "micheline" -> ok `Micheline
| _ -> simple_fail "bad michelson format"
let michelson_pp (mf : michelson_format) = match mf with
| `Michelson -> Michelson.pp
| `Micheline -> Michelson.pp_json
| `Text -> Michelson.pp
| `Json -> Michelson.pp_json
| `Hex -> Michelson.pp_hex

View File

@ -21,15 +21,12 @@ type display_format = [
| `Dev
]
val display_format_of_string : string -> display_format
val formatted_string_result_pp : display_format -> Format.formatter -> string Simple_utils.Trace.result -> unit
type michelson_format = [
| `Michelson
| `Micheline
| `Text
| `Json
| `Hex
]
val michelson_format_of_string : string -> michelson_format Simple_utils.Trace.result
val michelson_pp : michelson_format -> Format.formatter -> Tezos_utils.Michelson.michelson -> unit

View File

@ -1,7 +1,8 @@
open Tezos_micheline
open Micheline
include Memory_proto_alpha.Protocol.Michelson_v1_primitives
open Memory_proto_alpha.Protocol
include Michelson_v1_primitives
type michelson = (int, prim) node
type t = michelson
@ -98,3 +99,10 @@ let pp_json ppf (michelson : michelson) =
)
in
Format.fprintf ppf "%a" Tezos_data_encoding.Json.pp json
let pp_hex ppf (michelson : michelson) =
let canonical = strip_locations michelson in
let bytes = Tezos_data_encoding.Binary_writer.to_bytes_exn Script_repr.expr_encoding canonical in
let bytes = Tezos_stdlib.MBytes.to_bytes bytes in
let hex = Hex.of_bytes bytes in
Format.fprintf ppf "%a" Hex.pp hex