Implement 0x/0o/0b number prefixes (#1533)

This commit is contained in:
Sylvie
2024-10-08 15:56:00 -04:00
committed by GitHub
parent cf85146353
commit a13723c523
3 changed files with 41 additions and 7 deletions

View File

@@ -272,16 +272,16 @@ further below.
The instructions in the macro-language generally require constant expressions.
.Ss Numeric formats
There are a number of numeric formats.
.Bl -column -offset indent "Precise fixed-point" "Prefix"
.It Sy Format type Ta Sy Prefix Ta Sy Accepted characters
.It Hexadecimal Ta $ Ta 0123456789ABCDEF
.Bl -column -offset indent "Precise fixed-point" "Possible prefixes"
.It Sy Format type Ta Sy Possible prefixes Ta Sy Accepted characters
.It Decimal Ta none Ta 0123456789
.It Octal Ta & Ta 01234567
.It Binary Ta % Ta 01
.It Hexadecimal Ta Li $ , 0x , 0X Ta 0123456789ABCDEF
.It Octal Ta Li & , 0o , 0O Ta 01234567
.It Binary Ta Li % , 0b , 0B Ta 01
.It Fixed-point Ta none Ta 01234.56789
.It Precise fixed-point Ta none Ta 12.34q8
.It Character constant Ta none Ta \(dqABYZ\(dq
.It Game Boy graphics Ta \` Ta 0123
.It Game Boy graphics Ta Li \` Ta 0123
.El
.Pp
Underscores are also accepted in numbers, except at the beginning of one.

View File

@@ -1733,7 +1733,25 @@ static Token yylex_NORMAL() {
// Handle numbers
case '0': // Decimal or fixed-point number
case '0': // Decimal, fixed-point, or base-prefix number
switch (peek()) {
case 'x':
case 'X':
shiftChar();
return Token(T_(NUMBER), readHexNumber());
case 'o':
case 'O':
shiftChar();
return Token(T_(NUMBER), readNumber(8, 0));
case 'b':
case 'B':
shiftChar();
return Token(T_(NUMBER), readBinaryNumber());
}
[[fallthrough]];
// Decimal or fixed-point number
case '1':
case '2':
case '3':

View File

@@ -0,0 +1,16 @@
MACRO test
assert (\1) == (\2)
ENDM
test 0xDEADbeef, $DEADbeef
test 0o755, &755
test 0b101010, %101010
test 0XcafeBABE, $cafeBABE
test 0O644, &644
test 0B11100100, %11100100
pusho b.X
test 0b.X.X, %.X.X
test 0BX.X., %X.X.
popo