Add d as floating point suffix for double types.

This commit is contained in:
Christoffer Lerno
2025-05-28 12:02:24 +02:00
parent f4b9f375e0
commit 83d6b35afe
7 changed files with 25 additions and 14 deletions

View File

@@ -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

View File

@@ -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})*

View File

@@ -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 };

View File

@@ -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;

View File

@@ -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

View File

@@ -0,0 +1 @@
int x = 5d; // #error: 'double' cannot implicitly be converted to 'int'

View File

@@ -0,0 +1 @@
double x = 0x12.00; // #error: must end with 'p' or 'P'