Skip to content

Commit

Permalink
feat: allow melange to work with OCaml 4.14 (#1028)
Browse files Browse the repository at this point in the history
* 4.14 conditionals

* 4.14 AST

* wip

* wip
  • Loading branch information
anmonteiro authored Jan 17, 2024
1 parent df3860d commit d2715c4
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 27 deletions.
8 changes: 5 additions & 3 deletions bin/melc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ module As_ppx = struct
Cmd_ppx_apply.apply_rewriters ~tool_name:"melppx" kind ast


module Melange_ast_version = Melangelib.Ast_io.Melange_ast_version

module Convert =
Ppxlib_ast.Convert
(Ppxlib_ast__.Versions.OCaml_501)
(Melange_ast_version)
(Ppxlib_ast__.Versions.OCaml_current)

let apply_lazy ~source ~target =
Expand All @@ -98,7 +100,7 @@ let apply_lazy ~source ~target =
| Intf ast ->
let ast: Ppxlib_ast__.Versions.OCaml_current.Ast.Parsetree.signature =
let ast = apply ~kind:Ml_binary.Mli ast in
let ppxlib_ast: Ppxlib_ast__.Versions.OCaml_501.Ast.Parsetree.signature =
let ppxlib_ast: Melange_ast_version.Ast.Parsetree.signature =
Obj.magic ast
in
Convert.copy_signature ppxlib_ast
Expand All @@ -112,7 +114,7 @@ let apply_lazy ~source ~target =
let ast: Melange_compiler_libs.Parsetree.structure =
apply ~kind:Ml_binary.Ml ast
in
let ppxlib_ast: Ppxlib_ast__.Versions.OCaml_501.Ast.Parsetree.structure =
let ppxlib_ast: Melange_ast_version.Ast.Parsetree.structure =
Obj.magic ast
in
Convert.copy_structure ppxlib_ast
Expand Down
30 changes: 15 additions & 15 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion jscomp/core/ast_io.ml → jscomp/core/ast_io.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

open Import

module Melange_ast_version =
#if OCAML_VERSION >= (5, 1, 0)
Ppxlib_ast__.Versions.OCaml_501
#else
Ppxlib_ast__.Versions.OCaml_414
#endif

module Compiler_version = Ppxlib_ast.Compiler_version

module type OCaml_version = Ppxlib_ast.OCaml_version
Expand All @@ -35,7 +43,7 @@ module Intf_or_impl = struct
module Convert =
Ppxlib_ast.Convert
(Ppxlib_ast.Selected_ast)
(Ppxlib_ast__.Versions.OCaml_501)
(Melange_ast_version)

let ppxlib_impl : Ppxlib_ast.Ast.structure -> t =
fun stru ->
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/ast_io.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US. *)

open Import
module Melange_ast_version : Ppxlib_ast.OCaml_version
module Compiler_version = Ppxlib_ast.Compiler_version

module type OCaml_version = Ppxlib_ast.OCaml_version
Expand Down
52 changes: 52 additions & 0 deletions jscomp/core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,58 @@
ppxlib.ast
dune-build-info))

(rule
(targets lam_convert.ml)
(deps lam_convert.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))

(rule
(targets polyvar_pattern_match.ml)
(deps polyvar_pattern_match.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))

(rule
(targets initialization.ml)
(deps initialization.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))

(rule
(targets ast_io.ml)
(deps ast_io.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))

(rule
(target include_dirs.ml)
(deps include_dirs.dev.ml include_dirs.release.ml)
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/gen/record_fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ let make type_declaration =
[
Typ.var "state"; Typ.constr { txt = Lident name; loc } [];
])))
(StringSet.to_list customNames))
(StringSet.to_seq customNames |> List.of_seq))
in
let iter =
Ast_helper.Type.mk
Expand All @@ -228,7 +228,7 @@ let make type_declaration =
~f:(fun s ->
let lid = { Asttypes.txt = Longident.Lident s; loc } in
(lid, Ast_helper.Exp.ident lid))
(StringSet.to_list customNames))
(StringSet.to_seq customNames |> List.of_seq))
None
in
[%stri let super : 'state iter = [%e super]]
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/gen/record_iter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ let make type_declaration =
Type.field { txt = name; loc }
(Typ.constr { txt = Lident "fn"; loc }
[ Typ.constr { txt = Lident name; loc } [] ])))
(StringSet.to_list customNames))
(StringSet.to_seq customNames |> List.of_seq))
in
let iter = Ast_helper.Type.mk ~kind:record { txt = "iter"; loc } in
let fn =
Expand All @@ -206,7 +206,7 @@ let make type_declaration =
~f:(fun s ->
let lid = { Asttypes.txt = Longident.Lident s; loc } in
(lid, Ast_helper.Exp.ident lid))
(StringSet.to_list customNames))
(StringSet.to_seq customNames |> List.of_seq))
None
in
[%stri let super : iter = [%e super]]
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/gen/record_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ let make type_declaration =
Type.field { txt = name; loc }
(Typ.constr { txt = Lident "fn"; loc }
[ Typ.constr { txt = Lident name; loc } [] ])))
(StringSet.to_list customNames))
(StringSet.to_seq customNames |> List.of_seq))
in
let iter = Ast_helper.Type.mk ~kind:record { txt = "iter"; loc } in
let fn =
Expand All @@ -236,7 +236,7 @@ let make type_declaration =
~f:(fun s ->
let lid = { Asttypes.txt = Longident.Lident s; loc } in
(lid, Ast_helper.Exp.ident lid))
(StringSet.to_list customNames))
(StringSet.to_seq customNames |> List.of_seq))
None
in
[%stri let super : iter = [%e super]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ module Perfile = struct
Typemod.initial_env
~loc:(Location.in_file "command line")
~initially_opened_module
#if OCAML_VERSION < (5,0,0)
~safe_string:true
#endif
~open_implicit_modules:(List.rev !Clflags.open_modules)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
| Blk_na s ->
let info : Lam.Tag_info.t = Blk_na s in
prim ~primitive:(Pmakeblock (tag, info, mutable_flag)) ~args loc)
#if OCAML_VERSION >= (5, 1, 0)
| Pfield (id, _ptr, _mut, info) ->
#else
| Pfield (id, info) ->
#endif
prim ~primitive:(Pfield (id, info)) ~args loc
| Psetfield (id, _, _initialization_or_assignment, info) ->
prim ~primitive:(Psetfield (id, info)) ~args loc
Expand Down Expand Up @@ -428,11 +432,13 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
| Pbbswap i -> prim ~primitive:(Pbbswap i) ~args loc
| Pbswap16 -> prim ~primitive:Pbswap16 ~args loc
| Pduparray _ -> assert false
#if OCAML_VERSION >= (5, 1, 0)
| Prunstack | Pperform | Presume | Preperform | Patomic_exchange | Patomic_cas
| Patomic_fetch_add | Pdls_get | Patomic_load _ ->
Location.raise_errorf ~loc
"OCaml 5 multicore primitives (Effect, Condition, Semaphore) are not \
currently supported in Melange"
#endif

(* Does not exist since we compile array in js backend unlike native backend *)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,9 @@ let call_switcher_variant_constr (loc : Lambda.scoped_location)
( Alias,
Pgenval,
v,
#if OCAML_VERSION >= (5, 1, 0)
Lprim (Pfield (0, Pointer, Immutable, Fld_poly_var_tag), [ arg ], loc),
#else
Lprim (Pfield (0, Fld_poly_var_tag), [ arg ], loc),
#endif
call_switcher_variant_constant loc fail (Lvar v) int_lambda_list names )
2 changes: 1 addition & 1 deletion playground/mel_playground.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open Melstd
open Melangelib
open Melange_compiler_libs
module Js = Jsoo_runtime.Js
module Melange_OCaml_version = Ppxlib_ast__.Versions.OCaml_501
module Melange_OCaml_version = Ast_io.Melange_ast_version

module Melange_ast = struct
external to_ppxlib :
Expand Down
2 changes: 1 addition & 1 deletion vendor/melange-compiler-libs

0 comments on commit d2715c4

Please sign in to comment.