Skip to content

Commit 3811390

Browse files
authored
Print constants when we are able (#2009)
1 parent febccc3 commit 3811390

File tree

14 files changed

+103
-31
lines changed

14 files changed

+103
-31
lines changed

compiler/src/formatting/fmt.re

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ let needs_grouping = (~parent, ~side: infix_side, expr) => {
254254
} else {
255255
FormatterGrouping;
256256
}
257-
| (PExpConstant(PConstNumber(PConstNumberRational(_, _))), _)
257+
| (PExpConstant(PConstNumber(PConstNumberRational(_))), _)
258258
when op_precedence('/') <= precedence(parent) =>
259259
ParenGrouping
260260
| _ => FormatterGrouping
@@ -314,8 +314,43 @@ let build_document = (~original_source, parsed_program) => {
314314
| _ => failwith("Impossible: non- prefix or infix op")
315315
};
316316
}
317-
and print_constant = (~loc, constant) => {
318-
string(get_original_code(loc));
317+
and print_constant = constant => {
318+
switch (constant) {
319+
| PConstNumber(PConstNumberInt({txt: value})) => string(value)
320+
| PConstNumber(PConstNumberFloat({txt: value})) => string(value)
321+
| PConstNumber(PConstNumberRational({numerator, slash, denominator})) =>
322+
string(numerator.txt)
323+
++ print_comment_range(~lead=space, ~trail=space, numerator.loc, slash)
324+
++ string("/")
325+
++ print_comment_range(
326+
~lead=space,
327+
~trail=space,
328+
slash,
329+
denominator.loc,
330+
)
331+
++ string(denominator.txt)
332+
| PConstInt8({txt: value})
333+
| PConstUint8({txt: value})
334+
| PConstInt16({txt: value})
335+
| PConstUint16({txt: value})
336+
| PConstInt32({txt: value})
337+
| PConstUint32({txt: value})
338+
| PConstInt64({txt: value})
339+
| PConstUint64({txt: value})
340+
| PConstFloat32({txt: value})
341+
| PConstFloat64({txt: value})
342+
| PConstWasmI32({txt: value})
343+
| PConstWasmI64({txt: value})
344+
| PConstWasmF32({txt: value})
345+
| PConstWasmF64({txt: value})
346+
| PConstBigInt({txt: value})
347+
| PConstRational({txt: value})
348+
| PConstBytes({txt: value})
349+
| PConstString({txt: value})
350+
| PConstChar({txt: value}) => string(value)
351+
| PConstBool(value) => string(value ? "true" : "false")
352+
| PConstVoid => string("void")
353+
};
319354
}
320355
and print_punnable_pattern =
321356
(
@@ -508,7 +543,7 @@ let build_document = (~original_source, parsed_program) => {
508543
typ.ptyp_loc,
509544
)
510545
++ print_type(typ)
511-
| PPatConstant(constant) => print_constant(~loc=ppat_loc, constant)
546+
| PPatConstant(constant) => print_constant(constant)
512547
| PPatRecord(pats, closed_flag) =>
513548
braces(
514549
indent(
@@ -1222,8 +1257,7 @@ let build_document = (~original_source, parsed_program) => {
12221257
++ (
12231258
switch (expr.pexp_desc) {
12241259
| PExpId({txt: ident}) => print_identifier(ident)
1225-
| PExpConstant(constant) =>
1226-
print_constant(~loc=expr.pexp_loc, constant)
1260+
| PExpConstant(constant) => print_constant(constant)
12271261
| PExpConstruct({txt: ident, loc: ident_loc}, cstr_expr) =>
12281262
print_identifier(ident)
12291263
++ (

compiler/src/parsing/ast_helper.re

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ let normalize_string = (~loc, item) => {
5050
};
5151
};
5252

53+
module Number = {
54+
let rational = (numerator, slash, denominator) => {
55+
PConstNumberRational({numerator, slash, denominator});
56+
};
57+
};
58+
5359
module Constant = {
5460
let bytes = b => PConstBytes(b);
5561
let string = s => PConstString(s);
@@ -351,15 +357,20 @@ module Expression = {
351357
);
352358
switch (f, a, b) {
353359
| (
354-
{pexp_desc: PExpId({txt: IdentName({txt: "/"})})},
355-
{pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(x)))},
356-
{pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(y)))},
360+
{pexp_desc: PExpId({txt: IdentName({txt: "/", loc: slash_loc})})},
361+
{
362+
pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(numerator))),
363+
},
364+
{
365+
pexp_desc:
366+
PExpConstant(PConstNumber(PConstNumberInt(denominator))),
367+
},
357368
) =>
358369
constant(
359370
~loc,
360371
~core_loc,
361372
~attributes?,
362-
PConstNumber(PConstNumberRational(x, y)),
373+
Constant.number(Number.rational(numerator, slash_loc, denominator)),
363374
)
364375
| _ =>
365376
mk(

compiler/src/parsing/ast_helper.rei

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ type id = loc(Identifier.t);
2626
type str = loc(string);
2727
type loc = Location.t;
2828

29+
module Number: {
30+
let rational: (str, Location.t, str) => number_type;
31+
};
32+
2933
module Constant: {
3034
let bytes: str => constant;
3135
let string: str => constant;

compiler/src/parsing/parser.mly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pattern:
259259
| UNDERSCORE { Pattern.any ~loc:(to_loc $loc) () }
260260
| const { Pattern.constant ~loc:(to_loc (snd $1)) (fst $1) }
261261
// Allow rational numbers in patterns
262-
| DASH? NUMBER_INT SLASH DASH? NUMBER_INT { Pattern.constant ~loc:(to_loc $sloc) @@ Constant.number (PConstNumberRational ((if Option.is_some $1 then (mkstr (fst $loc($1), snd $loc($2)) ("-" ^ $2)) else mkstr $loc($2) $2), (if Option.is_some $4 then (mkstr (fst $loc($4), snd $loc($5)) ("-" ^ $5)) else mkstr $loc($5) $5))) }
262+
| DASH? NUMBER_INT SLASH DASH? NUMBER_INT { Pattern.constant ~loc:(to_loc $sloc) @@ Constant.number (Number.rational (if Option.is_some $1 then (mkstr (fst $loc($1), snd $loc($2)) ("-" ^ $2)) else mkstr $loc($2) $2) (to_loc($loc($3))) (if Option.is_some $4 then (mkstr (fst $loc($4), snd $loc($5)) ("-" ^ $5)) else mkstr $loc($5) $5)) }
263263
| LIDENT { Pattern.var ~loc:(to_loc $loc) (mkstr $loc $1) }
264264
| special_id { Pattern.var ~loc:(to_loc $loc) $1 }
265265
| primitive_ { Pattern.var ~loc:(to_loc $loc) (mkstr $loc $1) }

compiler/src/parsing/parsetree.re

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ type constant =
152152
and number_type =
153153
| PConstNumberInt(loc(string))
154154
| PConstNumberFloat(loc(string))
155-
| PConstNumberRational(loc(string), loc(string));
155+
| PConstNumberRational({
156+
numerator: loc(string),
157+
slash: Location.t,
158+
denominator: loc(string),
159+
});
156160

157161
[@deriving (sexp, yojson)]
158162
type list_item('a) =

compiler/src/parsing/well_formedness.re

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ let literal_has_zero_deniminator = s => {
262262
let no_zero_denominator_rational = (errs, super) => {
263263
let enter_expression = ({pexp_desc: desc, pexp_loc: loc} as e) => {
264264
switch (desc) {
265-
| PExpConstant(PConstNumber(PConstNumberRational(_, {txt: d})))
266-
when string_is_all_zeros_and_underscores(d) =>
265+
| PExpConstant(PConstNumber(PConstNumberRational({denominator})))
266+
when string_is_all_zeros_and_underscores(denominator.txt) =>
267267
errs := [RationalZeroDenominator(loc), ...errs^]
268268
| PExpConstant(PConstRational({txt: s}))
269269
when literal_has_zero_deniminator(s) =>
@@ -274,8 +274,8 @@ let no_zero_denominator_rational = (errs, super) => {
274274
};
275275
let enter_pattern = ({ppat_desc: desc, ppat_loc: loc} as p) => {
276276
switch (desc) {
277-
| PPatConstant(PConstNumber(PConstNumberRational(_, {txt: d})))
278-
when string_is_all_zeros_and_underscores(d) =>
277+
| PPatConstant(PConstNumber(PConstNumberRational({denominator})))
278+
when string_is_all_zeros_and_underscores(denominator.txt) =>
279279
errs := [RationalZeroDenominator(loc), ...errs^]
280280
| PPatConstant(PConstRational({txt: s}))
281281
when literal_has_zero_deniminator(s) =>

compiler/src/typed/checkertypes.re

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ let constant:
284284
),
285285
)
286286
}
287-
| PConstNumber(PConstNumberRational({txt: n}, {txt: d})) =>
287+
| PConstNumber(PConstNumberRational({numerator, denominator})) =>
288288
// TODO(#1168): allow arbitrary-length arguments in rational constants
289-
switch (Literals.conv_number_rational(n, d)) {
289+
switch (Literals.conv_number_rational(numerator.txt, denominator.txt)) {
290290
| Some((n, d)) when d == 1l =>
291291
Ok(Const_number(Const_number_int(Int64.of_int32(n))))
292292
| Some((n, d)) =>
@@ -307,8 +307,8 @@ let constant:
307307
Location.errorf(
308308
~loc,
309309
"Number literal %s/%s is outside of the rational number range of the Number type.",
310-
n,
311-
d,
310+
numerator.txt,
311+
denominator.txt,
312312
),
313313
)
314314
}

compiler/src/typed/parmatch.re

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,10 +1891,11 @@ let untype_constant =
18911891
)
18921892
| Const_number(Const_number_rational({rational_num_rep, rational_den_rep})) =>
18931893
Parsetree.PConstNumber(
1894-
Parsetree.PConstNumberRational(
1895-
Location.mknoloc(rational_num_rep),
1896-
Location.mknoloc(rational_den_rep),
1897-
),
1894+
Parsetree.PConstNumberRational({
1895+
numerator: Location.mknoloc(rational_num_rep),
1896+
slash: Location.dummy_loc,
1897+
denominator: Location.mknoloc(rational_den_rep),
1898+
}),
18981899
)
18991900
| Const_number(Const_number_bigint({bigint_rep})) =>
19001901
Parsetree.PConstNumber(

compiler/test/grainfmt/binops.expected.gr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ let p = 1 + 2 - 3
2929

3030
let p2 = 1 - 2 + 3
3131

32-
let y = 1 / 2 + 3
32+
let y = 1/2 + 3
3333

3434
let ya = 1 / (2 + 3)
3535

36-
let y1 = 1 + 2 /3
36+
let y1 = 1 + 2/3
3737

3838
let c = 1 < 2 || 2 < 3 || 3 < 4
3939

compiler/test/grainfmt/comments.expected.gr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,10 @@ let [> /*foo */] = 5
638638
[/*foo */]
639639
let [] = 5
640640
let [/*foo */] = 5
641+
642+
1 /* abc */ /2
643+
1/ /* abc */ 2
644+
1/ // abc
645+
2
646+
1 /* abc */ /2
647+
1 /* abc */ / /* xyz */ 2

0 commit comments

Comments
 (0)