diff --git a/port/linux/config/pika_config_default.h b/port/linux/config/pika_config_default.h index 2e0831406..ef316bd22 100644 --- a/port/linux/config/pika_config_default.h +++ b/port/linux/config/pika_config_default.h @@ -1,2 +1,3 @@ #define PIKA_STACK_BUFF_SIZE 1024 * 1024 -#define PIKA_ASSERT_ENABLE 1 \ No newline at end of file +#define PIKA_ASSERT_ENABLE 1 +#define PIKA_MATH_ENABLE 1 \ No newline at end of file diff --git a/port/linux/test/VM-test.cpp b/port/linux/test/VM-test.cpp index 4a02bbeba..abed1939b 100644 --- a/port/linux/test/VM-test.cpp +++ b/port/linux/test/VM-test.cpp @@ -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); +} diff --git a/src/PikaVM.c b/src/PikaVM.c index d877cf9f1..b02220206 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -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)) {