mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
fix fn and arg scope, support type() for basic arg
This commit is contained in:
parent
05c2f1e808
commit
68fac66530
@ -19,6 +19,9 @@ class SysObj:
|
||||
@staticmethod
|
||||
def int(arg: any) -> int: ...
|
||||
|
||||
@staticmethod
|
||||
def bool(arg: any) -> int: ...
|
||||
|
||||
@staticmethod
|
||||
def float(arg: any) -> float: ...
|
||||
|
||||
|
@ -28,13 +28,13 @@ Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
|
||||
}
|
||||
ArgType type = arg_getType(arg);
|
||||
if (ARG_TYPE_INT == type) {
|
||||
return arg_newStr("<class 'int'>");
|
||||
return arg_copy(obj_getMethodArg(self, "int"));
|
||||
}
|
||||
if (ARG_TYPE_FLOAT == type) {
|
||||
return arg_newStr("<class 'float'>");
|
||||
return arg_copy(obj_getMethodArg(self, "float"));
|
||||
}
|
||||
if (ARG_TYPE_STRING == type) {
|
||||
return arg_newStr("<class 'str'>");
|
||||
return arg_copy(obj_getMethodArg(self, "str"));
|
||||
}
|
||||
if (argType_isObject(type)) {
|
||||
PikaObj* obj = arg_getPtr(arg);
|
||||
@ -113,6 +113,10 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
|
||||
return -999999999;
|
||||
}
|
||||
|
||||
int PikaStdLib_SysObj_bool(PikaObj *self, Arg* arg){
|
||||
return PikaStdLib_SysObj_int(self, arg);
|
||||
}
|
||||
|
||||
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
|
||||
obj_removeArg(self, "__buf");
|
||||
ArgType type = arg_getType(arg);
|
||||
|
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -11,7 +11,7 @@
|
||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||
"args": [
|
||||
// "--gtest_filter=vm.dir_issue1lk"
|
||||
"--gtest_filter=json.dumps"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
@ -19,6 +19,9 @@ class SysObj:
|
||||
@staticmethod
|
||||
def int(arg: any) -> int: ...
|
||||
|
||||
@staticmethod
|
||||
def bool(arg: any) -> int: ...
|
||||
|
||||
@staticmethod
|
||||
def float(arg: any) -> float: ...
|
||||
|
||||
|
@ -43,16 +43,16 @@ def loads(json: str) -> dict:
|
||||
def _cjson_decode(d: dict):
|
||||
if d == None:
|
||||
return cjson.Null()
|
||||
elif str(type(d)) == "<class 'int'>":
|
||||
elif type(d) == int:
|
||||
return cjson.Number(d)
|
||||
elif str(type(d)) == "<class 'float'>":
|
||||
elif type(d) == float:
|
||||
return cjson.Number(d)
|
||||
elif str(type(d)) == "<class 'bool'>":
|
||||
elif type(d) == bool:
|
||||
if d:
|
||||
return cjson.True_()
|
||||
else:
|
||||
return cjson.False_()
|
||||
elif str(type(d)) == "<class 'str'>":
|
||||
elif type(d) == str:
|
||||
return cjson.String(d)
|
||||
elif str(type(d)) == "<class 'list'>":
|
||||
res = cjson.Array()
|
||||
|
@ -28,13 +28,13 @@ Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
|
||||
}
|
||||
ArgType type = arg_getType(arg);
|
||||
if (ARG_TYPE_INT == type) {
|
||||
return arg_newStr("<class 'int'>");
|
||||
return arg_copy(obj_getMethodArg(self, "int"));
|
||||
}
|
||||
if (ARG_TYPE_FLOAT == type) {
|
||||
return arg_newStr("<class 'float'>");
|
||||
return arg_copy(obj_getMethodArg(self, "float"));
|
||||
}
|
||||
if (ARG_TYPE_STRING == type) {
|
||||
return arg_newStr("<class 'str'>");
|
||||
return arg_copy(obj_getMethodArg(self, "str"));
|
||||
}
|
||||
if (argType_isObject(type)) {
|
||||
PikaObj* obj = arg_getPtr(arg);
|
||||
@ -113,6 +113,10 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
|
||||
return -999999999;
|
||||
}
|
||||
|
||||
int PikaStdLib_SysObj_bool(PikaObj *self, Arg* arg){
|
||||
return PikaStdLib_SysObj_int(self, arg);
|
||||
}
|
||||
|
||||
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
|
||||
obj_removeArg(self, "__buf");
|
||||
ArgType type = arg_getType(arg);
|
||||
|
22
src/PikaVM.c
22
src/PikaVM.c
@ -708,13 +708,6 @@ static Arg* VM_instruction_handler_REF(PikaObj* self,
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* host_object is self */
|
||||
if (NULL == host_object) {
|
||||
if (!strIsContain(arg_path, '.')) {
|
||||
host_object = vm->locals;
|
||||
}
|
||||
}
|
||||
|
||||
/* find in local list first */
|
||||
if (NULL == host_object) {
|
||||
host_object = obj_getHostObjWithIsTemp(vm->locals, arg_path, &is_temp);
|
||||
@ -750,11 +743,15 @@ static Arg* VM_instruction_handler_REF(PikaObj* self,
|
||||
res = args_getArg(vm->globals->list, arg_name);
|
||||
}
|
||||
|
||||
/* find res in globals prop */
|
||||
if (NULL == res) {
|
||||
res = _obj_getProp(vm->globals, arg_name);
|
||||
}
|
||||
|
||||
/* proxy */
|
||||
if (NULL == res) {
|
||||
res = _proxy_getattr(host_object, arg_name);
|
||||
}
|
||||
|
||||
exit:
|
||||
if (NULL == res) {
|
||||
VMState_setErrorCode(vm, PIKA_RES_ERR_ARG_NO_FOUND);
|
||||
@ -1567,7 +1564,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
method_host = obj_getHostObjWithIsTemp(vm->locals, run_path, &is_temp);
|
||||
}
|
||||
|
||||
/* get method host obj from local scope */
|
||||
/* get method host obj from global scope */
|
||||
if (NULL == method_host) {
|
||||
method_host = obj_getHostObjWithIsTemp(vm->globals, run_path, &is_temp);
|
||||
}
|
||||
@ -1606,6 +1603,9 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
/* get method in global */
|
||||
if (NULL == method) {
|
||||
method = obj_getMethodArg_noalloc(vm->globals, run_path, &arg_reg1);
|
||||
if (method != NULL) {
|
||||
obj_this = vm->globals;
|
||||
}
|
||||
}
|
||||
|
||||
/* assert method exist */
|
||||
@ -2263,6 +2263,10 @@ static void _OPT_EQU(OperatorInfo* op) {
|
||||
}
|
||||
goto exit;
|
||||
}
|
||||
if (argType_isCallable(op->t1) && argType_isCallable(op->t2)) {
|
||||
is_equ = (arg_getPtr(op->a1) == arg_getPtr(op->a2));
|
||||
goto exit;
|
||||
}
|
||||
/* default: int and float */
|
||||
is_equ = ((op->f1 - op->f2) * (op->f1 - op->f2) < (pika_float)0.000001);
|
||||
goto exit;
|
||||
|
@ -2526,6 +2526,66 @@ TEST(vm, dir_issue1lk) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(vm, type_int) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain,
|
||||
"i = type(1)\n"
|
||||
"ii = i(10)\n");
|
||||
/* collect */
|
||||
EXPECT_EQ(obj_getInt(pikaMain, "ii"), 10);
|
||||
/* assert */
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(vm, method_int) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain,
|
||||
"i = int\n"
|
||||
"ii = i(10)\n");
|
||||
/* collect */
|
||||
/* assert */
|
||||
EXPECT_EQ(obj_getInt(pikaMain, "ii"), 10);
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(vm, fn_method_int) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain,
|
||||
"def test():\n"
|
||||
" i = int\n"
|
||||
" ii = i(10)\n"
|
||||
" return ii\n"
|
||||
"ii = test()\n");
|
||||
/* collect */
|
||||
/* assert */
|
||||
EXPECT_EQ(obj_getInt(pikaMain, "ii"), 10);
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
TEST_END
|
@ -1,5 +1,7 @@
|
||||
import json
|
||||
|
||||
s0 = json.dumps(1)
|
||||
|
||||
s1 = json.dumps({"a": 1, "b": 2, "c": 3})
|
||||
print(s1)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user