From 6cf2f950cd0021fe0764c79c101ee7a0f419cf70 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 4 Mar 2024 17:11:56 +0800 Subject: [PATCH] some fix --- src/lexer.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lexer.cpp b/src/lexer.cpp index 3d9d7981..ec6d757c 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -289,7 +289,7 @@ static bool is_unicode_Lo_char(uint32_t c) { add_token(TK("@num"), int_out); return; case IntParsingResult::Overflow: - SyntaxError("integer literal too large"); + SyntaxError("int literal is too large"); return; case IntParsingResult::Failure: break; // do nothing @@ -502,8 +502,9 @@ IntParsingResult parse_int(std::string_view text, i64* out, int base){ if(text.length() == 0) return IntParsingResult::Failure; for(char c : text){ if(c >= '0' && c <= '9'){ + i64 prev_out = *out; *out = (*out * 10) + (c - '0'); - if(*out < 0) return IntParsingResult::Overflow; + if(*out < prev_out) return IntParsingResult::Overflow; }else{ return IntParsingResult::Failure; } @@ -515,8 +516,9 @@ IntParsingResult parse_int(std::string_view text, i64* out, int base){ if(text.length() == 0) return IntParsingResult::Failure; for(char c : text){ if(c == '0' || c == '1'){ + i64 prev_out = *out; *out = (*out << 1) | (c - '0'); - if(*out < 0) return IntParsingResult::Overflow; + if(*out < prev_out) return IntParsingResult::Overflow; }else{ return IntParsingResult::Failure; } @@ -528,8 +530,9 @@ IntParsingResult parse_int(std::string_view text, i64* out, int base){ if(text.length() == 0) return IntParsingResult::Failure; for(char c : text){ if(c >= '0' && c <= '7'){ + i64 prev_out = *out; *out = (*out << 3) | (c - '0'); - if(*out < 0) return IntParsingResult::Overflow; + if(*out < prev_out) return IntParsingResult::Overflow; }else{ return IntParsingResult::Failure; } @@ -540,15 +543,16 @@ IntParsingResult parse_int(std::string_view text, i64* out, int base){ if(f_startswith_2(text, "0x")) text.remove_prefix(2); if(text.length() == 0) return IntParsingResult::Failure; for(char c : text){ + i64 prev_out = *out; if(c >= '0' && c <= '9'){ *out = (*out << 4) | (c - '0'); - if(*out < 0) return IntParsingResult::Overflow; + if(*out < prev_out) return IntParsingResult::Overflow; }else if(c >= 'a' && c <= 'f'){ *out = (*out << 4) | (c - 'a' + 10); - if(*out < 0) return IntParsingResult::Overflow; + if(*out < prev_out) return IntParsingResult::Overflow; }else if(c >= 'A' && c <= 'F'){ *out = (*out << 4) | (c - 'A' + 10); - if(*out < 0) return IntParsingResult::Overflow; + if(*out < prev_out) return IntParsingResult::Overflow; }else{ return IntParsingResult::Failure; }