mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
add type check for operand
support int**float (need PikaMath enabled)
This commit is contained in:
parent
c056b6015d
commit
6c6304a896
@ -1,2 +1,3 @@
|
|||||||
#define PIKA_STACK_BUFF_SIZE 1024 * 1024
|
#define PIKA_STACK_BUFF_SIZE 1024 * 1024
|
||||||
#define PIKA_ASSERT_ENABLE 1
|
#define PIKA_ASSERT_ENABLE 1
|
||||||
|
#define PIKA_MATH_ENABLE 1
|
@ -1038,3 +1038,17 @@ TEST(VM, tuple_literal) {
|
|||||||
EXPECT_EQ(pikaMemNow(), 0);
|
EXPECT_EQ(pikaMemNow(), 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
TEST(VM, dvd_opt) {
|
||||||
|
char* line =
|
||||||
|
"a = 10%4\n";
|
||||||
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
||||||
|
obj_run(self, line);
|
||||||
|
/* collect */
|
||||||
|
int a = obj_getInt(self, "a");
|
||||||
|
/* assert */
|
||||||
|
EXPECT_EQ(a, 2);
|
||||||
|
/* deinit */
|
||||||
|
obj_deinit(self);
|
||||||
|
EXPECT_EQ(pikaMemNow(), 0);
|
||||||
|
}
|
||||||
|
72
src/PikaVM.c
72
src/PikaVM.c
@ -1158,7 +1158,14 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) {
|
|||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu("%", data)) {
|
if (strEqu("%", data)) {
|
||||||
outArg = arg_setInt(outArg, "", num1_i % num2_i);
|
if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) {
|
||||||
|
outArg = arg_setInt(outArg, "", num1_i % num2_i);
|
||||||
|
goto OPT_exit;
|
||||||
|
}
|
||||||
|
VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED);
|
||||||
|
__platform_printf(
|
||||||
|
"TypeError: unsupported operand type(s) for %: 'float'\n");
|
||||||
|
outArg = NULL;
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu("**", data)) {
|
if (strEqu("**", data)) {
|
||||||
@ -1177,16 +1184,28 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) {
|
|||||||
outArg = arg_setFloat(outArg, "", res);
|
outArg = arg_setFloat(outArg, "", res);
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
} else {
|
} else {
|
||||||
float res = 1;
|
|
||||||
#if PIKA_MATH_ENABLE
|
#if PIKA_MATH_ENABLE
|
||||||
|
float res = 1;
|
||||||
res = pow(num1_f, num2_f);
|
res = pow(num1_f, num2_f);
|
||||||
#endif
|
|
||||||
outArg = arg_setFloat(outArg, "", res);
|
outArg = arg_setFloat(outArg, "", res);
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
|
#else
|
||||||
|
VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED);
|
||||||
|
__platform_printf(
|
||||||
|
"Operation float ** float is not enabled, please set "
|
||||||
|
"PIKA_MATH_ENABLE\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (strEqu("//", data)) {
|
if (strEqu("//", data)) {
|
||||||
outArg = arg_setInt(outArg, "", num1_i / num2_i);
|
if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) {
|
||||||
|
outArg = arg_setInt(outArg, "", num1_i / num2_i);
|
||||||
|
goto OPT_exit;
|
||||||
|
}
|
||||||
|
VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED);
|
||||||
|
__platform_printf(
|
||||||
|
"TypeError: unsupported operand type(s) for //: 'float'\n");
|
||||||
|
outArg = NULL;
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu("==", data) || strEqu("!=", data)) {
|
if (strEqu("==", data) || strEqu("!=", data)) {
|
||||||
@ -1252,23 +1271,58 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) {
|
|||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu("&", data)) {
|
if (strEqu("&", data)) {
|
||||||
outArg = arg_setInt(outArg, "", num1_i & num2_i);
|
if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) {
|
||||||
|
outArg = arg_setInt(outArg, "", num1_i & num2_i);
|
||||||
|
goto OPT_exit;
|
||||||
|
}
|
||||||
|
VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED);
|
||||||
|
__platform_printf(
|
||||||
|
"TypeError: unsupported operand type(s) for &: 'float'\n");
|
||||||
|
outArg = NULL;
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu("|", data)) {
|
if (strEqu("|", data)) {
|
||||||
outArg = arg_setInt(outArg, "", num1_i | num2_i);
|
if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) {
|
||||||
|
outArg = arg_setInt(outArg, "", num1_i | num2_i);
|
||||||
|
goto OPT_exit;
|
||||||
|
}
|
||||||
|
VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED);
|
||||||
|
__platform_printf(
|
||||||
|
"TypeError: unsupported operand type(s) for |: 'float'\n");
|
||||||
|
outArg = NULL;
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu("~", data)) {
|
if (strEqu("~", data)) {
|
||||||
outArg = arg_setInt(outArg, "", ~num2_i);
|
if (type_arg2 == ARG_TYPE_INT) {
|
||||||
|
outArg = arg_setInt(outArg, "", ~num2_i);
|
||||||
|
goto OPT_exit;
|
||||||
|
}
|
||||||
|
VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED);
|
||||||
|
__platform_printf(
|
||||||
|
"TypeError: unsupported operand type(s) for ~: 'float'\n");
|
||||||
|
outArg = NULL;
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu(">>", data)) {
|
if (strEqu(">>", data)) {
|
||||||
outArg = arg_setInt(outArg, "", num1_i >> num2_i);
|
if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) {
|
||||||
|
outArg = arg_setInt(outArg, "", num1_i >> num2_i);
|
||||||
|
goto OPT_exit;
|
||||||
|
}
|
||||||
|
VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED);
|
||||||
|
__platform_printf(
|
||||||
|
"TypeError: unsupported operand type(s) for >>: 'float'\n");
|
||||||
|
outArg = NULL;
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu("<<", data)) {
|
if (strEqu("<<", data)) {
|
||||||
outArg = arg_setInt(outArg, "", num1_i << num2_i);
|
if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) {
|
||||||
|
outArg = arg_setInt(outArg, "", num1_i << num2_i);
|
||||||
|
goto OPT_exit;
|
||||||
|
}
|
||||||
|
VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED);
|
||||||
|
__platform_printf(
|
||||||
|
"TypeError: unsupported operand type(s) for <<: 'float'\n");
|
||||||
|
outArg = NULL;
|
||||||
goto OPT_exit;
|
goto OPT_exit;
|
||||||
}
|
}
|
||||||
if (strEqu(" and ", data)) {
|
if (strEqu(" and ", data)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user