Use strtoll() for hex literals to handle 64-bit ones even on 32-bit.

This commit is contained in:
Bob Nystrom
2017-01-12 21:53:21 -08:00
parent 0cc00c41c0
commit 5ddd783ff5
3 changed files with 16 additions and 9 deletions

View File

@ -130,9 +130,8 @@ class Literals is SyntaxExample {
1.0
-12.34
0x1000000
// Literals larger than 0x1000000 are only valid in 64 bit builds
// 0xdeadbeef
// 0x1234567890ABCDEF
0xdeadbeef
0x1234567890ABCDEF
}
strings {

View File

@ -712,16 +712,23 @@ static void makeNumber(Parser* parser, bool isHex)
{
errno = 0;
// We don't check that the entire token is consumed because we've already
// scanned it ourselves and know it's valid.
parser->current.value = NUM_VAL(isHex ? strtol(parser->tokenStart, NULL, 16)
: strtod(parser->tokenStart, NULL));
if (isHex)
{
parser->current.value = NUM_VAL(strtoll(parser->tokenStart, NULL, 16));
}
else
{
parser->current.value = NUM_VAL(strtod(parser->tokenStart, NULL));
}
if (errno == ERANGE)
{
lexError(parser, "Number literal was too large.");
lexError(parser, "Number literal was too large (%d).", sizeof(long int));
parser->current.value = NUM_VAL(0);
}
// We don't check that the entire token is consumed after calling strtoll()
// or strtod() because we've already scanned it ourselves and know it's valid.
makeToken(parser, TOKEN_NUMBER);
}

View File

@ -5,4 +5,5 @@ System.print(x + 1) // expect: 256
System.print(x == 255) // expect: true
System.print(0x09 is Num) // expect: true
System.print(x is Num) // expect: true
System.print(-0xFF) // expect: -255
System.print(-0xFF) // expect: -255
System.print(0xdeadbeef) // expect: 3735928559