!253 merge pika<Struct> and obj<Struct> method.

Merge pull request !253 from 李昂/PikaList
This commit is contained in:
李昂 2023-08-04 17:40:10 +00:00 committed by Gitee
commit 46ec9cdd32
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
27 changed files with 466 additions and 621 deletions

View File

@ -8,6 +8,12 @@
#include "dataStrs.h"
Arg* PikaStdData_Dict_get(PikaObj* self, char* key) {
Arg* aRet = pikaDict_get(self, key);
if (NULL == aRet) {
obj_setErrorCode(self, PIKA_RES_ERR_ARG_NO_FOUND);
pika_platform_printf("KeyError: %s\n", key);
return NULL;
}
return arg_copy(objDict_get(self, key));
}
@ -16,14 +22,14 @@ void PikaStdData_Dict___init__(PikaObj* self) {
}
void PikaStdData_Dict_set(PikaObj* self, char* key, Arg* arg) {
objDict_set(self, key, arg);
objDict_set(self, key, arg_copy(arg));
}
void PikaStdData_Dict_remove(PikaObj* self, char* key) {
PikaDict* dict = obj_getPtr(self, "dict");
PikaDict* keys = obj_getPtr(self, "_keys");
pikaDict_removeArg(dict, pikaDict_getArg(dict, key));
pikaDict_removeArg(keys, pikaDict_getArg(keys, key));
Args* dict = obj_getPtr(self, "dict");
Args* keys = obj_getPtr(self, "_keys");
args_removeArg(dict, args_getArg(dict, key));
args_removeArg(keys, args_getArg(keys, key));
}
Arg* PikaStdData_Dict___iter__(PikaObj* self) {
@ -33,8 +39,8 @@ Arg* PikaStdData_Dict___iter__(PikaObj* self) {
Arg* PikaStdData_Dict___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
PikaDict* keys = obj_getPtr(self, "_keys");
Arg* res = arg_copy(args_getArgByIndex(&keys->super, __iter_i));
Args* keys = _OBJ2KEYS(self);
Arg* res = arg_copy(args_getArgByIndex(keys, __iter_i));
if (NULL == res) {
return arg_newNone();
}
@ -43,8 +49,7 @@ Arg* PikaStdData_Dict___next__(PikaObj* self) {
}
void PikaStdData_Dict___setitem__(PikaObj* self, Arg* __key, Arg* __val) {
PikaStdData_Dict_set(self, obj_getStr(self, "__key"),
obj_getArg(self, "__val"));
PikaStdData_Dict_set(self, arg_getStr(__key), (__val));
}
Arg* PikaStdData_Dict___getitem__(PikaObj* self, Arg* __key) {
@ -52,11 +57,13 @@ Arg* PikaStdData_Dict___getitem__(PikaObj* self, Arg* __key) {
}
void PikaStdData_Dict___del__(PikaObj* self) {
PikaDict* dict = obj_getPtr(self, "dict");
PikaDict* keys = obj_getPtr(self, "_keys");
pikaDict_deinit(dict);
Args* keys = obj_getPtr(self, "_keys");
Args* dict = obj_getPtr(self, "dict");
if (NULL != dict) {
args_deinit(dict);
}
if (NULL != keys) {
pikaDict_deinit(keys);
args_deinit(keys);
}
}
@ -84,8 +91,8 @@ Arg* PikaStdData_dict_keys___iter__(PikaObj* self) {
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_getArgByIndex(&keys->super, __iter_i));
Args* keys = _OBJ2KEYS(dictptr);
Arg* res = arg_copy(args_getArgByIndex(keys, __iter_i));
if (NULL == res) {
return arg_newNone();
}
@ -97,11 +104,11 @@ char* builtins_str(PikaObj* self, Arg* arg);
char* PikaStdData_dict_keys___str__(PikaObj* self) {
Arg* str_arg = arg_newStr("dict_keys([");
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
Args* keys = _OBJ2KEYS(dictptr);
int i = 0;
while (PIKA_TRUE) {
Arg* item = args_getArgByIndex(&keys->super, i);
Arg* item = args_getArgByIndex(keys, i);
if (NULL == item) {
break;
}
@ -171,20 +178,20 @@ char* PikaStdData_Dict___str__(PikaObj* self) {
}
int PikaStdData_Dict___len__(PikaObj* self) {
PikaDict* dict = obj_getPtr(self, "dict");
return args_getSize(&dict->super);
Args* dict = _OBJ2DICT(self);
return args_getSize(dict);
}
int PikaStdData_dict_keys___len__(PikaObj* self) {
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
return args_getSize(&keys->super);
Args* keys = _OBJ2KEYS(dictptr);
return args_getSize(keys);
}
int dict_contains(PikaDict* dict, Arg* key) {
int i = 0;
while (PIKA_TRUE) {
Arg* item = args_getArgByIndex(&dict->super, i);
Arg* item = args_getArgByIndex(_OBJ2KEYS(dict), i);
if (NULL == item) {
break;
}
@ -197,8 +204,7 @@ int dict_contains(PikaDict* dict, Arg* key) {
}
int PikaStdData_Dict___contains__(PikaObj* self, Arg* val) {
PikaDict* dict = obj_getPtr(self, "_keys");
return dict_contains(dict, val);
return dict_contains(self, val);
}
Arg* PikaStdData_dict_items___iter__(PikaObj* self) {
@ -208,25 +214,23 @@ Arg* PikaStdData_dict_items___iter__(PikaObj* self) {
int PikaStdData_dict_items___len__(PikaObj* self) {
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
return args_getSize(&keys->super);
Args* keys = _OBJ2KEYS(dictptr);
return args_getSize(keys);
}
Arg* PikaStdData_dict_items___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
PikaDict* dict = obj_getPtr(dictptr, "dict");
Arg* key = args_getArgByIndex(&keys->super, __iter_i);
Arg* val = args_getArgByIndex(&dict->super, __iter_i);
Args* keys = _OBJ2KEYS(dictptr);
Args* dict = _OBJ2DICT(dictptr);
Arg* key = args_getArgByIndex(keys, __iter_i);
Arg* val = args_getArgByIndex(dict, __iter_i);
if (NULL == key) {
return arg_newNone();
}
PikaObj* tuple = newNormalObj(New_PikaStdData_Tuple);
PikaStdData_Tuple___init__(tuple);
PikaList* list = obj_getPtr(tuple, "list");
pikaList_append(list, key);
pikaList_append(list, val);
PikaObj* tuple = New_pikaTuple();
pikaList_append(tuple, arg_copy(key));
pikaList_append(tuple, arg_copy(val));
args_setInt(self->list, "__iter_i", __iter_i + 1);
return arg_newObj(tuple);
}

View File

@ -217,14 +217,8 @@ void PikaStdData_FILEIO_writelines(PikaObj* self, PikaObj* lines) {
__platform_printf("Error: can't write lines to file\n");
return;
}
PikaList* list = obj_getPtr(lines, "list");
if (list == NULL) {
obj_setErrorCode(self, PIKA_RES_ERR_IO);
__platform_printf("Error: can't write lines to file\n");
return;
}
for (size_t i = 0; i < pikaList_getSize(list); i++) {
char* line = pikaList_getStr(list, i);
for (size_t i = 0; i < pikaList_getSize(lines); i++) {
char* line = pikaList_getStr(lines, i);
Arg* arg_str = arg_newStr(line);
PikaStdData_FILEIO_write(self, arg_str);
arg_deinit(arg_str);

View File

@ -5,11 +5,11 @@
#include "dataStrs.h"
void PikaStdData_List_append(PikaObj* self, Arg* arg) {
objList_append(self, arg);
objList_append(self, arg_copy(arg));
}
void PikaStdData_List_set(PikaObj* self, int i, Arg* arg) {
objList_set(self, i, arg);
objList_set(self, i, arg_copy(arg));
}
void PikaStdData_List___setitem__(PikaObj* self, Arg* __key, Arg* __val) {
@ -64,30 +64,24 @@ char* PikaStdData_List___str__(PikaObj* self) {
}
void PikaStdData_List_reverse(PikaObj* self) {
PikaList* list = obj_getPtr(self, "list");
pikaList_reverse(list);
pikaList_reverse(self);
}
PikaObj* PikaStdData_List___add__(PikaObj* self, PikaObj* others) {
PikaObj* res = newNormalObj(New_PikaStdData_List);
PikaStdData_List___init__(res);
PikaList* list_res = obj_getPtr(res, "list");
PikaList* list1 = obj_getPtr(self, "list");
PikaList* list2 = obj_getPtr(others, "list");
for (size_t i = 0; i < pikaList_getSize(list1); i++) {
Arg* arg = pikaList_getArg(list1, i);
pikaList_append(list_res, arg);
PikaObj* res = New_pikaList();
for (size_t i = 0; i < pikaList_getSize(self); i++) {
Arg* arg = pikaList_get(self, i);
pikaList_append(res, arg_copy(arg));
}
for (size_t i = 0; i < pikaList_getSize(list2); i++) {
Arg* arg = pikaList_getArg(list2, i);
pikaList_append(list_res, arg);
for (size_t i = 0; i < pikaList_getSize(others); i++) {
Arg* arg = pikaList_get(others, i);
pikaList_append(res, arg_copy(arg));
}
return res;
}
void PikaStdData_List_insert(PikaObj* self, int i, Arg* arg) {
PikaList* list = obj_getPtr(self, "list");
if (PIKA_RES_OK != pikaList_insert(list, i, arg)) {
if (PIKA_RES_OK != pikaList_insert(self, i, arg)) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "Error: index exceeded lengh of list.");
}
@ -95,7 +89,6 @@ void PikaStdData_List_insert(PikaObj* self, int i, Arg* arg) {
Arg* PikaStdData_List_pop(PikaObj* self, PikaTuple* index) {
int i = 0;
PikaList* list = obj_getPtr(self, "list");
if (pikaTuple_getSize(index) == 1) {
if (pikaTuple_getType(index, 0) == ARG_TYPE_INT) {
i = pikaTuple_getInt(index, 0);
@ -104,13 +97,12 @@ Arg* PikaStdData_List_pop(PikaObj* self, PikaTuple* index) {
obj_setSysOut(self, "Error: index must be int.");
return NULL;
}
return pikaList_pop_withIndex(list, i);
return pikaList_pop_withIndex(self, i);
} else {
return pikaList_pop(list);
return pikaList_pop(self);
}
}
void PikaStdData_List_remove(PikaObj* self, Arg* val) {
PikaList* list = obj_getPtr(self, "list");
pikaList_remove(list, val);
pikaList_remove(self, val);
}

View File

@ -31,8 +31,7 @@ Arg* PikaStdData_Tuple___next__(PikaObj* self) {
Arg* PikaStdData_Tuple___getitem__(PikaObj* self, Arg* __key) {
int i = arg_getInt(__key);
PikaList* list = obj_getPtr(self, "list");
if (i < 0 || i >= pikaList_getSize(list)) {
if (i < 0 || i >= pikaList_getSize(self)) {
obj_setErrorCode(self, PIKA_RES_ERR_INDEX);
obj_setSysOut(self, "IndexError: index out of range");
return NULL;
@ -44,7 +43,7 @@ void PikaStdData_Tuple___del__(PikaObj* self) {
if (0 == obj_getInt(self, "needfree")) {
return;
}
Args* list = obj_getPtr(self, "list");
Args* list = _OBJ2LIST(self);
args_deinit(list);
}
@ -99,9 +98,8 @@ int PikaStdData_Tuple___len__(PikaObj* self) {
}
int PikaStdData_Tuple___contains__(PikaObj* self, Arg* val) {
PikaList* list = obj_getPtr(self, "list");
for (size_t i = 0; i < pikaList_getSize(list); i++) {
Arg* arg = pikaList_getArg(list, i);
for (size_t i = 0; i < pikaList_getSize(self); i++) {
Arg* arg = pikaList_get(self, i);
if (arg_isEqual(arg, val)) {
return 1;
}

View File

@ -11,22 +11,8 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=builtin.write_fn"
// "--gtest_filter=builtin.base_type"
// "--gtest_filter=parser.multi_comprehension"
// "--gtest_filter=parser.comprehension_indent"
// "--gtest_filter=parser.*"
// "--gtest_filter=pikaMain.slice2"
// "--gtest_filter=re.match"
// "--gtest_filter=parser.for_indent"
// "--gtest_filter=VM.comprehension"
// "--gtest_filter=zlib.zlib1"
// "--gtest_filter=builtin.write_issue"
// "--gtest_filter=VM.list_base"
// "--gtest_filter=vm.issue_dict_update"
// "--gtest_filter=builtin.tuple"
// "--gtest_filter=pikaMain.num_issue1"
"--gtest_filter=builtin.write_issue"
// "--gtest_filter=VM.dict_base"
"--gtest_filter=dict.in"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -8,6 +8,12 @@
#include "dataStrs.h"
Arg* PikaStdData_Dict_get(PikaObj* self, char* key) {
Arg* aRet = pikaDict_get(self, key);
if (NULL == aRet) {
obj_setErrorCode(self, PIKA_RES_ERR_ARG_NO_FOUND);
pika_platform_printf("KeyError: %s\n", key);
return NULL;
}
return arg_copy(objDict_get(self, key));
}
@ -16,14 +22,14 @@ void PikaStdData_Dict___init__(PikaObj* self) {
}
void PikaStdData_Dict_set(PikaObj* self, char* key, Arg* arg) {
objDict_set(self, key, arg);
objDict_set(self, key, arg_copy(arg));
}
void PikaStdData_Dict_remove(PikaObj* self, char* key) {
PikaDict* dict = obj_getPtr(self, "dict");
PikaDict* keys = obj_getPtr(self, "_keys");
pikaDict_removeArg(dict, pikaDict_getArg(dict, key));
pikaDict_removeArg(keys, pikaDict_getArg(keys, key));
Args* dict = obj_getPtr(self, "dict");
Args* keys = obj_getPtr(self, "_keys");
args_removeArg(dict, args_getArg(dict, key));
args_removeArg(keys, args_getArg(keys, key));
}
Arg* PikaStdData_Dict___iter__(PikaObj* self) {
@ -33,8 +39,8 @@ Arg* PikaStdData_Dict___iter__(PikaObj* self) {
Arg* PikaStdData_Dict___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
PikaDict* keys = obj_getPtr(self, "_keys");
Arg* res = arg_copy(args_getArgByIndex(&keys->super, __iter_i));
Args* keys = _OBJ2KEYS(self);
Arg* res = arg_copy(args_getArgByIndex(keys, __iter_i));
if (NULL == res) {
return arg_newNone();
}
@ -43,8 +49,7 @@ Arg* PikaStdData_Dict___next__(PikaObj* self) {
}
void PikaStdData_Dict___setitem__(PikaObj* self, Arg* __key, Arg* __val) {
PikaStdData_Dict_set(self, obj_getStr(self, "__key"),
obj_getArg(self, "__val"));
PikaStdData_Dict_set(self, arg_getStr(__key), (__val));
}
Arg* PikaStdData_Dict___getitem__(PikaObj* self, Arg* __key) {
@ -52,11 +57,13 @@ Arg* PikaStdData_Dict___getitem__(PikaObj* self, Arg* __key) {
}
void PikaStdData_Dict___del__(PikaObj* self) {
PikaDict* dict = obj_getPtr(self, "dict");
PikaDict* keys = obj_getPtr(self, "_keys");
pikaDict_deinit(dict);
Args* keys = obj_getPtr(self, "_keys");
Args* dict = obj_getPtr(self, "dict");
if (NULL != dict) {
args_deinit(dict);
}
if (NULL != keys) {
pikaDict_deinit(keys);
args_deinit(keys);
}
}
@ -84,8 +91,8 @@ Arg* PikaStdData_dict_keys___iter__(PikaObj* self) {
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_getArgByIndex(&keys->super, __iter_i));
Args* keys = _OBJ2KEYS(dictptr);
Arg* res = arg_copy(args_getArgByIndex(keys, __iter_i));
if (NULL == res) {
return arg_newNone();
}
@ -97,11 +104,11 @@ char* builtins_str(PikaObj* self, Arg* arg);
char* PikaStdData_dict_keys___str__(PikaObj* self) {
Arg* str_arg = arg_newStr("dict_keys([");
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
Args* keys = _OBJ2KEYS(dictptr);
int i = 0;
while (PIKA_TRUE) {
Arg* item = args_getArgByIndex(&keys->super, i);
Arg* item = args_getArgByIndex(keys, i);
if (NULL == item) {
break;
}
@ -171,20 +178,20 @@ char* PikaStdData_Dict___str__(PikaObj* self) {
}
int PikaStdData_Dict___len__(PikaObj* self) {
PikaDict* dict = obj_getPtr(self, "dict");
return args_getSize(&dict->super);
Args* dict = _OBJ2DICT(self);
return args_getSize(dict);
}
int PikaStdData_dict_keys___len__(PikaObj* self) {
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
return args_getSize(&keys->super);
Args* keys = _OBJ2KEYS(dictptr);
return args_getSize(keys);
}
int dict_contains(PikaDict* dict, Arg* key) {
int i = 0;
while (PIKA_TRUE) {
Arg* item = args_getArgByIndex(&dict->super, i);
Arg* item = args_getArgByIndex(_OBJ2KEYS(dict), i);
if (NULL == item) {
break;
}
@ -197,8 +204,7 @@ int dict_contains(PikaDict* dict, Arg* key) {
}
int PikaStdData_Dict___contains__(PikaObj* self, Arg* val) {
PikaDict* dict = obj_getPtr(self, "_keys");
return dict_contains(dict, val);
return dict_contains(self, val);
}
Arg* PikaStdData_dict_items___iter__(PikaObj* self) {
@ -208,25 +214,23 @@ Arg* PikaStdData_dict_items___iter__(PikaObj* self) {
int PikaStdData_dict_items___len__(PikaObj* self) {
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
return args_getSize(&keys->super);
Args* keys = _OBJ2KEYS(dictptr);
return args_getSize(keys);
}
Arg* PikaStdData_dict_items___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
PikaObj* dictptr = obj_getPtr(self, "dictptr");
PikaDict* keys = obj_getPtr(dictptr, "_keys");
PikaDict* dict = obj_getPtr(dictptr, "dict");
Arg* key = args_getArgByIndex(&keys->super, __iter_i);
Arg* val = args_getArgByIndex(&dict->super, __iter_i);
Args* keys = _OBJ2KEYS(dictptr);
Args* dict = _OBJ2DICT(dictptr);
Arg* key = args_getArgByIndex(keys, __iter_i);
Arg* val = args_getArgByIndex(dict, __iter_i);
if (NULL == key) {
return arg_newNone();
}
PikaObj* tuple = newNormalObj(New_PikaStdData_Tuple);
PikaStdData_Tuple___init__(tuple);
PikaList* list = obj_getPtr(tuple, "list");
pikaList_append(list, key);
pikaList_append(list, val);
PikaObj* tuple = New_pikaTuple();
pikaList_append(tuple, arg_copy(key));
pikaList_append(tuple, arg_copy(val));
args_setInt(self->list, "__iter_i", __iter_i + 1);
return arg_newObj(tuple);
}

View File

@ -217,14 +217,8 @@ void PikaStdData_FILEIO_writelines(PikaObj* self, PikaObj* lines) {
__platform_printf("Error: can't write lines to file\n");
return;
}
PikaList* list = obj_getPtr(lines, "list");
if (list == NULL) {
obj_setErrorCode(self, PIKA_RES_ERR_IO);
__platform_printf("Error: can't write lines to file\n");
return;
}
for (size_t i = 0; i < pikaList_getSize(list); i++) {
char* line = pikaList_getStr(list, i);
for (size_t i = 0; i < pikaList_getSize(lines); i++) {
char* line = pikaList_getStr(lines, i);
Arg* arg_str = arg_newStr(line);
PikaStdData_FILEIO_write(self, arg_str);
arg_deinit(arg_str);

View File

@ -5,11 +5,11 @@
#include "dataStrs.h"
void PikaStdData_List_append(PikaObj* self, Arg* arg) {
objList_append(self, arg);
objList_append(self, arg_copy(arg));
}
void PikaStdData_List_set(PikaObj* self, int i, Arg* arg) {
objList_set(self, i, arg);
objList_set(self, i, arg_copy(arg));
}
void PikaStdData_List___setitem__(PikaObj* self, Arg* __key, Arg* __val) {
@ -64,30 +64,24 @@ char* PikaStdData_List___str__(PikaObj* self) {
}
void PikaStdData_List_reverse(PikaObj* self) {
PikaList* list = obj_getPtr(self, "list");
pikaList_reverse(list);
pikaList_reverse(self);
}
PikaObj* PikaStdData_List___add__(PikaObj* self, PikaObj* others) {
PikaObj* res = newNormalObj(New_PikaStdData_List);
PikaStdData_List___init__(res);
PikaList* list_res = obj_getPtr(res, "list");
PikaList* list1 = obj_getPtr(self, "list");
PikaList* list2 = obj_getPtr(others, "list");
for (size_t i = 0; i < pikaList_getSize(list1); i++) {
Arg* arg = pikaList_getArg(list1, i);
pikaList_append(list_res, arg);
PikaObj* res = New_pikaList();
for (size_t i = 0; i < pikaList_getSize(self); i++) {
Arg* arg = pikaList_get(self, i);
pikaList_append(res, arg_copy(arg));
}
for (size_t i = 0; i < pikaList_getSize(list2); i++) {
Arg* arg = pikaList_getArg(list2, i);
pikaList_append(list_res, arg);
for (size_t i = 0; i < pikaList_getSize(others); i++) {
Arg* arg = pikaList_get(others, i);
pikaList_append(res, arg_copy(arg));
}
return res;
}
void PikaStdData_List_insert(PikaObj* self, int i, Arg* arg) {
PikaList* list = obj_getPtr(self, "list");
if (PIKA_RES_OK != pikaList_insert(list, i, arg)) {
if (PIKA_RES_OK != pikaList_insert(self, i, arg)) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "Error: index exceeded lengh of list.");
}
@ -95,7 +89,6 @@ void PikaStdData_List_insert(PikaObj* self, int i, Arg* arg) {
Arg* PikaStdData_List_pop(PikaObj* self, PikaTuple* index) {
int i = 0;
PikaList* list = obj_getPtr(self, "list");
if (pikaTuple_getSize(index) == 1) {
if (pikaTuple_getType(index, 0) == ARG_TYPE_INT) {
i = pikaTuple_getInt(index, 0);
@ -104,13 +97,12 @@ Arg* PikaStdData_List_pop(PikaObj* self, PikaTuple* index) {
obj_setSysOut(self, "Error: index must be int.");
return NULL;
}
return pikaList_pop_withIndex(list, i);
return pikaList_pop_withIndex(self, i);
} else {
return pikaList_pop(list);
return pikaList_pop(self);
}
}
void PikaStdData_List_remove(PikaObj* self, Arg* val) {
PikaList* list = obj_getPtr(self, "list");
pikaList_remove(list, val);
pikaList_remove(self, val);
}

View File

@ -31,8 +31,7 @@ Arg* PikaStdData_Tuple___next__(PikaObj* self) {
Arg* PikaStdData_Tuple___getitem__(PikaObj* self, Arg* __key) {
int i = arg_getInt(__key);
PikaList* list = obj_getPtr(self, "list");
if (i < 0 || i >= pikaList_getSize(list)) {
if (i < 0 || i >= pikaList_getSize(self)) {
obj_setErrorCode(self, PIKA_RES_ERR_INDEX);
obj_setSysOut(self, "IndexError: index out of range");
return NULL;
@ -44,7 +43,7 @@ void PikaStdData_Tuple___del__(PikaObj* self) {
if (0 == obj_getInt(self, "needfree")) {
return;
}
Args* list = obj_getPtr(self, "list");
Args* list = _OBJ2LIST(self);
args_deinit(list);
}
@ -99,9 +98,8 @@ int PikaStdData_Tuple___len__(PikaObj* self) {
}
int PikaStdData_Tuple___contains__(PikaObj* self, Arg* val) {
PikaList* list = obj_getPtr(self, "list");
for (size_t i = 0; i < pikaList_getSize(list); i++) {
Arg* arg = pikaList_getArg(list, i);
for (size_t i = 0; i < pikaList_getSize(self); i++) {
Arg* arg = pikaList_get(self, i);
if (arg_isEqual(arg, val)) {
return 1;
}

View File

@ -91,7 +91,7 @@ Arg* json_encode_jsmn(jsmntok_t* t,
break;
}
case JSMN_OBJECT: {
PikaObj* ret = objDict_new(NULL);
PikaObj* ret = New_pikaDict();
int num_keys = t[*index].size;
for (int i = 0; i < num_keys; i++) {
(*index)++;
@ -112,7 +112,7 @@ Arg* json_encode_jsmn(jsmntok_t* t,
break;
}
case JSMN_ARRAY: {
PikaObj* ret = objList_new(NULL);
PikaObj* ret = New_pikaListFromVarArgs(NULL);
jsmntok_t* key_tok = &t[*index];
int num_elements = key_tok->size;
for (int i = 0; i < num_elements; i++) {
@ -120,7 +120,6 @@ Arg* json_encode_jsmn(jsmntok_t* t,
Arg* val_nested =
json_encode_jsmn(t, index, json_str, token_count);
objList_append(ret, val_nested);
arg_deinit(val_nested);
}
val = arg_newObj(ret);
break;
@ -162,23 +161,21 @@ Arg* json_encode_cjson(cJSON* cjson) {
return arg_newStr(cjson->valuestring);
}
case pika_cJSON_Array: {
PikaObj* ret = objList_new(NULL);
PikaObj* ret = New_pikaListFromVarArgs(NULL);
for (int i = 0; i < pika_cJSON_GetArraySize(cjson); i++) {
cJSON* item = pika_cJSON_GetArrayItem(cjson, i);
Arg* nested_arg = json_encode_cjson(item);
objList_append(ret, nested_arg);
arg_deinit(nested_arg);
}
return arg_newObj(ret);
}
case pika_cJSON_Object: {
PikaObj* ret = objDict_new(NULL);
PikaObj* ret = New_pikaDictFromVarArgs(NULL);
cJSON* child = cjson->child;
for (int i = 0; i < pika_cJSON_GetArraySize(cjson); i++) {
char* key = child->string;
Arg* nested_arg = json_encode_cjson(child);
objDict_set(ret, key, nested_arg);
arg_deinit(nested_arg);
child = child->next;
}
return arg_newObj(ret);

View File

@ -289,7 +289,7 @@ PikaObj* os_path_split(PikaObj* self, char* path) {
goto __exit;
}
tuple = objTuple_new(arg_newStr(folder), arg_newStr(file));
tuple = New_pikaTupleFromVarArgs(arg_newStr(folder), arg_newStr(file));
pika_platform_free(folder);
pika_platform_free(file);

View File

@ -47,15 +47,14 @@ Arg* _lua_val_to_arg(lua_State* L) {
PIKA_BOOL get_list = PIKA_FALSE;
PIKA_BOOL get_dict = PIKA_FALSE;
lua_pushnil(L); // push the first key
// PikaObj* dict = objDict_new0();
while (lua_next(L, -2) != 0) {
// 'key' is at index -2 and 'value' at index -1
if (!get_list && !get_dict) {
if (lua_isinteger(L, -2)) {
ret = objList_new(NULL);
ret = New_pikaList();
get_list = PIKA_TRUE;
} else {
ret = objDict_new(NULL);
ret = New_pikaDict();
get_dict = PIKA_TRUE;
}
}

View File

@ -21,24 +21,6 @@
#define raise_error \
{ obj_setErrorCode(self, -__LINE__); }
#define tu_getNew(name, obj_name) \
PikaTuple* name = New_pikaTuple(); \
Any obj_name = newNormalObj(New_PikaStdData_Tuple); \
obj_setPtr(obj_name, "list", name);
#define tu_append(tup, val, type) \
{ \
Arg* _arg = arg_new##type(val); \
pikaList_append(&(tup)->super, _arg); \
arg_deinit(_arg); \
}
#define li_append(list, val, type) \
{ \
Arg* _arg = arg_new##type(val); \
PikaStdData_List_append(list, _arg); \
arg_deinit(_arg); \
}
typedef PikaObj* Any;
void re_Match___init__args(PikaObj* self, char* sub, int* vec, int ven);
@ -379,7 +361,7 @@ PikaObj* re_Match_groups(PikaObj* self) {
int ven = obj_getInt(self, "_ven");
if (!ven)
return NULL;
tu_getNew(tup, tup_obj);
PikaTuple* tup_obj = New_pikaTuple();
for (int i = 1; i < ven; i++) {
Arg* str_arg1;
@ -395,11 +377,11 @@ PikaObj* re_Match_groups(PikaObj* self) {
} else {
str_arg1 = arg_newStr("");
}
pikaList_append(&(tup)->super, str_arg1);
arg_deinit(str_arg1);
pikaList_append(tup_obj, str_arg1);
}
return tup_obj;
}
PikaObj* re_Match_span(PikaObj* self, PikaTuple* val) {
int group_n = 0;
int argn = pikaTuple_getSize(val);
@ -421,10 +403,11 @@ PikaObj* re_Match_span(PikaObj* self, PikaTuple* val) {
obj_setErrorCode(self, -__LINE__);
return NULL;
}
tu_getNew(tu, tu_obj);
tu_append(tu, vec[group_n * 2], Int);
tu_append(tu, vec[group_n * 2 + 1], Int);
return tu_obj;
// tu_getNew(tu, tu_obj);
PikaTuple* tuple = New_pikaTuple();
pikaList_append(tuple, arg_newInt(vec[group_n * 2]));
pikaList_append(tuple, arg_newInt(vec[group_n * 2 + 1]));
return tuple;
}
void re_Pattern___del__(PikaObj* self) {
@ -783,20 +766,14 @@ PikaObj* __findall(void* pattern__or__re,
&brackets, flags);
char* b = NULL;
Arg* str_arg1;
Arg* sub_arg;
if (!vcs) {
if (m_n < 0)
return NULL;
Any list = newNormalObj(New_PikaStdData_List);
PikaStdData_List___init__(list);
Any list = New_pikaList();
return list;
}
Any list = newNormalObj(New_PikaStdData_List);
PikaStdData_List___init__(list);
Any list = New_pikaList();
PikaTuple* tu;
Any sub_list = NULL;
if (brackets == 1) {
for (int i = 0; i < m_n; i++) {
int* v = vcs[i];
@ -810,9 +787,7 @@ PikaObj* __findall(void* pattern__or__re,
} else {
b = (char*)"";
}
str_arg1 = arg_newStr(b);
PikaStdData_List_append(list, str_arg1);
arg_deinit(str_arg1);
pikaList_append(list, arg_newStr(b));
if (length)
free(b);
}
@ -832,13 +807,9 @@ PikaObj* __findall(void* pattern__or__re,
length = v[j2 + 1] - v[j2];
b[length] = 0;
memcpy(b, subject + v[j2], length);
tu_append(tu, b, Str);
pikaList_append(tu, arg_newStr(b));
}
sub_list = newNormalObj(New_PikaStdData_Tuple);
obj_setPtr(sub_list, "list", tu);
sub_arg = arg_newObj(sub_list);
PikaStdData_List_append(list, sub_arg);
arg_deinit(sub_arg);
pikaList_append(list, arg_newObj(tu));
free(b);
}
goto exit;
@ -874,8 +845,8 @@ PikaObj* __subn(void* pattern__or__re,
}
if (s == subjet) {
PikaTuple* yup = New_pikaTuple();
tu_append(yup, s, Str);
tu_append(yup, 0, Int);
pikaList_append(yup, arg_newStr(s));
pikaList_append(yup, arg_newInt(0));
Any tuple_obj = newNormalObj(New_PikaStdData_Tuple);
obj_setPtr(tuple_obj, "list", yup);
@ -883,10 +854,10 @@ PikaObj* __subn(void* pattern__or__re,
}
PikaTuple* yup = New_pikaTuple();
tu_append(yup, s, Str);
pikaList_append(yup, arg_newStr(s));
free(s);
tu_append(yup, matched_times, Int);
pikaList_append(yup, arg_newInt(matched_times));
Any tuple_obj = newNormalObj(New_PikaStdData_Tuple);
obj_setPtr(tuple_obj, "list", yup);

View File

@ -1,4 +1,4 @@
#include "PikaPlatform_socket.h"
#include "PikaPlatform_socket.h"
/*
The functinos start with PIKA_WEAK are weak functions,
you need to override them in your platform.

View File

@ -1,4 +1,4 @@
#include "PikaObj.h"
#include "PikaObj.h"
#ifdef __linux__
#include <arpa/inet.h>
#include <errno.h>

View File

@ -1,4 +1,4 @@
#include "PikaPlatform_socket.h"
#include "PikaPlatform_socket.h"
#include "_socket_socket.h"
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 12, 0)

View File

@ -1,14 +1,20 @@
#include "test_common.h"
TEST_START
// #define _SKIP_EVENTLOOP_TEST
#if !PIKA_NANO_ENABLE
TEST_RUN_SINGLE_FILE(thread, test2, "test/python/_thread/test2.py")
#ifndef _SKIP_EVENTLOOP_TEST
TEST_RUN_SINGLE_FILE(eventloop, test1, "test/python/eventloop/test1.py")
TEST_RUN_SINGLE_FILE(eventloop, test2, "test/python/eventloop/test2.py")
TEST_RUN_SINGLE_FILE(eventloop, test3, "test/python/eventloop/test3.py")
TEST_RUN_SINGLE_FILE(eventloop, delay1, "test/python/eventloop/delay1.py")
TEST_RUN_SINGLE_FILE(eventloop, once1, "test/python/eventloop/once1.py")
TEST_RUN_SINGLE_FILE(eventloop, once2, "test/python/eventloop/once2.py")
#endif
TEST_RUN_SINGLE_FILE(fsm, test1, "test/python/fsm/test1.py")
TEST_RUN_SINGLE_FILE_PASS(builtin,

View File

@ -1,5 +1,7 @@
import json
import time
res = json.loads('{"x": 1}')
assert res['x'] == 1
res = json.loads('{"a": 1, "b": 2, "c": 3}')
assert res['a'] == 1

View File

@ -587,24 +587,24 @@ TEST(stddata, list_slice_issue) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(stddata, objDict_new) {
TEST(stddata, New_pikaDictFromVarArgs) {
PikaObj* new_dict =
objDict_new(arg_newStr("a"), arg_newInt(1), arg_newStr("b"),
arg_newInt(2), arg_newStr("c"), arg_newInt(3));
New_pikaDictFromVarArgs(arg_newStr("a"), arg_newInt(1), arg_newStr("b"),
arg_newInt(2), arg_newStr("c"), arg_newInt(3));
pika_platform_printf("%s\r\n", PikaStdData_Dict___str__(new_dict));
obj_deinit(new_dict);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(stddata, objDict_new0) {
PikaObj* new_dict = objDict_new(NULL);
TEST(stddata, New_pikaDictFromVarArgs0) {
PikaObj* new_dict = New_pikaDictFromVarArgs(NULL);
pika_platform_printf("%s\r\n", PikaStdData_Dict___str__(new_dict));
obj_deinit(new_dict);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(stddata, objList_new0) {
PikaObj* new_list = objList_new(NULL);
TEST(stddata, New_pikaListFromVarArgs0) {
PikaObj* new_list = New_pikaListFromVarArgs(NULL);
pika_platform_printf("%s\r\n", PikaStdData_List___str__(new_list));
obj_deinit(new_list);
EXPECT_EQ(pikaMemNow(), 0);

View File

@ -2280,20 +2280,20 @@ void pikaGC_markObj(PikaGC* gc, PikaObj* self) {
}
args_foreach(self->list, _pikaGC_markHandler, gc);
if (self->constructor == New_PikaStdData_Dict) {
PikaDict* dict = obj_getPtr(self, "dict");
Args* dict = _OBJ2DICT(self);
if (NULL == dict) {
goto __exit;
}
args_foreach(&dict->super, _pikaGC_markHandler, (void*)gc);
args_foreach(dict, _pikaGC_markHandler, (void*)gc);
goto __exit;
}
if (self->constructor == New_PikaStdData_List ||
self->constructor == New_PikaStdData_Tuple) {
PikaList* list = obj_getPtr(self, "list");
Args* list = _OBJ2LIST(self);
if (NULL == list) {
goto __exit;
}
args_foreach(&list->super, _pikaGC_markHandler, (void*)gc);
args_foreach(list, _pikaGC_markHandler, (void*)gc);
goto __exit;
}
__exit:
@ -3293,7 +3293,7 @@ Arg* builtins_list(PikaObj* self, PikaTuple* val) {
};
return pikaVM_runByteCodeReturn(self, (uint8_t*)bytes, "@res_list");
}
return arg_newObj(objList_new(NULL));
return arg_newObj(New_pikaListFromVarArgs(NULL));
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in list is not enabled.\r\n");
@ -3303,8 +3303,7 @@ Arg* builtins_list(PikaObj* self, PikaTuple* val) {
Arg* builtins_dict(PikaObj* self, PikaTuple* val) {
#if PIKA_BUILTIN_STRUCT_ENABLE
PikaObj* New_PikaStdData_Dict(Args * args);
return arg_newDirectObj(New_PikaStdData_Dict);
return arg_newObj(New_pikaDict());
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in dist is not enabled.\r\n");
@ -3377,11 +3376,10 @@ Arg* builtins_bytes(PikaObj* self, Arg* val) {
PikaObj* New_PikaStdData_Tuple(Args * args);
if (obj->constructor == New_PikaStdData_List ||
obj->constructor == New_PikaStdData_Tuple) {
PikaList* list = obj_getPtr(obj, "list");
Arg* bytes = arg_newBytes(NULL, pikaList_getSize(list));
Arg* bytes = arg_newBytes(NULL, pikaList_getSize(obj));
uint8_t* bytes_raw = arg_getBytes(bytes);
for (size_t i = 0; i < pikaList_getSize(list); i++) {
bytes_raw[i] = (uint8_t)pikaList_getInt(list, i);
for (size_t i = 0; i < pikaList_getSize(obj); i++) {
bytes_raw[i] = (uint8_t)pikaList_getInt(obj, i);
}
return bytes;
}
@ -3394,6 +3392,7 @@ Arg* builtins_bytes(PikaObj* self, Arg* val) {
void builtins_print(PikaObj* self, PikaTuple* val, PikaDict* ops) {
int arg_size = pikaTuple_getSize(val);
pika_assert(arg_size >= 0);
char* end = pikaDict_getStr(ops, "end");
if (NULL == end) {
/* default */
@ -3433,7 +3432,7 @@ char* builtins_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
#if PIKA_SYNTAX_FORMAT_ENABLE
Args buffs = {0};
pikaMemMaxReset();
char* res = strsFormatList(&buffs, fmt, &var->super);
char* res = strsFormatList(&buffs, fmt, var);
obj_setStr(self, "_buf", res);
res = obj_getStr(self, "_buf");
strsDeinit(&buffs);
@ -3476,14 +3475,13 @@ PikaObj* builtins_open(PikaObj* self, char* path, char* mode) {
/* __dir_each */
int32_t __dir_each(Arg* argEach, void* context) {
PikaObj* list = args_getPtr((Args*)context, "list");
if (argType_isCallable(arg_getType(argEach))) {
char name_buff[PIKA_LINE_BUFF_SIZE] = {0};
char* method_name =
methodArg_getName(argEach, name_buff, sizeof(name_buff));
Arg* arg_str = arg_newStr(method_name);
PikaList* list = args_getPtr(context, "_list");
objList_append(list, arg_str);
arg_deinit(arg_str);
}
return 0;
}
@ -3496,10 +3494,9 @@ PikaObj* builtins_dir(PikaObj* self, Arg* arg) {
}
PikaObj* obj = arg_getPtr(arg);
PikaObj* New_PikaStdData_List(Args * args);
PikaObj* list = newNormalObj(New_PikaStdData_List);
objList_init(list);
PikaObj* list = New_pikaList();
Args* context = New_args(NULL);
args_setPtr(context, "list", list);
args_setPtr(context, "_list", list);
args_foreach(obj->list, __dir_each, context);
args_deinit(context);
return list;
@ -3846,8 +3843,8 @@ Arg* _max_min(PikaObj* self, PikaTuple* val, uint8_t* bc) {
obj_setArg(self, "@list", pikaTuple_getArg(val, 0));
return pikaVM_runByteCodeReturn(self, (uint8_t*)bc, "@res_max");
}
PikaObj* oTuple = newNormalObj(New_PikaStdData_Tuple);
obj_setPtr(oTuple, "list", val);
PikaTuple* oTuple = newNormalObj(New_PikaStdData_Tuple);
obj_setPtr(oTuple, "list", obj_getPtr(val, "list"));
obj_setInt(oTuple, "needfree", 0);
Arg* aTuple = arg_newObj(oTuple);
obj_setArg(self, "@list", aTuple);
@ -3970,63 +3967,24 @@ char* builtins_bytearray_decode(PikaObj* self) {
return obj_getStr(self, "_buf");
}
int objList_getSize(PikaObj* self) {
PikaList* list = obj_getPtr(self, "list");
return pikaList_getSize(list);
}
void objList_set(PikaObj* self, int i, Arg* arg) {
PikaList* list = obj_getPtr(self, "list");
if (PIKA_RES_OK != pikaList_setArg(list, i, arg)) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "Error: index exceeded lengh of list.");
}
}
Arg* objList_get(PikaObj* self, int i) {
PikaList* list = obj_getPtr(self, "list");
return pikaList_getArg(list, i);
}
Arg* objDict_get(PikaObj* self, char* key) {
pika_assert_obj_alive(self);
PikaDict* dict = obj_getPtr(self, "dict");
Arg* res = pikaDict_getArg(dict, key);
if (NULL == res) {
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
__platform_printf("KeyError: %s\n", key);
}
pika_assert_arg_alive(res);
return res;
}
void objDict_set(PikaObj* self, char* key, Arg* arg) {
PikaDict* dict = obj_getPtr(self, "dict");
PikaDict* keys = obj_getPtr(self, "_keys");
Arg* arg_key = arg_setStr(NULL, key, key);
Arg* arg_new = arg_copy(arg);
arg_setName(arg_new, key);
pikaDict_setArg(dict, arg_new);
pikaDict_setArg(keys, arg_key);
}
int32_t objDict_forEach(PikaObj* self,
int32_t (*eachHandle)(PikaObj* self,
Arg* keyEach,
Arg* valEach,
void* context),
void* context) {
PikaDict* keys = obj_getPtr(self, "_keys");
PikaDict* dict = obj_getPtr(self, "dict");
Args* keys = _OBJ2KEYS(self);
Args* dict = _OBJ2DICT(self);
pika_assert(NULL != dict);
pika_assert(NULL != keys);
int i = 0;
while (1) {
Arg* item_key = args_getArgByIndex(&keys->super, i);
Arg* item_val = args_getArgByIndex(&dict->super, i);
if (NULL == item_key) {
Arg* item_key = args_getArgByIndex(keys, i);
Arg* item_val = args_getArgByIndex(dict, i);
if (NULL == item_val) {
break;
}
pika_assert(NULL != item_key);
// Call the handle function on each key-value pair
if (eachHandle(self, item_key, item_val, context) != 0) {
return -1;
@ -4042,10 +4000,9 @@ int32_t objList_forEach(PikaObj* self,
Arg* itemEach,
void* context),
void* context) {
PikaList* list = obj_getPtr(self, "list");
int i = 0;
while (PIKA_TRUE) {
Arg* item = pikaList_getArg(list, i);
Arg* item = pikaList_get(self, i);
if (NULL == item) {
break;
}
@ -4075,3 +4032,38 @@ void pika_sleep_ms(uint32_t ms) {
}
}
}
PikaObj* _New_pikaListOrTuple(int isTuple) {
Args* list = New_args(NULL);
/* set top index for append */
args_pushArg_name(list, "top", arg_newInt(0));
PikaObj* self = NULL;
if (isTuple) {
self = newNormalObj(New_PikaStdData_Tuple);
} else {
self = newNormalObj(New_PikaStdData_List);
}
obj_setPtr(self, "list", list);
return self;
}
PikaList* New_pikaList(void) {
return _New_pikaListOrTuple(0);
}
PikaTuple* New_pikaTuple(void) {
return _New_pikaListOrTuple(1);
}
void pikaDict_init(PikaObj* self) {
Args* dict = New_args(NULL);
Args* keys = New_args(NULL);
obj_setPtr(self, "dict", dict);
obj_setPtr(self, "_keys", keys);
}
PikaDict* New_pikaDict(void) {
PikaDict* self = newNormalObj(New_PikaStdData_Dict);
objDict_init(self);
return self;
}

View File

@ -441,19 +441,19 @@ PikaObj* _pika_dict_new(int num_args, ...);
/* Macro to create a new tuple PikaObj with the given arguments (of type Arg*).
*/
#define objTuple_new(...) \
#define New_pikaTupleFromVarArgs(...) \
_pika_tuple_new(sizeof((Arg*[]){__VA_ARGS__}) / sizeof(Arg*), __VA_ARGS__)
/* Macro to create a new list PikaObj with the given arguments (of type Arg*).
*/
#define objList_new(...) \
#define New_pikaListFromVarArgs(...) \
_pika_list_new(sizeof((Arg*[]){__VA_ARGS__}) / sizeof(Arg*), __VA_ARGS__)
/* Macro to create a new dict PikaObj with the given arguments (of type
* Arg*).
*/
#define objDict_new(...) \
#define New_pikaDictFromVarArgs(...) \
_pika_dict_new(sizeof((Arg*[]){__VA_ARGS__, NULL}) / sizeof(Arg*) - 1, \
__VA_ARGS__)
@ -743,17 +743,26 @@ uint32_t pikaGC_printFreeList(void);
int pika_GIL_EXIT(void);
int pika_GIL_ENTER(void);
typedef PikaObj PikaList;
typedef PikaObj PikaTuple;
typedef PikaObj PikaDict;
pika_bool pika_GIL_isInit(void);
/* builtins */
PikaObj* New_builtins(Args* args);
int objList_getSize(PikaObj* self);
void objList_set(PikaObj* self, int i, Arg* arg);
Arg* objList_get(PikaObj* self, int i);
void objList_append(PikaObj* self, Arg* arg);
void objList_init(PikaObj* self);
void objDict_init(PikaObj* self);
#define objList_getSize pikaList_getSize
#define objList_get pikaList_get
#define objList_set pikaList_set
#define objList_forEach pikaList_forEach
#define objList_init pikaList_init
#define objDict_get pikaDict_get
#define objDict_set pikaDict_set
#define objList_append pikaList_append
#define objDict_init pikaDict_init
int32_t objList_forEach(PikaObj* self,
int32_t (*eachHandle)(PikaObj* self,
int itemIndex,
@ -767,8 +776,6 @@ int32_t objList_forEach(PikaObj* self,
#define objTuple_forEach objList_forEach
#define objTuple_init objList_init
Arg* objDict_get(PikaObj* self, char* key);
void objDict_set(PikaObj* self, char* key, Arg* arg);
int32_t objDict_forEach(PikaObj* self,
int32_t (*eachHandle)(PikaObj* self,
Arg* keyEach,
@ -778,6 +785,168 @@ int32_t objDict_forEach(PikaObj* self,
void pika_sleep_ms(uint32_t ms);
PIKA_RES _transeBool(Arg* arg, pika_bool* res);
#define _OBJ2LIST(_obj) ((Args*)obj_getPtr((_obj), "list"))
#define _OBJ2DICT(_obj) ((Args*)obj_getPtr((_obj), "dict"))
#define _OBJ2KEYS(_obj) ((Args*)obj_getPtr((_obj), "_keys"))
static inline PIKA_RES pikaDict_setInt(PikaDict* self,
char* name,
int64_t val) {
return args_setInt(_OBJ2DICT(self), (name), (val));
}
static inline PIKA_RES pikaDict_setFloat(PikaDict* self,
char* name,
pika_float val) {
return args_setFloat(_OBJ2DICT(self), (name), (val));
}
static inline PIKA_RES pikaDict_setStr(PikaDict* self, char* name, char* val) {
return args_setStr(_OBJ2DICT(self), (name), (val));
}
static inline PIKA_RES pikaDict_setPtr(PikaDict* self, char* name, void* val) {
return args_setPtr(_OBJ2DICT(self), (name), (val));
}
static inline PIKA_RES _pikaDict_setVal(PikaDict* self, Arg* val) {
return args_setArg(_OBJ2DICT(self), (val));
}
static inline PIKA_RES pikaDict_set(PikaDict* self, char* name, Arg* val) {
val = arg_setName(val, name);
_pikaDict_setVal(self, val);
return args_setStr(_OBJ2KEYS(self), (name), (name));
}
static inline PIKA_RES pikaDict_removeArg(PikaDict* self, Arg* val) {
return args_removeArg(_OBJ2DICT(self), (val));
}
static inline PIKA_RES pikaDict_setBytes(PikaDict* self,
char* name,
uint8_t* val,
size_t size) {
return args_setBytes(_OBJ2DICT(self), (name), (val), (size));
}
static inline int64_t pikaDict_getInt(PikaDict* self, char* name) {
return args_getInt(_OBJ2DICT(self), (name));
}
static inline pika_float pikaDict_getFloat(PikaDict* self, char* name) {
return args_getFloat(_OBJ2DICT(self), (name));
}
static inline char* pikaDict_getStr(PikaDict* self, char* name) {
return args_getStr(_OBJ2DICT(self), (name));
}
static inline void* pikaDict_getPtr(PikaDict* self, char* name) {
return args_getPtr(_OBJ2DICT(self), (name));
}
static inline int pikaDict_getSize(PikaDict* self) {
return args_getSize(_OBJ2DICT(self));
}
static inline Arg* pikaDict_getArgByidex(PikaDict* self, int index) {
return args_getArgByIndex(_OBJ2DICT(self), (index));
}
static inline Arg* pikaDict_get(PikaDict* self, char* name) {
return args_getArg(_OBJ2DICT(self), (name));
}
static inline int32_t pikaDict_isArgExist(PikaDict* self, char* name) {
return args_isArgExist(_OBJ2DICT(self), (name));
}
static inline uint8_t* pikaDict_getBytes(PikaDict* self, char* name) {
return args_getBytes(_OBJ2DICT(self), (name));
}
static inline ArgType pikaDict_getType(PikaDict* self, char* name) {
return args_getType(_OBJ2DICT(self), (name));
}
static inline size_t pikaDict_getBytesSize(PikaDict* self, char* name) {
return args_getBytesSize(_OBJ2DICT(self), (name));
}
void pikaDict_init(PikaObj* self);
static inline void pikaDict_deinit(PikaDict* self) {
obj_deinit(self);
}
/* list api */
PIKA_RES pikaList_append(PikaList* self, Arg* arg);
PIKA_RES pikaList_set(PikaList* self, int index, Arg* arg);
void pikaList_init(PikaObj* self);
int pikaList_getInt(PikaList* self, int index);
pika_float pikaList_getFloat(PikaList* self, int index);
char* pikaList_getStr(PikaList* self, int index);
void* pikaList_getPtr(PikaList* self, int index);
Arg* pikaList_get(PikaList* self, int index);
size_t pikaList_getSize(PikaList* self);
void pikaList_reverse(PikaList* self);
PIKA_RES pikaList_insert(PikaList* self, int index, Arg* arg);
Arg* pikaList_pop(PikaList* list);
Arg* pikaList_pop_withIndex(PikaList* list, int index);
PIKA_RES pikaList_remove(PikaList* list, Arg* arg);
static inline void pikaList_deinit(PikaList* self) {
args_deinit(_OBJ2LIST(self));
}
static inline ArgType pikaList_getType(PikaList* self, int index) {
Arg* arg = pikaList_get(self, index);
return arg_getType(arg);
}
/* tuple api */
static inline void pikaTuple_deinit(PikaTuple* self) {
pikaList_deinit(self);
}
static inline Arg* pikaTuple_getArg(PikaTuple* self, int index) {
return pikaList_get(self, (index));
}
static inline size_t pikaTuple_getSize(PikaTuple* self) {
if (self == NULL) {
return 0;
}
return pikaList_getSize(self);
}
static inline int64_t pikaTuple_getInt(PikaTuple* self, int index) {
return pikaList_getInt(self, (index));
}
static inline pika_float pikaTuple_getFloat(PikaTuple* self, int index) {
return pikaList_getFloat(self, (index));
}
static inline char* pikaTuple_getStr(PikaTuple* self, int index) {
return pikaList_getStr(self, (index));
}
static inline void* pikaTuple_getPtr(PikaTuple* self, int index) {
return pikaList_getPtr(self, (index));
}
static inline ArgType pikaTuple_getType(PikaTuple* self, int index) {
return pikaList_getType(self, (index));
}
/* dict api */
PikaDict* New_pikaDict(void);
PikaList* New_pikaList(void);
PikaTuple* New_pikaTuple(void);
PikaTuple* args_getTuple(Args* self, char* name);
PikaDict* args_getDict(Args* self, char* name);
char* strsFormatList(Args* out_buffs, char* fmt, PikaList* list);
#endif
#ifdef __cplusplus
}

View File

@ -782,7 +782,6 @@ Arg* _vm_slice(VMState* vm,
Arg* aIndex = arg_newInt(i);
Arg* aItem = _vm_get(vm, self, aIndex, aObj);
objList_append(oSliced, aItem);
arg_deinit(aItem);
arg_deinit(aIndex);
}
return arg_newObj(oSliced);
@ -1174,10 +1173,9 @@ static char* _kw_pos_to_default_all(FunctionArgsInfo* f,
Arg* aKeyword = NULL;
/* load default arg from kws */
if (f->kw != NULL) {
aKeyword = pikaDict_getArg(f->kw, sArgName);
aKeyword = pikaDict_get(f->kw, sArgName);
if (aKeyword != NULL) {
Arg* aNew = arg_copy(aKeyword);
argv[(*argc)++] = aNew;
argv[(*argc)++] = arg_copy(aKeyword);
pikaDict_removeArg(f->kw, aKeyword);
goto __next;
}
@ -1212,7 +1210,7 @@ static int _kw_to_pos_one(FunctionArgsInfo* f,
if (f->kw == NULL) {
return 0;
}
Arg* pos_arg = pikaDict_getArg(f->kw, arg_name);
Arg* pos_arg = pikaDict_get(f->kw, arg_name);
if (pos_arg == NULL) {
return 0;
}
@ -1229,7 +1227,7 @@ static void _kw_to_pos_all(FunctionArgsInfo* f, int* argc, Arg* argv[]) {
for (int i = 0; i < arg_num_need; i++) {
char* arg_name = strPopLastToken(f->type_list, ',');
pika_assert(f->kw != NULL);
Arg* pos_arg = pikaDict_getArg(f->kw, arg_name);
Arg* pos_arg = pikaDict_get(f->kw, arg_name);
pika_assert(pos_arg != NULL);
argv[(*argc)++] = arg_copy(pos_arg);
pikaDict_removeArg(f->kw, pos_arg);
@ -1285,15 +1283,12 @@ static void _kw_push(FunctionArgsInfo* f, Arg* call_arg, int i) {
if (NULL == f->kw) {
f->kw = New_pikaDict();
}
if (NULL == f->kw_keys) {
f->kw_keys = New_pikaDict();
}
arg_setIsKeyword(call_arg, pika_false);
pikaDict_setArg(f->kw, call_arg);
char kw_keys_index_buff[11] = {0};
char* kw_keys_index = fast_itoa(kw_keys_index_buff, i);
pikaDict_setArg(f->kw_keys,
arg_setInt(NULL, kw_keys_index, arg_getNameHash(call_arg)));
Hash kw_hash = call_arg->name_hash;
char buff[32] = {0};
_pikaDict_setVal(f->kw, call_arg);
char* sHash = fast_itoa(buff, kw_hash);
args_setStr(_OBJ2KEYS(f->kw), sHash, sHash);
}
static void _load_call_arg(VMState* vm,
@ -1311,9 +1306,7 @@ static void _load_call_arg(VMState* vm,
/* load variable arg */
if (f->i_arg > f->n_positional) {
if (f->is_vars) {
pikaList_append(&(f->tuple)->super, call_arg);
/* the append would copy the arg */
arg_deinit(call_arg);
pikaList_append(f->tuple, call_arg);
return;
}
}
@ -1403,10 +1396,10 @@ static uint32_t _get_n_input_with_unpack(VMState* vm, int n_used) {
PikaObj* New_PikaStdData_Dict(Args * args);
PikaObj* obj = arg_getPtr(call_arg);
pika_assert(obj->constructor == New_PikaStdData_Dict);
PikaDict* dict = obj_getPtr(obj, "dict");
Args* dict = _OBJ2DICT(obj);
int i_item = 0;
while (pika_true) {
Arg* item_val = args_getArgByIndex(&dict->super, i_item);
Arg* item_val = args_getArgByIndex(dict, i_item);
if (NULL == item_val) {
break;
}
@ -1563,7 +1556,6 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
/* get kw dict name */
f.kw_dict_name = arg_def + 2;
f.kw = New_pikaDict();
f.kw_keys = New_pikaDict();
/* remove the format arg */
strPopLastToken(f.type_list, ',');
continue;
@ -1597,13 +1589,10 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
#endif
if (f.tuple != NULL) {
pikaList_reverse(&(f.tuple)->super);
pikaList_reverse(f.tuple);
/* load variable tuple */
PikaObj* New_PikaStdData_Tuple(Args * args);
PikaObj* tuple_obj = newNormalObj(New_PikaStdData_Tuple);
obj_setPtr(tuple_obj, "list", f.tuple);
Arg* argi =
arg_setPtr(NULL, f.var_tuple_name, ARG_TYPE_OBJECT, tuple_obj);
arg_setPtr(NULL, f.var_tuple_name, ARG_TYPE_OBJECT, f.tuple);
argv[argc++] = argi;
}
@ -1613,10 +1602,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
}
/* load kw dict */
PikaObj* New_PikaStdData_Dict(Args * args);
PikaObj* dict_obj = newNormalObj(New_PikaStdData_Dict);
obj_setPtr(dict_obj, "dict", f.kw);
obj_setPtr(dict_obj, "_keys", f.kw_keys);
Arg* argi = arg_setPtr(NULL, f.kw_dict_name, ARG_TYPE_OBJECT, dict_obj);
Arg* argi = arg_setPtr(NULL, f.kw_dict_name, ARG_TYPE_OBJECT, f.kw);
argv[argc++] = argi;
}
@ -1663,7 +1649,6 @@ static Arg* _vm_create_list_or_tuple(PikaObj* self,
Arg* arg = stack_popArg_alloc(&stack);
pika_assert(arg != NULL);
objList_append(list, arg);
arg_deinit(arg);
}
stack_deinit(&stack);
return arg_newObj(list);
@ -1704,13 +1689,6 @@ static Arg* VM_instruction_handler_TPL(PikaObj* self,
return _vm_create_list_or_tuple(self, vm, pika_false);
}
void objDict_init(PikaObj* self) {
PikaDict* dict = New_pikaDict();
PikaDict* keys = New_pikaDict();
obj_setPtr(self, "dict", dict);
obj_setPtr(self, "_keys", keys);
}
#if PIKA_BUILTIN_STRUCT_ENABLE
PikaObj* New_PikaStdData_Dict(Args* args);
#endif
@ -1721,8 +1699,7 @@ static Arg* VM_instruction_handler_DCT(PikaObj* self,
Arg* arg_ret_reg) {
#if PIKA_BUILTIN_STRUCT_ENABLE
uint32_t n_arg = VMState_getInputArgNum(vm);
PikaObj* dict = newNormalObj(New_PikaStdData_Dict);
objDict_init(dict);
PikaObj* dict = New_pikaDict();
Stack stack = {0};
stack_init(&stack);
/* load to local stack to change sort */
@ -1735,7 +1712,6 @@ static Arg* VM_instruction_handler_DCT(PikaObj* self,
Arg* val_arg = stack_popArg_alloc(&stack);
objDict_set(dict, arg_getStr(key_arg), val_arg);
arg_deinit(key_arg);
arg_deinit(val_arg);
}
stack_deinit(&stack);
return arg_newObj(dict);
@ -3495,7 +3471,6 @@ static int pikaVM_runInstructUnit(PikaObj* self,
PikaObj* oReg = vm->oreg[invoke_deepth - 1];
if (NULL != oReg && NULL != return_arg) {
objList_append(oReg, return_arg);
arg_deinit(return_arg);
return_arg = NULL;
}
}
@ -3599,8 +3574,8 @@ static ByteCodeFrame* _cache_bytecodeframe(PikaObj* self) {
static ByteCodeFrame* _cache_bcf_fn(PikaObj* self, char* py_lines) {
/* cache 'def' and 'class' to heap */
if ((NULL == strFindIgnoreQuoted(py_lines, "def ")) &&
(NULL == strFindIgnoreQuoted(py_lines, "class "))) {
if ((NULL == strstr(py_lines, "def ")) &&
(NULL == strstr(py_lines, "class "))) {
return NULL;
}
return _cache_bytecodeframe(self);

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 12
#define PIKA_VERSION_MICRO 4
#define PIKA_EDIT_TIME "2023/08/02 11:58:50"
#define PIKA_EDIT_TIME "2023/08/05 01:34:59"

View File

@ -85,6 +85,7 @@ PIKA_RES args_setRef(Args* self, char* name, void* argPointer) {
}
PIKA_RES args_setStr(Args* self, char* name, char* strIn) {
pika_assert(NULL != self);
PIKA_RES errCode = PIKA_RES_OK;
Arg* argNew = New_arg(NULL);
argNew = arg_setStr(argNew, name, strIn);
@ -491,55 +492,42 @@ Args* New_args(Args* args) {
return self;
}
PikaDict* New_pikaDict(void) {
PikaDict* self = (PikaDict*)New_args(NULL);
return self;
}
PikaList* New_pikaList(void) {
PikaList* self = (PikaList*)New_args(NULL);
/* set top index for append */
args_pushArg_name(&self->super, "top", arg_newInt(0));
return self;
}
PIKA_RES pikaList_setArg(PikaList* self, int index, Arg* arg) {
PIKA_RES pikaList_set(PikaList* self, int index, Arg* arg) {
char buff[11];
char* i_str = fast_itoa(buff, index);
int top = args_getInt(&self->super, "top");
int top = pikaList_getSize(self);
if (index > top) {
return PIKA_RES_ERR_OUT_OF_RANGE;
}
Arg* new_arg = arg_copy(arg);
new_arg = arg_setName(new_arg, i_str);
args_setArg(&self->super, new_arg);
arg = arg_setName(arg, i_str);
args_setArg(_OBJ2LIST(self), arg);
return PIKA_RES_OK;
}
Arg* pikaList_getArg(PikaList* self, int index) {
Arg* pikaList_get(PikaList* self, int index) {
pika_assert(NULL != self);
char buff[11];
char* i_str = fast_itoa(buff, index);
return args_getArg(&self->super, i_str);
return args_getArg(_OBJ2LIST(self), i_str);
}
int pikaList_getInt(PikaList* self, int index) {
Arg* arg = pikaList_getArg(self, index);
Arg* arg = pikaList_get(self, index);
return arg_getInt(arg);
}
pika_float pikaList_getFloat(PikaList* self, int index) {
Arg* arg = pikaList_getArg(self, index);
Arg* arg = pikaList_get(self, index);
return arg_getFloat(arg);
}
char* pikaList_getStr(PikaList* self, int index) {
Arg* arg = pikaList_getArg(self, index);
Arg* arg = pikaList_get(self, index);
return arg_getStr(arg);
}
void* pikaList_getPtr(PikaList* self, int index) {
Arg* arg = pikaList_getArg(self, index);
Arg* arg = pikaList_get(self, index);
return arg_getPtr(arg);
}
@ -547,49 +535,49 @@ PIKA_RES pikaList_append(PikaList* self, Arg* arg) {
if (NULL == arg) {
return PIKA_RES_ERR_ARG_NO_FOUND;
}
int top = args_getInt(&self->super, "top");
int top = pikaList_getSize(self);
char buff[11];
char* topStr = fast_itoa(buff, top);
Arg* arg_to_push = arg_copy(arg);
Arg* arg_to_push = arg;
arg_setName(arg_to_push, topStr);
args_setArg(&self->super, arg_to_push);
args_setArg(_OBJ2LIST(self), arg_to_push);
/* top++ */
return args_setInt(&self->super, "top", top + 1);
return args_setInt(_OBJ2LIST(self), "top", top + 1);
}
Arg* pikaList_pop_withIndex(PikaList* list, int index) {
int top = args_getInt(&list->super, "top");
Arg* pikaList_pop_withIndex(PikaList* self, int index) {
int top = pikaList_getSize(self);
if (top <= 0) {
return NULL;
}
if (index < 0) {
index = top + index;
}
Arg* arg = pikaList_getArg(list, index);
Arg* arg = pikaList_get(self, index);
Arg* res = arg_copy(arg);
pikaList_remove(list, arg);
pikaList_remove(self, arg);
return res;
}
Arg* pikaList_pop(PikaList* list) {
int top = args_getInt(&list->super, "top");
Arg* pikaList_pop(PikaList* self) {
int top = pikaList_getSize(self);
if (top <= 0) {
return NULL;
}
return pikaList_pop_withIndex(list, top - 1);
return pikaList_pop_withIndex(self, top - 1);
}
PIKA_RES pikaList_remove(PikaList* list, Arg* arg) {
int top = args_getInt(&list->super, "top");
PIKA_RES pikaList_remove(PikaList* self, Arg* arg) {
int top = pikaList_getSize(self);
int i_remove = 0;
if (top <= 0) {
return PIKA_RES_ERR_OUT_OF_RANGE;
}
for (int i = 0; i < top; i++) {
Arg* arg_now = pikaList_getArg(list, i);
Arg* arg_now = pikaList_get(self, i);
if (arg_isEqual(arg_now, arg)) {
i_remove = i;
args_removeArg(&list->super, arg_now);
args_removeArg(_OBJ2LIST(self), arg_now);
break;
}
}
@ -597,15 +585,15 @@ PIKA_RES pikaList_remove(PikaList* list, Arg* arg) {
for (int i = i_remove + 1; i < top; i++) {
char buff[11];
char* i_str = fast_itoa(buff, i - 1);
Arg* arg_now = pikaList_getArg(list, i);
Arg* arg_now = pikaList_get(self, i);
arg_setName(arg_now, i_str);
}
args_setInt(&list->super, "top", top - 1);
args_setInt(_OBJ2LIST(self), "top", top - 1);
return PIKA_RES_OK;
}
PIKA_RES pikaList_insert(PikaList* self, int index, Arg* arg) {
int top = args_getInt(&self->super, "top");
int top = pikaList_getSize(self);
if (index > top) {
return PIKA_RES_ERR_OUT_OF_RANGE;
}
@ -613,15 +601,15 @@ PIKA_RES pikaList_insert(PikaList* self, int index, Arg* arg) {
for (int i = top - 1; i >= index; i--) {
char buff[11];
char* i_str = fast_itoa(buff, i + 1);
Arg* arg_now = pikaList_getArg(self, i);
Arg* arg_now = pikaList_get(self, i);
arg_setName(arg_now, i_str);
}
char buff[11];
char* i_str = fast_itoa(buff, index);
Arg* arg_to_push = arg_copy(arg);
arg_setName(arg_to_push, i_str);
args_setArg(&self->super, arg_to_push);
args_setInt(&self->super, "top", top + 1);
args_setArg(_OBJ2LIST(self), arg_to_push);
args_setInt(_OBJ2LIST(self), "top", top + 1);
return PIKA_RES_OK;
}
@ -629,16 +617,14 @@ size_t pikaList_getSize(PikaList* self) {
if (NULL == self) {
return 0;
}
return args_getInt(&self->super, "top");
int64_t ret = args_getInt(_OBJ2LIST(self), "top");
pika_assert(ret >= 0);
return ret;
}
void objList_append(PikaObj* self, Arg* arg) {
PikaList* list = obj_getPtr(self, "list");
pikaList_append(list, arg);
}
void objList_init(PikaObj* self) {
PikaList* list = New_pikaList();
void pikaList_init(PikaObj* self) {
Args* list = New_args(NULL);
args_pushArg_name(list, "top", arg_newInt(0));
obj_setPtr(self, "list", list);
}
@ -646,20 +632,13 @@ void pikaList_reverse(PikaList* self) {
pika_assert(NULL != self);
int top = pikaList_getSize(self);
for (int i = 0; i < top / 2; i++) {
Arg* arg_i = arg_copy(pikaList_getArg(self, i));
Arg* arg_top = arg_copy(pikaList_getArg(self, top - i - 1));
pikaList_setArg(self, i, arg_top);
pikaList_setArg(self, top - i - 1, arg_i);
arg_deinit(arg_i);
arg_deinit(arg_top);
Arg* arg_i = arg_copy(pikaList_get(self, i));
Arg* arg_top = arg_copy(pikaList_get(self, top - i - 1));
pikaList_set(self, i, arg_top);
pikaList_set(self, top - i - 1, arg_i);
}
}
PikaTuple* New_pikaTuple(void) {
PikaTuple* self = (PikaTuple*)New_pikaList();
return self;
}
char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg) {
Args buffs = {0};
char* res = NULL;
@ -722,7 +701,7 @@ char* strsFormatList(Args* out_buffs, char* fmt, PikaList* list) {
for (size_t i = 0; i < pikaList_getSize(list); i++) {
Args buffs_item = {0};
Arg* arg = pikaList_getArg(list, i);
Arg* arg = pikaList_get(list, i);
char* fmt_item = strsPopToken(&buffs_item, &fmt_buff, '%');
fmt_item = strsAppend(&buffs_item, "%", fmt_item);
char* str_format = strsFormatArg(&buffs_item, fmt_item, arg);
@ -748,7 +727,7 @@ PikaTuple* args_getTuple(Args* self, char* name) {
return NULL;
}
PikaObj* tuple_obj = args_getPtr(self, name);
return obj_getPtr(tuple_obj, "list");
return tuple_obj;
}
/* dict */
@ -756,8 +735,8 @@ PikaDict* args_getDict(Args* self, char* name) {
if (NULL == self) {
return NULL;
}
PikaObj* tuple_obj = args_getPtr(self, name);
return obj_getPtr(tuple_obj, "dict");
PikaObj* dict_obj = args_getPtr(self, name);
return dict_obj;
}
char* args_cacheStr(Args* self, char* str) {

View File

@ -124,169 +124,6 @@ size_t args_getBytesSize(Args* self, char* name);
Args* New_args(Args* args);
typedef struct PikaList PikaList;
struct PikaList {
Args super;
};
typedef struct PikaTuple PikaTuple;
struct PikaTuple {
PikaList super;
};
typedef struct PikaDict PikaDict;
struct PikaDict {
Args super;
};
/* dict api */
PikaDict* New_pikaDict(void);
static inline PIKA_RES pikaDict_setInt(PikaDict* self,
char* name,
int64_t val) {
return args_setInt((&((self)->super)), (name), (val));
}
static inline PIKA_RES pikaDict_setFloat(PikaDict* self,
char* name,
pika_float val) {
return args_setFloat((&((self)->super)), (name), (val));
}
static inline PIKA_RES pikaDict_setStr(PikaDict* self, char* name, char* val) {
return args_setStr((&((self)->super)), (name), (val));
}
static inline PIKA_RES pikaDict_setPtr(PikaDict* self, char* name, void* val) {
return args_setPtr((&((self)->super)), (name), (val));
}
static inline PIKA_RES pikaDict_setArg(PikaDict* self, Arg* val) {
return args_setArg((&((self)->super)), (val));
}
static inline PIKA_RES pikaDict_removeArg(PikaDict* self, Arg* val) {
return args_removeArg((&((self)->super)), (val));
}
static inline PIKA_RES pikaDict_setBytes(PikaDict* self,
char* name,
uint8_t* val,
size_t size) {
return args_setBytes((&((self)->super)), (name), (val), (size));
}
static inline int64_t pikaDict_getInt(PikaDict* self, char* name) {
return args_getInt((&((self)->super)), (name));
}
static inline pika_float pikaDict_getFloat(PikaDict* self, char* name) {
return args_getFloat((&((self)->super)), (name));
}
static inline char* pikaDict_getStr(PikaDict* self, char* name) {
return args_getStr((&((self)->super)), (name));
}
static inline void* pikaDict_getPtr(PikaDict* self, char* name) {
return args_getPtr((&((self)->super)), (name));
}
static inline int pikaDict_getSize(PikaDict* self) {
return args_getSize((&((self)->super)));
}
static inline Arg* pikaDict_getArgByidex(PikaDict* self, int index) {
return args_getArgByIndex((&((self)->super)), (index));
}
static inline Arg* pikaDict_getArg(PikaDict* self, char* name) {
return args_getArg((&((self)->super)), (name));
}
static inline int32_t pikaDict_isArgExist(PikaDict* self, char* name) {
return args_isArgExist((&((self)->super)), (name));
}
static inline uint8_t* pikaDict_getBytes(PikaDict* self, char* name) {
return args_getBytes((&((self)->super)), (name));
}
static inline ArgType pikaDict_getType(PikaDict* self, char* name) {
return args_getType((&((self)->super)), (name));
}
static inline size_t pikaDict_getBytesSize(PikaDict* self, char* name) {
return args_getBytesSize((&((self)->super)), (name));
}
static inline void pikaDict_deinit(PikaDict* self) {
args_deinit((&((self)->super)));
}
/* list api */
PIKA_RES pikaList_append(PikaList* self, Arg* arg);
PIKA_RES pikaList_setArg(PikaList* self, int index, Arg* arg);
int pikaList_getInt(PikaList* self, int index);
pika_float pikaList_getFloat(PikaList* self, int index);
char* pikaList_getStr(PikaList* self, int index);
void* pikaList_getPtr(PikaList* self, int index);
Arg* pikaList_getArg(PikaList* self, int index);
size_t pikaList_getSize(PikaList* self);
void pikaList_reverse(PikaList* self);
PIKA_RES pikaList_insert(PikaList* self, int index, Arg* arg);
Arg* pikaList_pop(PikaList* list);
Arg* pikaList_pop_withIndex(PikaList* list, int index);
PIKA_RES pikaList_remove(PikaList* list, Arg* arg);
static inline void pikaList_deinit(PikaList* self) {
args_deinit((&((self)->super)));
}
static inline ArgType pikaList_getType(PikaList* self, int index) {
Arg* arg = pikaList_getArg(self, index);
return arg_getType(arg);
}
/* tuple api */
static inline void pikaTuple_deinit(PikaTuple* self) {
pikaList_deinit((&((self)->super)));
}
static inline Arg* pikaTuple_getArg(PikaTuple* self, int index) {
return pikaList_getArg((&((self)->super)), (index));
}
static inline size_t pikaTuple_getSize(PikaTuple* self) {
if (self == NULL) {
return 0;
}
return pikaList_getSize((&((self)->super)));
}
static inline int64_t pikaTuple_getInt(PikaTuple* self, int index) {
return pikaList_getInt((&((self)->super)), (index));
}
static inline pika_float pikaTuple_getFloat(PikaTuple* self, int index) {
return pikaList_getFloat((&((self)->super)), (index));
}
static inline char* pikaTuple_getStr(PikaTuple* self, int index) {
return pikaList_getStr((&((self)->super)), (index));
}
static inline void* pikaTuple_getPtr(PikaTuple* self, int index) {
return pikaList_getPtr((&((self)->super)), (index));
}
static inline ArgType pikaTuple_getType(PikaTuple* self, int index) {
return pikaList_getType((&((self)->super)), (index));
}
PikaList* New_pikaList(void);
PikaTuple* New_pikaTuple(void);
PikaTuple* args_getTuple(Args* self, char* name);
PikaDict* args_getDict(Args* self, char* name);
char* strsFormatList(Args* out_buffs, char* fmt, PikaList* list);
char* args_cacheStr(Args* self, char* str);
char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg);

View File

@ -453,46 +453,3 @@ int strOnly(char* string, char ch) {
}
return 1;
}
char* strFindIgnoreQuoted(char* haystack, char* needle) {
int haystack_len = strlen(haystack);
int needle_len = strlen(needle);
int in_single_quotes = 0, in_double_quotes = 0, escaped = 0;
int i, j;
if (needle_len == 0)
return haystack;
if (haystack_len < needle_len)
return NULL;
for (i = 0, j = 0; i < haystack_len; ++i) {
char c = haystack[i];
if (escaped) {
escaped = 0;
continue;
}
if (c == '\\') {
escaped = 1;
continue;
}
if (c == '\'' && !in_double_quotes) {
in_single_quotes = !in_single_quotes;
continue;
}
if (c == '\"' && !in_single_quotes) {
in_double_quotes = !in_double_quotes;
continue;
}
if (!in_single_quotes && !in_double_quotes) {
if (c == needle[j]) {
if (j == needle_len - 1) {
return haystack + i - needle_len + 1;
}
++j;
} else {
j = 0;
}
}
}
return NULL;
}

View File

@ -74,7 +74,6 @@ int strGetIndent(char* string);
int strIsBlank(char* string);
int strOnly(char* string, char ch);
int _strCountSign(char* strIn, char sign, pika_bool bracketDepth0);
char* strFindIgnoreQuoted(char* haystack, char* needle);
#endif
#ifdef __cplusplus