support type(bool)

This commit is contained in:
lyon 2023-02-18 16:50:24 +08:00
parent 85d989028e
commit 2aafbbdc84
5 changed files with 22 additions and 20 deletions

View File

@ -107,11 +107,10 @@ typedef enum {
/* clang-format off */ /* clang-format off */
/* pikascript bool type */ /* pikascript bool type */
typedef enum { #define PIKA_BOOL int64_t
PIKA_TRUE = 1, #define PIKA_TRUE 1
PIKA_FALSE = 0, #define PIKA_FALSE 0
_PIKA_BOOL_ERR = -1, #define _PIKA_BOOL_ERR -1
} PIKA_BOOL;
#define _PIKA_INT_ERR (-999999999) #define _PIKA_INT_ERR (-999999999)
#define _PIKA_FLOAT_ERR (-999999999.0) #define _PIKA_FLOAT_ERR (-999999999.0)

View File

@ -2406,9 +2406,9 @@ static void _OPT_EQU(OperatorInfo* op) {
goto exit; goto exit;
exit: exit:
if (op->opt[0] == '=') { if (op->opt[0] == '=') {
op->res = arg_setInt(op->res, "", is_equ); op->res = arg_setBool(op->res, "", is_equ);
} else { } else {
op->res = arg_setInt(op->res, "", !is_equ); op->res = arg_setBool(op->res, "", !is_equ);
} }
return; return;
} }
@ -2492,7 +2492,7 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
if (data[1] == 0) { if (data[1] == 0) {
switch (data[0]) { switch (data[0]) {
case '<': case '<':
op.res = arg_setInt(op.res, "", op.f1 < op.f2); op.res = arg_setBool(op.res, "", op.f1 < op.f2);
goto exit; goto exit;
case '*': case '*':
if (op.num == 1) { if (op.num == 1) {
@ -2554,9 +2554,9 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
if (data[1] == 'i' && data[2] == 'n') { if (data[1] == 'i' && data[2] == 'n') {
if (op.t1 == ARG_TYPE_STRING && op.t2 == ARG_TYPE_STRING) { if (op.t1 == ARG_TYPE_STRING && op.t2 == ARG_TYPE_STRING) {
if (strstr(arg_getStr(op.a2), arg_getStr(op.a1))) { if (strstr(arg_getStr(op.a2), arg_getStr(op.a1))) {
op.res = arg_setInt(op.res, "", 1); op.res = arg_setBool(op.res, "", PIKA_TRUE);
} else { } else {
op.res = arg_setInt(op.res, "", 0); op.res = arg_setBool(op.res, "", PIKA_FALSE);
} }
goto exit; goto exit;
} }
@ -2584,7 +2584,7 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
0x00, /* const pool */ 0x00, /* const pool */
}; };
pikaVM_runByteCode(obj2, (uint8_t*)bytes); pikaVM_runByteCode(obj2, (uint8_t*)bytes);
op.res = arg_setInt(op.res, "", obj_getInt(obj2, "__res")); op.res = arg_setBool(op.res, "", obj_getInt(obj2, "__res"));
goto exit; goto exit;
} }
} }
@ -2628,14 +2628,14 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
goto exit; goto exit;
} }
if (data[0] == '>' && data[1] == '=') { if (data[0] == '>' && data[1] == '=') {
op.res = arg_setInt( op.res = arg_setBool(
op.res, "", op.res, "",
(op.f1 > op.f2) || (op.f1 > op.f2) ||
((op.f1 - op.f2) * (op.f1 - op.f2) < (pika_float)0.000001)); ((op.f1 - op.f2) * (op.f1 - op.f2) < (pika_float)0.000001));
goto exit; goto exit;
} }
if (data[0] == '<' && data[1] == '=') { if (data[0] == '<' && data[1] == '=') {
op.res = arg_setInt( op.res = arg_setBool(
op.res, "", op.res, "",
(op.f1 < op.f2) || (op.f1 < op.f2) ||
((op.f1 - op.f2) * (op.f1 - op.f2) < (pika_float)0.000001)); ((op.f1 - op.f2) * (op.f1 - op.f2) < (pika_float)0.000001));
@ -2665,17 +2665,17 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
} }
if (data[0] == ' ' && data[1] == 'a' && data[2] == 'n' && data[3] == 'd' && if (data[0] == ' ' && data[1] == 'a' && data[2] == 'n' && data[3] == 'd' &&
data[4] == ' ') { data[4] == ' ') {
op.res = arg_setInt(op.res, "", op.i1 && op.i2); op.res = arg_setBool(op.res, "", op.i1 && op.i2);
goto exit; goto exit;
} }
if (data[0] == ' ' && data[1] == 'o' && data[2] == 'r' && data[3] == ' ' && if (data[0] == ' ' && data[1] == 'o' && data[2] == 'r' && data[3] == ' ' &&
data[4] == 0) { data[4] == 0) {
op.res = arg_setInt(op.res, "", op.i1 || op.i2); op.res = arg_setBool(op.res, "", op.i1 || op.i2);
goto exit; goto exit;
} }
if (data[0] == ' ' && data[1] == 'n' && data[2] == 'o' && data[3] == 't' && if (data[0] == ' ' && data[1] == 'n' && data[2] == 'o' && data[3] == 't' &&
data[4] == ' ' && data[5] == 0) { data[4] == ' ' && data[5] == 0) {
op.res = arg_setInt(op.res, "", !op.i2); op.res = arg_setBool(op.res, "", !op.i2);
goto exit; goto exit;
} }
exit: exit:

View File

@ -309,6 +309,7 @@ Arg* arg_toStrArg(Arg* arg) {
if (type == ARG_TYPE_METHOD_NATIVE) { if (type == ARG_TYPE_METHOD_NATIVE) {
MethodProp* method_store = (MethodProp*)arg_getContent(arg); MethodProp* method_store = (MethodProp*)arg_getContent(arg);
if (strEqu(method_store->name, "int") || if (strEqu(method_store->name, "int") ||
strEqu(method_store->name, "bool") ||
strEqu(method_store->name, "float") || strEqu(method_store->name, "float") ||
strEqu(method_store->name, "str") || strEqu(method_store->name, "str") ||
strEqu(method_store->name, "bytes") || strEqu(method_store->name, "bytes") ||

View File

@ -195,6 +195,8 @@ int64_t args_getInt(Args* self, char* name) {
return arg_getInt(arg); return arg_getInt(arg);
} else if (arg_type == ARG_TYPE_FLOAT) { } else if (arg_type == ARG_TYPE_FLOAT) {
return (int)arg_getFloat(arg); return (int)arg_getFloat(arg);
} else if (arg_type == ARG_TYPE_BOOL) {
return arg_getBool(arg);
} }
return _PIKA_INT_ERR; return _PIKA_INT_ERR;
} }

View File

@ -1082,10 +1082,10 @@ TEST(VM, is) {
obj_run(self, line); obj_run(self, line);
/* collect */ /* collect */
/* assert */ /* assert */
EXPECT_EQ(obj_getInt(self, "res1"), 1); EXPECT_EQ(obj_getBool(self, "res1"), PIKA_TRUE);
EXPECT_EQ(obj_getInt(self, "res2"), 1); EXPECT_EQ(obj_getBool(self, "res2"), PIKA_TRUE);
EXPECT_EQ(obj_getInt(self, "res3"), 1); EXPECT_EQ(obj_getBool(self, "res3"), PIKA_TRUE);
EXPECT_EQ(obj_getInt(self, "res4"), 0); EXPECT_EQ(obj_getBool(self, "res4"), PIKA_FALSE);
/* deinit */ /* deinit */
obj_deinit(self); obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0); EXPECT_EQ(pikaMemNow(), 0);