Skip to content

Commit

Permalink
caramelc: add target to dump ast of compilation from ocaml to erlang
Browse files Browse the repository at this point in the history
  • Loading branch information
leostera committed Oct 30, 2020
1 parent f7c5b3d commit d933a9b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
30 changes: 29 additions & 1 deletion src/bin/cmd_parse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/compiler/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ocaml_to_erlang/ast_transl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ocaml_to_erlang/ast_transl.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val from_typedtree :
module_name:string ->
signature:Types.signature option ->
Typedtree.structure ->
Types.signature option ->
Erlang.Ast.t list
3 changes: 2 additions & 1 deletion src/compiler/ocaml_to_erlang/ocaml_to_erlang.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open Compile_common
module Ast_transl = Ast_transl

let tool_name = "caramelc:ml-to-erl"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d933a9b

Please sign in to comment.