add some comments
This commit is contained in:
342
src/utils.cpp
342
src/utils.cpp
@ -1,5 +1,16 @@
|
||||
#include "utils.h"
|
||||
|
||||
/**
|
||||
* @brief definition of function Any2Int
|
||||
*
|
||||
* @param value any type value
|
||||
*
|
||||
* @return ZYM::int2048
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details convert any type value to ZYM::int2048
|
||||
*/
|
||||
inline ZYM::int2048 Any2Int(const std::any &value) {
|
||||
if (auto ptr = std::any_cast<ZYM::int2048>(&value))
|
||||
return *ptr;
|
||||
@ -27,6 +38,17 @@ inline ZYM::int2048 Any2Int(const std::any &value) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Any2Float
|
||||
*
|
||||
* @param value any type value
|
||||
*
|
||||
* @return double
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details convert any type value to double
|
||||
*/
|
||||
double Any2Float(const std::any &value) {
|
||||
if (auto ptr = std::any_cast<double>(&value))
|
||||
return *ptr;
|
||||
@ -51,6 +73,17 @@ double Any2Float(const std::any &value) {
|
||||
throw FatalError("Any2Float: unknown type", 30);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Any2String
|
||||
*
|
||||
* @param value any type value
|
||||
*
|
||||
* @return std::string
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details convert any type value to std::string
|
||||
*/
|
||||
std::string Any2String(const std::any &value) {
|
||||
std::stringstream buf;
|
||||
std::string res;
|
||||
@ -73,6 +106,17 @@ std::string Any2String(const std::any &value) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Any2Bool
|
||||
*
|
||||
* @param value any type value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details convert any type value to bool
|
||||
*/
|
||||
bool Any2Bool(const std::any &value) {
|
||||
if (auto ptr = std::any_cast<ZYM::int2048>(&value)) {
|
||||
return (*ptr) != 0;
|
||||
@ -88,6 +132,19 @@ bool Any2Bool(const std::any &value) {
|
||||
throw FatalError("Any2Bool: unknown type", 32);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function DeQuate
|
||||
*
|
||||
* @param val any type value
|
||||
*
|
||||
* @param Variables VariableContainer
|
||||
*
|
||||
* @return std::any
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details replace all RawVarible in val with its value
|
||||
*/
|
||||
std::any DeQuate(std::any val, VariableContainer &Variables) {
|
||||
if (auto ptr = std::any_cast<std::vector<std::any>>(&val)) {
|
||||
for (int i = 0; i < ptr->size(); i++)
|
||||
@ -104,6 +161,21 @@ std::any DeQuate(std::any val, VariableContainer &Variables) {
|
||||
// throw FatalError("DeQuate: unknown type");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function ConverToSameArithType
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @param allow_string_operation bool
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details convert a and b to the same type
|
||||
*/
|
||||
int ConverToSameArithType(std::any &a, std::any &b,
|
||||
bool allow_string_operation) {
|
||||
if (std::any_cast<NoneType>(&a) != nullptr &&
|
||||
@ -152,6 +224,19 @@ int ConverToSameArithType(std::any &a, std::any &b,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Add
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return std::any
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details add a and b
|
||||
*/
|
||||
std::any Add(std::any a, std::any b) {
|
||||
int status = ConverToSameArithType(a, b, true);
|
||||
bool *ptr_a_bool = std::any_cast<bool>(&a);
|
||||
@ -178,6 +263,20 @@ std::any Add(std::any a, std::any b) {
|
||||
else
|
||||
throw FatalError("Add: Type Error", 38);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Sub
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return std::any
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details sub a and b
|
||||
*/
|
||||
std::any Sub(std::any a, std::any b) {
|
||||
if (std::any_cast<std::string>(&a) != nullptr) a = ZYM::int2048(0);
|
||||
if (std::any_cast<std::string>(&b) != nullptr) b = ZYM::int2048(0);
|
||||
@ -202,6 +301,20 @@ std::any Sub(std::any a, std::any b) {
|
||||
throw FatalError("Sub: Type Error", 39);
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Mul
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return std::any
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details mul a and b
|
||||
*/
|
||||
std::any Mul(std::any a, std::any b) {
|
||||
if (std::any_cast<NoneType>(&a)) a = (bool)false;
|
||||
if (std::any_cast<NoneType>(&b)) b = (bool)false;
|
||||
@ -271,6 +384,20 @@ std::any Mul(std::any a, std::any b) {
|
||||
} else
|
||||
throw FatalError("Mul: Type Error", 42);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Div
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return std::any
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details div a and b
|
||||
*/
|
||||
std::any Div(std::any a, std::any b) {
|
||||
if (std::any_cast<NoneType>(&a)) a = (bool)false;
|
||||
if (std::any_cast<NoneType>(&b)) b = (bool)false;
|
||||
@ -286,15 +413,25 @@ std::any Div(std::any a, std::any b) {
|
||||
throw InterpretException(
|
||||
"Div: string operation not allowed in this situation", 43);
|
||||
double t_a = Any2Float(a);
|
||||
// std::cerr << t_a << std::endl;
|
||||
double t_b = Any2Float(b);
|
||||
// std::cerr << t_b << std::endl;
|
||||
// if (t_b == 0) throw InterpretException("Div: divided by zero", 44);
|
||||
t_a = t_a / t_b;
|
||||
// std::cerr << t_a << std::endl;
|
||||
if (t_a > -1e-8 && t_a < 0) t_a = 0;
|
||||
return t_a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Divv
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return std::any
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details divv a and b
|
||||
*/
|
||||
std::any Divv(std::any a, std::any b) {
|
||||
if (std::any_cast<NoneType>(&a)) a = (bool)false;
|
||||
if (std::any_cast<NoneType>(&b)) b = (bool)false;
|
||||
@ -308,22 +445,30 @@ std::any Divv(std::any a, std::any b) {
|
||||
double *ptr_a_float = std::any_cast<double>(&a);
|
||||
double *ptr_b_float = std::any_cast<double>(&b);
|
||||
if ((ptr_a_float != nullptr) && (ptr_b_float != nullptr)) {
|
||||
// if ((*ptr_b_float) == 0)
|
||||
// throw InterpretException("Divv: divided by zero", 45);
|
||||
double tmp = std::floor((*ptr_a_float) / (*ptr_b_float));
|
||||
if (tmp > -1e-8 && tmp < 0) tmp = 0;
|
||||
return tmp;
|
||||
} else if ((ptr_a_int2048 != nullptr) && (ptr_b_int2048 != nullptr)) {
|
||||
if ((*ptr_b_int2048) == 0)
|
||||
// throw InterpretException("Divv: divided by zero", 46);
|
||||
return (*ptr_a_int2048) / (*ptr_b_int2048);
|
||||
} else if ((ptr_a_bool != nullptr) && (ptr_b_bool != nullptr)) {
|
||||
if ((*ptr_b_bool) == 0)
|
||||
// throw InterpretException("Divv: divided by zero", 47);
|
||||
return ZYM::int2048(int(*ptr_a_bool) / int(*ptr_b_bool));
|
||||
} else
|
||||
throw FatalError("Divv: Type Error", 48);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Mod
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return std::any
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details mod a and b
|
||||
*/
|
||||
std::any Mod(std::any a, std::any b) {
|
||||
if (std::any_cast<NoneType>(&a)) a = (bool)false;
|
||||
if (std::any_cast<NoneType>(&b)) b = (bool)false;
|
||||
@ -336,47 +481,59 @@ std::any Mod(std::any a, std::any b) {
|
||||
double *ptr_a_float = std::any_cast<double>(&a);
|
||||
double *ptr_b_float = std::any_cast<double>(&b);
|
||||
if ((ptr_a_float != nullptr) && (ptr_b_float != nullptr)) {
|
||||
// if ((*ptr_b_float) == 0)
|
||||
// throw InterpretException("Mod: divided by zero", 49);
|
||||
double tmp = (*ptr_a_float) -
|
||||
(*ptr_b_float) * std::floor((*ptr_a_float) / (*ptr_b_float));
|
||||
if (tmp > -1e-8 && tmp < 0) tmp = 0;
|
||||
return tmp;
|
||||
} else if ((ptr_a_int2048 != nullptr) && (ptr_b_int2048 != nullptr)) {
|
||||
// if ((*ptr_b_int2048) == 0)
|
||||
// throw InterpretException("Mod: divided by zero", 50);
|
||||
} else if ((ptr_a_int2048 != nullptr) && (ptr_b_int2048 != nullptr))
|
||||
return (*ptr_a_int2048) % (*ptr_b_int2048);
|
||||
} else if ((ptr_a_bool != nullptr) && (ptr_b_bool != nullptr)) {
|
||||
// if ((*ptr_b_bool) == 0)
|
||||
// throw InterpretException("Mod: divided by zero", 51);
|
||||
else if ((ptr_a_bool != nullptr) && (ptr_b_bool != nullptr))
|
||||
return ZYM::int2048(int(*ptr_a_bool) % int(*ptr_b_bool));
|
||||
} else
|
||||
else
|
||||
throw FatalError("Mod: Type Error", 52);
|
||||
}
|
||||
|
||||
std::any &SelfAdd(std::any &a, std::any b) {
|
||||
a = Add(a, b);
|
||||
return a;
|
||||
}
|
||||
|
||||
std::any &SelfSub(std::any &a, std::any b) {
|
||||
a = Sub(a, b);
|
||||
return a;
|
||||
}
|
||||
|
||||
std::any &SelfMul(std::any &a, std::any b) {
|
||||
a = Mul(a, b);
|
||||
return a;
|
||||
}
|
||||
|
||||
std::any &SelfDiv(std::any &a, std::any b) {
|
||||
a = Div(a, b);
|
||||
return a;
|
||||
}
|
||||
|
||||
std::any &SelfDivv(std::any &a, std::any b) {
|
||||
a = Divv(a, b);
|
||||
return a;
|
||||
}
|
||||
|
||||
std::any &SelfMod(std::any &a, std::any b) {
|
||||
a = Mod(a, b);
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Neg
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @return std::any
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details get -a
|
||||
*/
|
||||
std::any Neg(std::any a) {
|
||||
if (std::any_cast<NoneType>(&a)) a = (bool)false;
|
||||
bool *ptr_a_bool = std::any_cast<bool>(&a);
|
||||
@ -392,6 +549,19 @@ std::any Neg(std::any a) {
|
||||
throw FatalError("Neg: Type Error", 53);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Greater
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details judge if a>b
|
||||
*/
|
||||
bool Greater(std::any a, std::any b) {
|
||||
ConverToSameArithType(a, b, true);
|
||||
bool *ptr_a_bool = std::any_cast<bool>(&a);
|
||||
@ -412,20 +582,10 @@ bool Greater(std::any a, std::any b) {
|
||||
return (*ptr_a_string) > (*ptr_b_string);
|
||||
else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) {
|
||||
if (std::any_cast<NoneType>(&b) && *ptr_a_string != "") return true;
|
||||
if (ptr_a_bool != nullptr) {
|
||||
b = Any2Bool(b);
|
||||
return false;
|
||||
// return int(*ptr_a_bool) > int(std::any_cast<bool>(b));
|
||||
}
|
||||
if (ptr_b_bool != nullptr) {
|
||||
a = Any2Bool(a);
|
||||
return int(std::any_cast<bool>(a)) > int(*ptr_b_bool);
|
||||
}
|
||||
if (ptr_a_int2048 != nullptr) {
|
||||
b = ZYM::int2048(Any2Bool(b));
|
||||
return false;
|
||||
// return *ptr_a_int2048 > std::any_cast<ZYM::int2048>(b);
|
||||
}
|
||||
if (ptr_b_int2048 != nullptr) {
|
||||
a = ZYM::int2048(Any2Bool(a));
|
||||
return std::any_cast<ZYM::int2048>(a) > *ptr_b_int2048;
|
||||
@ -434,6 +594,20 @@ bool Greater(std::any a, std::any b) {
|
||||
} else
|
||||
throw FatalError("Greater: Type Error", 54);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Less
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details judge if a<b
|
||||
*/
|
||||
bool Less(std::any a, std::any b) {
|
||||
ConverToSameArithType(a, b, true);
|
||||
bool *ptr_a_bool = std::any_cast<bool>(&a);
|
||||
@ -458,24 +632,28 @@ bool Less(std::any a, std::any b) {
|
||||
b = Any2Bool(b);
|
||||
return int(*ptr_a_bool) < int(std::any_cast<bool>(b));
|
||||
}
|
||||
if (ptr_b_bool != nullptr) {
|
||||
a = Any2Bool(a);
|
||||
return 0;
|
||||
// return int(std::any_cast<bool>(a)) < int(*ptr_b_bool);
|
||||
}
|
||||
if (ptr_a_int2048 != nullptr) {
|
||||
b = ZYM::int2048(Any2Bool(b));
|
||||
return *ptr_a_int2048 < std::any_cast<ZYM::int2048>(b);
|
||||
}
|
||||
if (ptr_b_int2048 != nullptr) {
|
||||
a = ZYM::int2048(Any2Bool(a));
|
||||
return 0;
|
||||
// return std::any_cast<ZYM::int2048>(a) < *ptr_b_int2048;
|
||||
}
|
||||
return false;
|
||||
} else
|
||||
throw FatalError("Less: Type Error", 55);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function Equal
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details judge if a==b
|
||||
*/
|
||||
bool Equal(std::any a, std::any b) {
|
||||
if ((std::any_cast<NoneType>(&a) != nullptr ? 1 : 0) +
|
||||
(std::any_cast<NoneType>(&b) != nullptr ? 1 : 0) ==
|
||||
@ -498,19 +676,25 @@ 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))) {
|
||||
// if (ptr_a_bool != nullptr) {
|
||||
// b = Any2Bool(b);
|
||||
// return int(*ptr_a_bool) == int(std::any_cast<bool>(b));
|
||||
// }
|
||||
// if (ptr_b_bool != nullptr) {
|
||||
// a = Any2Bool(a);
|
||||
// return int(std::any_cast<bool>(a)) == int(*ptr_b_bool);
|
||||
// }
|
||||
else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr)))
|
||||
return false;
|
||||
} else
|
||||
else
|
||||
throw FatalError("Equal: Type Error", 56);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function NotEqual
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details judge if a!=b
|
||||
*/
|
||||
bool NotEqual(std::any a, std::any b) {
|
||||
if ((std::any_cast<NoneType>(&a) != nullptr ? 1 : 0) +
|
||||
(std::any_cast<NoneType>(&b) != nullptr ? 1 : 0) ==
|
||||
@ -533,19 +717,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))) {
|
||||
// if (ptr_a_bool != nullptr) {
|
||||
// b = Any2Bool(b);
|
||||
// return int(*ptr_a_bool) != int(std::any_cast<bool>(b));
|
||||
// }
|
||||
// if (ptr_b_bool != nullptr) {
|
||||
// a = Any2Bool(a);
|
||||
// return int(std::any_cast<bool>(a)) != int(*ptr_b_bool);
|
||||
// }
|
||||
else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr)))
|
||||
return true;
|
||||
} else
|
||||
else
|
||||
throw FatalError("NotEqual: Type Error", 57);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function GreaterEqual
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details judge if a>=b
|
||||
*/
|
||||
bool GreaterEqual(std::any a, std::any b) {
|
||||
int cnt = (std::any_cast<NoneType>(&a) != nullptr ? 1 : 0) +
|
||||
(std::any_cast<NoneType>(&b) != nullptr ? 1 : 0);
|
||||
@ -571,20 +761,10 @@ bool GreaterEqual(std::any a, std::any b) {
|
||||
return (*ptr_a_string) >= (*ptr_b_string);
|
||||
else if (((ptr_a_string != nullptr) || (ptr_b_string != nullptr))) {
|
||||
if (std::any_cast<NoneType>(&b)) return true;
|
||||
if (ptr_a_bool != nullptr) {
|
||||
b = Any2Bool(b);
|
||||
return 0;
|
||||
// return int(*ptr_a_bool) >= int(std::any_cast<bool>(b));
|
||||
}
|
||||
if (ptr_b_bool != nullptr) {
|
||||
a = Any2Bool(a);
|
||||
return int(std::any_cast<bool>(a)) >= int(*ptr_b_bool);
|
||||
}
|
||||
if (ptr_a_int2048 != nullptr) {
|
||||
b = ZYM::int2048(Any2Bool(b));
|
||||
return false;
|
||||
// return *ptr_a_int2048 > std::any_cast<ZYM::int2048>(b);
|
||||
}
|
||||
if (ptr_b_int2048 != nullptr) {
|
||||
a = ZYM::int2048(Any2Bool(a));
|
||||
return std::any_cast<ZYM::int2048>(a) >= *ptr_b_int2048;
|
||||
@ -593,6 +773,20 @@ bool GreaterEqual(std::any a, std::any b) {
|
||||
} else
|
||||
throw FatalError("GreaterEqual: Type Error", 58);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief definition of function LessEqual
|
||||
*
|
||||
* @param a any type value
|
||||
*
|
||||
* @param b any type value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throw FatalError
|
||||
*
|
||||
* @details judge if a<=b
|
||||
*/
|
||||
bool LessEqual(std::any a, std::any b) {
|
||||
int cnt = (std::any_cast<NoneType>(&a) != nullptr ? 1 : 0) +
|
||||
(std::any_cast<NoneType>(&b) != nullptr ? 1 : 0);
|
||||
@ -622,20 +816,10 @@ bool LessEqual(std::any a, std::any b) {
|
||||
b = Any2Bool(b);
|
||||
return int(*ptr_a_bool) <= int(std::any_cast<bool>(b));
|
||||
}
|
||||
if (ptr_b_bool != nullptr) {
|
||||
a = Any2Bool(a);
|
||||
return false;
|
||||
// return int(std::any_cast<bool>(a)) <= int(*ptr_b_bool);
|
||||
}
|
||||
if (ptr_a_int2048 != nullptr) {
|
||||
b = ZYM::int2048(Any2Bool(b));
|
||||
return *ptr_a_int2048 <= std::any_cast<ZYM::int2048>(b);
|
||||
}
|
||||
if (ptr_b_int2048 != nullptr) {
|
||||
a = ZYM::int2048(Any2Bool(a));
|
||||
return false;
|
||||
// return std::any_cast<ZYM::int2048>(a) <= *ptr_b_int2048;
|
||||
}
|
||||
return false;
|
||||
} else
|
||||
throw FatalError("LessEqual: Type Error", 59);
|
||||
|
Reference in New Issue
Block a user