add type check for operand

support int**float (need PikaMath enabled)
This commit is contained in:
pikastech 2022-07-30 11:59:44 +08:00
parent c056b6015d
commit 6c6304a896
3 changed files with 79 additions and 10 deletions

View File

@ -1,2 +1,3 @@
#define PIKA_STACK_BUFF_SIZE 1024 * 1024
#define PIKA_ASSERT_ENABLE 1
#define PIKA_ASSERT_ENABLE 1
#define PIKA_MATH_ENABLE 1

View File

@ -1038,3 +1038,17 @@ TEST(VM, tuple_literal) {
EXPECT_EQ(pikaMemNow(), 0);
}
#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);
}

View File

@ -1158,7 +1158,14 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) {
goto OPT_exit;
}
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;
}
if (strEqu("**", data)) {
@ -1177,16 +1184,28 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) {
outArg = arg_setFloat(outArg, "", res);
goto OPT_exit;
} else {
float res = 1;
#if PIKA_MATH_ENABLE
float res = 1;
res = pow(num1_f, num2_f);
#endif
outArg = arg_setFloat(outArg, "", res);
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)) {
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;
}
if (strEqu("==", data) || strEqu("!=", data)) {
@ -1252,23 +1271,58 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) {
goto OPT_exit;
}
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;
}
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;
}
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;
}
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;
}
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;
}
if (strEqu(" and ", data)) {