Fix auto-detection of syntax, use a polymorphic variant to ensure all functions were updated.

This commit is contained in:
Georges Dupéron 2019-06-13 01:43:16 +02:00
parent 3f2a74fa42
commit ded9c4f58f
7 changed files with 70 additions and 51 deletions

View File

@ -45,7 +45,7 @@ let source n =
let open Arg in let open Arg in
let info = let info =
let docv = "SOURCE_FILE" in 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 info ~docv ~doc [] in
required @@ pos n (some string) None info required @@ pos n (some string) None info
@ -85,7 +85,7 @@ let compile_file =
toplevel @@ toplevel @@
let%bind contract = let%bind contract =
trace (simple_info "compiling contract to michelson") @@ 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 ; Format.printf "%s\n" contract ;
ok () ok ()
in in
@ -100,7 +100,7 @@ let compile_parameter =
toplevel @@ toplevel @@
let%bind value = let%bind value =
trace (simple_error "compile-input") @@ 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; Format.printf "%s\n" value;
ok () ok ()
in in
@ -115,7 +115,7 @@ let compile_storage =
toplevel @@ toplevel @@
let%bind value = let%bind value =
trace (simple_error "compile-storage") @@ 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; Format.printf "%s\n" value;
ok () ok ()
in in
@ -129,7 +129,7 @@ let dry_run =
let f source entry_point storage input amount syntax = let f source entry_point storage input amount syntax =
toplevel @@ toplevel @@
let%bind output = 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 ; Format.printf "%a\n" Ast_simplified.PP.expression output ;
ok () ok ()
in in
@ -143,7 +143,7 @@ let run_function =
let f source entry_point parameter amount syntax = let f source entry_point parameter amount syntax =
toplevel @@ toplevel @@
let%bind output = 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 ; Format.printf "%a\n" Ast_simplified.PP.expression output ;
ok () ok ()
in in
@ -157,7 +157,7 @@ let evaluate_value =
let f source entry_point amount syntax = let f source entry_point amount syntax =
toplevel @@ toplevel @@
let%bind output = 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 ; Format.printf "%a\n" Ast_simplified.PP.expression output ;
ok () ok ()
in in

View File

@ -95,40 +95,54 @@ let parsify_expression_ligodity = fun source ->
Simplify.Ligodity.simpl_expression raw in Simplify.Ligodity.simpl_expression raw in
ok simplified ok simplified
let detect_syntax = fun syntax source -> type s_syntax = Syntax_name of string
if String.equal syntax "auto" then type v_syntax = [`pascaligo | `cameligo ]
begin
let syntax_to_variant : s_syntax -> string option -> v_syntax result =
fun syntax source_filename ->
let subr s n = let subr s n =
String.sub s (String.length s - n) n in String.sub s (String.length s - n) n in
if String.equal (subr source 5) ".ligo" let endswith s suffix =
then ok "pascaligo" let suffixlen = String.length suffix in
else if String.equal (subr source 6) ".mligo" ( String.length s >= suffixlen
then ok "cameligo" && String.equal (subr s suffixlen) suffix)
else simple_fail "cannot auto-detect syntax, pleas use -s name_of_syntax"
end
else
ok syntax
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"
in 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 parsify_expression = fun syntax source ->
let%bind syntax = detect_syntax syntax source in
let%bind parsify = match syntax with let%bind parsify = match syntax with
| "pascaligo" -> ok parsify_expression_pascaligo | `pascaligo -> ok parsify_expression_pascaligo
| "cameligo" -> ok parsify_expression_ligodity | `cameligo -> ok parsify_expression_ligodity
| _ -> simple_fail "unrecognized parser"
in in
parsify source parsify source
let compile_contract_file : string -> string -> string -> string result = fun source entry_point syntax -> let compile_contract_file : string -> string -> s_syntax -> string result = fun source_filename entry_point syntax ->
let%bind simplified = parsify syntax source in let%bind syntax = syntax_to_variant syntax (Some source_filename) in
let%bind simplified = parsify syntax source_filename in
let%bind () = let%bind () =
assert_entry_point_defined simplified entry_point in assert_entry_point_defined simplified entry_point in
let%bind typed = 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 Format.asprintf "%a" Michelson.pp_stripped michelson in
ok str 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 (program , parameter_tv) =
let%bind simplified = parsify syntax source in let%bind simplified = parsify syntax source_filename in
let%bind () = let%bind () =
assert_entry_point_defined simplified entry_point in assert_entry_point_defined simplified entry_point in
let%bind typed = let%bind typed =
@ -182,9 +197,10 @@ let compile_contract_parameter : string -> string -> string -> string -> string
ok expr 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 (program , storage_tv) =
let%bind simplified = parsify syntax source in let%bind simplified = parsify syntax source_filename in
let%bind () = let%bind () =
assert_entry_point_defined simplified entry_point in assert_entry_point_defined simplified entry_point in
let%bind typed = let%bind typed =
@ -220,8 +236,8 @@ let compile_contract_storage : string -> string -> string -> string -> string re
ok expr ok expr
let type_file ?(debug_simplify = false) ?(debug_typed = false) let type_file ?(debug_simplify = false) ?(debug_typed = false)
syntax (path:string) : Ast_typed.program result = syntax (source_filename:string) : Ast_typed.program result =
let%bind simpl = parsify syntax path in let%bind simpl = parsify syntax source_filename in
(if debug_simplify then (if debug_simplify then
Format.(printf "Simplified : %a\n%!" Ast_simplified.PP.program simpl) 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 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 = let%bind typed =
type_file syntax source in type_file syntax source_filename in
let%bind storage_simpl = let%bind storage_simpl =
parsify_expression syntax storage in parsify_expression syntax storage in
let%bind input_simpl = let%bind input_simpl =
@ -246,9 +263,10 @@ let run_contract ?amount source entry_point storage input syntax =
(make_options ?amount ()) in (make_options ?amount ()) in
Run_simplified.run_simplityped ~options typed entry_point (Ast_simplified.e_pair storage_simpl input_simpl) 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 = let%bind typed =
type_file syntax source in type_file syntax source_filename in
let%bind parameter' = let%bind parameter' =
parsify_expression syntax parameter in parsify_expression syntax parameter in
let options = let options =
@ -257,9 +275,10 @@ let run_function ?amount source entry_point parameter syntax =
(make_options ?amount ()) in (make_options ?amount ()) in
Run_simplified.run_simplityped ~options typed entry_point parameter' 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 = let%bind typed =
type_file syntax source in type_file syntax source_filename in
let options = let options =
let open Proto_alpha_utils.Memory_proto_alpha in let open Proto_alpha_utils.Memory_proto_alpha in
let amount = Option.bind (fun amount -> Alpha_context.Tez.of_string amount) amount in let amount = Option.bind (fun amount -> Alpha_context.Tez.of_string amount) amount in

View File

@ -4,7 +4,7 @@ open Test_helpers
let compile_contract_basic () : unit result = let compile_contract_basic () : unit result =
let%bind _ = let%bind _ =
compile_contract_file "./contracts/dispatch-counter.ligo" "main" "pascaligo" compile_contract_file "./contracts/dispatch-counter.ligo" "main" (Syntax_name "pascaligo")
in in
ok () ok ()

View File

@ -4,7 +4,7 @@ open Trace
open Ligo.Run open Ligo.Run
open Test_helpers open Test_helpers
let type_file = type_file "pascaligo" let type_file = type_file `pascaligo
let get_program = let get_program =
let s = ref None in let s = ref None in

View File

@ -2,7 +2,7 @@ open Trace
open Ligo.Run open Ligo.Run
open Test_helpers open Test_helpers
let type_file = type_file "pascaligo" let type_file = type_file `pascaligo
let get_program = let get_program =
let s = ref None in let s = ref None in

View File

@ -4,8 +4,8 @@ open Test_helpers
open Ast_simplified.Combinators open Ast_simplified.Combinators
let mtype_file ?debug_simplify ?debug_typed = type_file ?debug_simplify ?debug_typed "cameligo" let mtype_file ?debug_simplify ?debug_typed = type_file ?debug_simplify ?debug_typed `cameligo
let type_file = type_file "pascaligo" let type_file = type_file `pascaligo
let type_alias () : unit result = let type_alias () : unit result =
let%bind program = type_file "./contracts/type-alias.ligo" in let%bind program = type_file "./contracts/type-alias.ligo" in

View File

@ -7,7 +7,7 @@ let get_program =
fun () -> match !s with fun () -> match !s with
| Some s -> ok s | Some s -> ok s
| None -> ( | None -> (
let%bind program = type_file "cameligo" "./contracts/vote.mligo" in let%bind program = type_file `cameligo "./contracts/vote.mligo" in
s := Some program ; s := Some program ;
ok program ok program
) )