From 83d6b35afef11c7613a0408a32e832b35a178de7 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 28 May 2025 12:02:24 +0200 Subject: [PATCH] Add `d` as floating point suffix for `double` types. --- releasenotes.md | 1 + resources/grammar/c3.l | 2 +- src/compiler/float.c | 26 +++++++++++-------- src/compiler/lexer.c | 4 +++ test/test_suite/constants/float_type.c3t | 4 +-- test/test_suite/floats/double_float.c3 | 1 + .../test_suite/floats/hex_floats_malformed.c3 | 1 + 7 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 test/test_suite/floats/double_float.c3 create mode 100644 test/test_suite/floats/hex_floats_malformed.c3 diff --git a/releasenotes.md b/releasenotes.md index 747d4cc7c..fd013c502 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -19,6 +19,7 @@ - Allow the use of `has_tagof` on builtin types. - `@jump` now included in `--list-attributes` #2155. - Add `$$matrix_mul` and `$$matrix_transpose` builtins. +- Add `d` as floating point suffix for `double` types. ### Fixes - Assert triggered when casting from `int[2]` to `uint[2]` #2115 diff --git a/resources/grammar/c3.l b/resources/grammar/c3.l index 724ce27f8..6c3a76c59 100644 --- a/resources/grammar/c3.l +++ b/resources/grammar/c3.l @@ -23,7 +23,7 @@ P [Pp][+-]?{D}+ B64 [ \t\v\n\f]?[A-Za-z0-9+/][ \t\v\n\fA-Za-z0-9+/=]+ HEX [ \t\v\n\f]?[A-Fa-f0-9][ \t\v\n\fA-Fa-f0-9]+ INTTYPE ([ui](8|16|32|64|128)|[Uu][Ll]?|[Ll]) -REALTYPE ([f](8|16|32|64|128)?) +REALTYPE ([fd]) INT {D}(_?{D})* HINT {H}(_?{H})* OINT {O}(_?{O})* diff --git a/src/compiler/float.c b/src/compiler/float.c index 269e34287..e9b663941 100644 --- a/src/compiler/float.c +++ b/src/compiler/float.c @@ -100,6 +100,7 @@ Float float_neg(Float op) static char *err_invalid_float_width = "The float width is not valid, it must be one of 16, 32, 64 and 128."; static char *err_float_out_of_range = "The float value is out of range."; static char *err_float_format_invalid = "The float format is invalid."; +static char *err_hex_float_format_invalid = "Hex floating points must end with 'p' or 'P' and a valid exponent, e.g. 0x1.0p10 or 0x1.0P10."; TypeKind float_suffix(char c, const char **index_ref, char** error_ref) { @@ -108,6 +109,7 @@ TypeKind float_suffix(char c, const char **index_ref, char** error_ref) (*index_ref) += 4; return TYPE_BF16; } + if (c == 'd') return TYPE_F64; if (c == 'f') { int i = 0; @@ -226,19 +228,21 @@ Float float_from_hex(const char *string, char **error) scratch_buffer_append_char(c); } } - if (c == 'p' || c == 'P') + if (c != 'p' && c != 'P') + { + *error = err_hex_float_format_invalid; + return (Float){ .type = TYPE_POISONED }; + } + scratch_buffer_append_char(c); + if (*index == '-') + { + scratch_buffer_append_char('-'); + index++; + } + else if (*index == '+') index++; + while ((c = *(index++)) && (c >= '0' && c <= '9')) { scratch_buffer_append_char(c); - if (*index == '-') - { - scratch_buffer_append_char('-'); - index++; - } - else if (*index == '+') index++; - while ((c = *(index++)) && (c >= '0' && c <= '9')) - { - scratch_buffer_append_char(c); - } } TypeKind kind = float_suffix(c, &index, error); if (kind == TYPE_POISONED) return (Float){ .type = TYPE_POISONED }; diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index e548dcab3..f90f1f186 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -408,6 +408,10 @@ static bool scan_number_suffix(Lexer *lexer, bool *is_float) next(lexer); while (char_is_digit(c = peek(lexer))) next(lexer); break; + case 'd': + c = next(lexer); + *is_float = true; + break; case 'f': next(lexer); *is_float = true; diff --git a/test/test_suite/constants/float_type.c3t b/test/test_suite/constants/float_type.c3t index 262fd0652..6b8b75dcd 100644 --- a/test/test_suite/constants/float_type.c3t +++ b/test/test_suite/constants/float_type.c3t @@ -1,8 +1,8 @@ // #target: macos-x64 module test; uint f0 = $typeof(1.0f).sizeof; -uint f32 = $typeof(1.0f32).sizeof; -uint f64 = $typeof(1.0f64).sizeof; +uint f32 = $typeof(1.0f).sizeof; +uint f64 = $typeof(1.0d).sizeof; /* #expect: test.ll diff --git a/test/test_suite/floats/double_float.c3 b/test/test_suite/floats/double_float.c3 new file mode 100644 index 000000000..cc3159086 --- /dev/null +++ b/test/test_suite/floats/double_float.c3 @@ -0,0 +1 @@ +int x = 5d; // #error: 'double' cannot implicitly be converted to 'int' \ No newline at end of file diff --git a/test/test_suite/floats/hex_floats_malformed.c3 b/test/test_suite/floats/hex_floats_malformed.c3 new file mode 100644 index 000000000..1ff706860 --- /dev/null +++ b/test/test_suite/floats/hex_floats_malformed.c3 @@ -0,0 +1 @@ +double x = 0x12.00; // #error: must end with 'p' or 'P' \ No newline at end of file