mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-24 05:20:17 +00:00
This commit is contained in:
parent
90a9a404e2
commit
214395fab6
@ -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"
|
||||||
|
|||||||
@ -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()");
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user