Merge branch 'bugfix/auto-detect-syntax' into 'dev'

Fix auto-detection of syntax

See merge request ligolang/ligo!34
This commit is contained in:
Tom Jack 2019-06-13 00:34:06 +00:00
commit b1b0597510
7 changed files with 70 additions and 51 deletions

View File

@ -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

View File

@ -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
type s_syntax = Syntax_name of string
type v_syntax = [`pascaligo | `cameligo ]
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
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
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 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

View File

@ -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 ()

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
)