pikapython/package/PikaStdLib/PikaStdLib_SysObj.c

407 lines
13 KiB
C
Raw Normal View History

2021-10-01 20:00:15 +08:00
#include "BaseObj.h"
2021-12-28 01:21:47 +08:00
#include "PikaStdLib_RangeObj.h"
2022-01-09 23:49:23 +08:00
#include "PikaStdLib_StringObj.h"
2021-10-01 20:00:15 +08:00
#include "dataStrs.h"
void PikaStdLib_SysObj_remove(PikaObj* self, char* argPath) {
obj_setErrorCode(self, 0);
int32_t res = obj_removeArg(self, argPath);
if (1 == res) {
obj_setSysOut(self, "[error] del: object no found.");
obj_setErrorCode(self, 1);
return;
}
if (2 == res) {
obj_setSysOut(self, "[error] del: arg not match.");
obj_setErrorCode(self, 2);
return;
}
}
void PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
2021-10-01 20:00:15 +08:00
if (NULL == arg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return;
}
2021-11-15 09:58:54 +08:00
ArgType type = arg_getType(arg);
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_INT == type) {
2022-04-27 23:38:00 +08:00
obj_setSysOut(self, "<class 'int'>");
2021-11-15 09:58:54 +08:00
return;
}
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_FLOAT == type) {
2022-04-27 23:38:00 +08:00
obj_setSysOut(self, "<class 'float'>");
2021-11-15 09:58:54 +08:00
return;
}
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_STRING == type) {
2022-04-27 23:38:00 +08:00
obj_setSysOut(self, "<class 'str'>");
2021-11-15 09:58:54 +08:00
return;
}
2022-04-26 10:43:50 +08:00
if (ARG_TYPE_OBJECT == type) {
2022-04-27 23:38:00 +08:00
obj_setSysOut(self, "<class 'object'>");
2021-11-15 09:58:54 +08:00
return;
}
if (ARG_TYPE_OBJECT_META == type) {
obj_setSysOut(self, "<class 'meta object'>");
2021-11-15 09:58:54 +08:00
return;
}
if (ARG_TYPE_BYTES == type) {
obj_setSysOut(self, "<class 'bytes'>");
return;
}
2022-04-27 23:38:00 +08:00
if (ARG_TYPE_METHOD_NATIVE == type) {
obj_setSysOut(self, "<class 'buitin_function_or_method'>");
return;
}
2022-04-28 00:29:32 +08:00
if (ARG_TYPE_METHOD_OBJECT == type) {
2022-04-27 23:38:00 +08:00
obj_setSysOut(self, "<class 'method'>");
return;
}
2022-04-28 00:29:32 +08:00
if (ARG_TYPE_METHOD_STATIC == type) {
2022-04-27 23:38:00 +08:00
obj_setSysOut(self, "<class 'function'>");
2021-11-15 09:58:54 +08:00
return;
}
2021-10-01 20:00:15 +08:00
}
float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {
ArgType type = arg_getType(arg);
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_INT == type) {
return (float)arg_getInt(arg);
}
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_FLOAT == type) {
return (float)arg_getFloat(arg);
}
obj_setSysOut(self, "[error] convert to float type faild.");
obj_setErrorCode(self, 1);
return -99999.99999;
}
int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
ArgType type = arg_getType(arg);
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_INT == type) {
return (int)arg_getInt(arg);
}
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_FLOAT == type) {
return (int)arg_getFloat(arg);
}
if (ARG_TYPE_STRING == type) {
return (int)fast_atoi(arg_getStr(arg));
}
2022-05-27 10:29:57 +08:00
if (ARG_TYPE_BYTES == type) {
size_t size = arg_getBytesSize(arg);
if (size != 1) {
obj_setSysOut(self, "ValueError: invalid literal for int()");
obj_setErrorCode(self, 1);
return -999999999;
}
uint8_t val = *arg_getBytes(arg);
return val;
}
obj_setSysOut(self, "[error] convert to int type faild.");
obj_setErrorCode(self, 1);
return -999999999;
2021-12-28 01:21:47 +08:00
}
2022-01-09 00:13:02 +08:00
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
ArgType type = arg_getType(arg);
2022-03-19 09:44:53 +08:00
Args buffs = {0};
2022-01-09 00:13:02 +08:00
char* res = NULL;
2022-05-25 13:57:02 +08:00
if (ARG_TYPE_INT == type) {
int val = arg_getInt(arg);
res = strsFormat(&buffs, 11, "%d", val);
goto exit;
}
if (ARG_TYPE_FLOAT == type) {
float val = arg_getFloat(arg);
res = strsFormat(&buffs, 11, "%f", val);
goto exit;
}
if (argType_isObject(type)) {
2022-05-25 13:57:02 +08:00
res = obj_toStr(arg_getPtr(arg));
if (NULL != res) {
goto exit;
2022-01-09 00:13:02 +08:00
}
2022-05-25 13:57:02 +08:00
}
exit:
obj_setStr(self, "__buf", res);
2022-03-17 16:47:03 +08:00
strsDeinit(&buffs);
2022-05-25 13:57:02 +08:00
return obj_getStr(self, "__buf");
2022-01-09 00:13:02 +08:00
}
2021-12-28 01:21:47 +08:00
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {
2022-01-09 23:49:23 +08:00
/* a String, return a StringObj */
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_STRING == arg_getType(arg)) {
2022-01-09 23:49:23 +08:00
obj_setStr(self, "_sobj", arg_getStr(arg));
2022-04-28 00:29:32 +08:00
return arg_newMetaObj(New_PikaStdLib_StringObj);
2022-01-09 23:49:23 +08:00
}
/* a MATE object, return itself */
if (ARG_TYPE_OBJECT_META == arg_getType(arg)) {
2021-12-28 01:21:47 +08:00
return arg_copy(arg);
}
/* object */
2022-04-26 10:43:50 +08:00
if (ARG_TYPE_OBJECT == arg_getType(arg)) {
2021-12-28 01:21:47 +08:00
PikaObj* arg_obj = arg_getPtr(arg);
2022-03-19 09:44:53 +08:00
// pikaVM_runAsm(arg_obj,
// "B0\n"
// "0 RUN __iter__\n"
// "0 OUT __res\n");
const uint8_t bytes[] = {
0x08, 0x00, /* instruct array size */
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0a, 0x00, /* instruct array */
0x10, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x69, 0x74, 0x65, 0x72, 0x5f,
0x5f, 0x00, 0x5f, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */
};
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
2022-04-26 10:12:34 +08:00
Arg* res = arg_copy(args_getArg(arg_obj->list, "__res"));
obj_removeArg(arg_obj, "__res");
return res;
2021-12-28 01:21:47 +08:00
}
return arg_setNull(NULL);
}
2022-03-30 01:22:25 +08:00
Arg* PikaStdLib_SysObj_range(PikaObj* self, int a1, int a2) {
2021-12-28 01:21:47 +08:00
/* set template arg to create rangeObj */
2021-12-29 01:15:05 +08:00
obj_setInt(self, "_r1", a1);
obj_setInt(self, "_r2", a2);
2022-04-28 00:29:32 +08:00
return arg_newMetaObj(New_PikaStdLib_RangeObj);
2021-12-28 01:21:47 +08:00
}
2022-01-13 21:57:32 +08:00
Arg* PikaStdLib_SysObj___get__(PikaObj* self, Arg* key, Arg* obj) {
ArgType obj_type = arg_getType(obj);
int index = 0;
if (ARG_TYPE_INT == arg_getType(key)) {
index = arg_getInt(key);
}
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_STRING == obj_type) {
2022-01-18 23:52:20 +08:00
char* str_pyload = arg_getStr(obj);
char char_buff[] = " ";
if (index < 0) {
index = strGetSize(str_pyload) + index;
}
2022-01-18 23:52:20 +08:00
char_buff[0] = str_pyload[index];
return arg_setStr(NULL, "", char_buff);
}
2022-05-21 19:23:34 +08:00
if (ARG_TYPE_BYTES == obj_type) {
uint8_t* bytes_pyload = arg_getBytes(obj);
uint8_t byte_buff[] = " ";
if (index < 0) {
index = arg_getBytesSize(obj) + index;
}
2022-05-21 19:23:34 +08:00
byte_buff[0] = bytes_pyload[index];
return arg_setBytes(NULL, "", byte_buff, 1);
}
2022-04-26 11:09:18 +08:00
if (ARG_TYPE_OBJECT == obj_type) {
2022-01-13 21:57:32 +08:00
PikaObj* arg_obj = arg_getPtr(obj);
obj_setArg(arg_obj, "__key", key);
2022-03-19 09:44:53 +08:00
// pikaVM_runAsm(arg_obj,
// "B0\n"
// "1 REF __key\n"
// "0 RUN __get__\n"
// "0 OUT __res\n");
const uint8_t bytes[] = {
0x0c, 0x00, /* instruct array size */
0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x07, 0x00, 0x00, 0x04, 0x0f,
0x00,
/* instruct array */
0x15, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x6b, 0x65, 0x79, 0x00, 0x5f, 0x5f, 0x67, 0x65,
0x74, 0x5f, 0x5f, 0x00, 0x5f, 0x5f, 0x72, 0x65, 0x73,
0x00, /* const pool */
};
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
2022-01-13 21:57:32 +08:00
return arg_copy(args_getArg(arg_obj->list, "__res"));
}
return arg_setNull(NULL);
}
2022-01-18 23:52:20 +08:00
void PikaStdLib_SysObj___set__(PikaObj* self,
Arg* key,
Arg* obj,
char* obj_str,
Arg* val) {
2022-01-13 21:57:32 +08:00
ArgType obj_type = arg_getType(obj);
2022-03-02 15:27:19 +08:00
if (ARG_TYPE_STRING == obj_type) {
2022-01-18 23:52:20 +08:00
int index = arg_getInt(key);
char* str_val = arg_getStr(val);
char* str_pyload = arg_getStr(obj);
str_pyload[index] = str_val[0];
obj_setStr(self, obj_str, str_pyload);
}
2022-05-21 19:23:34 +08:00
if (ARG_TYPE_BYTES == obj_type) {
int index = arg_getInt(key);
uint8_t* bytes_val = arg_getBytes(val);
uint8_t* bytes_pyload = arg_getBytes(obj);
size_t bytes_len = arg_getBytesSize(obj);
bytes_pyload[index] = bytes_val[0];
obj_setBytes(self, obj_str, bytes_pyload, bytes_len);
}
2022-04-26 11:09:18 +08:00
if (ARG_TYPE_OBJECT == obj_type) {
2022-01-13 21:57:32 +08:00
PikaObj* arg_obj = arg_getPtr(obj);
obj_setArg(arg_obj, "__key", key);
obj_setArg(arg_obj, "__val", val);
2022-03-19 09:44:53 +08:00
// pikaVM_runAsm(arg_obj,
// "B0\n"
// "1 REF __key\n"
// "1 REF __val\n"
// "0 RUN __set__\n");
const uint8_t bytes[] = {
0x0c, 0x00, /* instruct array size */
0x10, 0x81, 0x01, 0x00, 0x10, 0x01, 0x07, 0x00, 0x00, 0x02, 0x0d,
0x00,
/* instruct array */
0x15, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x6b, 0x65, 0x79, 0x00, 0x5f, 0x5f, 0x76, 0x61,
0x6c, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x5f,
0x00, /* const pool */
};
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
2022-01-13 21:57:32 +08:00
}
}
int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
if (ARG_TYPE_STRING == arg_getType(arg)) {
return strGetSize(arg_getStr(arg));
}
obj_setErrorCode(self, 1);
__platform_printf("[Error] len: arg type not support\r\n");
return -1;
}
2022-04-13 13:23:45 +08:00
2022-04-25 15:01:58 +08:00
Arg* PikaStdLib_SysObj_list(PikaObj* self) {
#if PIKA_BUILTIN_LIST_ENABLE
2022-04-25 15:01:58 +08:00
PikaObj* New_PikaStdData_List(Args * args);
2022-04-13 13:23:45 +08:00
return obj_newObjInPackage(New_PikaStdData_List);
2022-04-14 15:58:24 +08:00
#endif
obj_setErrorCode(self, 1);
2022-04-14 16:12:18 +08:00
__platform_printf("[Error] built-in list is not enabled.\r\n");
return arg_setNull(NULL);
}
2022-04-25 15:01:58 +08:00
Arg* PikaStdLib_SysObj_dict(PikaObj* self) {
#if PIKA_BUILTIN_DICT_ENABLE
2022-04-25 15:01:58 +08:00
PikaObj* New_PikaStdData_Dict(Args * args);
2022-04-14 16:12:18 +08:00
return obj_newObjInPackage(New_PikaStdData_Dict);
#endif
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in dist is not enabled.\r\n");
2022-04-14 15:58:24 +08:00
return arg_setNull(NULL);
2022-04-13 13:23:45 +08:00
}
2022-05-25 15:42:25 +08:00
char* PikaStdLib_SysObj_hex(PikaObj* self, int val) {
2022-05-25 15:42:25 +08:00
char buff[PIKA_SPRINTF_BUFF_SIZE] = {0};
if (val > 0) {
2022-05-25 15:42:25 +08:00
__platform_sprintf(buff, "0x%02x", val);
} else {
2022-05-25 15:42:25 +08:00
__platform_sprintf(buff, "-0x%02x", -val);
}
/* load the string from stack to heap */
obj_setStr(self, "__buf", buff);
return obj_getStr(self, "__buf");
}
int PikaStdLib_SysObj_ord(PikaObj* self, char* val) {
return (int)val[0];
}
char* PikaStdLib_SysObj_chr(PikaObj* self, int val) {
char buff[PIKA_SPRINTF_BUFF_SIZE] = {0};
char to_str[] = "0";
to_str[0] = val;
__platform_sprintf(buff, "%s", to_str);
/* load the string from stack to heap */
obj_setStr(self, "__buf", buff);
return obj_getStr(self, "__buf");
}
2022-05-26 15:46:34 +08:00
Arg* PikaStdLib_SysObj_bytes(PikaObj* self, Arg* val) {
ArgType type = arg_getType(val);
if (ARG_TYPE_INT == type) {
int size = arg_getInt(val);
/* src is NULL so the bytes are all '\0' */
Arg* bytes = arg_setBytes(NULL, "", NULL, size);
return bytes;
}
if (ARG_TYPE_BYTES == type) {
return arg_copy(val);
}
if (ARG_TYPE_STRING == type) {
int size = strGetSize(arg_getStr(val));
Arg* bytes = arg_setBytes(NULL, "", (uint8_t*)arg_getStr(val), size);
return bytes;
}
obj_setErrorCode(self, 1);
__platform_printf("Error: input arg type not supported.\r\n");
return arg_setNull(NULL);
}
Arg* PikaStdLib_SysObj___slice__(PikaObj* self,
Arg* end,
Arg* obj,
Arg* start,
int step) {
2022-05-30 16:38:38 +08:00
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
/* No interger index only support __get__ */
if (!(arg_getType(start) == ARG_TYPE_INT &&
arg_getType(end) == ARG_TYPE_INT)) {
return PikaStdLib_SysObj___get__(self, start, obj);
}
int start_i = arg_getInt(start);
int end_i = arg_getInt(end);
/* __slice__ is equal to __get__ */
if (end_i - start_i == 1) {
return PikaStdLib_SysObj___get__(self, start, obj);
}
2022-05-30 14:22:19 +08:00
if (ARG_TYPE_STRING == arg_getType(obj)) {
size_t len = strGetSize(arg_getStr(obj));
if (start_i < 0) {
start_i += len;
}
if (end_i < 0) {
end_i += len + 1;
}
2022-05-30 14:22:19 +08:00
Arg* sliced_arg = arg_setStr(NULL, "", "");
for (int i = start_i; i < end_i; i++) {
Arg* i_arg = arg_setInt(NULL, "", i);
Arg* item_arg = PikaStdLib_SysObj___get__(self, i_arg, obj);
sliced_arg = arg_strAppend(sliced_arg, arg_getStr(item_arg));
arg_deinit(item_arg);
arg_deinit(i_arg);
}
return sliced_arg;
}
if (ARG_TYPE_BYTES == arg_getType(obj)) {
size_t len = arg_getBytesSize(obj);
if (start_i < 0) {
start_i += len;
}
if (end_i < 0) {
end_i += len + 1;
}
Arg* sliced_arg = arg_setBytes(NULL, "", NULL, 0);
for (int i = start_i; i < end_i; i++) {
Arg* i_arg = arg_setInt(NULL, "", i);
Arg* item_arg = PikaStdLib_SysObj___get__(self, i_arg, obj);
uint8_t* bytes_origin = arg_getBytes(sliced_arg);
size_t size_origin = arg_getBytesSize(sliced_arg);
Arg* sliced_arg_new = arg_setBytes(NULL, "", NULL, size_origin + 1);
__platform_memcpy(arg_getBytes(sliced_arg_new), bytes_origin,
size_origin);
__platform_memcpy(arg_getBytes(sliced_arg_new) + size_origin,
arg_getBytes(item_arg), 1);
arg_deinit(sliced_arg);
sliced_arg = sliced_arg_new;
arg_deinit(item_arg);
arg_deinit(i_arg);
}
return sliced_arg;
}
2022-05-30 16:38:38 +08:00
#endif
return arg_setNull(NULL);
}