upd: include exception info in return value

This commit is contained in:
2023-11-10 08:43:57 +08:00
parent fb9acd28bf
commit 169969dc8a
5 changed files with 103 additions and 91 deletions

View File

@ -25,7 +25,7 @@ inline ZYM::int2048 Any2Int(const std::any &value) {
return std::move(ZYM::int2048(0));
else {
std::cerr << value.type().name() << std::endl;
throw FatalError("Any2Int2048: unknown type");
throw FatalError("Any2Int2048: unknown type", 29);
}
}
@ -48,7 +48,7 @@ double Any2Float(const std::any &value) {
else if (auto ptr = std::any_cast<std::string>(&value))
return std::stod(*ptr);
else
throw FatalError("Any2Float: unknown type");
throw FatalError("Any2Float: unknown type", 30);
}
std::string Any2String(const std::any &value) {
@ -68,7 +68,7 @@ std::string Any2String(const std::any &value) {
else if (auto ptr = std::any_cast<NoneType>(&value))
buf << "None";
else
throw FatalError("Any2String: unknown type");
throw FatalError("Any2String: unknown type", 31);
std::getline(buf, res);
return res;
}
@ -83,7 +83,7 @@ bool Any2Bool(const std::any &value) {
} else if (auto ptr = std::any_cast<std::string>(&value)) {
return (*ptr) != "";
} else
throw FatalError("Any2Bool: unknown type");
throw FatalError("Any2Bool: unknown type", 32);
}
std::any DeQuate(std::any val, VariableContainer &Variables) {
@ -115,16 +115,19 @@ int ConverToSameArithType(std::any &a, std::any &b,
if ((!allow_string_operation) && (ptr_a_string || ptr_b_string))
throw InterpretException(
"ConverToSameArithType: string operation not allowed in this "
"situation");
"situation",
33);
if (allow_string_operation && (ptr_a_string || ptr_b_string)) {
if (!(ptr_a_string || ptr_a_int2048))
throw InterpretException(
"ConverToSameArithType: string operation not allowed in this "
"situation");
"situation",
34);
if (!(ptr_b_string || ptr_b_int2048))
throw InterpretException(
"ConverToSameArithType: string operation not allowed in this "
"situation");
"situation",
35);
return 1;
}
int level_a = (ptr_a_bool != nullptr ? 1 : 0) +
@ -146,7 +149,7 @@ int ConverToSameArithType(std::any &a, std::any &b,
b = Any2Float(b);
break;
default:
throw FatalError("ConverToSameArithType: unknown type");
throw FatalError("ConverToSameArithType: unknown type", 36);
}
return 0;
}
@ -164,7 +167,7 @@ std::any Add(std::any a, std::any b) {
if ((ptr_a_string != nullptr ? 1 : 0) + (ptr_b_string != nullptr ? 1 : 0) ==
1) {
throw InterpretException(
"Add: string operation not allowed in this situation");
"Add: string operation not allowed in this situation", 37);
}
if ((ptr_a_string != nullptr ? 1 : 0) + (ptr_b_string != nullptr ? 1 : 0) ==
2)
@ -176,7 +179,7 @@ std::any Add(std::any a, std::any b) {
else if ((ptr_a_float != nullptr) && (ptr_b_float != nullptr))
return (*ptr_a_float) + (*ptr_b_float);
else
throw FatalError("Add: Type Error");
throw FatalError("Add: Type Error", 38);
}
std::any Sub(std::any a, std::any b) {
ConverToSameArithType(a, b);
@ -193,7 +196,7 @@ std::any Sub(std::any a, std::any b) {
else if ((ptr_a_float != nullptr) && (ptr_b_float != nullptr))
return (*ptr_a_float) - (*ptr_b_float);
else
throw FatalError("Sub: Type Error");
throw FatalError("Sub: Type Error", 39);
return a;
}
std::any Mul(std::any a, std::any b) {
@ -209,7 +212,7 @@ std::any Mul(std::any a, std::any b) {
if ((ptr_a_string != nullptr ? 1 : 0) + (ptr_b_string != nullptr ? 1 : 0) ==
2) {
throw InterpretException(
"Mul: string operation not allowed in this situation");
"Mul: string operation not allowed in this situation", 40);
} else if ((ptr_a_string != nullptr ? 1 : 0) +
(ptr_b_string != nullptr ? 1 : 0) ==
1) {
@ -220,7 +223,7 @@ std::any Mul(std::any a, std::any b) {
std::swap(ptr_a_float, ptr_b_float);
}
if (ptr_b_float != nullptr)
throw InterpretException("Mul: string*float not allowed");
throw InterpretException("Mul: string*float not allowed", 41);
if (ptr_b_bool != nullptr) {
if (*ptr_b_bool) {
return *ptr_a_string;
@ -241,7 +244,7 @@ std::any Mul(std::any a, std::any b) {
else if ((ptr_a_float != nullptr) && (ptr_b_float != nullptr))
return (*ptr_a_float) * (*ptr_b_float);
else
throw FatalError("Mul: Type Error");
throw FatalError("Mul: Type Error", 42);
}
std::any Div(std::any a, std::any b) {
bool *ptr_a_bool = std::any_cast<bool>(&a);
@ -254,10 +257,10 @@ std::any Div(std::any a, std::any b) {
std::string *ptr_b_string = std::any_cast<std::string>(&b);
if ((ptr_a_string != nullptr) || (ptr_b_string != nullptr))
throw InterpretException(
"Div: string operation not allowed in this situation");
"Div: string operation not allowed in this situation", 43);
double t_a = Any2Float(a);
double t_b = Any2Float(b);
if (t_b == 0) throw InterpretException("Div: divided by zero");
if (t_b == 0) throw InterpretException("Div: divided by zero", 44);
t_a = t_a / t_b;
return a;
}
@ -270,17 +273,19 @@ 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");
if ((*ptr_b_float) == 0)
throw InterpretException("Divv: divided by zero", 45);
return ZYM::int2048(std::floor((*ptr_a_float) / (*ptr_b_float)));
} else if ((ptr_a_int2048 != nullptr) && (ptr_b_int2048 != nullptr)) {
if ((*ptr_b_int2048) == 0)
throw InterpretException("Divv: divided by zero");
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");
if ((*ptr_b_bool) == 0)
throw InterpretException("Divv: divided by zero", 47);
return ZYM::int2048((*ptr_a_bool) / (*ptr_b_bool));
} else
throw FatalError("Divv: Type Error");
throw FatalError("Divv: Type Error", 48);
}
std::any Mod(std::any a, std::any b) {
ConverToSameArithType(a, b);
@ -291,18 +296,21 @@ 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");
if ((*ptr_b_float) == 0)
throw InterpretException("Mod: divided by zero", 49);
return ZYM::int2048((*ptr_a_float) -
(*ptr_b_float) *
std::floor((*ptr_a_float) / (*ptr_b_float)));
} else if ((ptr_a_int2048 != nullptr) && (ptr_b_int2048 != nullptr)) {
if ((*ptr_b_int2048) == 0) throw InterpretException("Mod: divided by zero");
if ((*ptr_b_int2048) == 0)
throw InterpretException("Mod: divided by zero", 50);
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");
if ((*ptr_b_bool) == 0)
throw InterpretException("Mod: divided by zero", 51);
return ZYM::int2048((*ptr_a_bool) % (*ptr_b_bool));
} else
throw FatalError("Mod: Type Error");
throw FatalError("Mod: Type Error", 52);
}
std::any &SelfAdd(std::any &a, std::any b) {
a = Add(a, b);
@ -339,7 +347,7 @@ std::any Neg(std::any a) {
else if (ptr_a_float != nullptr)
return -(*ptr_a_float);
else
throw FatalError("Neg: Type Error");
throw FatalError("Neg: Type Error", 53);
}
bool Greater(std::any a, std::any b) {
@ -361,7 +369,7 @@ bool Greater(std::any a, std::any b) {
else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr))
return (*ptr_a_string) > (*ptr_b_string);
else
throw FatalError("Greater: Type Error");
throw FatalError("Greater: Type Error", 54);
}
bool Less(std::any a, std::any b) {
ConverToSameArithType(a, b, true);
@ -382,7 +390,7 @@ bool Less(std::any a, std::any b) {
else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr))
return (*ptr_a_string) < (*ptr_b_string);
else
throw FatalError("Less: Type Error");
throw FatalError("Less: Type Error", 55);
}
bool Equal(std::any a, std::any b) {
ConverToSameArithType(a, b, true);
@ -403,7 +411,7 @@ bool Equal(std::any a, std::any b) {
else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr))
return (*ptr_a_string) == (*ptr_b_string);
else
throw FatalError("Equal: Type Error");
throw FatalError("Equal: Type Error", 56);
}
bool NotEqual(std::any a, std::any b) {
ConverToSameArithType(a, b, true);
@ -424,7 +432,7 @@ bool NotEqual(std::any a, std::any b) {
else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr))
return (*ptr_a_string) != (*ptr_b_string);
else
throw FatalError("NotEqual: Type Error");
throw FatalError("NotEqual: Type Error", 57);
}
bool GreaterEqual(std::any a, std::any b) {
ConverToSameArithType(a, b, true);
@ -445,7 +453,7 @@ bool GreaterEqual(std::any a, std::any b) {
else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr))
return (*ptr_a_string) >= (*ptr_b_string);
else
throw FatalError("GreaterEqual: Type Error");
throw FatalError("GreaterEqual: Type Error", 58);
}
bool LessEqual(std::any a, std::any b) {
ConverToSameArithType(a, b, true);
@ -466,5 +474,5 @@ bool LessEqual(std::any a, std::any b) {
else if ((ptr_a_string != nullptr) && (ptr_b_string != nullptr))
return (*ptr_a_string) <= (*ptr_b_string);
else
throw FatalError("LessEqual: Type Error");
throw FatalError("LessEqual: Type Error", 59);
}