refactor(plugins): add scope as argument for the apply function

This commit is contained in:
Emile Rolley 2022-07-26 17:27:42 +02:00
parent 8e3fd34424
commit 6ce6ea8afc
5 changed files with 27 additions and 14 deletions

View File

@ -345,7 +345,8 @@ let driver source_file (options : Cli.options) : int =
p.Plugin.name; p.Plugin.name;
Cli.debug_print "Writing to %s..." Cli.debug_print "Writing to %s..."
(Option.value ~default:"stdout" output_file); (Option.value ~default:"stdout" output_file);
p.Plugin.apply output_file prgm type_ordering p.Plugin.apply ~output_file ~scope:options.ex_scope prgm
type_ordering
| (`Python | `Scalc | `Plugin (Plugin.Scalc _)) as backend -> ( | (`Python | `Scalc | `Plugin (Plugin.Scalc _)) as backend -> (
let prgm = Scalc.Compile_from_lambda.translate_program prgm in let prgm = Scalc.Compile_from_lambda.translate_program prgm in
match backend with match backend with
@ -383,7 +384,8 @@ let driver source_file (options : Cli.options) : int =
p.Plugin.name; p.Plugin.name;
Cli.debug_print "Writing to %s..." Cli.debug_print "Writing to %s..."
(Option.value ~default:"stdout" output_file); (Option.value ~default:"stdout" output_file);
p.Plugin.apply output_file prgm type_ordering))))))); p.Plugin.apply ~output_file ~scope:options.ex_scope prgm
type_ordering)))))));
0 0
with with
| Errors.StructuredError (msg, pos) -> | Errors.StructuredError (msg, pos) ->

View File

@ -14,10 +14,17 @@
License for the specific language governing permissions and limitations under License for the specific language governing permissions and limitations under
the License. *) the License. *)
type 'ast plugin_apply_fun_typ =
output_file:string option ->
scope:string option ->
'ast ->
Scopelang.Dependency.TVertex.t list ->
unit
type 'ast gen = { type 'ast gen = {
name : string; name : string;
extension : string; extension : string;
apply : string option -> 'ast -> Scopelang.Dependency.TVertex.t list -> unit; apply : 'ast plugin_apply_fun_typ;
} }
type t = type t =

View File

@ -16,10 +16,17 @@
(** {2 catala-facing API} *) (** {2 catala-facing API} *)
type 'ast plugin_apply_fun_typ =
output_file:string option ->
scope:string option ->
'ast ->
Scopelang.Dependency.TVertex.t list ->
unit
type 'ast gen = { type 'ast gen = {
name : string; name : string;
extension : string; extension : string;
apply : string option -> 'ast -> Scopelang.Dependency.TVertex.t list -> unit; apply : 'ast plugin_apply_fun_typ;
} }
type t = type t =
@ -41,19 +48,13 @@ module PluginAPI : sig
val register_lcalc : val register_lcalc :
name:string -> name:string ->
extension:string -> extension:string ->
(string option -> Dcalc.Ast.untyped Lcalc.Ast.program plugin_apply_fun_typ ->
Dcalc.Ast.untyped Lcalc.Ast.program ->
Scopelang.Dependency.TVertex.t list ->
unit) ->
unit unit
val register_scalc : val register_scalc :
name:string -> name:string ->
extension:string -> extension:string ->
(string option -> Scalc.Ast.program plugin_apply_fun_typ ->
Scalc.Ast.program ->
Scopelang.Dependency.TVertex.t list ->
unit) ->
unit unit
end end

View File

@ -414,9 +414,11 @@ module To_jsoo = struct
end end
let apply let apply
(output_file : string option) ~(output_file : string option)
~(scope : string option)
(prgm : 'm Lcalc.Ast.program) (prgm : 'm Lcalc.Ast.program)
(type_ordering : Scopelang.Dependency.TVertex.t list) = (type_ordering : Scopelang.Dependency.TVertex.t list) =
let _ = scope in
let filename_without_ext_opt = let filename_without_ext_opt =
Option.map Option.map
(fun f -> Filename.basename f |> String.split_on_char '.' |> List.hd) (fun f -> Filename.basename f |> String.split_on_char '.' |> List.hd)

View File

@ -23,7 +23,8 @@
let name = "python-plugin" let name = "python-plugin"
let extension = ".py" let extension = ".py"
let apply output_file prgm type_ordering = let apply ~output_file ~scope prgm type_ordering =
ignore scope;
Utils.File.with_formatter_of_opt_file output_file @@ fun fmt -> Utils.File.with_formatter_of_opt_file output_file @@ fun fmt ->
Scalc.To_python.format_program fmt prgm type_ordering Scalc.To_python.format_program fmt prgm type_ordering