This commit is contained in:
blueloveTH 2024-08-05 22:34:33 +08:00
parent 4254de48bf
commit ee29eadcd3
6 changed files with 170 additions and 145 deletions

View File

@ -160,8 +160,8 @@ void pk__add_module_math() {
py_newfloat(py_emplacedict(mod, py_name("pi")), 3.1415926535897932384); py_newfloat(py_emplacedict(mod, py_name("pi")), 3.1415926535897932384);
py_newfloat(py_emplacedict(mod, py_name("e")), 2.7182818284590452354); py_newfloat(py_emplacedict(mod, py_name("e")), 2.7182818284590452354);
py_newfloat(py_emplacedict(mod, py_name("inf")), 1.0 / 0.0); py_newfloat(py_emplacedict(mod, py_name("inf")), INFINITY);
py_newfloat(py_emplacedict(mod, py_name("nan")), 0.0 / 0.0); py_newfloat(py_emplacedict(mod, py_name("nan")), NAN);
py_bindfunc(mod, "ceil", math_ceil); py_bindfunc(mod, "ceil", math_ceil);
py_bindfunc(mod, "fabs", math_fabs); py_bindfunc(mod, "fabs", math_fabs);

View File

@ -336,14 +336,14 @@ static bool dict__ne__(int argc, py_Ref argv) {
return true; return true;
} }
static bool dict__clear(int argc, py_Ref argv) { static bool dict_clear(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
Dict__clear(self); Dict__clear(self);
return true; return true;
} }
static bool dict__copy(int argc, py_Ref argv) { static bool dict_copy(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
Dict* new_dict = py_newobject(py_retval(), tp_dict, 0, sizeof(Dict)); Dict* new_dict = py_newobject(py_retval(), tp_dict, 0, sizeof(Dict));
@ -356,7 +356,7 @@ static bool dict__copy(int argc, py_Ref argv) {
return true; return true;
} }
static bool dict__update(int argc, py_Ref argv) { static bool dict_update(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
PY_CHECK_ARG_TYPE(1, tp_dict); PY_CHECK_ARG_TYPE(1, tp_dict);
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
@ -369,7 +369,7 @@ static bool dict__update(int argc, py_Ref argv) {
return true; return true;
} }
static bool dict__get(int argc, py_Ref argv) { static bool dict_get(int argc, py_Ref argv) {
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
if(argc > 3) return TypeError("get() takes at most 3 arguments (%d given)", argc); if(argc > 3) return TypeError("get() takes at most 3 arguments (%d given)", argc);
py_Ref default_val = argc == 3 ? py_arg(2) : py_None; py_Ref default_val = argc == 3 ? py_arg(2) : py_None;
@ -379,7 +379,7 @@ static bool dict__get(int argc, py_Ref argv) {
return true; return true;
} }
static bool dict__pop(int argc, py_Ref argv) { static bool dict_pop(int argc, py_Ref argv) {
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
if(argc < 2 || argc > 3) return TypeError("pop() takes 2 or 3 arguments (%d given)", argc); if(argc < 2 || argc > 3) return TypeError("pop() takes 2 or 3 arguments (%d given)", argc);
py_Ref default_val = argc == 3 ? py_arg(2) : py_None; py_Ref default_val = argc == 3 ? py_arg(2) : py_None;
@ -388,7 +388,7 @@ static bool dict__pop(int argc, py_Ref argv) {
return true; return true;
} }
static bool dict__items(int argc, py_Ref argv) { static bool dict_items(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
DictIterator* ud = py_newobject(py_retval(), tp_dict_items, 1, sizeof(DictIterator)); DictIterator* ud = py_newobject(py_retval(), tp_dict_items, 1, sizeof(DictIterator));
@ -397,7 +397,7 @@ static bool dict__items(int argc, py_Ref argv) {
return true; return true;
} }
static bool dict__keys(int argc, py_Ref argv) { static bool dict_keys(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
py_newtuple(py_retval(), self->length); py_newtuple(py_retval(), self->length);
@ -413,7 +413,7 @@ static bool dict__keys(int argc, py_Ref argv) {
return true; return true;
} }
static bool dict__values(int argc, py_Ref argv) { static bool dict_values(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
py_newtuple(py_retval(), self->length); py_newtuple(py_retval(), self->length);
@ -443,14 +443,14 @@ py_Type pk_dict__register() {
py_bindmagic(type, __eq__, dict__eq__); py_bindmagic(type, __eq__, dict__eq__);
py_bindmagic(type, __ne__, dict__ne__); py_bindmagic(type, __ne__, dict__ne__);
py_bindmethod(type, "clear", dict__clear); py_bindmethod(type, "clear", dict_clear);
py_bindmethod(type, "copy", dict__copy); py_bindmethod(type, "copy", dict_copy);
py_bindmethod(type, "update", dict__update); py_bindmethod(type, "update", dict_update);
py_bindmethod(type, "get", dict__get); py_bindmethod(type, "get", dict_get);
py_bindmethod(type, "pop", dict__pop); py_bindmethod(type, "pop", dict_pop);
py_bindmethod(type, "items", dict__items); py_bindmethod(type, "items", dict_items);
py_bindmethod(type, "keys", dict__keys); py_bindmethod(type, "keys", dict_keys);
py_bindmethod(type, "values", dict__values); py_bindmethod(type, "values", dict_values);
py_setdict(py_tpobject(type), __hash__, py_None); py_setdict(py_tpobject(type), __hash__, py_None);
return type; return type;

View File

@ -216,7 +216,7 @@ static bool list__mul__(int argc, py_Ref argv) {
static bool list__rmul__(int argc, py_Ref argv) { return list__mul__(argc, argv); } static bool list__rmul__(int argc, py_Ref argv) { return list__mul__(argc, argv); }
static bool list__append(int argc, py_Ref argv) { static bool list_append(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
py_list__append(py_arg(0), py_arg(1)); py_list__append(py_arg(0), py_arg(1));
py_newnone(py_retval()); py_newnone(py_retval());
@ -243,7 +243,7 @@ static bool list__repr__(int argc, py_Ref argv) {
return true; return true;
} }
static bool list__extend(int argc, py_Ref argv) { static bool list_extend(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
List* self = py_touserdata(py_arg(0)); List* self = py_touserdata(py_arg(0));
PY_CHECK_ARG_TYPE(1, tp_list); PY_CHECK_ARG_TYPE(1, tp_list);
@ -253,7 +253,7 @@ static bool list__extend(int argc, py_Ref argv) {
return true; return true;
} }
static bool list__count(int argc, py_Ref argv) { static bool list_count(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
int count = 0; int count = 0;
for(int i = 0; i < py_list__len(py_arg(0)); i++) { for(int i = 0; i < py_list__len(py_arg(0)); i++) {
@ -265,14 +265,14 @@ static bool list__count(int argc, py_Ref argv) {
return true; return true;
} }
static bool list__clear(int argc, py_Ref argv) { static bool list_clear(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_list__clear(py_arg(0)); py_list__clear(py_arg(0));
py_newnone(py_retval()); py_newnone(py_retval());
return true; return true;
} }
static bool list__copy(int argc, py_Ref argv) { static bool list_copy(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_newlist(py_retval()); py_newlist(py_retval());
List* self = py_touserdata(py_arg(0)); List* self = py_touserdata(py_arg(0));
@ -281,7 +281,7 @@ static bool list__copy(int argc, py_Ref argv) {
return true; return true;
} }
static bool list__index(int argc, py_Ref argv) { static bool list_index(int argc, py_Ref argv) {
if(argc > 3) return TypeError("index() takes at most 3 arguments"); if(argc > 3) return TypeError("index() takes at most 3 arguments");
int start = 0; int start = 0;
if(argc == 3) { if(argc == 3) {
@ -299,7 +299,7 @@ static bool list__index(int argc, py_Ref argv) {
return ValueError("list.index(x): x not in list"); return ValueError("list.index(x): x not in list");
} }
static bool list__reverse(int argc, py_Ref argv) { static bool list_reverse(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
List* self = py_touserdata(py_arg(0)); List* self = py_touserdata(py_arg(0));
c11__reverse(py_TValue, self); c11__reverse(py_TValue, self);
@ -307,7 +307,7 @@ static bool list__reverse(int argc, py_Ref argv) {
return true; return true;
} }
static bool list__remove(int argc, py_Ref argv) { static bool list_remove(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
for(int i = 0; i < py_list__len(py_arg(0)); i++) { for(int i = 0; i < py_list__len(py_arg(0)); i++) {
int res = py_equal(py_list__getitem(py_arg(0), i), py_arg(1)); int res = py_equal(py_list__getitem(py_arg(0), i), py_arg(1));
@ -321,7 +321,7 @@ static bool list__remove(int argc, py_Ref argv) {
return ValueError("list.remove(x): x not in list"); return ValueError("list.remove(x): x not in list");
} }
static bool list__pop(int argc, py_Ref argv) { static bool list_pop(int argc, py_Ref argv) {
int index; int index;
if(argc == 1) { if(argc == 1) {
index = -1; index = -1;
@ -339,7 +339,7 @@ static bool list__pop(int argc, py_Ref argv) {
return true; return true;
} }
static bool list__insert(int argc, py_Ref argv) { static bool list_insert(int argc, py_Ref argv) {
PY_CHECK_ARGC(3); PY_CHECK_ARGC(3);
PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(1, tp_int);
List* self = py_touserdata(py_arg(0)); List* self = py_touserdata(py_arg(0));
@ -375,7 +375,7 @@ static int lt_with_key(py_TValue* a, py_TValue* b, py_TValue* key) {
} }
// sort(self, key=None, reverse=False) // sort(self, key=None, reverse=False)
static bool list__sort(int argc, py_Ref argv) { static bool list_sort(int argc, py_Ref argv) {
List* self = py_touserdata(py_arg(0)); List* self = py_touserdata(py_arg(0));
py_Ref key = py_arg(1); py_Ref key = py_arg(1);
@ -423,19 +423,19 @@ py_Type pk_list__register() {
py_bindmagic(type, __iter__, list__iter__); py_bindmagic(type, __iter__, list__iter__);
py_bindmagic(type, __contains__, list__contains__); py_bindmagic(type, __contains__, list__contains__);
py_bindmethod(type, "append", list__append); py_bindmethod(type, "append", list_append);
py_bindmethod(type, "extend", list__extend); py_bindmethod(type, "extend", list_extend);
py_bindmethod(type, "count", list__count); py_bindmethod(type, "count", list_count);
py_bindmethod(type, "clear", list__clear); py_bindmethod(type, "clear", list_clear);
py_bindmethod(type, "copy", list__copy); py_bindmethod(type, "copy", list_copy);
py_bindmethod(type, "index", list__index); py_bindmethod(type, "index", list_index);
py_bindmethod(type, "reverse", list__reverse); py_bindmethod(type, "reverse", list_reverse);
py_bindmethod(type, "remove", list__remove); py_bindmethod(type, "remove", list_remove);
py_bindmethod(type, "pop", list__pop); py_bindmethod(type, "pop", list_pop);
py_bindmethod(type, "insert", list__insert); py_bindmethod(type, "insert", list_insert);
py_bindmethod(type, "sort", list__sort); py_bindmethod(type, "sort", list_sort);
py_bind(py_tpobject(type), "sort(self, key=None, reverse=False)", list__sort); py_bind(py_tpobject(type), "sort(self, key=None, reverse=False)", list_sort);
py_setdict(py_tpobject(type), __hash__, py_None); py_setdict(py_tpobject(type), __hash__, py_None);
return type; return type;

View File

@ -5,7 +5,7 @@
#include <math.h> #include <math.h>
#define DEF_NUM_BINARY_OP(name, op, rint, rfloat) \ #define DEF_NUM_BINARY_OP(name, op, rint, rfloat) \
static bool _py_int##name(int argc, py_Ref argv) { \ static bool int##name(int argc, py_Ref argv) { \
PY_CHECK_ARGC(2); \ PY_CHECK_ARGC(2); \
if(py_isint(&argv[1])) { \ if(py_isint(&argv[1])) { \
py_i64 lhs = py_toint(&argv[0]); \ py_i64 lhs = py_toint(&argv[0]); \
@ -20,7 +20,7 @@
} \ } \
return true; \ return true; \
} \ } \
static bool _py_float##name(int argc, py_Ref argv) { \ static bool float##name(int argc, py_Ref argv) { \
PY_CHECK_ARGC(2); \ PY_CHECK_ARGC(2); \
py_f64 lhs = py_tofloat(&argv[0]); \ py_f64 lhs = py_tofloat(&argv[0]); \
py_f64 rhs; \ py_f64 rhs; \
@ -45,21 +45,21 @@ DEF_NUM_BINARY_OP(__ge__, >=, py_newbool, py_newbool)
#undef DEF_NUM_BINARY_OP #undef DEF_NUM_BINARY_OP
static bool _py_int__neg__(int argc, py_Ref argv) { static bool int__neg__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_i64 val = py_toint(&argv[0]); py_i64 val = py_toint(&argv[0]);
py_newint(py_retval(), -val); py_newint(py_retval(), -val);
return true; return true;
} }
static bool _py_float__neg__(int argc, py_Ref argv) { static bool float__neg__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_f64 val = py_tofloat(&argv[0]); py_f64 val = py_tofloat(&argv[0]);
py_newfloat(py_retval(), -val); py_newfloat(py_retval(), -val);
return true; return true;
} }
static bool _py_int__truediv__(int argc, py_Ref argv) { static bool int__truediv__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
py_i64 lhs = py_toint(&argv[0]); py_i64 lhs = py_toint(&argv[0]);
py_f64 rhs; py_f64 rhs;
@ -71,7 +71,7 @@ static bool _py_int__truediv__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_float__truediv__(int argc, py_Ref argv) { static bool float__truediv__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
py_f64 lhs = py_tofloat(&argv[0]); py_f64 lhs = py_tofloat(&argv[0]);
py_f64 rhs; py_f64 rhs;
@ -85,7 +85,7 @@ static bool _py_float__truediv__(int argc, py_Ref argv) {
#define ZeroDivisionError(msg) false #define ZeroDivisionError(msg) false
static bool _py_number__pow__(int argc, py_Ref argv) { static bool number__pow__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
if(py_isint(&argv[0]) && py_isint(&argv[1])) { if(py_isint(&argv[0]) && py_isint(&argv[1])) {
py_i64 lhs = py_toint(&argv[0]); py_i64 lhs = py_toint(&argv[0]);
@ -119,7 +119,7 @@ static bool _py_number__pow__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_int__floordiv__(int argc, py_Ref argv) { static bool int__floordiv__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
py_i64 lhs = py_toint(&argv[0]); py_i64 lhs = py_toint(&argv[0]);
if(py_isint(&argv[1])) { if(py_isint(&argv[1])) {
@ -132,7 +132,7 @@ static bool _py_int__floordiv__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_int__mod__(int argc, py_Ref argv) { static bool int__mod__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
py_i64 lhs = py_toint(&argv[0]); py_i64 lhs = py_toint(&argv[0]);
if(py_isint(&argv[1])) { if(py_isint(&argv[1])) {
@ -145,14 +145,14 @@ static bool _py_int__mod__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_int__invert__(int argc, py_Ref argv) { static bool int__invert__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_i64 val = py_toint(&argv[0]); py_i64 val = py_toint(&argv[0]);
py_newint(py_retval(), ~val); py_newint(py_retval(), ~val);
return true; return true;
} }
static bool _py_int__bit_length(int argc, py_Ref argv) { static bool int_bit_length(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_i64 x = py_toint(py_arg(0)); py_i64 x = py_toint(py_arg(0));
if(x < 0) x = -x; if(x < 0) x = -x;
@ -166,7 +166,7 @@ static bool _py_int__bit_length(int argc, py_Ref argv) {
} }
#define DEF_INT_BITWISE_OP(name, op) \ #define DEF_INT_BITWISE_OP(name, op) \
static bool _py_int##name(int argc, py_Ref argv) { \ static bool int##name(int argc, py_Ref argv) { \
PY_CHECK_ARGC(2); \ PY_CHECK_ARGC(2); \
py_i64 lhs = py_toint(&argv[0]); \ py_i64 lhs = py_toint(&argv[0]); \
if(py_isint(&argv[1])) { \ if(py_isint(&argv[1])) { \
@ -186,7 +186,7 @@ DEF_INT_BITWISE_OP(__rshift__, >>)
#undef DEF_INT_BITWISE_OP #undef DEF_INT_BITWISE_OP
static bool _py_int__repr__(int argc, py_Ref argv) { static bool int__repr__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_i64 val = py_toint(&argv[0]); py_i64 val = py_toint(&argv[0]);
char buf[32]; char buf[32];
@ -195,7 +195,7 @@ static bool _py_int__repr__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_float__repr__(int argc, py_Ref argv) { static bool float__repr__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_f64 val = py_tofloat(&argv[0]); py_f64 val = py_tofloat(&argv[0]);
c11_sbuf buf; c11_sbuf buf;
@ -223,7 +223,7 @@ static py_i64 c11_8bytes__hash(union c11_8bytes u) {
return u._i64; return u._i64;
} }
static bool _py_int__hash__(int argc, py_Ref argv) { static bool int__hash__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_i64 val = py_toint(&argv[0]); py_i64 val = py_toint(&argv[0]);
union c11_8bytes u = {._i64 = val}; union c11_8bytes u = {._i64 = val};
@ -231,7 +231,7 @@ static bool _py_int__hash__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_float__hash__(int argc, py_Ref argv) { static bool float__hash__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_f64 val = py_tofloat(&argv[0]); py_f64 val = py_tofloat(&argv[0]);
union c11_8bytes u = {._f64 = val}; union c11_8bytes u = {._f64 = val};
@ -239,21 +239,21 @@ static bool _py_float__hash__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_int__abs__(int argc, py_Ref argv) { static bool int__abs__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_i64 val = py_toint(&argv[0]); py_i64 val = py_toint(&argv[0]);
py_newint(py_retval(), val < 0 ? -val : val); py_newint(py_retval(), val < 0 ? -val : val);
return true; return true;
} }
static bool _py_float__abs__(int argc, py_Ref argv) { static bool float__abs__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_f64 val = py_tofloat(&argv[0]); py_f64 val = py_tofloat(&argv[0]);
py_newfloat(py_retval(), val < 0 ? -val : val); py_newfloat(py_retval(), val < 0 ? -val : val);
return true; return true;
} }
static bool _py_int__new__(int argc, py_Ref argv) { static bool int__new__(int argc, py_Ref argv) {
if(argc == 1 + 0) { if(argc == 1 + 0) {
// int() == 0 // int() == 0
py_newint(py_retval(), 0); py_newint(py_retval(), 0);
@ -308,7 +308,7 @@ static bool _py_int__new__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_float__new__(int argc, py_Ref argv) { static bool float__new__(int argc, py_Ref argv) {
if(argc == 1 + 0) { if(argc == 1 + 0) {
// float() == 0.0 // float() == 0.0
py_newfloat(py_retval(), 0.0); py_newfloat(py_retval(), 0.0);
@ -356,7 +356,7 @@ static bool _py_float__new__(int argc, py_Ref argv) {
} }
// tp_bool // tp_bool
static bool _py_bool__new__(int argc, py_Ref argv) { static bool bool__new__(int argc, py_Ref argv) {
assert(argc > 0); assert(argc > 0);
if(argc == 1){ if(argc == 1){
py_newbool(py_retval(), false); py_newbool(py_retval(), false);
@ -371,21 +371,21 @@ static bool _py_bool__new__(int argc, py_Ref argv) {
return TypeError("bool() takes at most 1 argument"); return TypeError("bool() takes at most 1 argument");
} }
static bool _py_bool__hash__(int argc, py_Ref argv) { static bool bool__hash__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
bool res = py_tobool(argv); bool res = py_tobool(argv);
py_newint(py_retval(), res); py_newint(py_retval(), res);
return true; return true;
} }
static bool _py_bool__repr__(int argc, py_Ref argv) { static bool bool__repr__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
bool res = py_tobool(argv); bool res = py_tobool(argv);
py_newstr(py_retval(), res ? "True" : "False"); py_newstr(py_retval(), res ? "True" : "False");
return true; return true;
} }
static bool _py_bool__eq__(int argc, py_Ref argv) { static bool bool__eq__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
bool lhs = py_tobool(&argv[0]); bool lhs = py_tobool(&argv[0]);
if(argv[1].type == tp_bool) { if(argv[1].type == tp_bool) {
@ -397,7 +397,7 @@ static bool _py_bool__eq__(int argc, py_Ref argv) {
return true; return true;
} }
static bool _py_bool__ne__(int argc, py_Ref argv) { static bool bool__ne__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
bool lhs = py_tobool(&argv[0]); bool lhs = py_tobool(&argv[0]);
if(argv[1].type == tp_bool) { if(argv[1].type == tp_bool) {
@ -411,74 +411,74 @@ static bool _py_bool__ne__(int argc, py_Ref argv) {
void pk_number__register() { void pk_number__register() {
/****** tp_int & tp_float ******/ /****** tp_int & tp_float ******/
py_bindmagic(tp_int, __add__, _py_int__add__); py_bindmagic(tp_int, __add__, int__add__);
py_bindmagic(tp_float, __add__, _py_float__add__); py_bindmagic(tp_float, __add__, float__add__);
py_bindmagic(tp_int, __sub__, _py_int__sub__); py_bindmagic(tp_int, __sub__, int__sub__);
py_bindmagic(tp_float, __sub__, _py_float__sub__); py_bindmagic(tp_float, __sub__, float__sub__);
py_bindmagic(tp_int, __mul__, _py_int__mul__); py_bindmagic(tp_int, __mul__, int__mul__);
py_bindmagic(tp_float, __mul__, _py_float__mul__); py_bindmagic(tp_float, __mul__, float__mul__);
py_bindmagic(tp_int, __eq__, _py_int__eq__); py_bindmagic(tp_int, __eq__, int__eq__);
py_bindmagic(tp_float, __eq__, _py_float__eq__); py_bindmagic(tp_float, __eq__, float__eq__);
py_bindmagic(tp_int, __ne__, _py_int__ne__); py_bindmagic(tp_int, __ne__, int__ne__);
py_bindmagic(tp_float, __ne__, _py_float__ne__); py_bindmagic(tp_float, __ne__, float__ne__);
py_bindmagic(tp_int, __lt__, _py_int__lt__); py_bindmagic(tp_int, __lt__, int__lt__);
py_bindmagic(tp_float, __lt__, _py_float__lt__); py_bindmagic(tp_float, __lt__, float__lt__);
py_bindmagic(tp_int, __le__, _py_int__le__); py_bindmagic(tp_int, __le__, int__le__);
py_bindmagic(tp_float, __le__, _py_float__le__); py_bindmagic(tp_float, __le__, float__le__);
py_bindmagic(tp_int, __gt__, _py_int__gt__); py_bindmagic(tp_int, __gt__, int__gt__);
py_bindmagic(tp_float, __gt__, _py_float__gt__); py_bindmagic(tp_float, __gt__, float__gt__);
py_bindmagic(tp_int, __ge__, _py_int__ge__); py_bindmagic(tp_int, __ge__, int__ge__);
py_bindmagic(tp_float, __ge__, _py_float__ge__); py_bindmagic(tp_float, __ge__, float__ge__);
// __neg__ // __neg__
py_bindmagic(tp_int, __neg__, _py_int__neg__); py_bindmagic(tp_int, __neg__, int__neg__);
py_bindmagic(tp_float, __neg__, _py_float__neg__); py_bindmagic(tp_float, __neg__, float__neg__);
// __repr__ // __repr__
py_bindmagic(tp_int, __repr__, _py_int__repr__); py_bindmagic(tp_int, __repr__, int__repr__);
py_bindmagic(tp_float, __repr__, _py_float__repr__); py_bindmagic(tp_float, __repr__, float__repr__);
// __hash__ // __hash__
py_bindmagic(tp_int, __hash__, _py_int__hash__); py_bindmagic(tp_int, __hash__, int__hash__);
py_bindmagic(tp_float, __hash__, _py_float__hash__); py_bindmagic(tp_float, __hash__, float__hash__);
// __abs__ // __abs__
py_bindmagic(tp_int, __abs__, _py_int__abs__); py_bindmagic(tp_int, __abs__, int__abs__);
py_bindmagic(tp_float, __abs__, _py_float__abs__); py_bindmagic(tp_float, __abs__, float__abs__);
// __new__ // __new__
py_bindmagic(tp_int, __new__, _py_int__new__); py_bindmagic(tp_int, __new__, int__new__);
py_bindmagic(tp_float, __new__, _py_float__new__); py_bindmagic(tp_float, __new__, float__new__);
// __truediv__ // __truediv__
py_bindmagic(tp_int, __truediv__, _py_int__truediv__); py_bindmagic(tp_int, __truediv__, int__truediv__);
py_bindmagic(tp_float, __truediv__, _py_float__truediv__); py_bindmagic(tp_float, __truediv__, float__truediv__);
// __pow__ // __pow__
py_bindmagic(tp_int, __pow__, _py_number__pow__); py_bindmagic(tp_int, __pow__, number__pow__);
py_bindmagic(tp_float, __pow__, _py_number__pow__); py_bindmagic(tp_float, __pow__, number__pow__);
// __floordiv__ & __mod__ // __floordiv__ & __mod__
py_bindmagic(tp_int, __floordiv__, _py_int__floordiv__); py_bindmagic(tp_int, __floordiv__, int__floordiv__);
py_bindmagic(tp_int, __mod__, _py_int__mod__); py_bindmagic(tp_int, __mod__, int__mod__);
// int.__invert__ & int.<BITWISE OP> // int.__invert__ & int.<BITWISE OP>
py_bindmagic(tp_int, __invert__, _py_int__invert__); py_bindmagic(tp_int, __invert__, int__invert__);
py_bindmagic(tp_int, __and__, _py_int__and__); py_bindmagic(tp_int, __and__, int__and__);
py_bindmagic(tp_int, __or__, _py_int__or__); py_bindmagic(tp_int, __or__, int__or__);
py_bindmagic(tp_int, __xor__, _py_int__xor__); py_bindmagic(tp_int, __xor__, int__xor__);
py_bindmagic(tp_int, __lshift__, _py_int__lshift__); py_bindmagic(tp_int, __lshift__, int__lshift__);
py_bindmagic(tp_int, __rshift__, _py_int__rshift__); py_bindmagic(tp_int, __rshift__, int__rshift__);
// int.bit_length // int.bit_length
py_bindmethod(tp_int, "bit_length", _py_int__bit_length); py_bindmethod(tp_int, "bit_length", int_bit_length);
/* tp_bool */ /* tp_bool */
py_bindmagic(tp_bool, __new__, _py_bool__new__); py_bindmagic(tp_bool, __new__, bool__new__);
py_bindmagic(tp_bool, __hash__, _py_bool__hash__); py_bindmagic(tp_bool, __hash__, bool__hash__);
py_bindmagic(tp_bool, __repr__, _py_bool__repr__); py_bindmagic(tp_bool, __repr__, bool__repr__);
py_bindmagic(tp_bool, __eq__, _py_bool__eq__); py_bindmagic(tp_bool, __eq__, bool__eq__);
py_bindmagic(tp_bool, __ne__, _py_bool__ne__); py_bindmagic(tp_bool, __ne__, bool__ne__);
} }

View File

@ -42,10 +42,35 @@ static bool slice__repr__(int argc, py_Ref argv) {
return true; return true;
} }
static bool slice_start__getter(int argc, py_Ref argv) {
py_Ref self = py_arg(0);
py_TValue* val = py_getslot(self, 0);
py_assign(py_retval(), val);
return true;
}
static bool slice_stop__getter(int argc, py_Ref argv) {
py_Ref self = py_arg(0);
py_TValue* val = py_getslot(self, 1);
py_assign(py_retval(), val);
return true;
}
static bool slice_step__getter(int argc, py_Ref argv) {
py_Ref self = py_arg(0);
py_TValue* val = py_getslot(self, 2);
py_assign(py_retval(), val);
return true;
}
py_Type pk_slice__register() { py_Type pk_slice__register() {
py_Type type = pk_newtype("slice", tp_object, NULL, NULL, false, true); py_Type type = pk_newtype("slice", tp_object, NULL, NULL, false, true);
py_bindmagic(type, __new__, slice__new__); py_bindmagic(type, __new__, slice__new__);
py_bindmagic(type, __repr__, slice__repr__); py_bindmagic(type, __repr__, slice__repr__);
py_bindproperty(type, "start", slice_start__getter, NULL);
py_bindproperty(type, "stop", slice_stop__getter, NULL);
py_bindproperty(type, "step", slice_step__getter, NULL);
return type; return type;
} }

View File

@ -212,7 +212,7 @@ DEF_STR_CMP_OP(__ge__, c11_sv__cmp, res >= 0)
#undef DEF_STR_CMP_OP #undef DEF_STR_CMP_OP
static bool str__lower(int argc, py_Ref argv) { static bool str_lower(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
c11_string* self = py_touserdata(&argv[0]); c11_string* self = py_touserdata(&argv[0]);
int total_size = sizeof(c11_string) + self->size + 1; int total_size = sizeof(c11_string) + self->size + 1;
@ -227,7 +227,7 @@ static bool str__lower(int argc, py_Ref argv) {
return true; return true;
} }
static bool str__upper(int argc, py_Ref argv) { static bool str_upper(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
c11_string* self = py_touserdata(&argv[0]); c11_string* self = py_touserdata(&argv[0]);
int total_size = sizeof(c11_string) + self->size + 1; int total_size = sizeof(c11_string) + self->size + 1;
@ -242,7 +242,7 @@ static bool str__upper(int argc, py_Ref argv) {
return true; return true;
} }
static bool str__startswith(int argc, py_Ref argv) { static bool str_startswith(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
c11_string* self = py_touserdata(&argv[0]); c11_string* self = py_touserdata(&argv[0]);
PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(1, tp_str);
@ -251,7 +251,7 @@ static bool str__startswith(int argc, py_Ref argv) {
return true; return true;
} }
static bool str__endswith(int argc, py_Ref argv) { static bool str_endswith(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
c11_string* self = py_touserdata(&argv[0]); c11_string* self = py_touserdata(&argv[0]);
PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(1, tp_str);
@ -260,7 +260,7 @@ static bool str__endswith(int argc, py_Ref argv) {
return true; return true;
} }
static bool str__join(int argc, py_Ref argv) { static bool str_join(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
c11_sv self = c11_string__sv(py_touserdata(&argv[0])); c11_sv self = c11_string__sv(py_touserdata(&argv[0]));
py_Ref _1 = py_arg(1); py_Ref _1 = py_arg(1);
@ -292,7 +292,7 @@ static bool str__join(int argc, py_Ref argv) {
return true; return true;
} }
static bool str__replace(int argc, py_Ref argv) { static bool str_replace(int argc, py_Ref argv) {
PY_CHECK_ARGC(3); PY_CHECK_ARGC(3);
c11_string* self = py_touserdata(&argv[0]); c11_string* self = py_touserdata(&argv[0]);
PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(1, tp_str);
@ -306,7 +306,7 @@ static bool str__replace(int argc, py_Ref argv) {
return true; return true;
} }
static bool str__split(int argc, py_Ref argv) { static bool str_split(int argc, py_Ref argv) {
c11_sv self = c11_string__sv(py_touserdata(&argv[0])); c11_sv self = c11_string__sv(py_touserdata(&argv[0]));
c11_vector res; c11_vector res;
if(argc > 2) return TypeError("split() takes at most 2 arguments"); if(argc > 2) return TypeError("split() takes at most 2 arguments");
@ -329,7 +329,7 @@ static bool str__split(int argc, py_Ref argv) {
return true; return true;
} }
static bool str__count(int argc, py_Ref argv) { static bool str_count(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
c11_string* self = py_touserdata(&argv[0]); c11_string* self = py_touserdata(&argv[0]);
PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(1, tp_str);
@ -355,19 +355,19 @@ static bool str__strip_impl(bool left, bool right, int argc, py_Ref argv) {
return true; return true;
} }
static bool str__strip(int argc, py_Ref argv) { static bool str_strip(int argc, py_Ref argv) {
return str__strip_impl(true, true, argc, argv); return str__strip_impl(true, true, argc, argv);
} }
static bool str__lstrip(int argc, py_Ref argv) { static bool str_lstrip(int argc, py_Ref argv) {
return str__strip_impl(true, false, argc, argv); return str__strip_impl(true, false, argc, argv);
} }
static bool str__rstrip(int argc, py_Ref argv) { static bool str_rstrip(int argc, py_Ref argv) {
return str__strip_impl(false, true, argc, argv); return str__strip_impl(false, true, argc, argv);
} }
static bool str__zfill(int argc, py_Ref argv) { static bool str_zfill(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
c11_sv self = c11_string__sv(py_touserdata(&argv[0])); c11_sv self = c11_string__sv(py_touserdata(&argv[0]));
PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(1, tp_int);
@ -423,15 +423,15 @@ static bool str__widthjust_impl(bool left, int argc, py_Ref argv) {
return true; return true;
} }
static bool str__ljust(int argc, py_Ref argv) { static bool str_ljust(int argc, py_Ref argv) {
return str__widthjust_impl(true, argc, argv); return str__widthjust_impl(true, argc, argv);
} }
static bool str__rjust(int argc, py_Ref argv) { static bool str_rjust(int argc, py_Ref argv) {
return str__widthjust_impl(false, argc, argv); return str__widthjust_impl(false, argc, argv);
} }
static bool str__find(int argc, py_Ref argv) { static bool str_find(int argc, py_Ref argv) {
if(argc > 3) return TypeError("find() takes at most 3 arguments"); if(argc > 3) return TypeError("find() takes at most 3 arguments");
int start = 0; int start = 0;
if(argc == 3) { if(argc == 3) {
@ -446,8 +446,8 @@ static bool str__find(int argc, py_Ref argv) {
return true; return true;
} }
static bool str__index(int argc, py_Ref argv) { static bool str_index(int argc, py_Ref argv) {
bool ok = str__find(argc, argv); bool ok = str_find(argc, argv);
if(!ok) return false; if(!ok) return false;
if(py_toint(py_retval()) == -1) return ValueError("substring not found"); if(py_toint(py_retval()) == -1) return ValueError("substring not found");
return true; return true;
@ -476,22 +476,22 @@ py_Type pk_str__register() {
py_bindmagic(tp_str, __gt__, str__gt__); py_bindmagic(tp_str, __gt__, str__gt__);
py_bindmagic(tp_str, __ge__, str__ge__); py_bindmagic(tp_str, __ge__, str__ge__);
py_bindmethod(tp_str, "lower", str__lower); py_bindmethod(tp_str, "lower", str_lower);
py_bindmethod(tp_str, "upper", str__upper); py_bindmethod(tp_str, "upper", str_upper);
py_bindmethod(tp_str, "startswith", str__startswith); py_bindmethod(tp_str, "startswith", str_startswith);
py_bindmethod(tp_str, "endswith", str__endswith); py_bindmethod(tp_str, "endswith", str_endswith);
py_bindmethod(tp_str, "join", str__join); py_bindmethod(tp_str, "join", str_join);
py_bindmethod(tp_str, "replace", str__replace); py_bindmethod(tp_str, "replace", str_replace);
py_bindmethod(tp_str, "split", str__split); py_bindmethod(tp_str, "split", str_split);
py_bindmethod(tp_str, "count", str__count); py_bindmethod(tp_str, "count", str_count);
py_bindmethod(tp_str, "strip", str__strip); py_bindmethod(tp_str, "strip", str_strip);
py_bindmethod(tp_str, "lstrip", str__lstrip); py_bindmethod(tp_str, "lstrip", str_lstrip);
py_bindmethod(tp_str, "rstrip", str__rstrip); py_bindmethod(tp_str, "rstrip", str_rstrip);
py_bindmethod(tp_str, "zfill", str__zfill); py_bindmethod(tp_str, "zfill", str_zfill);
py_bindmethod(tp_str, "ljust", str__ljust); py_bindmethod(tp_str, "ljust", str_ljust);
py_bindmethod(tp_str, "rjust", str__rjust); py_bindmethod(tp_str, "rjust", str_rjust);
py_bindmethod(tp_str, "find", str__find); py_bindmethod(tp_str, "find", str_find);
py_bindmethod(tp_str, "index", str__index); py_bindmethod(tp_str, "index", str_index);
return type; return type;
} }