Add option to execute scope; the command 'catala run -s S' executes scope S

This commit is contained in:
Nicolas Chataing 2020-08-06 15:44:51 +02:00
parent d162bbc9f0
commit f801a164e6
2 changed files with 29 additions and 15 deletions

View File

@ -39,7 +39,7 @@ let backend =
& pos 0 (some string) None
& info [] ~docv:"BACKEND" ~doc:"Backend selection among: LaTeX, Makefile, Html, Interpret")
type backend_option = Latex | Makefile | Html | Interpret
type backend_option = Latex | Makefile | Html | Run
let language =
Arg.(
@ -48,6 +48,10 @@ let language =
& info [ "l"; "language" ] ~docv:"LANG"
~doc:"Input language among: en, fr, non-verbose (default non-verbose)")
let ex_scope =
Arg.(
value & opt (some string) None & info [ "s"; "scope" ] ~docv:"SCOPE" ~doc:"Scope to be executed")
type frontend_lang = [ `Fr | `En | `NonVerbose ]
type backend_lang = [ `Fr | `En ]
@ -71,7 +75,9 @@ let pygmentize_loc =
~doc:"Location of a custom pygmentize executable for LaTeX source code highlighting")
let catala_t f =
Term.(const f $ file $ debug $ wrap_weaved_output $ pygmentize_loc $ backend $ language $ output)
Term.(
const f $ file $ debug $ wrap_weaved_output $ pygmentize_loc $ backend $ language $ ex_scope
$ output)
let info =
let doc =

View File

@ -15,7 +15,7 @@
(** Entry function for the executable. Returns a negative number in case of error. *)
let driver (source_file : string) (debug : bool) (wrap_weaved_output : bool)
(pygmentize_loc : string option) (backend : string) (language : string option)
(output_file : string option) : int =
(ex_scope : string option) (output_file : string option) : int =
Cli.debug_flag := debug;
Cli.debug_print "Reading files...";
if Filename.extension source_file <> ".catala" then begin
@ -38,7 +38,7 @@ let driver (source_file : string) (debug : bool) (wrap_weaved_output : bool)
if backend = "Makefile" then Cli.Makefile
else if backend = "LaTeX" then Cli.Latex
else if backend = "HTML" then Cli.Html
else if backend = "Interpret" then Cli.Interpret
else if backend = "run" then Cli.Run
else begin
Cli.error_print
(Printf.sprintf "The selected backend (%s) is not supported by Catala" backend);
@ -101,21 +101,29 @@ let driver (source_file : string) (debug : bool) (wrap_weaved_output : bool)
with Errors.WeavingError msg ->
Cli.error_print msg;
exit (-1) )
| Cli.Interpret -> (
| Cli.Run -> (
try
let ctxt = Context.form_context program in
let scope_uid =
match ex_scope with
| None ->
Cli.error_print "No scope was provided for execution.";
assert false
| Some name -> (
match Context.IdentMap.find_opt name ctxt.scope_id_to_uid with
| None ->
Cli.error_print (Printf.sprintf "There is no scope %s inside the program." name);
assert false
| Some uid -> uid )
in
let prgm = Firstpass.translate_program_to_scope ctxt program in
let scope = Uid.UidMap.find scope_uid prgm in
let exec_ctxt = Interpreter.execute_scope ctxt prgm scope in
Uid.UidMap.iter
(fun _uid scope ->
Printf.printf "Execution of scope %s:\n" (Uid.get_ident scope.Scope.scope_uid);
let exec_ctxt = Interpreter.execute_scope ctxt prgm scope in
Uid.UidMap.iter
(fun uid value ->
Printf.printf "Var %s:\t%s\n" (Uid.get_ident uid)
(Debug.print_term ((value, Pos.no_pos), TDummy)))
exec_ctxt;
Printf.printf "\n")
prgm;
(fun uid value ->
Printf.printf "Var %s:\t%s\n" (Uid.get_ident uid)
(Debug.print_term ((value, Pos.no_pos), TDummy)))
exec_ctxt;
0
with Errors.ContextError msg | Errors.DefaultConflict msg ->
Cli.error_print msg;