From 1caa9e71c3d20baf8fa177668fac0409a7720e66 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 4 Mar 2024 17:27:13 +0800 Subject: [PATCH] fix https://github.com/pocketpy/pocketpy/issues/213 --- src/lexer.cpp | 10 +++++++++- tests/02_float.py | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/lexer.cpp b/src/lexer.cpp index ec6d757c..4ecf9629 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -273,10 +273,18 @@ static bool is_unicode_Lo_char(uint32_t c) { void Lexer::eat_number() { const char* i = token_start; while(kValidChars.count(*i)) i++; + + bool is_scientific_notation = false; + if(*(i-1) == 'e' && (*i == '+' || *i == '-')){ + i++; + while(isdigit(*i) || *i=='j') i++; + is_scientific_notation = true; + } + std::string_view text(token_start, i - token_start); this->curr_char = i; - if(text[0] != '.'){ + if(text[0] != '.' && !is_scientific_notation){ // try long if(i[-1] == 'L'){ add_token(TK("@long")); diff --git a/tests/02_float.py b/tests/02_float.py index a4868875..2b2587c3 100644 --- a/tests/02_float.py +++ b/tests/02_float.py @@ -67,3 +67,20 @@ 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) + + +assert 1e3 == 1000.0 +assert 1e-3 == 0.001 +assert -1e3 == -1000.0 +assert -1e-3 == -0.001 +assert 1e0 == 1.0 +assert 1e-0 == 1.0 + +assert 2e3 == 2000.0 +assert 2e3j == 2000j +assert -2e-3 == -0.002 +assert -2e-3j == -0.002j + +assert 3.4e-3 == 0.0034 +assert 3.4e+3 == 3400.0 +