mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
...
This commit is contained in:
parent
ee29eadcd3
commit
7ca97f03a7
@ -430,27 +430,27 @@ bool py_len(py_Ref val) PY_RAISE;
|
|||||||
|
|
||||||
/************* Unchecked Functions *************/
|
/************* Unchecked Functions *************/
|
||||||
|
|
||||||
py_ObjectRef py_tuple__data(py_Ref self);
|
py_ObjectRef py_tuple_data(py_Ref self);
|
||||||
py_ObjectRef py_tuple__getitem(py_Ref self, int i);
|
py_ObjectRef py_tuple_getitem(py_Ref self, int i);
|
||||||
void py_tuple__setitem(py_Ref self, int i, py_Ref val);
|
void py_tuple_setitem(py_Ref self, int i, py_Ref val);
|
||||||
int py_tuple__len(py_Ref self);
|
int py_tuple_len(py_Ref self);
|
||||||
|
|
||||||
py_TmpRef py_list__data(py_Ref self);
|
py_TmpRef py_list_data(py_Ref self);
|
||||||
py_TmpRef py_list__getitem(py_Ref self, int i);
|
py_TmpRef py_list_getitem(py_Ref self, int i);
|
||||||
void py_list__setitem(py_Ref self, int i, py_Ref val);
|
void py_list_setitem(py_Ref self, int i, py_Ref val);
|
||||||
void py_list__delitem(py_Ref self, int i);
|
void py_list_delitem(py_Ref self, int i);
|
||||||
int py_list__len(py_Ref self);
|
int py_list_len(py_Ref self);
|
||||||
void py_list__append(py_Ref self, py_Ref val);
|
void py_list_append(py_Ref self, py_Ref val);
|
||||||
void py_list__clear(py_Ref self);
|
void py_list_clear(py_Ref self);
|
||||||
void py_list__insert(py_Ref self, int i, py_Ref val);
|
void py_list_insert(py_Ref self, int i, py_Ref val);
|
||||||
void py_list__reverse(py_Ref self);
|
void py_list_reverse(py_Ref self);
|
||||||
|
|
||||||
py_TmpRef py_dict__getitem(py_Ref self, py_Ref key) PY_RAISE;
|
py_TmpRef py_dict_getitem(py_Ref self, py_Ref key) PY_RAISE;
|
||||||
void py_dict__setitem(py_Ref self, py_Ref key, py_Ref val) PY_RAISE;
|
void py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) PY_RAISE;
|
||||||
void py_dict__delitem(py_Ref self, py_Ref key) PY_RAISE;
|
void py_dict_delitem(py_Ref self, py_Ref key) PY_RAISE;
|
||||||
bool py_dict__contains(py_Ref self, py_Ref key) PY_RAISE;
|
bool py_dict_contains(py_Ref self, py_Ref key) PY_RAISE;
|
||||||
int py_dict__len(py_Ref self);
|
int py_dict_len(py_Ref self);
|
||||||
bool py_dict__apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx) PY_RAISE;
|
bool py_dict_apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx) PY_RAISE;
|
||||||
|
|
||||||
/************* Others *************/
|
/************* Others *************/
|
||||||
|
|
||||||
|
@ -2262,7 +2262,7 @@ static Error* read_literal(Compiler* self, py_Ref out) {
|
|||||||
consume(TK_RPAREN);
|
consume(TK_RPAREN);
|
||||||
py_newtuple(out, count);
|
py_newtuple(out, count);
|
||||||
for(int i = 0; i < count; i++) {
|
for(int i = 0; i < count; i++) {
|
||||||
py_tuple__setitem(out, i, &cpnts[i]);
|
py_tuple_setitem(out, i, &cpnts[i]);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -438,7 +438,7 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
py_newtuple(&tmp, byte.arg);
|
py_newtuple(&tmp, byte.arg);
|
||||||
py_TValue* begin = SP() - byte.arg;
|
py_TValue* begin = SP() - byte.arg;
|
||||||
for(int i = 0; i < byte.arg; i++) {
|
for(int i = 0; i < byte.arg; i++) {
|
||||||
py_tuple__setitem(&tmp, i, begin + i);
|
py_tuple_setitem(&tmp, i, begin + i);
|
||||||
}
|
}
|
||||||
SP() = begin;
|
SP() = begin;
|
||||||
PUSH(&tmp);
|
PUSH(&tmp);
|
||||||
@ -449,7 +449,7 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
py_newlistn(&tmp, byte.arg);
|
py_newlistn(&tmp, byte.arg);
|
||||||
py_TValue* begin = SP() - byte.arg;
|
py_TValue* begin = SP() - byte.arg;
|
||||||
for(int i = 0; i < byte.arg; i++) {
|
for(int i = 0; i < byte.arg; i++) {
|
||||||
py_list__setitem(&tmp, i, begin + i);
|
py_list_setitem(&tmp, i, begin + i);
|
||||||
}
|
}
|
||||||
SP() = begin;
|
SP() = begin;
|
||||||
PUSH(&tmp);
|
PUSH(&tmp);
|
||||||
@ -460,7 +460,7 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
py_Ref tmp = py_pushtmp();
|
py_Ref tmp = py_pushtmp();
|
||||||
py_newdict(tmp);
|
py_newdict(tmp);
|
||||||
for(int i = 0; i < byte.arg * 2; i += 2) {
|
for(int i = 0; i < byte.arg * 2; i += 2) {
|
||||||
py_dict__setitem(tmp, begin + i, begin + i + 1);
|
py_dict_setitem(tmp, begin + i, begin + i + 1);
|
||||||
if(py_checkexc()) goto __ERROR;
|
if(py_checkexc()) goto __ERROR;
|
||||||
}
|
}
|
||||||
SP() = begin;
|
SP() = begin;
|
||||||
@ -668,9 +668,9 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
py_TValue* kwargs = py_getslot(&curr[1], 0);
|
py_TValue* kwargs = py_getslot(&curr[1], 0);
|
||||||
if(kwargs->type == tp_dict) {
|
if(kwargs->type == tp_dict) {
|
||||||
py_TValue* p = buf + n;
|
py_TValue* p = buf + n;
|
||||||
if(!py_dict__apply(kwargs, unpack_dict_to_buffer, &p)) goto __ERROR;
|
if(!py_dict_apply(kwargs, unpack_dict_to_buffer, &p)) goto __ERROR;
|
||||||
n = p - buf;
|
n = p - buf;
|
||||||
kwargc += py_dict__len(kwargs) - 1;
|
kwargc += py_dict_len(kwargs) - 1;
|
||||||
} else {
|
} else {
|
||||||
TypeError("**kwargs must be a dict, got '%t'", kwargs->type);
|
TypeError("**kwargs must be a dict, got '%t'", kwargs->type);
|
||||||
goto __ERROR;
|
goto __ERROR;
|
||||||
@ -706,13 +706,13 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
/////////
|
/////////
|
||||||
case OP_LIST_APPEND: {
|
case OP_LIST_APPEND: {
|
||||||
// [list, iter, value]
|
// [list, iter, value]
|
||||||
py_list__append(THIRD(), TOP());
|
py_list_append(THIRD(), TOP());
|
||||||
POP();
|
POP();
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
case OP_DICT_ADD: {
|
case OP_DICT_ADD: {
|
||||||
// [dict, iter, key, value]
|
// [dict, iter, key, value]
|
||||||
py_dict__setitem(FOURTH(), SECOND(), TOP());
|
py_dict_setitem(FOURTH(), SECOND(), TOP());
|
||||||
if(py_checkexc()) goto __ERROR;
|
if(py_checkexc()) goto __ERROR;
|
||||||
STACK_SHRINK(2);
|
STACK_SHRINK(2);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
@ -838,7 +838,7 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
}
|
}
|
||||||
py_newlistn(SP()++, exceed);
|
py_newlistn(SP()++, exceed);
|
||||||
for(int i = 0; i < exceed; i++) {
|
for(int i = 0; i < exceed; i++) {
|
||||||
py_list__setitem(TOP(), i, p + byte.arg + i);
|
py_list_setitem(TOP(), i, p + byte.arg + i);
|
||||||
}
|
}
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
|
@ -328,7 +328,7 @@ static bool
|
|||||||
py_Ref vargs = &buffer[decl->starred_arg];
|
py_Ref vargs = &buffer[decl->starred_arg];
|
||||||
py_newtuple(vargs, exceed_argc);
|
py_newtuple(vargs, exceed_argc);
|
||||||
for(int j = 0; j < exceed_argc; j++) {
|
for(int j = 0; j < exceed_argc; j++) {
|
||||||
py_tuple__setitem(vargs, j, t++);
|
py_tuple_setitem(vargs, j, t++);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// kwdefaults override
|
// kwdefaults override
|
||||||
@ -361,7 +361,7 @@ static bool
|
|||||||
py_Ref tmp = py_pushtmp();
|
py_Ref tmp = py_pushtmp();
|
||||||
c11_sv key_sv = py_name2sv(key);
|
c11_sv key_sv = py_name2sv(key);
|
||||||
py_newstrn(tmp, key_sv.data, key_sv.size);
|
py_newstrn(tmp, key_sv.data, key_sv.size);
|
||||||
py_dict__setitem(&buffer[decl->starred_kwarg], tmp, &p1[2 * j + 1]);
|
py_dict_setitem(&buffer[decl->starred_kwarg], tmp, &p1[2 * j + 1]);
|
||||||
py_pop();
|
py_pop();
|
||||||
if(py_checkexc()) return false;
|
if(py_checkexc()) return false;
|
||||||
}
|
}
|
||||||
@ -584,11 +584,11 @@ void pk_print_stack(VM* self, Frame* frame, Bytecode byte) {
|
|||||||
case tp_bool: c11_sbuf__write_cstr(&buf, p->_bool ? "True" : "False"); break;
|
case tp_bool: c11_sbuf__write_cstr(&buf, p->_bool ? "True" : "False"); break;
|
||||||
case tp_NoneType: c11_sbuf__write_cstr(&buf, "None"); break;
|
case tp_NoneType: c11_sbuf__write_cstr(&buf, "None"); break;
|
||||||
case tp_list: {
|
case tp_list: {
|
||||||
pk_sprintf(&buf, "list(%d)", py_list__len(p));
|
pk_sprintf(&buf, "list(%d)", py_list_len(p));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case tp_tuple: {
|
case tp_tuple: {
|
||||||
pk_sprintf(&buf, "tuple(%d)", py_tuple__len(p));
|
pk_sprintf(&buf, "tuple(%d)", py_tuple_len(p));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case tp_function: {
|
case tp_function: {
|
||||||
|
@ -37,8 +37,8 @@ static bool math_fsum(int argc, py_Ref argv) {
|
|||||||
py_Ref list = py_arg(0);
|
py_Ref list = py_arg(0);
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
double c = 0;
|
double c = 0;
|
||||||
for(int i = 0; i < py_list__len(list); i++) {
|
for(int i = 0; i < py_list_len(list); i++) {
|
||||||
py_Ref item = py_list__getitem(list, i);
|
py_Ref item = py_list_getitem(list, i);
|
||||||
double x;
|
double x;
|
||||||
if(!py_castfloat(item, &x)) return false;
|
if(!py_castfloat(item, &x)) return false;
|
||||||
double y = x - c;
|
double y = x - c;
|
||||||
@ -136,8 +136,8 @@ static bool math_modf(int argc, py_Ref argv) {
|
|||||||
double i;
|
double i;
|
||||||
double f = modf(py_tofloat(py_arg(0)), &i);
|
double f = modf(py_tofloat(py_arg(0)), &i);
|
||||||
py_newtuple(py_retval(), 2);
|
py_newtuple(py_retval(), 2);
|
||||||
py_Ref _0 = py_tuple__getitem(py_retval(), 0);
|
py_Ref _0 = py_tuple_getitem(py_retval(), 0);
|
||||||
py_Ref _1 = py_tuple__getitem(py_retval(), 1);
|
py_Ref _1 = py_tuple_getitem(py_retval(), 1);
|
||||||
py_newfloat(_0, f);
|
py_newfloat(_0, f);
|
||||||
py_newfloat(_1, i);
|
py_newfloat(_1, i);
|
||||||
return true;
|
return true;
|
||||||
|
@ -101,9 +101,9 @@ FuncDecl_ FuncDecl__build(c11_sv name,
|
|||||||
}
|
}
|
||||||
if(starred_arg.size) { FuncDecl__add_starred_arg(decl, py_namev(starred_arg)); }
|
if(starred_arg.size) { FuncDecl__add_starred_arg(decl, py_namev(starred_arg)); }
|
||||||
assert(py_istype(kwdefaults, tp_tuple));
|
assert(py_istype(kwdefaults, tp_tuple));
|
||||||
assert(py_tuple__len(kwdefaults) == kwargc);
|
assert(py_tuple_len(kwdefaults) == kwargc);
|
||||||
for(int i = 0; i < kwargc; i++) {
|
for(int i = 0; i < kwargc; i++) {
|
||||||
FuncDecl__add_kwarg(decl, py_namev(kwargs[i]), py_tuple__getitem(kwdefaults, i));
|
FuncDecl__add_kwarg(decl, py_namev(kwargs[i]), py_tuple_getitem(kwdefaults, i));
|
||||||
}
|
}
|
||||||
if(starred_kwarg.size) FuncDecl__add_starred_kwarg(decl, py_namev(starred_kwarg));
|
if(starred_kwarg.size) FuncDecl__add_starred_kwarg(decl, py_namev(starred_kwarg));
|
||||||
decl->docstring = docstring;
|
decl->docstring = docstring;
|
||||||
|
@ -174,7 +174,7 @@ static bool builtins_reversed(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
// convert _0 to list object
|
// convert _0 to list object
|
||||||
if(!py_tpcall(tp_list, 1, argv)) return false;
|
if(!py_tpcall(tp_list, 1, argv)) return false;
|
||||||
py_list__reverse(py_retval());
|
py_list_reverse(py_retval());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,9 +329,9 @@ static bool builtins_eval(int argc, py_Ref argv) {
|
|||||||
static bool builtins_isinstance(int argc, py_Ref argv) {
|
static bool builtins_isinstance(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(2);
|
PY_CHECK_ARGC(2);
|
||||||
if(py_istuple(py_arg(1))) {
|
if(py_istuple(py_arg(1))) {
|
||||||
int length = py_tuple__len(py_arg(1));
|
int length = py_tuple_len(py_arg(1));
|
||||||
for(int i = 0; i < length; i++) {
|
for(int i = 0; i < length; i++) {
|
||||||
py_Ref item = py_tuple__getitem(py_arg(1), i);
|
py_Ref item = py_tuple_getitem(py_arg(1), i);
|
||||||
if(!py_checktype(item, tp_type)) return false;
|
if(!py_checktype(item, tp_type)) return false;
|
||||||
if(py_isinstance(py_arg(0), py_totype(item))) {
|
if(py_isinstance(py_arg(0), py_totype(item))) {
|
||||||
py_newbool(py_retval(), true);
|
py_newbool(py_retval(), true);
|
||||||
@ -452,7 +452,7 @@ static bool function__closure__getter(int argc, py_Ref argv) {
|
|||||||
c11__foreach(NameDict_KV, ud->closure, it) {
|
c11__foreach(NameDict_KV, ud->closure, it) {
|
||||||
// printf("%s -> %s\n", py_name2str(it->key), py_tpname(it->value.type));
|
// printf("%s -> %s\n", py_name2str(it->key), py_tpname(it->value.type));
|
||||||
py_newstr(r0, py_name2str(it->key));
|
py_newstr(r0, py_name2str(it->key));
|
||||||
py_dict__setitem(retval, r0, &it->value);
|
py_dict_setitem(retval, r0, &it->value);
|
||||||
if(py_checkexc()) {
|
if(py_checkexc()) {
|
||||||
py_shrink(2);
|
py_shrink(2);
|
||||||
return false;
|
return false;
|
||||||
|
@ -13,11 +13,11 @@ typedef struct array_iterator {
|
|||||||
|
|
||||||
py_TValue* pk_arrayview(py_Ref self, int* length) {
|
py_TValue* pk_arrayview(py_Ref self, int* length) {
|
||||||
if(self->type == tp_list) {
|
if(self->type == tp_list) {
|
||||||
*length = py_list__len(self);
|
*length = py_list_len(self);
|
||||||
return py_list__data(self);
|
return py_list_data(self);
|
||||||
}
|
}
|
||||||
if(self->type == tp_tuple) {
|
if(self->type == tp_tuple) {
|
||||||
*length = py_tuple__len(self);
|
*length = py_tuple_len(self);
|
||||||
return PyObject__slots(self->_obj);
|
return PyObject__slots(self->_obj);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -217,13 +217,13 @@ static bool dict__init__(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(1, tp_list);
|
PY_CHECK_ARG_TYPE(1, tp_list);
|
||||||
Dict* self = py_touserdata(argv);
|
Dict* self = py_touserdata(argv);
|
||||||
py_Ref list = py_arg(1);
|
py_Ref list = py_arg(1);
|
||||||
for(int i = 0; i < py_list__len(list); i++) {
|
for(int i = 0; i < py_list_len(list); i++) {
|
||||||
py_Ref tuple = py_list__getitem(list, i);
|
py_Ref tuple = py_list_getitem(list, i);
|
||||||
if(!py_istuple(tuple) || py_tuple__len(tuple) != 2) {
|
if(!py_istuple(tuple) || py_tuple_len(tuple) != 2) {
|
||||||
return TypeError("dict.__init__() argument must be a list of tuple-2");
|
return TypeError("dict.__init__() argument must be a list of tuple-2");
|
||||||
}
|
}
|
||||||
py_Ref key = py_tuple__getitem(tuple, 0);
|
py_Ref key = py_tuple_getitem(tuple, 0);
|
||||||
py_Ref val = py_tuple__getitem(tuple, 1);
|
py_Ref val = py_tuple_getitem(tuple, 1);
|
||||||
if(!Dict__set(self, key, val)) return false;
|
if(!Dict__set(self, key, val)) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -407,7 +407,7 @@ static bool dict_keys(int argc, py_Ref argv) {
|
|||||||
while(1) {
|
while(1) {
|
||||||
DictEntry* entry = DictIterator__next(&iter);
|
DictEntry* entry = DictIterator__next(&iter);
|
||||||
if(!entry) break;
|
if(!entry) break;
|
||||||
py_tuple__setitem(py_retval(), i++, &entry->key);
|
py_tuple_setitem(py_retval(), i++, &entry->key);
|
||||||
}
|
}
|
||||||
assert(i == self->length);
|
assert(i == self->length);
|
||||||
return true;
|
return true;
|
||||||
@ -423,7 +423,7 @@ static bool dict_values(int argc, py_Ref argv) {
|
|||||||
while(1) {
|
while(1) {
|
||||||
DictEntry* entry = DictIterator__next(&iter);
|
DictEntry* entry = DictIterator__next(&iter);
|
||||||
if(!entry) break;
|
if(!entry) break;
|
||||||
py_tuple__setitem(py_retval(), i++, &entry->val);
|
py_tuple_setitem(py_retval(), i++, &entry->val);
|
||||||
}
|
}
|
||||||
assert(i == self->length);
|
assert(i == self->length);
|
||||||
return true;
|
return true;
|
||||||
@ -469,8 +469,8 @@ static bool dict_items__next__(int argc, py_Ref argv) {
|
|||||||
DictEntry* entry = (DictIterator__next(iter));
|
DictEntry* entry = (DictIterator__next(iter));
|
||||||
if(entry) {
|
if(entry) {
|
||||||
py_newtuple(py_retval(), 2);
|
py_newtuple(py_retval(), 2);
|
||||||
py_tuple__setitem(py_retval(), 0, &entry->key);
|
py_tuple_setitem(py_retval(), 0, &entry->key);
|
||||||
py_tuple__setitem(py_retval(), 1, &entry->val);
|
py_tuple_setitem(py_retval(), 1, &entry->val);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return StopIteration();
|
return StopIteration();
|
||||||
@ -490,7 +490,7 @@ void py_newdict(py_Ref out) {
|
|||||||
Dict__ctor(ud, 8);
|
Dict__ctor(ud, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_dict__getitem(py_Ref self, py_Ref key) {
|
py_Ref py_dict_getitem(py_Ref self, py_Ref key) {
|
||||||
assert(py_isdict(self));
|
assert(py_isdict(self));
|
||||||
Dict* ud = py_touserdata(self);
|
Dict* ud = py_touserdata(self);
|
||||||
DictEntry* entry;
|
DictEntry* entry;
|
||||||
@ -499,19 +499,19 @@ py_Ref py_dict__getitem(py_Ref self, py_Ref key) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_dict__delitem(py_Ref self, py_Ref key) {
|
void py_dict_delitem(py_Ref self, py_Ref key) {
|
||||||
assert(py_isdict(self));
|
assert(py_isdict(self));
|
||||||
Dict* ud = py_touserdata(self);
|
Dict* ud = py_touserdata(self);
|
||||||
Dict__pop(ud, key);
|
Dict__pop(ud, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_dict__setitem(py_Ref self, py_Ref key, py_Ref val) {
|
void py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) {
|
||||||
assert(py_isdict(self));
|
assert(py_isdict(self));
|
||||||
Dict* ud = py_touserdata(self);
|
Dict* ud = py_touserdata(self);
|
||||||
Dict__set(ud, key, val);
|
Dict__set(ud, key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool py_dict__contains(py_Ref self, py_Ref key) {
|
bool py_dict_contains(py_Ref self, py_Ref key) {
|
||||||
assert(py_isdict(self));
|
assert(py_isdict(self));
|
||||||
Dict* ud = py_touserdata(self);
|
Dict* ud = py_touserdata(self);
|
||||||
DictEntry* entry;
|
DictEntry* entry;
|
||||||
@ -519,13 +519,13 @@ bool py_dict__contains(py_Ref self, py_Ref key) {
|
|||||||
return ok && entry != NULL;
|
return ok && entry != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int py_dict__len(py_Ref self) {
|
int py_dict_len(py_Ref self) {
|
||||||
assert(py_isdict(self));
|
assert(py_isdict(self));
|
||||||
Dict* ud = py_touserdata(self);
|
Dict* ud = py_touserdata(self);
|
||||||
return ud->length;
|
return ud->length;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool py_dict__apply(py_Ref self, bool (*f)(py_Ref, py_Ref, void*), void* ctx) {
|
bool py_dict_apply(py_Ref self, bool (*f)(py_Ref, py_Ref, void*), void* ctx) {
|
||||||
Dict* ud = py_touserdata(self);
|
Dict* ud = py_touserdata(self);
|
||||||
for(int i = 0; i < ud->entries.count; i++) {
|
for(int i = 0; i < ud->entries.count; i++) {
|
||||||
DictEntry* entry = c11__at(DictEntry, &ud->entries, i);
|
DictEntry* entry = c11__at(DictEntry, &ud->entries, i);
|
||||||
|
@ -19,47 +19,47 @@ void py_newlistn(py_Ref out, int n) {
|
|||||||
userdata->count = n;
|
userdata->count = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_list__data(py_Ref self) {
|
py_Ref py_list_data(py_Ref self) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
return userdata->data;
|
return userdata->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_list__getitem(py_Ref self, int i) {
|
py_Ref py_list_getitem(py_Ref self, int i) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
return c11__at(py_TValue, userdata, i);
|
return c11__at(py_TValue, userdata, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list__setitem(py_Ref self, int i, py_Ref val) {
|
void py_list_setitem(py_Ref self, int i, py_Ref val) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
c11__setitem(py_TValue, userdata, i, *val);
|
c11__setitem(py_TValue, userdata, i, *val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list__delitem(py_Ref self, int i) {
|
void py_list_delitem(py_Ref self, int i) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
c11_vector__erase(py_TValue, userdata, i);
|
c11_vector__erase(py_TValue, userdata, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int py_list__len(py_Ref self) {
|
int py_list_len(py_Ref self) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
return userdata->count;
|
return userdata->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list__append(py_Ref self, py_Ref val) {
|
void py_list_append(py_Ref self, py_Ref val) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
c11_vector__push(py_TValue, userdata, *val);
|
c11_vector__push(py_TValue, userdata, *val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list__clear(py_Ref self) {
|
void py_list_clear(py_Ref self) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
c11_vector__clear(userdata);
|
c11_vector__clear(userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list__insert(py_Ref self, int i, py_Ref val) {
|
void py_list_insert(py_Ref self, int i, py_Ref val) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
c11_vector__insert(py_TValue, userdata, i, *val);
|
c11_vector__insert(py_TValue, userdata, i, *val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list__reverse(py_Ref self) {
|
void py_list_reverse(py_Ref self) {
|
||||||
List* userdata = py_touserdata(self);
|
List* userdata = py_touserdata(self);
|
||||||
c11__reverse(py_TValue, userdata);
|
c11__reverse(py_TValue, userdata);
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ void py_list__reverse(py_Ref self) {
|
|||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
static bool list__len__(int argc, py_Ref argv) {
|
static bool list__len__(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
py_i64 res = py_list__len(py_arg(0));
|
py_i64 res = py_list_len(py_arg(0));
|
||||||
py_newint(py_retval(), res);
|
py_newint(py_retval(), res);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -107,7 +107,7 @@ static bool list__new__(int argc, py_Ref argv) {
|
|||||||
if(p) {
|
if(p) {
|
||||||
py_newlistn(py_retval(), length);
|
py_newlistn(py_retval(), length);
|
||||||
for(int i = 0; i < length; i++) {
|
for(int i = 0; i < length; i++) {
|
||||||
py_list__setitem(py_retval(), i, p + i);
|
py_list_setitem(py_retval(), i, p + i);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -125,7 +125,7 @@ static bool list__new__(int argc, py_Ref argv) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(!res) break;
|
if(!res) break;
|
||||||
py_list__append(list, py_retval());
|
py_list_append(list, py_retval());
|
||||||
}
|
}
|
||||||
*py_retval() = *list;
|
*py_retval() = *list;
|
||||||
py_shrink(2);
|
py_shrink(2);
|
||||||
@ -218,7 +218,7 @@ 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());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -256,8 +256,8 @@ static bool list_extend(int argc, py_Ref argv) {
|
|||||||
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++) {
|
||||||
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));
|
||||||
if(res == -1) return false;
|
if(res == -1) return false;
|
||||||
if(res) count++;
|
if(res) count++;
|
||||||
}
|
}
|
||||||
@ -267,7 +267,7 @@ static bool list_count(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -288,8 +288,8 @@ static bool list_index(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||||
start = py_toint(py_arg(2));
|
start = py_toint(py_arg(2));
|
||||||
}
|
}
|
||||||
for(int i = start; i < py_list__len(py_arg(0)); i++) {
|
for(int i = start; 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));
|
||||||
if(res == -1) return false;
|
if(res == -1) return false;
|
||||||
if(res) {
|
if(res) {
|
||||||
py_newint(py_retval(), i);
|
py_newint(py_retval(), i);
|
||||||
@ -309,11 +309,11 @@ static bool list_reverse(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
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));
|
||||||
if(res == -1) return false;
|
if(res == -1) return false;
|
||||||
if(res) {
|
if(res) {
|
||||||
py_list__delitem(py_arg(0), i);
|
py_list_delitem(py_arg(0), i);
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -268,11 +268,11 @@ static bool str_join(int argc, py_Ref argv) {
|
|||||||
py_TValue* p;
|
py_TValue* p;
|
||||||
int length;
|
int length;
|
||||||
if(py_istype(_1, tp_list)) {
|
if(py_istype(_1, tp_list)) {
|
||||||
p = py_list__getitem(_1, 0);
|
p = py_list_getitem(_1, 0);
|
||||||
length = py_list__len(_1);
|
length = py_list_len(_1);
|
||||||
} else if(py_istype(_1, tp_tuple)) {
|
} else if(py_istype(_1, tp_tuple)) {
|
||||||
p = py_tuple__getitem(_1, 0);
|
p = py_tuple_getitem(_1, 0);
|
||||||
length = py_tuple__len(_1);
|
length = py_tuple_len(_1);
|
||||||
} else {
|
} else {
|
||||||
return TypeError("join() argument must be a list or tuple");
|
return TypeError("join() argument must be a list or tuple");
|
||||||
}
|
}
|
||||||
@ -323,7 +323,7 @@ static bool str_split(int argc, py_Ref argv) {
|
|||||||
py_newlistn(py_retval(), res.count);
|
py_newlistn(py_retval(), res.count);
|
||||||
for(int i = 0; i < res.count; i++) {
|
for(int i = 0; i < res.count; i++) {
|
||||||
c11_sv item = c11__getitem(c11_sv, &res, i);
|
c11_sv item = c11__getitem(c11_sv, &res, i);
|
||||||
py_newstrn(py_list__getitem(py_retval(), i), item.data, item.size);
|
py_newstrn(py_list_getitem(py_retval(), i), item.data, item.size);
|
||||||
}
|
}
|
||||||
c11_vector__dtor(&res);
|
c11_vector__dtor(&res);
|
||||||
return true;
|
return true;
|
||||||
|
@ -13,17 +13,17 @@ void py_newtuple(py_Ref out, int n) {
|
|||||||
out->_obj = obj;
|
out->_obj = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_tuple__getitem(py_Ref self, int i) { return py_getslot(self, i); }
|
py_Ref py_tuple_getitem(py_Ref self, int i) { return py_getslot(self, i); }
|
||||||
|
|
||||||
py_Ref py_tuple__data(py_Ref self) { return PyObject__slots(self->_obj); }
|
py_Ref py_tuple_data(py_Ref self) { return PyObject__slots(self->_obj); }
|
||||||
|
|
||||||
void py_tuple__setitem(py_Ref self, int i, py_Ref val) { py_setslot(self, i, val); }
|
void py_tuple_setitem(py_Ref self, int i, py_Ref val) { py_setslot(self, i, val); }
|
||||||
|
|
||||||
int py_tuple__len(py_Ref self) { return self->_obj->slots; }
|
int py_tuple_len(py_Ref self) { return self->_obj->slots; }
|
||||||
|
|
||||||
//////////////
|
//////////////
|
||||||
static bool tuple__len__(int argc, py_Ref argv) {
|
static bool tuple__len__(int argc, py_Ref argv) {
|
||||||
py_newint(py_retval(), py_tuple__len(argv));
|
py_newint(py_retval(), py_tuple_len(argv));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ static bool tuple__repr__(int argc, py_Ref argv) {
|
|||||||
c11_sbuf buf;
|
c11_sbuf buf;
|
||||||
c11_sbuf__ctor(&buf);
|
c11_sbuf__ctor(&buf);
|
||||||
c11_sbuf__write_char(&buf, '(');
|
c11_sbuf__write_char(&buf, '(');
|
||||||
int length = py_tuple__len(argv);
|
int length = py_tuple_len(argv);
|
||||||
for(int i = 0; i < length; i++) {
|
for(int i = 0; i < length; i++) {
|
||||||
py_TValue* val = py_getslot(argv, i);
|
py_TValue* val = py_getslot(argv, i);
|
||||||
bool ok = py_repr(val);
|
bool ok = py_repr(val);
|
||||||
@ -58,10 +58,10 @@ static bool tuple__new__(int argc, py_Ref argv) {
|
|||||||
if(!ok) return false;
|
if(!ok) return false;
|
||||||
py_Ref tmp = py_pushtmp();
|
py_Ref tmp = py_pushtmp();
|
||||||
*tmp = *py_retval(); // backup the list
|
*tmp = *py_retval(); // backup the list
|
||||||
int length = py_list__len(tmp);
|
int length = py_list_len(tmp);
|
||||||
py_newtuple(py_retval(), length);
|
py_newtuple(py_retval(), length);
|
||||||
for(int i = 0; i < py_tuple__len(py_retval()); i++) {
|
for(int i = 0; i < py_tuple_len(py_retval()); i++) {
|
||||||
py_tuple__setitem(py_retval(), i, py_list__getitem(tmp, i));
|
py_tuple_setitem(py_retval(), i, py_list_getitem(tmp, i));
|
||||||
}
|
}
|
||||||
py_pop();
|
py_pop();
|
||||||
return true;
|
return true;
|
||||||
@ -71,7 +71,7 @@ static bool tuple__new__(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
static bool tuple__getitem__(int argc, py_Ref argv) {
|
static bool tuple__getitem__(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(2);
|
PY_CHECK_ARGC(2);
|
||||||
int length = py_tuple__len(argv);
|
int length = py_tuple_len(argv);
|
||||||
py_Ref _1 = py_arg(1);
|
py_Ref _1 = py_arg(1);
|
||||||
if(_1->type == tp_int) {
|
if(_1->type == tp_int) {
|
||||||
int index = py_toint(py_arg(1));
|
int index = py_toint(py_arg(1));
|
||||||
@ -84,11 +84,11 @@ static bool tuple__getitem__(int argc, py_Ref argv) {
|
|||||||
if(!ok) return false;
|
if(!ok) return false;
|
||||||
py_Ref tmp = py_pushtmp();
|
py_Ref tmp = py_pushtmp();
|
||||||
py_newlist(tmp);
|
py_newlist(tmp);
|
||||||
PK_SLICE_LOOP(i, start, stop, step) py_list__append(tmp, py_getslot(argv, i));
|
PK_SLICE_LOOP(i, start, stop, step) py_list_append(tmp, py_getslot(argv, i));
|
||||||
// convert list to tuple
|
// convert list to tuple
|
||||||
py_newtuple(py_retval(), py_list__len(tmp));
|
py_newtuple(py_retval(), py_list_len(tmp));
|
||||||
for(int i = 0; i < py_tuple__len(py_retval()); i++) {
|
for(int i = 0; i < py_tuple_len(py_retval()); i++) {
|
||||||
py_tuple__setitem(py_retval(), i, py_list__getitem(tmp, i));
|
py_tuple_setitem(py_retval(), i, py_list_getitem(tmp, i));
|
||||||
}
|
}
|
||||||
py_pop();
|
py_pop();
|
||||||
return true;
|
return true;
|
||||||
@ -133,8 +133,8 @@ static bool tuple__contains__(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
static bool tuple__hash__(int argc, py_Ref argv) {
|
static bool tuple__hash__(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
int length = py_tuple__len(argv);
|
int length = py_tuple_len(argv);
|
||||||
py_TValue* data = py_tuple__data(argv);
|
py_TValue* data = py_tuple_data(argv);
|
||||||
uint64_t x = 1000003;
|
uint64_t x = 1000003;
|
||||||
for(int i = 0; i < length; i++) {
|
for(int i = 0; i < length; i++) {
|
||||||
py_i64 y;
|
py_i64 y;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user