Fix errors caused by checking escaped quotes out of range

This commit is contained in:
sam-astro 2022-05-21 13:38:41 -04:00
parent 53e8f53fdf
commit 777066e069
4 changed files with 55 additions and 40 deletions

View File

@ -131,7 +131,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
bool inQuotes = false; bool inQuotes = false;
#if DEVELOPER_MESSAGES == true #if DEVELOPER_MESSAGES == true
InterpreterLog(" old expression: |" + expression + "|"); //InterpreterLog(" old expression: |" + expression + "|");
#endif #endif
bool isFunc = IsFunction(split(expression, '(')[0]); bool isFunc = IsFunction(split(expression, '(')[0]);
@ -174,7 +174,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
for (int i = 0; i < expression.size(); i++) for (int i = 0; i < expression.size(); i++)
{ {
if (expression[i] == '\"' && expression[i-1] != '\\') if (expression[i] == '\"' && !isEscaped(newExpression, i))
inQuotes = !inQuotes; inQuotes = !inQuotes;
if (isalpha(expression[i])) if (isalpha(expression[i]))
@ -232,12 +232,12 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
} }
} }
#if DEVELOPER_MESSAGES == true #if DEVELOPER_MESSAGES == true
InterpreterLog(" new expression: |" + newExpression + "|"); //InterpreterLog(" new expression: |" + newExpression + "|");
#endif #endif
bool addStrings = false; bool addStrings = false;
for (int i = 0; i < (int)newExpression.size(); i++) for (int i = 0; i < (int)newExpression.size(); i++)
if (isalpha(newExpression[i]) || (newExpression[i] == '\"' && expression[i-1] != '\\')) if (isalpha(newExpression[i]) || (newExpression[i] == '\"' && !isEscaped(newExpression, i)))
{ {
addStrings = true; addStrings = true;
break; break;
@ -248,7 +248,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
string withoutParenthesis = ""; string withoutParenthesis = "";
for (int i = 0; i < (int)newExpression.size(); i++) for (int i = 0; i < (int)newExpression.size(); i++)
{ {
if (newExpression[i] == '\"' && expression[i-1] != '\\') if (newExpression[i] == '\"' && !isEscaped(newExpression, i))
{ {
inQuotes = !inQuotes; inQuotes = !inQuotes;
continue; continue;
@ -553,7 +553,7 @@ boost::any ExecuteFunction(const string& functionName, const vector<boost::any>&
for (int i = 0; i < (int)inputVarVals.size(); i++) for (int i = 0; i < (int)inputVarVals.size(); i++)
{ {
if(i < funcArgs.size()) if (i < funcArgs.size())
{ {
variableValues[funcArgs[i]] = inputVarVals[i]; variableValues[funcArgs[i]] = inputVarVals[i];
#if DEVELOPER_MESSAGES == true #if DEVELOPER_MESSAGES == true
@ -581,18 +581,18 @@ boost::any ExecuteFunction(const string& functionName, const vector<boost::any>&
int parseZSharp(string script) int parseZSharp(string script)
{ {
script = replace(script, " ", "\t"); script = replace(script, " ", "\t");
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
InterpreterLog("Contents:\n" + script); InterpreterLog("Contents:\n" + script);
#endif #endif
vector<string> lines = split(script, ';'); vector<string> lines = split(script, '\n');
vector<vector<string>> words; vector<vector<string>> words;
for (int i = 0; i < (int)lines.size(); i++) for (int i = 0; i < (int)lines.size(); i++)
words.push_back(split(lines.at(i), ' ')); words.push_back(split(lines.at(i), ' '));
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
InterpreterLog("Gather variables & functions..."); InterpreterLog("Gather variables & functions...");
#endif #endif
// First go through entire script and iterate through all types to see if line is a variable // First go through entire script and iterate through all types to see if line is a variable
// or function declaration, then store it with it's value // or function declaration, then store it with it's value
for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) for (int lineNum = 0; lineNum < (int)words.size(); lineNum++)
@ -629,38 +629,38 @@ int parseZSharp(string script)
} }
else else
{ {
if (words.at(lineNum).at(0) == "string"){ if (words.at(lineNum).at(0) == "string") {
globalVariableValues[words.at(lineNum).at(1)] = StringRaw(words.at(lineNum).at(3)); globalVariableValues[words.at(lineNum).at(1)] = StringRaw(words.at(lineNum).at(3));
#if DEVELOPER_MESSAGES == true #if DEVELOPER_MESSAGES == true
InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "...");
#endif #endif
} }
else if (words.at(lineNum).at(0) == "int"){ else if (words.at(lineNum).at(0) == "int") {
globalVariableValues[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3)); globalVariableValues[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3));
#if DEVELOPER_MESSAGES == true #if DEVELOPER_MESSAGES == true
InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "...");
#endif #endif
} }
else if (words.at(lineNum).at(0) == "float"){ else if (words.at(lineNum).at(0) == "float") {
globalVariableValues[words.at(lineNum).at(1)] = stof(words.at(lineNum).at(3)); globalVariableValues[words.at(lineNum).at(1)] = stof(words.at(lineNum).at(3));
#if DEVELOPER_MESSAGES == true #if DEVELOPER_MESSAGES == true
InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "...");
#endif #endif
} }
else if (words.at(lineNum).at(0) == "bool"){ else if (words.at(lineNum).at(0) == "bool") {
globalVariableValues[words.at(lineNum).at(1)] = stob(words.at(lineNum).at(3)); globalVariableValues[words.at(lineNum).at(1)] = stob(words.at(lineNum).at(3));
#if DEVELOPER_MESSAGES == true #if DEVELOPER_MESSAGES == true
InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "...");
#endif #endif
} }
/*else /*else
LogWarning("unrecognized type \'" + words.at(lineNum).at(0) + "\' on line: " + to_string(lineNum));*/ LogWarning("unrecognized type \'" + words.at(lineNum).at(0) + "\' on line: " + to_string(lineNum));*/
} }
} }
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
InterpreterLog("Start Main()"); InterpreterLog("Start Main()");
#endif #endif
// Executes main, which is the starting function // Executes main, which is the starting function
ExecuteFunction("Main", vector<boost::any> {}); ExecuteFunction("Main", vector<boost::any> {});
@ -680,9 +680,13 @@ int main(int argc, char* argv[])
std::string scriptTextContents; std::string scriptTextContents;
// If scriptname is supplied and not in developer mode // If scriptname is supplied and not in developer mode
if (argc > 1) if (argc > 1 || EXAMPLE_PROJECT)
{ {
std::string scriptPath = argv[1]; std::string scriptPath;
if (EXAMPLE_PROJECT)
scriptPath = "D:\\Code\\Z-Sharp\\Releases\\ZS-Win-x64-Base\\Pong-Example-Project\\script.zs";
else
scriptPath = argv[1];
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
cout << scriptPath << endl; cout << scriptPath << endl;
#endif #endif
@ -694,35 +698,35 @@ int main(int argc, char* argv[])
ifstream input_file(scriptPath); ifstream input_file(scriptPath);
ss << input_file.rdbuf(); ss << input_file.rdbuf();
scriptTextContents = ss.str(); scriptTextContents = ss.str();
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
InterpreterLog("Gather script contents..."); InterpreterLog("Gather script contents...");
#endif #endif
chdir(projectDirectory.c_str()); chdir(projectDirectory.c_str());
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
InterpreterLog("Change directory to " + projectDirectory + "..."); InterpreterLog("Change directory to " + projectDirectory + "...");
#endif #endif
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
string newPath = filesystem::current_path(); string newPath = filesystem::current_path();
InterpreterLog("Current working directory is " + newPath); InterpreterLog("Current working directory is " + newPath);
#endif #endif
#elif WINDOWS #elif WINDOWS
// Get script contents // Get script contents
ifstream script(scriptPath); ifstream script(scriptPath);
stringstream scriptString; stringstream scriptString;
scriptString << script.rdbuf(); scriptString << script.rdbuf();
scriptTextContents = scriptString.str(); scriptTextContents = scriptString.str();
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
InterpreterLog("Gather script contents..."); InterpreterLog("Gather script contents...");
#endif #endif
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide = converter.from_bytes(projectDirectory); std::wstring wide = converter.from_bytes(projectDirectory);
LPCWSTR s = wide.c_str(); LPCWSTR s = wide.c_str();
SetCurrentDirectory(s); SetCurrentDirectory(s);
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
InterpreterLog("Change directory to " + projectDirectory + "..."); InterpreterLog("Change directory to " + projectDirectory + "...");
#endif #endif
#endif #endif
} }
else else
@ -735,9 +739,9 @@ int main(int argc, char* argv[])
//system("pause"); //system("pause");
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
InterpreterLog("Parsing..."); InterpreterLog("Parsing...");
#endif #endif
parseZSharp(scriptTextContents); parseZSharp(scriptTextContents);

View File

@ -83,7 +83,7 @@
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<IncludePath>D:\Code\SDL2-2.0.18\include;D:\Code\SDL2_image-2.0.5\include;D:\Code\SDL2_ttf-2.0.15\include;$(IncludePath)</IncludePath> <IncludePath>D:\Code\SDL2-2.0.18\include;D:\Code\SDL2_image-2.0.5\include;D:\Code\SDL2_ttf-2.0.15\include;$(IncludePath)</IncludePath>
<LibraryPath>D:\Code\SDL2_ttf-2.0.15\lib\x64;D:\Code\SDL2-2.0.18\lib\x64;D:\Code\SDL2_image-2.0.5\lib\x64;$(LibraryPath)</LibraryPath> <LibraryPath>D:\Code\SDL2_ttf-2.0.15\lib\x64;D:\Code\SDL2-2.0.18\lib\x64;D:\Code\SDL2_image-2.0.5\lib\x64;$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)\Releases\$(ProjectName)</OutDir> <OutDir>$(SolutionDir)\Releases\ZS-Win-x64-Base</OutDir>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>

View File

@ -342,3 +342,12 @@ string replace(const string& str, const string& strToReplace, const string& repl
return newStr; return newStr;
} }
bool isEscaped(const string& str, int curChar)
{
if (curChar > 0)
if (str[curChar - 1] == '\\')
return true;
return false;
}

View File

@ -60,4 +60,6 @@ float floatval(const string& s);
string replace(const string& str, const string& strToReplace, const string& replaceWith); string replace(const string& str, const string& strToReplace, const string& replaceWith);
bool isEscaped(const string& str, int curChar);
#endif #endif