blueloveTH 2024-02-26 10:40:37 +08:00
parent 90a9a404e2
commit 214395fab6
3 changed files with 18 additions and 4 deletions

View File

@ -20,7 +20,7 @@
#include <deque> #include <deque>
#include <initializer_list> #include <initializer_list>
#define PK_VERSION "1.4.1" #define PK_VERSION "1.4.2"
#include "config.h" #include "config.h"
#include "export.h" #include "export.h"

View File

@ -411,10 +411,17 @@ void init_builtins(VM* _vm) {
int base = 10; int base = 10;
if(args.size() == 1+2) base = CAST(i64, args[2]); if(args.size() == 1+2) base = CAST(i64, args[2]);
const Str& s = CAST(Str&, args[1]); const Str& s = CAST(Str&, args[1]);
i64 val; std::string_view sv = s.sv();
if(!parse_int(s.sv(), &val, base)){ bool negative = false;
vm->ValueError("invalid literal for int(): " + s.escape()); if(!sv.empty() && (sv[0] == '+' || sv[0] == '-')){
negative = sv[0] == '-';
sv.remove_prefix(1);
} }
i64 val;
if(!parse_int(sv, &val, base)){
vm->ValueError(_S("invalid literal for int() with base ", base, ": ", s.escape()));
}
if(negative) val = -val;
return VAR(val); return VAR(val);
} }
vm->TypeError("invalid arguments for int()"); vm->TypeError("invalid arguments for int()");

View File

@ -62,6 +62,13 @@ assert int(1.5) == 1
assert int(-1.5) == -1 assert int(-1.5) == -1
assert int("123") == 123 assert int("123") == 123
assert int("0x123", 16) == 291
assert int("0o123", 8) == 83
assert int("-0x123", 16) == -291
assert int("-0o123", 8) == -83
assert int("-123") == -123
assert int("+123") == 123
# test >> << & | ^ # test >> << & | ^
assert 12 >> 1 == 6 assert 12 >> 1 == 6
assert 12 << 1 == 24 assert 12 << 1 == 24