From d933a9bc08e66d50d34bd562a4e8c5fdedd6bb88 Mon Sep 17 00:00:00 2001 From: Leandro Ostera Date: Fri, 30 Oct 2020 18:10:26 +0100 Subject: [PATCH] caramelc: add target to dump ast of compilation from ocaml to erlang --- src/bin/cmd_parse.ml | 30 ++++++++++++++++++- src/compiler/compiler.ml | 1 + src/compiler/ocaml_to_erlang/ast_transl.ml | 4 +-- src/compiler/ocaml_to_erlang/ast_transl.mli | 2 +- .../ocaml_to_erlang/ocaml_to_erlang.ml | 3 +- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/bin/cmd_parse.ml b/src/bin/cmd_parse.ml index 3ca5c47c7d..87095db23d 100644 --- a/src/bin/cmd_parse.ml +++ b/src/bin/cmd_parse.ml @@ -39,12 +39,37 @@ let pp_ocaml_typedtree ~stdlib_path source_file = |> Printtyped.implementation_with_coercion i.ppf_dump with Env.Error err -> Env.report_error i.ppf_dump err) +let pp_ocaml_to_erlang_parsetree ~stdlib_path source_file = + let tool_name = "caramelc-" ^ name in + Compile_common.with_info ~native:false ~tool_name ~source_file + ~output_prefix:".none" ~dump_ext:"cmo" (fun i -> + Caramel_compiler.Compiler.initialize_compiler ~stdlib_path (); + let typed, _ = + try + Compile_common.parse_impl i + |> Typemod.type_implementation i.source_file i.output_prefix + i.module_name i.env + with Env.Error err -> + Env.report_error i.ppf_dump err; + exit 1 + in + let signature = + Caramel_compiler.Compiler.Ocaml_to_erlang.read_signature i + in + typed + |> Caramel_compiler.Compiler.Ocaml_to_erlang.Ast_transl.from_typedtree + ~module_name:source_file ~signature + |> List.iter (fun t -> + Erlang.Ast.sexp_of_t t |> Sexplib.Sexp.pp_hum_indent 2 i.ppf_dump; + Format.fprintf i.ppf_dump "\n\n%!")) + let run stdlib_path sources language tree = let parser = match (language, tree) with | `Erlang, _ -> pp_erlang_parsetree | `OCaml, `Parsetree -> pp_ocaml_parsetree | `OCaml, `Typedtree -> pp_ocaml_typedtree ~stdlib_path + | `OCaml_to_erlang, _ -> pp_ocaml_to_erlang_parsetree ~stdlib_path in List.iter parser sources @@ -62,7 +87,10 @@ let cmd = & info [ "t"; "tree" ] ~docv:"tree" ~doc:"Which stage AST to print") in let language = - let languages = Arg.enum [ ("erl", `Erlang); ("ml", `OCaml) ] in + let languages = + Arg.enum + [ ("erl", `Erlang); ("ml", `OCaml); ("ml-to-erl", `OCaml_to_erlang) ] + in Arg.( value & opt ~vopt:`Erlang languages `Erlang diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index 0546b0fe2f..0ee50c9d04 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -2,6 +2,7 @@ open Comp_misc.Opts module Dependency_sorter = Comp_misc.Dependency_sorter module Source_tagger = Comp_misc.Source_tagger module Target = Comp_misc.Target +module Ocaml_to_erlang = Ocaml_to_erlang let tool_name = "caramelc" diff --git a/src/compiler/ocaml_to_erlang/ast_transl.ml b/src/compiler/ocaml_to_erlang/ast_transl.ml index 830cacd854..cd233aec86 100644 --- a/src/compiler/ocaml_to_erlang/ast_transl.ml +++ b/src/compiler/ocaml_to_erlang/ast_transl.ml @@ -74,10 +74,10 @@ let rec find_modules : *) let from_typedtree : module_name:string -> + signature:Types.signature option -> Typedtree.structure -> - Types.signature option -> Erlang.Ast.t list = - fun ~module_name typedtree signature -> + fun ~module_name ~signature typedtree -> let top_module = Erl.Atom.(lowercase (mk module_name)) in let modules = List.fold_left diff --git a/src/compiler/ocaml_to_erlang/ast_transl.mli b/src/compiler/ocaml_to_erlang/ast_transl.mli index aab4e5baa7..1f250ebdc7 100644 --- a/src/compiler/ocaml_to_erlang/ast_transl.mli +++ b/src/compiler/ocaml_to_erlang/ast_transl.mli @@ -1,5 +1,5 @@ val from_typedtree : module_name:string -> + signature:Types.signature option -> Typedtree.structure -> - Types.signature option -> Erlang.Ast.t list diff --git a/src/compiler/ocaml_to_erlang/ocaml_to_erlang.ml b/src/compiler/ocaml_to_erlang/ocaml_to_erlang.ml index dce9c99644..42cdc7d864 100644 --- a/src/compiler/ocaml_to_erlang/ocaml_to_erlang.ml +++ b/src/compiler/ocaml_to_erlang/ocaml_to_erlang.ml @@ -1,4 +1,5 @@ open Compile_common +module Ast_transl = Ast_transl let tool_name = "caramelc:ml-to-erl" @@ -43,7 +44,7 @@ let compile ~source_file ~output_prefix ~opts:_ = let _ = emit_bytecode info bytecode in let signature = read_signature info in let module_name = info.module_name in - let erl_ast = Ast_transl.from_typedtree ~module_name typed signature in + let erl_ast = Ast_transl.from_typedtree ~module_name ~signature typed in Erlang.Printer.to_sources erl_ast in Compile_common.with_info ~native:false ~tool_name ~source_file ~output_prefix