229 lines
7.4 KiB
C
Raw Normal View History

2021-10-01 00:21:50 +08:00
#include "BaseObj.h"
2021-12-27 21:57:08 +08:00
#include "PikaStdLib_RangeObj.h"
#include "PikaStdLib_StringObj.h"
2021-10-01 00:21:50 +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;
}
}
2021-11-20 09:33:38 +08:00
void PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
2021-10-01 00:21:50 +08:00
if (NULL == arg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return;
}
ArgType type = arg_getType(arg);
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_INT == type) {
obj_setSysOut(self, "int");
return;
}
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_FLOAT == type) {
obj_setSysOut(self, "float");
return;
}
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_STRING == type) {
obj_setSysOut(self, "string");
return;
}
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_POINTER == type) {
obj_setSysOut(self, "pointer");
return;
}
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_MATE_OBJECT == type) {
obj_setSysOut(self, "mate_object");
return;
}
2022-03-14 13:53:52 +08:00
if (ARG_TYPE_NATIVE_METHOD == type) {
obj_setSysOut(self, "method");
return;
}
2021-10-01 00:21:50 +08:00
}
2021-11-20 09:28:21 +08:00
float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {
ArgType type = arg_getType(arg);
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_INT == type) {
2021-11-20 09:28:21 +08:00
return (float)arg_getInt(arg);
}
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_FLOAT == type) {
2021-11-20 09:28:21 +08:00
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 10:34:33 +08:00
if (ARG_TYPE_INT == type) {
2021-11-20 09:28:21 +08:00
return (int)arg_getInt(arg);
}
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_FLOAT == type) {
2021-11-20 09:28:21 +08:00
return (int)arg_getFloat(arg);
}
if (ARG_TYPE_STRING == type) {
return (int)fast_atoi(arg_getStr(arg));
}
2021-11-20 09:28:21 +08:00
obj_setSysOut(self, "[error] convert to int type faild.");
obj_setErrorCode(self, 1);
return -999999999;
2021-12-24 22:44:56 +08:00
}
2022-01-09 00:12:38 +08:00
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
ArgType type = arg_getType(arg);
2022-03-18 23:00:51 +08:00
Args buffs = {0};
2022-01-09 00:12:38 +08:00
char* res = NULL;
do {
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_INT == type) {
2022-01-09 00:12:38 +08:00
int val = arg_getInt(arg);
res = strsFormat(&buffs, 11, "%d", val);
2022-01-09 00:12:38 +08:00
break;
}
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_FLOAT == type) {
2022-01-09 00:12:38 +08:00
float val = arg_getFloat(arg);
res = strsFormat(&buffs, 11, "%f", val);
2022-01-09 00:12:38 +08:00
break;
}
} while (0);
obj_setStr(self, "__strtmp", res);
strsDeinit(&buffs);
2022-01-09 00:12:38 +08:00
return obj_getStr(self, "__strtmp");
}
2021-12-27 21:57:08 +08:00
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {
/* a String, return a StringObj */
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_STRING == arg_getType(arg)) {
obj_setStr(self, "_sobj", arg_getStr(arg));
return arg_setMetaObj("", "PikaStdLib_StringObj",
New_PikaStdLib_StringObj);
}
/* a MATE object, return itself */
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_MATE_OBJECT == arg_getType(arg)) {
2021-12-27 23:54:31 +08:00
return arg_copy(arg);
2021-12-27 23:17:21 +08:00
}
2021-12-27 23:54:31 +08:00
/* object */
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_POINTER == arg_getType(arg)) {
2021-12-27 23:17:21 +08:00
PikaObj* arg_obj = arg_getPtr(arg);
2022-03-18 23:00:51 +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);
2021-12-27 23:17:21 +08:00
return arg_copy(args_getArg(arg_obj->list, "__res"));
}
return arg_setNull(NULL);
2021-12-24 22:44:56 +08:00
}
2021-12-28 00:49:13 +08:00
2022-03-29 18:02:55 +08:00
Arg* PikaStdLib_SysObj_range(PikaObj* self, int a1, int a2) {
/* set template arg to create rangeObj */
obj_setInt(self, "_r1", a1);
obj_setInt(self, "_r2", a2);
2021-12-27 21:57:08 +08:00
return arg_setMetaObj("", "PikaStdLib_RangeObj", New_PikaStdLib_RangeObj);
}
Arg* PikaStdLib_SysObj___get__(PikaObj* self, Arg* key, Arg* obj) {
ArgType obj_type = arg_getType(obj);
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_STRING == obj_type) {
2022-01-18 21:55:08 +08:00
int index = arg_getInt(key);
char* str_pyload = arg_getStr(obj);
char char_buff[] = " ";
char_buff[0] = str_pyload[index];
return arg_setStr(NULL, "", char_buff);
}
2022-03-02 10:34:33 +08:00
if ((ARG_TYPE_OBJECT == obj_type) || (ARG_TYPE_POINTER == obj_type)) {
PikaObj* arg_obj = arg_getPtr(obj);
obj_setArg(arg_obj, "__key", key);
2022-03-18 23:00:51 +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);
return arg_copy(args_getArg(arg_obj->list, "__res"));
}
return arg_setNull(NULL);
}
2022-01-18 23:37:48 +08:00
void PikaStdLib_SysObj___set__(PikaObj* self,
Arg* key,
Arg* obj,
char* obj_str,
Arg* val) {
ArgType obj_type = arg_getType(obj);
2022-03-02 10:34:33 +08:00
if (ARG_TYPE_STRING == obj_type) {
2022-01-18 21:55:08 +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];
2022-01-18 23:37:48 +08:00
obj_setStr(self, obj_str, str_pyload);
2022-01-18 21:55:08 +08:00
}
2022-03-02 10:34:33 +08:00
if ((ARG_TYPE_OBJECT == obj_type) || (ARG_TYPE_POINTER == obj_type)) {
PikaObj* arg_obj = arg_getPtr(obj);
obj_setArg(arg_obj, "__key", key);
obj_setArg(arg_obj, "__val", val);
2022-03-18 23:00:51 +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);
}
}
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 12:03:10 +08:00
Arg* PikaStdLib_SysObj_list(PikaObj *self){
2022-04-14 15:58:24 +08:00
#if PIKA_BUILTIN_LIST_ENBALE
PikaObj *New_PikaStdData_List(Args *args);
2022-04-13 12:03:10 +08:00
return obj_newObjInPackage(New_PikaStdData_List);
2022-04-14 15:58:24 +08:00
#endif
obj_setErrorCode(self, 1);
__platform_printf("[Error] list: built-in list is not enabled.\r\n");
return arg_setNull(NULL);
2022-04-13 12:03:10 +08:00
}