Merge pull request #181 from blueloveTH/support-an-alt-float-literal-form

Support an alternative float literal form
This commit is contained in:
BLUELOVETH 2023-12-02 18:36:04 +08:00 committed by GitHub
commit 34f3ac32e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 13 deletions

View File

@ -273,24 +273,25 @@ static bool is_unicode_Lo_char(uint32_t c) {
const char* i = token_start; const char* i = token_start;
while(kValidChars.count(*i)) i++; while(kValidChars.count(*i)) i++;
std::string_view text(token_start, i - token_start); 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'){ if(i[-1] == 'L'){
add_token(TK("@long")); add_token(TK("@long"));
return; return;
} }
// try integer // try integer
i64 int_out; i64 int_out;
bool ok = parse_int(text, &int_out, -1); if(parse_int(text, &int_out, -1)){
if(ok){
add_token(TK("@num"), int_out); add_token(TK("@num"), int_out);
return; return;
} }
}
// try float // try float
double float_out; double float_out;
char* p_end; char* p_end;
try{ try{
float_out = std::strtod(text.data(), &p_end); float_out = std::strtod(text.data(), &p_end);
}catch(...){ }catch(...){
@ -345,9 +346,14 @@ static bool is_unicode_Lo_char(uint32_t c) {
} else { } else {
add_token(TK("..")); add_token(TK(".."));
} }
} else {
char next_char = peekchar();
if(next_char >= '0' && next_char <= '9'){
eat_number();
}else{ }else{
add_token(TK(".")); add_token(TK("."));
} }
}
return true; return true;
} }
case '=': add_token_2('=', TK("="), TK("==")); return true; case '=': add_token_2('=', TK("="), TK("==")); return true;

View File

@ -59,3 +59,11 @@ assert math.isnan(0/0)
assert 2**-600 == 0.0 assert 2**-600 == 0.0
assert 2.0 ** 600 == inf assert 2.0 ** 600 == inf
assert (-2.0) ** 601 == -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)