From ded9c4f58f2c60ae3ff0f118917c60390c807a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Thu, 13 Jun 2019 01:43:16 +0200 Subject: [PATCH] Fix auto-detection of syntax, use a polymorphic variant to ensure all functions were updated. --- src/bin/cli.ml | 14 +++--- src/main/run_source.ml | 95 +++++++++++++++++++++-------------- src/test/bin_tests.ml | 2 +- src/test/coase_tests.ml | 2 +- src/test/heap_tests.ml | 2 +- src/test/integration_tests.ml | 4 +- src/test/vote_tests.ml | 2 +- 7 files changed, 70 insertions(+), 51 deletions(-) diff --git a/src/bin/cli.ml b/src/bin/cli.ml index f73a054cf..86c5c9ea6 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -45,7 +45,7 @@ let source n = let open Arg in let info = let docv = "SOURCE_FILE" in - let doc = "$(docv) is the path to the .ligo file of the contract." in + let doc = "$(docv) is the path to the .ligo or .mligo file of the contract." in info ~docv ~doc [] in required @@ pos n (some string) None info @@ -85,7 +85,7 @@ let compile_file = toplevel @@ let%bind contract = trace (simple_info "compiling contract to michelson") @@ - Ligo.Run.compile_contract_file source entry_point syntax in + Ligo.Run.compile_contract_file source entry_point (Syntax_name syntax) in Format.printf "%s\n" contract ; ok () in @@ -100,7 +100,7 @@ let compile_parameter = toplevel @@ let%bind value = trace (simple_error "compile-input") @@ - Ligo.Run.compile_contract_parameter source entry_point expression syntax in + Ligo.Run.compile_contract_parameter source entry_point expression (Syntax_name syntax) in Format.printf "%s\n" value; ok () in @@ -115,7 +115,7 @@ let compile_storage = toplevel @@ let%bind value = trace (simple_error "compile-storage") @@ - Ligo.Run.compile_contract_storage source entry_point expression syntax in + Ligo.Run.compile_contract_storage source entry_point expression (Syntax_name syntax) in Format.printf "%s\n" value; ok () in @@ -129,7 +129,7 @@ let dry_run = let f source entry_point storage input amount syntax = toplevel @@ let%bind output = - Ligo.Run.run_contract ~amount source entry_point storage input syntax in + Ligo.Run.run_contract ~amount source entry_point storage input (Syntax_name syntax) in Format.printf "%a\n" Ast_simplified.PP.expression output ; ok () in @@ -143,7 +143,7 @@ let run_function = let f source entry_point parameter amount syntax = toplevel @@ let%bind output = - Ligo.Run.run_function ~amount source entry_point parameter syntax in + Ligo.Run.run_function ~amount source entry_point parameter (Syntax_name syntax) in Format.printf "%a\n" Ast_simplified.PP.expression output ; ok () in @@ -157,7 +157,7 @@ let evaluate_value = let f source entry_point amount syntax = toplevel @@ let%bind output = - Ligo.Run.evaluate_value ~amount source entry_point syntax in + Ligo.Run.evaluate_value ~amount source entry_point (Syntax_name syntax) in Format.printf "%a\n" Ast_simplified.PP.expression output ; ok () in diff --git a/src/main/run_source.ml b/src/main/run_source.ml index c961b7cad..a0a18be96 100644 --- a/src/main/run_source.ml +++ b/src/main/run_source.ml @@ -95,40 +95,54 @@ let parsify_expression_ligodity = fun source -> Simplify.Ligodity.simpl_expression raw in ok simplified -let detect_syntax = fun syntax source -> - if String.equal syntax "auto" then - begin - let subr s n = - String.sub s (String.length s - n) n in - if String.equal (subr source 5) ".ligo" - then ok "pascaligo" - else if String.equal (subr source 6) ".mligo" - then ok "cameligo" - else simple_fail "cannot auto-detect syntax, pleas use -s name_of_syntax" - end - else - ok syntax +type s_syntax = Syntax_name of string +type v_syntax = [`pascaligo | `cameligo ] -let parsify = fun syntax source -> - let%bind syntax = detect_syntax syntax source in - let%bind parsify = match syntax with - | "pascaligo" -> ok parsify_pascaligo - | "cameligo" -> ok parsify_ligodity - | _ -> simple_fail "unrecognized parser" +let syntax_to_variant : s_syntax -> string option -> v_syntax result = + fun syntax source_filename -> + let subr s n = + String.sub s (String.length s - n) n in + let endswith s suffix = + let suffixlen = String.length suffix in + ( String.length s >= suffixlen + && String.equal (subr s suffixlen) suffix) in - parsify source + match syntax with + Syntax_name syntax -> + begin + if String.equal syntax "auto" then + begin + match source_filename with + | Some source_filename + when endswith source_filename ".ligo" + -> ok `pascaligo + | Some source_filename + when endswith source_filename ".mligo" + -> ok `cameligo + | _ -> simple_fail "cannot auto-detect syntax, pleas use -s name_of_syntax" + end + else if String.equal syntax "pascaligo" then ok `pascaligo + else if String.equal syntax "cameligo" then ok `cameligo + else simple_fail "unrecognized parser" + end + +let parsify = fun (syntax : v_syntax) source_filename -> + let%bind parsify = match syntax with + | `pascaligo -> ok parsify_pascaligo + | `cameligo -> ok parsify_ligodity + in + parsify source_filename let parsify_expression = fun syntax source -> - let%bind syntax = detect_syntax syntax source in let%bind parsify = match syntax with - | "pascaligo" -> ok parsify_expression_pascaligo - | "cameligo" -> ok parsify_expression_ligodity - | _ -> simple_fail "unrecognized parser" + | `pascaligo -> ok parsify_expression_pascaligo + | `cameligo -> ok parsify_expression_ligodity in parsify source -let compile_contract_file : string -> string -> string -> string result = fun source entry_point syntax -> - let%bind simplified = parsify syntax source in +let compile_contract_file : string -> string -> s_syntax -> string result = fun source_filename entry_point syntax -> + let%bind syntax = syntax_to_variant syntax (Some source_filename) in + let%bind simplified = parsify syntax source_filename in let%bind () = assert_entry_point_defined simplified entry_point in let%bind typed = @@ -144,9 +158,10 @@ let compile_contract_file : string -> string -> string -> string result = fun so Format.asprintf "%a" Michelson.pp_stripped michelson in ok str -let compile_contract_parameter : string -> string -> string -> string -> string result = fun source entry_point expression syntax -> +let compile_contract_parameter : string -> string -> string -> s_syntax -> string result = fun source_filename entry_point expression syntax -> + let%bind syntax = syntax_to_variant syntax (Some source_filename) in let%bind (program , parameter_tv) = - let%bind simplified = parsify syntax source in + let%bind simplified = parsify syntax source_filename in let%bind () = assert_entry_point_defined simplified entry_point in let%bind typed = @@ -182,9 +197,10 @@ let compile_contract_parameter : string -> string -> string -> string -> string ok expr -let compile_contract_storage : string -> string -> string -> string -> string result = fun source entry_point expression syntax -> +let compile_contract_storage : string -> string -> string -> s_syntax -> string result = fun source_filename entry_point expression syntax -> + let%bind syntax = syntax_to_variant syntax (Some source_filename) in let%bind (program , storage_tv) = - let%bind simplified = parsify syntax source in + let%bind simplified = parsify syntax source_filename in let%bind () = assert_entry_point_defined simplified entry_point in let%bind typed = @@ -220,8 +236,8 @@ let compile_contract_storage : string -> string -> string -> string -> string re ok expr let type_file ?(debug_simplify = false) ?(debug_typed = false) - syntax (path:string) : Ast_typed.program result = - let%bind simpl = parsify syntax path in + syntax (source_filename:string) : Ast_typed.program result = + let%bind simpl = parsify syntax source_filename in (if debug_simplify then Format.(printf "Simplified : %a\n%!" Ast_simplified.PP.program simpl) ) ; @@ -233,9 +249,10 @@ let type_file ?(debug_simplify = false) ?(debug_typed = false) )) ; ok typed -let run_contract ?amount source entry_point storage input syntax = +let run_contract ?amount source_filename entry_point storage input syntax = + let%bind syntax = syntax_to_variant syntax (Some source_filename) in let%bind typed = - type_file syntax source in + type_file syntax source_filename in let%bind storage_simpl = parsify_expression syntax storage in let%bind input_simpl = @@ -246,9 +263,10 @@ let run_contract ?amount source entry_point storage input syntax = (make_options ?amount ()) in Run_simplified.run_simplityped ~options typed entry_point (Ast_simplified.e_pair storage_simpl input_simpl) -let run_function ?amount source entry_point parameter syntax = +let run_function ?amount source_filename entry_point parameter syntax = + let%bind syntax = syntax_to_variant syntax (Some source_filename) in let%bind typed = - type_file syntax source in + type_file syntax source_filename in let%bind parameter' = parsify_expression syntax parameter in let options = @@ -257,9 +275,10 @@ let run_function ?amount source entry_point parameter syntax = (make_options ?amount ()) in Run_simplified.run_simplityped ~options typed entry_point parameter' -let evaluate_value ?amount source entry_point syntax = +let evaluate_value ?amount source_filename entry_point syntax = + let%bind syntax = syntax_to_variant syntax (Some source_filename) in let%bind typed = - type_file syntax source in + type_file syntax source_filename in let options = let open Proto_alpha_utils.Memory_proto_alpha in let amount = Option.bind (fun amount -> Alpha_context.Tez.of_string amount) amount in diff --git a/src/test/bin_tests.ml b/src/test/bin_tests.ml index d54239a64..2ee1485bc 100644 --- a/src/test/bin_tests.ml +++ b/src/test/bin_tests.ml @@ -4,7 +4,7 @@ open Test_helpers let compile_contract_basic () : unit result = let%bind _ = - compile_contract_file "./contracts/dispatch-counter.ligo" "main" "pascaligo" + compile_contract_file "./contracts/dispatch-counter.ligo" "main" (Syntax_name "pascaligo") in ok () diff --git a/src/test/coase_tests.ml b/src/test/coase_tests.ml index 9d385ebc0..0db0e53f8 100644 --- a/src/test/coase_tests.ml +++ b/src/test/coase_tests.ml @@ -4,7 +4,7 @@ open Trace open Ligo.Run open Test_helpers -let type_file = type_file "pascaligo" +let type_file = type_file `pascaligo let get_program = let s = ref None in diff --git a/src/test/heap_tests.ml b/src/test/heap_tests.ml index a7be1fbb4..5a6f440df 100644 --- a/src/test/heap_tests.ml +++ b/src/test/heap_tests.ml @@ -2,7 +2,7 @@ open Trace open Ligo.Run open Test_helpers -let type_file = type_file "pascaligo" +let type_file = type_file `pascaligo let get_program = let s = ref None in diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index bbc62d39a..55445db99 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -4,8 +4,8 @@ open Test_helpers open Ast_simplified.Combinators -let mtype_file ?debug_simplify ?debug_typed = type_file ?debug_simplify ?debug_typed "cameligo" -let type_file = type_file "pascaligo" +let mtype_file ?debug_simplify ?debug_typed = type_file ?debug_simplify ?debug_typed `cameligo +let type_file = type_file `pascaligo let type_alias () : unit result = let%bind program = type_file "./contracts/type-alias.ligo" in diff --git a/src/test/vote_tests.ml b/src/test/vote_tests.ml index 228005abc..d4d1f9336 100644 --- a/src/test/vote_tests.ml +++ b/src/test/vote_tests.ml @@ -7,7 +7,7 @@ let get_program = fun () -> match !s with | Some s -> ok s | None -> ( - let%bind program = type_file "cameligo" "./contracts/vote.mligo" in + let%bind program = type_file `cameligo "./contracts/vote.mligo" in s := Some program ; ok program )