mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
Merge pull request #181 from blueloveTH/support-an-alt-float-literal-form
Support an alternative float literal form
This commit is contained in:
commit
34f3ac32e7
@ -273,24 +273,25 @@ static bool is_unicode_Lo_char(uint32_t c) {
|
||||
const char* i = token_start;
|
||||
while(kValidChars.count(*i)) i++;
|
||||
std::string_view text(token_start, i - token_start);
|
||||
curr_char = i;
|
||||
this->curr_char = i;
|
||||
|
||||
if(text[0] != '.'){
|
||||
// try long
|
||||
if(i[-1] == 'L'){
|
||||
add_token(TK("@long"));
|
||||
return;
|
||||
}
|
||||
|
||||
// try integer
|
||||
i64 int_out;
|
||||
bool ok = parse_int(text, &int_out, -1);
|
||||
if(ok){
|
||||
if(parse_int(text, &int_out, -1)){
|
||||
add_token(TK("@num"), int_out);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// try float
|
||||
double float_out;
|
||||
char* p_end;
|
||||
|
||||
try{
|
||||
float_out = std::strtod(text.data(), &p_end);
|
||||
}catch(...){
|
||||
@ -346,8 +347,13 @@ static bool is_unicode_Lo_char(uint32_t c) {
|
||||
add_token(TK(".."));
|
||||
}
|
||||
} else {
|
||||
char next_char = peekchar();
|
||||
if(next_char >= '0' && next_char <= '9'){
|
||||
eat_number();
|
||||
}else{
|
||||
add_token(TK("."));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case '=': add_token_2('=', TK("="), TK("==")); return true;
|
||||
|
@ -59,3 +59,11 @@ assert math.isnan(0/0)
|
||||
assert 2**-600 == 0.0
|
||||
assert 2.0 ** 600 == inf
|
||||
assert (-2.0) ** 601 == -inf
|
||||
|
||||
# test .123 forms
|
||||
assert float(".123") == 0.123
|
||||
assert .123 == 0.123
|
||||
assert eq(.5 *2, 1.0)
|
||||
assert eq(2 * .5, 1.0)
|
||||
assert eq(2 * (.5), 1.0)
|
||||
assert eq(2 * (.5 + 1), 3.0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user