Add more error checking

This commit is contained in:
sam-astro 2022-05-27 08:40:19 -04:00 committed by GitHub
parent 438f366ba4
commit 49429461e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,7 +78,10 @@ boost::any GetVariableValue(const string& varName, const unordered_map<string, b
// Check if there is a variable with the specified name // Check if there is a variable with the specified name
bool IsVar(const string& varName, const unordered_map<string, boost::any>& variableValues) bool IsVar(const string& varName, const unordered_map<string, boost::any>& variableValues)
{ {
if (variableValues.find(split(varName, '.')[0]) != variableValues.end() && split(varName, '.')[0] != "ZS") if(split(varName, '.')[0] == "ZS")
return false;
if (variableValues.find(split(varName, '.')[0]) != variableValues.end())
return true; return true;
else else
return false; return false;
@ -121,10 +124,7 @@ bool IsFunction(const string& funcName)
} }
bool IsZSFunction(const string& funcName) bool IsZSFunction(const string& funcName)
{ {
if (funcName[0] == 'Z' && funcName[1] == 'S' && funcName[2] == '.') return startsWith(funcName, "ZS.");
return true;
else
return false;
} }
boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& variableValues) boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& variableValues)
@ -406,11 +406,11 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
else if (words.at(lineNum).at(0) == "return") else if (words.at(lineNum).at(0) == "return")
return EvalExpression(unWrapVec(vector<string>(words.at(lineNum).begin() + 1, words.at(lineNum).end())), variableValues); return EvalExpression(unWrapVec(vector<string>(words.at(lineNum).begin() + 1, words.at(lineNum).end())), variableValues);
// Check if it is ZS Builtin function // Check if it is ZS Builtin function call
else if (startsWith(words.at(lineNum).at(0), "ZS.")) else if (startsWith(words.at(lineNum).at(0), "ZS."))
return EvalExpression(unWrapVec(words.at(lineNum)), variableValues); return EvalExpression(unWrapVec(words.at(lineNum)), variableValues);
// Check if it is function // Check if it is function call
else if (IsFunction(trim(split(words.at(lineNum).at(0), '(')[0]))) else if (IsFunction(trim(split(words.at(lineNum).at(0), '(')[0])))
{ {
// No args provided // No args provided
@ -447,7 +447,14 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
// Check if global variable declaration // Check if global variable declaration
else if (trim(words.at(lineNum).at(0)) == "global") else if (trim(words.at(lineNum).at(0)) == "global")
{ {
globalVariableValues[words.at(lineNum).at(2)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 4, -1)), variableValues); try
{
globalVariableValues[words.at(lineNum).at(2)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 4, -1)), variableValues);
}
catch (const std::exception&)
{
LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum)) + ", couldn't initialize variable.");
}
return nullType; return nullType;
} }
@ -455,8 +462,14 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
// re-inits a variable then store it with it's value // re-inits a variable then store it with it's value
else if (countInVector(types, trim(words.at(lineNum).at(0))) > 0) else if (countInVector(types, trim(words.at(lineNum).at(0))) > 0)
{ {
//cout << words.at(lineNum).at(1) << "=" << unWrapVec(slice(words.at(lineNum), 3, -1)) << "=" << AnyAsString(EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues)) << endl; try
variableValues[words.at(lineNum).at(1)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues); {
variableValues[words.at(lineNum).at(1)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues);
}
catch (const std::exception&)
{
LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum)) + ", couldn't initialize variable.");
}
return nullType; return nullType;
} }
@ -673,6 +686,7 @@ boost::any ExecuteFunction(const string& functionName, const vector<boost::any>&
} }
catch (const std::exception&) catch (const std::exception&)
{ {
LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum)));
} }
} }
return nullType; return nullType;