Skip to content

Commit 9643530

Browse files
authored
Merge pull request #219 from fourcolor/fix/hex-0X-support
Fix parsing to support uppercase 0X hex literals
2 parents a39f318 + ba2c2dc commit 9643530

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

src/lexer.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ bool is_digit(char c)
4242

4343
bool is_hex(char c)
4444
{
45-
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || c == 'x' ||
46-
(c >= 'A' && c <= 'F'));
45+
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
46+
(c >= 'A' && c <= 'F');
4747
}
4848

4949
bool is_numeric(char buffer[])
5050
{
5151
bool hex = false;
5252
int size = strlen(buffer);
5353

54-
if (size > 2)
55-
hex = !strncmp(buffer, "0x", 2);
54+
if (size > 2 && buffer[0] == '0' && (buffer[1] | 32) == 'x')
55+
hex = true;
5656

57-
for (int i = 0; i < size; i++) {
57+
for (int i = hex ? 2 : 0; i < size; i++) {
5858
if (hex && !is_hex(buffer[i]))
5959
return false;
6060
if (!hex && !is_digit(buffer[i]))
@@ -177,9 +177,38 @@ token_t lex_token_internal(bool aliasing)
177177

178178
if (is_digit(next_char)) {
179179
int i = 0;
180-
do {
180+
token_str[i++] = next_char;
181+
read_char(false);
182+
183+
if (token_str[0] == '0' && ((next_char | 32) == 'x')) {
184+
/* Hexadecimal: starts with 0x or 0X */
181185
token_str[i++] = next_char;
182-
} while (is_hex(read_char(false)));
186+
187+
read_char(false);
188+
if (!is_hex(next_char))
189+
error("Invalid hex literal: expected hex digit after 0x");
190+
191+
do {
192+
token_str[i++] = next_char;
193+
} while (is_hex(read_char(false)));
194+
195+
} else if (token_str[0] == '0') {
196+
/* Octal: starts with 0 but not followed by 'x' */
197+
while (is_digit(next_char)) {
198+
if (next_char >= '8')
199+
error("Invalid octal digit: must be in range 0-7");
200+
token_str[i++] = next_char;
201+
read_char(false);
202+
}
203+
204+
} else {
205+
/* Decimal */
206+
while (is_digit(next_char)) {
207+
token_str[i++] = next_char;
208+
read_char(false);
209+
}
210+
}
211+
183212
token_str[i] = 0;
184213
skip_whitespace();
185214
return T_numeric;

src/parser.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ int read_numeric_constant(char buffer[])
295295
int i = 0;
296296
int value = 0;
297297
while (buffer[i]) {
298-
if (i == 1 && (buffer[i] == 'x')) { /* hexadecimal */
298+
if (i == 1 && (buffer[i] | 32) == 'x') { /* hexadecimal */
299299
value = 0;
300300
i = 2;
301301
while (buffer[i]) {
@@ -784,7 +784,7 @@ void read_numeric_param(block_t *parent, basic_block_t *bb, int is_neg)
784784
i++;
785785
}
786786
if (token[0] == '0') {
787-
if (token[1] == 'x') { /* hexdecimal */
787+
if ((token[1] | 32) == 'x') { /* hexdecimal */
788788
i = 2;
789789
do {
790790
c = token[i++];

tests/driver.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,4 +1855,32 @@ int main(void)
18551855
}
18561856
EOF
18571857

1858+
try_output 0 "2748 6719 105884 0" << EOF
1859+
int main()
1860+
{
1861+
int a = 0XABC;
1862+
int b = 0X1a3f;
1863+
int c = 0XDEaD + 0xBeEF;
1864+
int d = 0X0;
1865+
printf("%d %d %d %d", a, b, c, d);
1866+
return 0;
1867+
}
1868+
EOF
1869+
1870+
try_compile_error << EOF
1871+
int main()
1872+
{
1873+
int x = 0X;
1874+
return 0;
1875+
}
1876+
EOF
1877+
1878+
try_compile_error << EOF
1879+
int main()
1880+
{
1881+
int x = 0XGHI;
1882+
return 0;
1883+
}
1884+
EOF
1885+
18581886
echo OK

0 commit comments

Comments
 (0)