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

Auto-detect syntax based on file extension

See merge request ligolang/ligo!27
This commit is contained in:
Matej Šima 2019-06-12 13:23:52 +00:00
commit 69f867edee
2 changed files with 24 additions and 8 deletions

View File

@ -57,9 +57,9 @@ let entry_point n =
info ~docv ~doc [] in
required @@ pos n (some string) (Some "main") info
let expression n =
let expression purpose n =
let open Arg in
let docv = "EXPRESSION" in
let docv = purpose ^ "_EXPRESSION" in
let doc = "$(docv) is the expression that will be compiled." in
let info = info ~docv ~doc [] in
required @@ pos n (some string) None info
@ -68,9 +68,9 @@ let syntax =
let open Arg in
let info =
let docv = "SYNTAX" in
let doc = "$(docv) is the syntax that will be used. Currently supported syntaxes are \"pascaligo\" and \"cameligo\". \"pascaligo\" is the default." in
let doc = "$(docv) is the syntax that will be used. Currently supported syntaxes are \"pascaligo\" and \"cameligo\". By default, the syntax is guessed from the extension (.ligo and .mligo, respectively)." in
info ~docv ~doc ["syntax" ; "s"] in
value @@ opt string "pascaligo" info
value @@ opt string "auto" info
let compile_file =
let f source entry_point syntax =
@ -97,7 +97,7 @@ let compile_parameter =
ok ()
in
let term =
Term.(const f $ source 0 $ entry_point 1 $ expression 2 $ syntax) in
Term.(const f $ source 0 $ entry_point 1 $ expression "PARAMETER" 2 $ syntax) 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)
@ -112,7 +112,7 @@ let compile_storage =
ok ()
in
let term =
Term.(const f $ source 0 $ entry_point 1 $ expression 2 $ syntax) in
Term.(const f $ source 0 $ entry_point 1 $ expression "STORAGE" 2 $ syntax) 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)
@ -126,7 +126,7 @@ let dry_run =
ok ()
in
let term =
Term.(const f $ source 0 $ entry_point 1 $ expression 2 $ expression 3 $ syntax) in
Term.(const f $ source 0 $ entry_point 1 $ expression "PARAMETER" 2 $ expression "STORAGE" 3 $ syntax) in
let cmdname = "dry-run" in
let docs = "Subcommand: run a smart-contract with the given storage and input." in
(term , Term.info ~docs cmdname)
@ -140,7 +140,7 @@ let run_function =
ok ()
in
let term =
Term.(const f $ source 0 $ entry_point 1 $ expression 2 $ syntax) in
Term.(const f $ source 0 $ entry_point 1 $ expression "PARAMETER" 2 $ syntax) in
let cmdname = "run-function" in
let docs = "Subcommand: run a function with the given parameter." in
(term , Term.info ~docs cmdname)

View File

@ -95,7 +95,22 @@ 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
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
@ -104,6 +119,7 @@ let parsify = fun syntax source ->
parsify source
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