diff --git a/include/pocketpy/compiler.h b/include/pocketpy/compiler.h index 6daa785b..6f491892 100644 --- a/include/pocketpy/compiler.h +++ b/include/pocketpy/compiler.h @@ -135,7 +135,7 @@ struct TokenDeserializer{ std::string_view read_string(char c); Str read_string_from_hex(char c); - i64 read_int(char c); + i64 read_uint(char c); f64 read_float(char c); }; diff --git a/include/pocketpy/lexer.h b/include/pocketpy/lexer.h index 8cd51fbd..ff268454 100644 --- a/include/pocketpy/lexer.h +++ b/include/pocketpy/lexer.h @@ -139,6 +139,6 @@ enum class IntParsingResult{ Overflow, }; -IntParsingResult parse_int(std::string_view text, i64* out, int base); +IntParsingResult parse_uint(std::string_view text, i64* out, int base); } // namespace pkpy diff --git a/src/compiler.cpp b/src/compiler.cpp index b2927219..ba91e8a7 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -1281,29 +1281,29 @@ __EAT_DOTS_END: if(version != PK_VERSION){ SyntaxError(_S("precompiled version mismatch: ", version, "!=" PK_VERSION)); } - if(deserializer.read_int('\n') != (i64)mode()){ + if(deserializer.read_uint('\n') != (i64)mode()){ SyntaxError("precompiled mode mismatch"); } lexer.src->_precompiled_tokens = deserializer.read_string('\n'); deserializer.curr += 1; // skip '=' - i64 count = deserializer.read_int('\n'); + i64 count = deserializer.read_uint('\n'); const char* tokens_c_str = lexer.src->_precompiled_tokens.c_str(); for(int i=0; i(buffer, s.size()/2); } - i64 TokenDeserializer::read_int(char c){ - std::string_view sv = read_string(c); - i64 out; - IntParsingResult res = parse_int(sv, &out, 10); - PK_ASSERT(res == IntParsingResult::Success); + i64 TokenDeserializer::read_uint(char c){ + i64 out = 0; + while(*curr != c){ + out = out*10 + (*curr-'0'); + curr++; + } + curr++; // skip the delimiter return out; } diff --git a/src/lexer.cpp b/src/lexer.cpp index 64384827..918dec48 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -292,7 +292,7 @@ static bool is_unicode_Lo_char(uint32_t c) { } // try integer i64 int_out; - switch(parse_int(text, &int_out, -1)){ + switch(parse_uint(text, &int_out, -1)){ case IntParsingResult::Success: add_token(TK("@num"), int_out); return; @@ -491,13 +491,13 @@ static bool is_unicode_Lo_char(uint32_t c) { return std::move(nexts); } -IntParsingResult parse_int(std::string_view text, i64* out, int base){ - *out = 0; - - const auto f_startswith_2 = [](std::string_view t, const char* prefix) -> bool{ +inline constexpr bool f_startswith_2(std::string_view t, const char* prefix){ if(t.length() < 2) return false; return t[0] == prefix[0] && t[1] == prefix[1]; - }; +} + +IntParsingResult parse_uint(std::string_view text, i64* out, int base){ + *out = 0; if(base == -1){ if(f_startswith_2(text, "0b")) base = 2; diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index b3962679..90647f27 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -443,7 +443,7 @@ void init_builtins(VM* _vm) { sv.remove_prefix(1); } i64 val; - if(parse_int(sv, &val, base) != IntParsingResult::Success){ + if(parse_uint(sv, &val, base) != IntParsingResult::Success){ vm->ValueError(_S("invalid literal for int() with base ", base, ": ", s.escape())); } if(negative) val = -val;