pikapython/package/PikaStdLib/PikaStdData_List.c

109 lines
3.1 KiB
C
Raw Normal View History

2021-12-28 01:21:47 +08:00
#include "BaseObj.h"
2021-12-13 22:14:59 +08:00
#include "PikaObj.h"
#include "dataStrs.h"
2021-12-13 21:32:45 +08:00
void PikaStdData_List_append(PikaObj* self, Arg* arg) {
PikaList* list = obj_getPtr(self, "list");
list_append(list, arg);
2021-12-13 21:32:45 +08:00
}
int PikaStdData_List_len(PikaObj* self) {
PikaList* list = obj_getPtr(self, "list");
return list_getSize(list);
2021-12-13 21:32:45 +08:00
}
Arg* PikaStdData_List_get(PikaObj* self, int i) {
PikaList* list = obj_getPtr(self, "list");
return arg_copy(list_getArg(list, i));
2021-12-13 21:32:45 +08:00
}
2021-12-13 21:32:45 +08:00
void PikaStdData_List___init__(PikaObj* self) {
2022-06-16 01:18:11 +08:00
if (!obj_isArgExist(self, "list")) {
PikaList* list = New_list();
obj_setPtr(self, "list", list);
}
2021-12-13 21:32:45 +08:00
}
void PikaStdData_List_set(PikaObj* self, Arg* arg, int i) {
PikaList* list = obj_getPtr(self, "list");
if (PIKA_RES_OK != list_setArg(list, i, arg)) {
2021-12-13 21:32:45 +08:00
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error]: index exceeded lengh of list.");
}
}
2021-12-28 01:21:47 +08:00
Arg* PikaStdData_List___iter__(PikaObj* self) {
obj_setInt(self, "__iter_i", 0);
2022-04-28 00:49:40 +08:00
return arg_setRef(NULL, "", self);
2021-12-28 01:21:47 +08:00
}
Arg* PikaStdData_List___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
Arg* res = PikaStdData_List_get(self, __iter_i);
2022-01-13 21:57:32 +08:00
if (NULL == res) {
2021-12-28 01:21:47 +08:00
return arg_setNull(NULL);
}
args_setInt(self->list, "__iter_i", __iter_i + 1);
return res;
2022-01-13 21:57:32 +08:00
}
Arg* PikaStdData_List___get__(PikaObj* self) {
return PikaStdData_List_get(self, obj_getInt(self, "__key"));
}
2022-01-13 21:57:32 +08:00
void PikaStdData_List___set__(PikaObj* self) {
PikaStdData_List_set(self, obj_getArg(self, "__val"),
obj_getInt(self, "__key"));
2022-01-30 17:37:21 +08:00
}
2022-03-25 21:18:54 +08:00
void PikaStdData_ByteArray_fromString(PikaObj* self, char* s) {
for (uint32_t i = 0; i < strGetSize(s); i++) {
obj_setInt(self, "__val", (int)s[i]);
PIKA_PYTHON_BEGIN
2022-05-25 14:05:18 +08:00
/* clang-format off */
PIKA_PYTHON(
append(__val)
)
/* clang-format on */
2022-03-25 21:18:54 +08:00
const uint8_t bytes[] = {
0x08, 0x00, /* instruct array size */
0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x07, 0x00, /* instruct array */
0x0e, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x76, 0x61, 0x6c, 0x00, 0x61,
0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, /* const pool */
};
pikaVM_runByteCode(self, (uint8_t*)bytes);
PIKA_PYTHON_END
}
2022-03-25 21:32:01 +08:00
}
void PikaStdData_List___del__(PikaObj* self) {
Args* list = obj_getPtr(self, "list");
args_deinit(list);
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg);
char* PikaStdData_List___str__(PikaObj* self) {
Arg* str_arg = arg_setStr(NULL, "", "[");
PikaList* list = obj_getPtr(self, "list");
int i = 0;
while (PIKA_TRUE) {
Arg* item = list_getArg(list, i);
if (NULL == item) {
break;
}
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
str_arg = arg_strAppend(str_arg, item_str);
i++;
}
str_arg = arg_strAppend(str_arg, "]");
obj_setStr(self, "_buf", arg_getStr(str_arg));
arg_deinit(str_arg);
return obj_getStr(self, "_buf");
}