mirror of
https://github.com/pocketpy/pocketpy
synced 2026-02-03 22:20:16 +00:00
Compare commits
2 Commits
08cc247be9
...
979addecf9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
979addecf9 | ||
|
|
9ef38d605b |
@ -567,6 +567,66 @@ static bool bool__invert__(int argc, py_Ref argv) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool bool_try_cast_i64(py_Ref arg, py_i64* out) {
|
||||
if (arg->type == tp_int) {
|
||||
*out = py_toint(arg);
|
||||
return true;
|
||||
} else if (arg->type == tp_bool) {
|
||||
*out = py_tobool(arg);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool bool__add__(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
py_i64 lhs = py_tobool(py_arg(0));
|
||||
py_i64 rhs;
|
||||
if (bool_try_cast_i64(py_arg(1), &rhs)) {
|
||||
py_newint(py_retval(), lhs + rhs);
|
||||
} else {
|
||||
py_newnotimplemented(py_retval());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool bool__sub__(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
py_i64 lhs = py_tobool(py_arg(0));
|
||||
py_i64 rhs;
|
||||
if (bool_try_cast_i64(py_arg(1), &rhs)) {
|
||||
py_newint(py_retval(), lhs - rhs);
|
||||
} else {
|
||||
py_newnotimplemented(py_retval());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool bool__rsub__(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
py_i64 lhs = py_tobool(py_arg(0));
|
||||
py_i64 rhs;
|
||||
if (bool_try_cast_i64(py_arg(1), &rhs)) {
|
||||
py_newint(py_retval(), rhs - lhs);
|
||||
} else {
|
||||
py_newnotimplemented(py_retval());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool bool__mul__(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
py_i64 lhs = py_tobool(py_arg(0));
|
||||
py_i64 rhs;
|
||||
if (bool_try_cast_i64(py_arg(1), &rhs)) {
|
||||
py_newint(py_retval(), lhs * rhs);
|
||||
} else {
|
||||
py_newnotimplemented(py_retval());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void pk_number__register() {
|
||||
/****** tp_int & tp_float ******/
|
||||
py_bindmagic(tp_int, __add__, int__add__);
|
||||
@ -651,6 +711,12 @@ void pk_number__register() {
|
||||
py_bindmagic(tp_bool, __or__, bool__or__);
|
||||
py_bindmagic(tp_bool, __xor__, bool__xor__);
|
||||
py_bindmagic(tp_bool, __invert__, bool__invert__);
|
||||
py_bindmagic(tp_bool, __add__, bool__add__);
|
||||
py_bindmagic(tp_bool, __sub__, bool__sub__);
|
||||
py_bindmagic(tp_bool, __mul__, bool__mul__);
|
||||
py_bindmagic(tp_bool, __radd__, bool__add__);
|
||||
py_bindmagic(tp_bool, __rsub__, bool__rsub__);
|
||||
py_bindmagic(tp_bool, __rmul__, bool__mul__);
|
||||
}
|
||||
|
||||
#undef DEF_NUM_BINARY_OP
|
||||
|
||||
@ -218,12 +218,21 @@ static bool Random_randint(int argc, py_Ref argv) {
|
||||
static bool Random_choice(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
mt19937* ud = py_touserdata(py_arg(0));
|
||||
py_TValue* p;
|
||||
int length = pk_arrayview(py_arg(1), &p);
|
||||
if(length == -1) return TypeError("choice(): argument must be a list or tuple");
|
||||
if(length == 0) return IndexError("cannot choose from an empty sequence");
|
||||
int index = mt19937__randint(ud, 0, length - 1);
|
||||
py_assign(py_retval(), p + index);
|
||||
if (py_isstr(py_arg(1))) {
|
||||
c11_sv sv = py_tosv(py_arg(1));
|
||||
int length = c11_sv__u8_length(sv);
|
||||
if(length == 0) return IndexError("cannot choose from an empty sequence");
|
||||
int index = mt19937__randint(ud, 0, length - 1);
|
||||
c11_sv ch = c11_sv__u8_getitem(sv, index);
|
||||
py_newstrv(py_retval(), ch);
|
||||
} else {
|
||||
py_TValue* p;
|
||||
int length = pk_arrayview(py_arg(1), &p);
|
||||
if(length == -1) return TypeError("choice(): argument must be a list, tuple or str");
|
||||
if(length == 0) return IndexError("cannot choose from an empty sequence");
|
||||
int index = mt19937__randint(ud, 0, length - 1);
|
||||
py_assign(py_retval(), p + index);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -263,8 +263,22 @@ static bool list_extend(int argc, py_Ref argv) {
|
||||
List* self = py_touserdata(py_arg(0));
|
||||
py_TValue* p;
|
||||
int length = pk_arrayview(py_arg(1), &p);
|
||||
if(length == -1) return TypeError("extend() argument must be a list or tuple");
|
||||
c11_vector__extend(self, p, length);
|
||||
if(length >= 0) {
|
||||
c11_vector__extend(self, p, length);
|
||||
} else {
|
||||
// get iterator
|
||||
if (!py_iter(py_arg(1))) return false;
|
||||
py_StackRef tmp_iter = py_pushtmp();
|
||||
py_assign(tmp_iter, py_retval());
|
||||
while(true) {
|
||||
int res = py_next(tmp_iter);
|
||||
if (res == 0) break;
|
||||
if (res == -1) return false;
|
||||
assert(res == 1);
|
||||
c11_vector__push(py_TValue, self, *py_retval());
|
||||
}
|
||||
py_pop();
|
||||
}
|
||||
py_newnone(py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -31,3 +31,9 @@ assert NotImplemented is NotImplemented
|
||||
assert True is True
|
||||
assert False is False
|
||||
|
||||
assert True + 1 == 2
|
||||
assert True - 1 == 0
|
||||
assert True * 3 == 3
|
||||
assert 1 + True == 2
|
||||
assert 1 - True == 0
|
||||
assert 3 * True == 3
|
||||
|
||||
@ -170,6 +170,13 @@ b = a.copy(); del b[-1:]; assert b == [1, 2, 3]
|
||||
b = a.copy(); del b[:]; assert b == []
|
||||
assert a == [1, 2, 3, 4]
|
||||
|
||||
# test extend with iterable
|
||||
c = [1]
|
||||
c.extend('123')
|
||||
assert c == [1, '1', '2', '3']
|
||||
c.extend(range(1, 6))
|
||||
assert c == [1, '1', '2', '3', 1, 2, 3, 4, 5]
|
||||
|
||||
# test cyclic reference
|
||||
# a = []
|
||||
# a.append(0)
|
||||
|
||||
@ -20,6 +20,9 @@ for i in range(10):
|
||||
for i in range(10):
|
||||
assert r.choice(tuple(a)) in a
|
||||
|
||||
for i in range(10):
|
||||
assert r.choice('hello') in 'hello'
|
||||
|
||||
for i in range(10):
|
||||
assert r.randint(1, 1) == 1
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user