From bcef9b17ba24225d5608ab0feaa1cd3504195f83 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Fri, 10 Nov 2023 22:57:41 +0800 Subject: [PATCH] upd: fixing compare --- src/utils.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 12 deletions(-) diff --git a/src/utils.cpp b/src/utils.cpp index bcf923c..c18b081 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -410,9 +410,18 @@ bool Greater(std::any a, std::any b) { return (*ptr_a_float) > (*ptr_b_float); else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr)) return (*ptr_a_string) > (*ptr_b_string); - else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) + else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) { + if (std::any_cast(&b) && *ptr_a_string != "") return true; + if (ptr_a_bool != nullptr) { + b = Any2Bool(b); + return int(*ptr_a_bool) > int(std::any_cast(b)); + } + if (ptr_b_bool != nullptr) { + a = Any2Bool(a); + return int(std::any_cast(a)) > int(*ptr_b_bool); + } return false; - else + } else throw FatalError("Greater: Type Error", 54); } bool Less(std::any a, std::any b) { @@ -433,12 +442,25 @@ bool Less(std::any a, std::any b) { return (*ptr_a_float) < (*ptr_b_float); else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr)) return (*ptr_a_string) < (*ptr_b_string); - else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) + else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) { + if (std::any_cast(&a) && *ptr_b_string != "") return true; + if (ptr_a_bool != nullptr) { + b = Any2Bool(b); + return int(*ptr_a_bool) < int(std::any_cast(b)); + } + if (ptr_b_bool != nullptr) { + a = Any2Bool(a); + return int(std::any_cast(a)) < int(*ptr_b_bool); + } return false; - else + } else throw FatalError("Less: Type Error", 55); } bool Equal(std::any a, std::any b) { + if ((std::any_cast(&a) != nullptr ? 1 : 0) + + (std::any_cast(&b) != nullptr ? 1 : 0) == + 1) + return false; ConverToSameArithType(a, b, true); bool *ptr_a_bool = std::any_cast(&a); bool *ptr_b_bool = std::any_cast(&b); @@ -456,12 +478,24 @@ bool Equal(std::any a, std::any b) { return (*ptr_a_float) == (*ptr_b_float); else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr)) return (*ptr_a_string) == (*ptr_b_string); - else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) + else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) { + if (ptr_a_bool != nullptr) { + b = Any2Bool(b); + return int(*ptr_a_bool) == int(std::any_cast(b)); + } + if (ptr_b_bool != nullptr) { + a = Any2Bool(a); + return int(std::any_cast(a)) == int(*ptr_b_bool); + } return false; - else + } else throw FatalError("Equal: Type Error", 56); } bool NotEqual(std::any a, std::any b) { + if ((std::any_cast(&a) != nullptr ? 1 : 0) + + (std::any_cast(&b) != nullptr ? 1 : 0) == + 1) + return true; ConverToSameArithType(a, b, true); bool *ptr_a_bool = std::any_cast(&a); bool *ptr_b_bool = std::any_cast(&b); @@ -479,12 +513,25 @@ bool NotEqual(std::any a, std::any b) { return (*ptr_a_float) != (*ptr_b_float); else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr)) return (*ptr_a_string) != (*ptr_b_string); - else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) + else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) { + if (ptr_a_bool != nullptr) { + b = Any2Bool(b); + return int(*ptr_a_bool) != int(std::any_cast(b)); + } + if (ptr_b_bool != nullptr) { + a = Any2Bool(a); + return int(std::any_cast(a)) != int(*ptr_b_bool); + } return false; - else + } else throw FatalError("NotEqual: Type Error", 57); } bool GreaterEqual(std::any a, std::any b) { + int cnt = (std::any_cast(&a) != nullptr ? 1 : 0) + + (std::any_cast(&b) != nullptr ? 1 : 0); + if (cnt == 2) return true; + if (std::any_cast(&a)) a = (bool)false; + if (std::any_cast(&b)) b = (bool)false; ConverToSameArithType(a, b, true); bool *ptr_a_bool = std::any_cast(&a); bool *ptr_b_bool = std::any_cast(&b); @@ -502,12 +549,26 @@ bool GreaterEqual(std::any a, std::any b) { return (*ptr_a_float) >= (*ptr_b_float); else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr)) return (*ptr_a_string) >= (*ptr_b_string); - else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) + else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) { + if (std::any_cast(&b)) return true; + if (ptr_a_bool != nullptr) { + b = Any2Bool(b); + return int(*ptr_a_bool) >= int(std::any_cast(b)); + } + if (ptr_b_bool != nullptr) { + a = Any2Bool(a); + return int(std::any_cast(a)) >= int(*ptr_b_bool); + } return false; - else + } else throw FatalError("GreaterEqual: Type Error", 58); } bool LessEqual(std::any a, std::any b) { + int cnt = (std::any_cast(&a) != nullptr ? 1 : 0) + + (std::any_cast(&b) != nullptr ? 1 : 0); + if (cnt == 2) return true; + if (std::any_cast(&a)) a = (bool)false; + if (std::any_cast(&b)) b = (bool)false; ConverToSameArithType(a, b, true); bool *ptr_a_bool = std::any_cast(&a); bool *ptr_b_bool = std::any_cast(&b); @@ -525,8 +586,17 @@ bool LessEqual(std::any a, std::any b) { return (*ptr_a_float) <= (*ptr_b_float); else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr)) return (*ptr_a_string) <= (*ptr_b_string); - else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) + else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) { + if (std::any_cast(&a)) return true; + if (ptr_a_bool != nullptr) { + b = Any2Bool(b); + return int(*ptr_a_bool) <= int(std::any_cast(b)); + } + if (ptr_b_bool != nullptr) { + a = Any2Bool(a); + return int(std::any_cast(a)) <= int(*ptr_b_bool); + } return false; - else + } else throw FatalError("LessEqual: Type Error", 59); } \ No newline at end of file