195 lines
5.7 KiB
C
Raw Normal View History

2022-06-23 17:46:50 +08:00
#include "PikaStdData_Dict.h"
2022-01-06 10:56:14 +08:00
#include "BaseObj.h"
2022-01-06 11:13:43 +08:00
#include "PikaObj.h"
2022-06-23 17:46:50 +08:00
#include "PikaStdData_dict_keys.h"
2022-01-06 11:13:43 +08:00
#include "PikaStdLib_SysObj.h"
#include "dataStrs.h"
2021-12-13 23:28:58 +08:00
Arg* PikaStdData_Dict_get(PikaObj* self, char* key) {
PikaDict* dict = obj_getPtr(self, "dict");
Arg* res = dict_getArg(dict, key);
if (NULL == res) {
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
__platform_printf("KeyError: %s\n", key);
}
return arg_copy(res);
2021-12-13 23:28:58 +08:00
}
2022-01-06 10:56:14 +08:00
void PikaStdData_Dict___init__(PikaObj* self) {
2022-07-11 23:17:25 +08:00
__vm_Dict___init__(self);
2022-01-06 10:56:14 +08:00
}
2021-12-13 23:28:58 +08:00
void PikaStdData_Dict_set(PikaObj* self, char* key, Arg* arg) {
2022-07-11 23:17:25 +08:00
__vm_Dict_set(self, arg, key);
2021-12-13 23:28:58 +08:00
}
void PikaStdData_Dict_remove(PikaObj* self, char* key) {
PikaDict* dict = obj_getPtr(self, "dict");
2022-06-23 17:46:50 +08:00
PikaDict* keys = obj_getPtr(self, "_keys");
dict_removeArg(dict, dict_getArg(dict, key));
2022-06-23 17:46:50 +08:00
dict_removeArg(keys, dict_getArg(keys, key));
2021-12-24 22:44:56 +08:00
}
2022-01-06 11:13:43 +08:00
Arg* PikaStdData_Dict___iter__(PikaObj* self) {
2022-01-06 10:56:14 +08:00
obj_setInt(self, "__iter_i", 0);
2022-07-20 10:32:01 +08:00
return arg_newRef(self);
2021-12-24 22:44:56 +08:00
}
2022-01-06 11:13:43 +08:00
Arg* PikaStdData_Dict___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
2022-08-02 13:52:20 +08:00
PikaDict* keys = obj_getPtr(self, "_keys");
Arg* res = arg_copy(args_getArgByidex(&keys->super, __iter_i));
2022-01-06 11:13:43 +08:00
if (NULL == res) {
2022-07-20 10:32:01 +08:00
return arg_newNull();
2022-01-06 11:13:43 +08:00
}
args_setInt(self->list, "__iter_i", __iter_i + 1);
return res;
}
void PikaStdData_Dict___setitem__(PikaObj* self, Arg* __key, Arg* __val) {
PikaStdData_Dict_set(self, obj_getStr(self, "__key"),
obj_getArg(self, "__val"));
}
Arg* PikaStdData_Dict___getitem__(PikaObj* self, Arg* __key) {
return PikaStdData_Dict_get(self, obj_getStr(self, "__key"));
}
void PikaStdData_Dict___del__(PikaObj* self) {
PikaDict* dict = obj_getPtr(self, "dict");
2022-06-23 17:46:50 +08:00
PikaDict* keys = obj_getPtr(self, "_keys");
dict_deinit(dict);
if (NULL != keys) {
dict_deinit(keys);
}
2022-06-23 17:46:50 +08:00
}
void PikaStdData_dict_keys___init__(PikaObj* self, PikaObj* dict) {
obj_setPtr(self, "dictptr", dict);
}
PikaObj* PikaStdData_Dict_keys(PikaObj* self) {
PikaObj* dict_keys = newNormalObj(New_PikaStdData_dict_keys);
PikaStdData_dict_keys___init__(dict_keys, self);
return dict_keys;
}
Arg* PikaStdData_dict_keys___iter__(PikaObj* self) {
obj_setInt(self, "__iter_i", 0);
2022-07-20 10:32:01 +08:00
return arg_newRef(self);
2022-06-23 17:46:50 +08:00
}
Arg* PikaStdData_dict_keys___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
Arg* res = arg_copy(args_getArgByidex(&keys->super, __iter_i));
if (NULL == res) {
2022-07-20 10:32:01 +08:00
return arg_newNull();
2022-06-23 17:46:50 +08:00
}
args_setInt(self->list, "__iter_i", __iter_i + 1);
return res;
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg);
char* PikaStdData_dict_keys___str__(PikaObj* self) {
2022-07-20 10:32:01 +08:00
Arg* str_arg = arg_newStr("dict_keys([");
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
int i = 0;
while (PIKA_TRUE) {
Arg* item = args_getArgByidex(&keys->super, i);
if (NULL == item) {
break;
}
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
if (arg_getType(item) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}
str_arg = arg_strAppend(str_arg, item_str);
if (arg_getType(item) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}
i++;
}
str_arg = arg_strAppend(str_arg, "])");
obj_setStr(self, "_buf", arg_getStr(str_arg));
arg_deinit(str_arg);
return obj_getStr(self, "_buf");
}
char* PikaStdData_Dict___str__(PikaObj* self) {
2022-07-20 10:32:01 +08:00
Arg* str_arg = arg_newStr("{");
PikaDict* keys = obj_getPtr(self, "_keys");
PikaDict* dict = obj_getPtr(self, "dict");
int i = 0;
while (PIKA_TRUE) {
Arg* item_key = args_getArgByidex(&keys->super, i);
Arg* item_val = args_getArgByidex(&dict->super, i);
if (NULL == item_key) {
break;
}
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* key_str = PikaStdLib_SysObj_str(self, item_key);
str_arg = arg_strAppend(str_arg, "'");
str_arg = arg_strAppend(str_arg, key_str);
str_arg = arg_strAppend(str_arg, "'");
str_arg = arg_strAppend(str_arg, ": ");
char* val_str = PikaStdLib_SysObj_str(self, item_val);
if (arg_getType(item_val) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}
str_arg = arg_strAppend(str_arg, val_str);
if (arg_getType(item_val) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}
i++;
}
str_arg = arg_strAppend(str_arg, "}");
obj_setStr(self, "_buf", arg_getStr(str_arg));
arg_deinit(str_arg);
return obj_getStr(self, "_buf");
}
int PikaStdData_Dict___len__(PikaObj* self) {
PikaDict* dict = obj_getPtr(self, "dict");
return args_getSize(&dict->super);
}
int PikaStdData_dict_keys___len__(PikaObj* self) {
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
return args_getSize(&keys->super);
}
2022-09-05 15:39:15 +08:00
int dict_contains(PikaDict* dict, Arg* key) {
int i = 0;
while (PIKA_TRUE) {
Arg* item = args_getArgByidex(&dict->super, i);
if (NULL == item) {
break;
}
if (arg_isEqual(item, key)) {
return PIKA_TRUE;
}
i++;
}
return PIKA_FALSE;
}
int PikaStdData_Dict___contains__(PikaObj *self, Arg* val){
PikaDict* dict = obj_getPtr(self, "_keys");
return dict_contains(dict, val);
}