commit e271d92eca4513e7c86634c5b5ba59ce3acd9ddb
parent 1647c0a2bfde02822f90d210221d28452d82c16f
Author: Sebastian <sebastian@sebsite.pw>
Date: Thu, 7 Nov 2024 19:21:19 -0500
hare::unparse: always include decimal point for fconst
Previously, flexible float literals were unparsed as integer literals if
they were whole numbers.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
2 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/hare/parse/+test/expr_test.ha b/hare/parse/+test/expr_test.ha
@@ -111,7 +111,7 @@
"};\n");
};
-@test fn constant() void = {
+@test fn literal() void = {
roundtrip(`export fn main() void = {
2 + (-4 + void) * true % done / ("hello" << '?');
[1, 2, 3, 4];
@@ -151,6 +151,11 @@
coords {
...
};
+ 0.0;
+ 0f32;
+ 0f64;
+ 1.0;
+ 1.0e10;
13.37;
13.37f32;
13.37f64;
diff --git a/hare/unparse/expr.ha b/hare/unparse/expr.ha
@@ -592,37 +592,39 @@ fn literal(
z += syn(ctx, "]", synkind::PUNCTUATION)?;
return z;
case let v: ast::number_literal =>
- const s = fmt::asprintf("{}{}", v.value, switch (v.suff) {
+ const s = switch (v.suff) {
case ltok::LIT_U8 =>
- yield "u8";
+ yield fmt::asprintf("{}u8", v.value);
case ltok::LIT_U16 =>
- yield "u16";
+ yield fmt::asprintf("{}u16", v.value);
case ltok::LIT_U32 =>
- yield "u32";
+ yield fmt::asprintf("{}u32", v.value);
case ltok::LIT_U64 =>
- yield "u64";
+ yield fmt::asprintf("{}u64", v.value);
case ltok::LIT_UINT =>
- yield "u";
+ yield fmt::asprintf("{}u", v.value);
case ltok::LIT_SIZE =>
- yield "z";
+ yield fmt::asprintf("{}z", v.value);
case ltok::LIT_I8 =>
- yield "i8";
+ yield fmt::asprintf("{}i8", v.value);
case ltok::LIT_I16 =>
- yield "i16";
+ yield fmt::asprintf("{}i16", v.value);
case ltok::LIT_I32 =>
- yield "i32";
+ yield fmt::asprintf("{}i32", v.value);
case ltok::LIT_I64 =>
- yield "i64";
+ yield fmt::asprintf("{}i64", v.value);
case ltok::LIT_INT =>
- yield "i";
- case ltok::LIT_ICONST, ltok::LIT_FCONST =>
- yield "";
+ yield fmt::asprintf("{}i", v.value);
+ case ltok::LIT_ICONST =>
+ yield fmt::asprint(v.value);
+ case ltok::LIT_FCONST =>
+ yield fmt::asprintf("{:F.}", v.value);
case ltok::LIT_F32 =>
- yield "f32";
+ yield fmt::asprintf("{}f32", v.value);
case ltok::LIT_F64 =>
- yield "f64";
+ yield fmt::asprintf("{}f64", v.value);
case => abort();
- });
+ };
defer free(s);
return syn(ctx, s, synkind::NUMBER)?;
case let sc: ast::struct_literal =>