Skip to content

Commit

Permalink
fix: print comments again
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Sep 25, 2024
1 parent 02345a6 commit 16be3eb
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
melange-compiler-libs = {
# this changes rarely, and it's better than having to rely on nix's poor
# support for submodules
url = "github:melange-re/melange-compiler-libs";
url = "github:melange-re/melange-compiler-libs/anmonteiro/mel-as-variants";
inputs.flake-utils.follows = "flake-utils";
inputs.nixpkgs.follows = "nixpkgs";
};
Expand Down
12 changes: 6 additions & 6 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,9 @@ and expression_desc cxt ~(level : int) x : cxt =
in
( Js_op.Lit L.tag,
{
(Option.value
(Option.map E.as_value as_value.as_modifier)
~default:tag)
(match as_value.as_modifier with
| Some modifier -> E.as_value modifier
| None -> tag)
with
comment = Some as_value.name;
} )
Expand All @@ -824,9 +824,9 @@ and expression_desc cxt ~(level : int) x : cxt =
in
( Js_op.Lit L.tag,
{
(Option.value
(Option.map E.as_value as_value.as_modifier)
~default:tag)
(match as_value.as_modifier with
| Some modifier -> E.as_value modifier
| None -> tag)
with
comment = Some as_value.name;
} )
Expand Down
11 changes: 8 additions & 3 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,14 @@ let true_ : t = make_expression (Bool true)
let false_ : t = make_expression (Bool false)
let bool v = if v then true_ else false_

let as_value = function
| Lambda.String s -> str s (* ~delim:DStarJ *)
| Int i -> small_int i
let as_value ?comment modifier =
{
(match modifier with
| Lambda.String s -> str s (* ~delim:DStarJ *)
| Int i -> small_int i)
with
comment;
}

let array_index ?loc ?comment (e0 : t) (e1 : t) : t =
match (e0.expression_desc, e1.expression_desc) with
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ val assign_by_int : ?loc:Location.t -> ?comment:string -> t -> int32 -> t -> t

val assign_by_exp : t -> t -> t -> t
val assign : ?loc:Location.t -> ?comment:string -> t -> t -> t
val as_value : Import.Lambda.as_modifier -> t
val as_value : ?comment:string -> Import.Lambda.as_modifier -> t
val triple_equal : ?loc:Location.t -> ?comment:string -> t -> t -> t
(* TODO: reduce [triple_equal] use *)

Expand Down
3 changes: 1 addition & 2 deletions jscomp/core/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ let int_switch ?(comment : string option)

let string_switch ?(comment : string option)
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
(e : J.expression) (clauses : (Lambda.as_modifier * J.case_clause) list) : t
=
(e : J.expression) (clauses : J.string_clause list) : t =
match e.expression_desc with
| Str (_, txt) | Unicode txt -> (
let continuation =
Expand Down
16 changes: 8 additions & 8 deletions jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,7 @@ and compile_cases cxt (switch_exp : E.t) table default ~get_cstr_name =
(fun i ->
match get_cstr_name i with
| Some { name; as_modifier = Some modifier } ->
{
(match modifier with
| Int modifier -> E.small_int modifier
| String s -> E.str s)
with
comment = Some name;
}
E.as_value ~comment:name modifier
| Some { name; as_modifier = None } ->
{ (E.small_int i) with comment = Some name }
| None -> E.small_int i)
Expand Down Expand Up @@ -757,7 +751,13 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
| EffectCall _ | Assign _ -> Js_output.make (compile_whole lambda_cxt)

and compile_string_cases ~get_cstr_name cxt switch_exp table default =
compile_general_cases ~get_cstr_name E.as_value E.string_equal cxt
compile_general_cases ~get_cstr_name
(fun as_value ->
let comment =
get_cstr_name as_value |> Option.map (fun x -> x.Lambda.name)
in
E.as_value ?comment as_value)
E.string_equal cxt
(fun ?default ?declaration e clauses ->
S.string_switch ?default ?declaration e clauses)
switch_exp table default
Expand Down
2 changes: 1 addition & 1 deletion test/blackbox-tests/complex-constant-inline.t
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
message = "b";
} else {
let tmp = 12345;
message = typeof tmp === "number" || tmp.TAG === /* Vacations */0 ? "a" : "b";
message = /* tag */typeof tmp === "number" || typeof tmp === "string" || tmp.TAG === /* Vacations */0 ? "a" : "b";
}
const Test1 = {
Expand Down
3 changes: 2 additions & 1 deletion test/blackbox-tests/mel-as-inline-records.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Test `@mel.as` in inline records / record extensions
renamed: "Corentin"
};
const user2 = /* User */{
const user2 = {
TAG: /* User */0,
renamed: "Corentin"
};
Expand Down
3 changes: 2 additions & 1 deletion test/blackbox-tests/stdout.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
const Stdlib__Format = require("melange/format.js");
Stdlib__Format.eprintf(/* Format */{
Stdlib__Format.eprintf({
TAG: /* Format */0,
_0: {
TAG: /* String_literal */11,
_0: "hello, world",
Expand Down
3 changes: 2 additions & 1 deletion test/blackbox-tests/unicode-format-strings.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
const Curry = require("melange.js/curry.js");
const Stdlib__Format = require("melange/format.js");
console.log(Curry._1(Stdlib__Format.sprintf(/* Format */{
console.log(Curry._1(Stdlib__Format.sprintf({
TAG: /* Format */0,
_0: {
TAG: /* String_literal */11,
_0: "你 ",
Expand Down

0 comments on commit 16be3eb

Please sign in to comment.