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

@ -6,7 +6,7 @@ inline void VariableContainer::CreateFrame() {
StackScopes.push(std::unordered_map<std::string, std::any>());
}
void VariableContainer::DestroyFrame() {
if (StackScopes.empty()) throw FatalError("DestroyFrame: empty stack");
if (StackScopes.empty()) throw FatalError("DestroyFrame: empty stack", 20);
StackScopes.pop();
}
std::any VariableContainer::ReadVariable(const std::string &name) {
@ -15,7 +15,7 @@ std::any VariableContainer::ReadVariable(const std::string &name) {
if (top.find(name) != top.end()) return top[name];
}
if (GlobalScope.find(name) != GlobalScope.end()) return GlobalScope[name];
throw InterpretException(("ReadVariable: not found"));
throw InterpretException(("ReadVariable: not found"), 21);
}
void VariableContainer::WriteVariable(const std::string &name,
const std::any &value, bool cover) {
@ -41,7 +41,7 @@ void VariableContainer::WriteVariable(const std::string &name,
void FucntionContainer::AddFunction(const std::string &name,
const FunctionItem &item) {
if (FunctionIndex.find(name) != FunctionIndex.end()) {
throw InterpretException("AddFunction: function already exists");
throw InterpretException("AddFunction: function already exists", 22);
}
FunctionIndex[name] = item;
}
@ -66,32 +66,32 @@ std::any FucntionContainer::CallFunction(const std::string &name,
} else if (name == "str") {
if (args.size() != 1)
throw InterpretException(
"CallFunction: str() should take exactly one argument");
"CallFunction: str() should take exactly one argument", 23);
return Any2String(args[0].value);
} else if (name == "int") {
if (args.size() == 1) {
return Any2Int(args[0].value);
} else {
throw InterpretException(
"CallFunction: int() should take exactly one argument");
"CallFunction: int() should take exactly one argument", 24);
}
} else if (name == "float") {
if (args.size() == 1) {
return Any2Float(args[0].value);
} else {
throw InterpretException(
"CallFunction: float() should take exactly one argument");
"CallFunction: float() should take exactly one argument", 25);
}
} else if (name == "bool") {
if (args.size() == 1) {
return Any2Bool(args[0].value);
} else {
throw InterpretException(
"CallFunction: bool() should take exactly one argument");
"CallFunction: bool() should take exactly one argument", 26);
}
}
if (FunctionIndex.find(name) == FunctionIndex.end()) {
throw InterpretException("CallFunction: function not found");
throw InterpretException("CallFunction: function not found", 27);
}
const FunctionItem &func = FunctionIndex[name];
std::unordered_set<std::string> args_with_data;
@ -121,7 +121,8 @@ std::any FucntionContainer::CallFunction(const std::string &name,
i++;
if (i >= func.para_list.size()) break;
if (!func.para_list[i].value.has_value())
throw InterpretException("CallFunction: function parameter not found");
throw InterpretException("CallFunction: function parameter not found",
28);
Variables.WriteVariable(func.para_list[i].name, func.para_list[i].value,
true);
args_with_data.insert(func.para_list[i].name);