Skip to content

Commit

Permalink
Implement 0x/0o/0b number prefixes (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 authored Oct 8, 2024
1 parent cf85146 commit a13723c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
12 changes: 6 additions & 6 deletions man/rgbasm.5
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 19 additions & 1 deletion src/asm/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
16 changes: 16 additions & 0 deletions test/asm/number-prefixes.asm
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a13723c

Please sign in to comment.