mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-24 21:40:16 +00:00
Added octal literal support
This commit is contained in:
parent
7312afdad2
commit
4d2b3e59a1
@ -260,7 +260,7 @@ static bool is_unicode_Lo_char(uint32_t c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Lexer::eat_number() {
|
void Lexer::eat_number() {
|
||||||
PK_LOCAL_STATIC const std::regex pattern("^(0x)?[0-9a-fA-F]+(\\.[0-9]+)?(L)?");
|
PK_LOCAL_STATIC const std::regex pattern("^(0[xo])?[0-9a-fA-F]+(\\.[0-9]+)?(L)?");
|
||||||
std::smatch m;
|
std::smatch m;
|
||||||
|
|
||||||
const char* i = token_start;
|
const char* i = token_start;
|
||||||
@ -278,20 +278,28 @@ static bool is_unicode_Lo_char(uint32_t c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(m[1].matched && m[2].matched){
|
if(m[1].matched && m[2].matched){
|
||||||
SyntaxError("hex literal should not contain a dot");
|
SyntaxError("hex/octal literal should not contain a dot");
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
int base = 10;
|
int base = 10;
|
||||||
size_t size;
|
size_t size;
|
||||||
if (m[1].matched) base = 16;
|
if (m[1].matched) {
|
||||||
|
if (m[1].str() == "0o") base=8;
|
||||||
|
else base = 16;
|
||||||
|
}
|
||||||
if (m[2].matched) {
|
if (m[2].matched) {
|
||||||
PK_ASSERT(base == 10);
|
PK_ASSERT(base == 10);
|
||||||
add_token(TK("@num"), Number::stof(m[0], &size));
|
add_token(TK("@num"), Number::stof(m[0], &size));
|
||||||
} else {
|
} else {
|
||||||
add_token(TK("@num"), (i64)std::stoll(m[0], &size, base));
|
// If we're base 8, chop off the "o"
|
||||||
|
std::string match = m[0].str();
|
||||||
|
if (base == 8) match.erase(1, 1);
|
||||||
|
add_token(TK("@num"), (i64)std::stoll(match, &size, base));
|
||||||
}
|
}
|
||||||
PK_ASSERT((int)size == (int)m.length());
|
// HACK: We need to check length-1 for octal since python octals are "0o..." and c/c++ octals are "0..."
|
||||||
|
if (base == 8) {PK_ASSERT((int)size == (int)m.length()-1);}
|
||||||
|
else {PK_ASSERT((int)size == (int)m.length());}
|
||||||
}catch(...){
|
}catch(...){
|
||||||
SyntaxError("invalid number literal");
|
SyntaxError("invalid number literal");
|
||||||
}
|
}
|
||||||
@ -466,4 +474,4 @@ static bool is_unicode_Lo_char(uint32_t c) {
|
|||||||
return std::move(nexts);
|
return std::move(nexts);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
||||||
|
|||||||
@ -5,6 +5,10 @@ assert 0x7fffffff == 2147483647
|
|||||||
# test 64-bit
|
# test 64-bit
|
||||||
assert 2**60-1 + 546 - 0xfffffffffffff == 1148417904979477026
|
assert 2**60-1 + 546 - 0xfffffffffffff == 1148417904979477026
|
||||||
|
|
||||||
|
# test oct literals
|
||||||
|
assert 0o1234 == 668
|
||||||
|
assert 0o17777777777 == 2147483647
|
||||||
|
|
||||||
# test == != >= <= < >
|
# test == != >= <= < >
|
||||||
assert -1 == -1
|
assert -1 == -1
|
||||||
assert -1 != 1
|
assert -1 != 1
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user