From 777066e069d46590f2e30b02fd13b36255f95885 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Sat, 21 May 2022 13:38:41 -0400 Subject: [PATCH] Fix errors caused by checking escaped quotes out of range --- ZSharp/Main.cpp | 82 +++++++++++++++++++++++-------------------- ZSharp/ZSharp.vcxproj | 2 +- ZSharp/strops.cpp | 9 +++++ ZSharp/strops.h | 2 ++ 4 files changed, 55 insertions(+), 40 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index cabc199..a1322c7 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -131,7 +131,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v bool inQuotes = false; #if DEVELOPER_MESSAGES == true - InterpreterLog(" old expression: |" + expression + "|"); + //InterpreterLog(" old expression: |" + expression + "|"); #endif bool isFunc = IsFunction(split(expression, '(')[0]); @@ -174,7 +174,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v for (int i = 0; i < expression.size(); i++) { - if (expression[i] == '\"' && expression[i-1] != '\\') + if (expression[i] == '\"' && !isEscaped(newExpression, i)) inQuotes = !inQuotes; if (isalpha(expression[i])) @@ -232,12 +232,12 @@ boost::any EvalExpression(const string& ex, unordered_map& v } } #if DEVELOPER_MESSAGES == true - InterpreterLog(" new expression: |" + newExpression + "|"); + //InterpreterLog(" new expression: |" + newExpression + "|"); #endif bool addStrings = false; 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; break; @@ -248,7 +248,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v string withoutParenthesis = ""; for (int i = 0; i < (int)newExpression.size(); i++) { - if (newExpression[i] == '\"' && expression[i-1] != '\\') + if (newExpression[i] == '\"' && !isEscaped(newExpression, i)) { inQuotes = !inQuotes; continue; @@ -548,12 +548,12 @@ boost::any ExecuteFunction(const string& functionName, const vector& std::vector> words = functionValues[functionName]; unordered_map variableValues = {}; - + std::vector funcArgs = words.at(0); - + for (int i = 0; i < (int)inputVarVals.size(); i++) { - if(i < funcArgs.size()) + if (i < funcArgs.size()) { variableValues[funcArgs[i]] = inputVarVals[i]; #if DEVELOPER_MESSAGES == true @@ -581,18 +581,18 @@ boost::any ExecuteFunction(const string& functionName, const vector& int parseZSharp(string script) { script = replace(script, " ", "\t"); - #if DEVELOPER_MESSAGES +#if DEVELOPER_MESSAGES InterpreterLog("Contents:\n" + script); - #endif +#endif - vector lines = split(script, ';'); + vector lines = split(script, '\n'); vector> words; for (int i = 0; i < (int)lines.size(); i++) words.push_back(split(lines.at(i), ' ')); - #if DEVELOPER_MESSAGES +#if DEVELOPER_MESSAGES InterpreterLog("Gather variables & functions..."); - #endif +#endif // 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 for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) @@ -629,38 +629,38 @@ int parseZSharp(string script) } 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)); #if DEVELOPER_MESSAGES == true InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); #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)); #if DEVELOPER_MESSAGES == true InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); #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)); #if DEVELOPER_MESSAGES == true InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); #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)); #if DEVELOPER_MESSAGES == true InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); #endif -} + } /*else LogWarning("unrecognized type \'" + words.at(lineNum).at(0) + "\' on line: " + to_string(lineNum));*/ } } - #if DEVELOPER_MESSAGES +#if DEVELOPER_MESSAGES InterpreterLog("Start Main()"); - #endif +#endif // Executes main, which is the starting function ExecuteFunction("Main", vector {}); @@ -680,9 +680,13 @@ int main(int argc, char* argv[]) std::string scriptTextContents; // 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 cout << scriptPath << endl; #endif @@ -694,35 +698,35 @@ int main(int argc, char* argv[]) ifstream input_file(scriptPath); ss << input_file.rdbuf(); scriptTextContents = ss.str(); - #if DEVELOPER_MESSAGES +#if DEVELOPER_MESSAGES InterpreterLog("Gather script contents..."); - #endif - +#endif + chdir(projectDirectory.c_str()); - #if DEVELOPER_MESSAGES +#if DEVELOPER_MESSAGES InterpreterLog("Change directory to " + projectDirectory + "..."); - #endif - #if DEVELOPER_MESSAGES +#endif +#if DEVELOPER_MESSAGES string newPath = filesystem::current_path(); InterpreterLog("Current working directory is " + newPath); - #endif +#endif #elif WINDOWS // Get script contents ifstream script(scriptPath); stringstream scriptString; scriptString << script.rdbuf(); scriptTextContents = scriptString.str(); - #if DEVELOPER_MESSAGES +#if DEVELOPER_MESSAGES InterpreterLog("Gather script contents..."); - #endif - +#endif + std::wstring_convert> converter; std::wstring wide = converter.from_bytes(projectDirectory); LPCWSTR s = wide.c_str(); SetCurrentDirectory(s); - #if DEVELOPER_MESSAGES +#if DEVELOPER_MESSAGES InterpreterLog("Change directory to " + projectDirectory + "..."); - #endif +#endif #endif } else @@ -735,9 +739,9 @@ int main(int argc, char* argv[]) //system("pause"); - #if DEVELOPER_MESSAGES +#if DEVELOPER_MESSAGES InterpreterLog("Parsing..."); - #endif +#endif parseZSharp(scriptTextContents); diff --git a/ZSharp/ZSharp.vcxproj b/ZSharp/ZSharp.vcxproj index b210e85..863a2f0 100644 --- a/ZSharp/ZSharp.vcxproj +++ b/ZSharp/ZSharp.vcxproj @@ -83,7 +83,7 @@ true D:\Code\SDL2-2.0.18\include;D:\Code\SDL2_image-2.0.5\include;D:\Code\SDL2_ttf-2.0.15\include;$(IncludePath) 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) - $(SolutionDir)\Releases\$(ProjectName) + $(SolutionDir)\Releases\ZS-Win-x64-Base false diff --git a/ZSharp/strops.cpp b/ZSharp/strops.cpp index 0c4b2d4..be2fa03 100644 --- a/ZSharp/strops.cpp +++ b/ZSharp/strops.cpp @@ -341,4 +341,13 @@ string replace(const string& str, const string& strToReplace, const string& repl } return newStr; +} + +bool isEscaped(const string& str, int curChar) +{ + if (curChar > 0) + if (str[curChar - 1] == '\\') + return true; + + return false; } \ No newline at end of file diff --git a/ZSharp/strops.h b/ZSharp/strops.h index 215f29f..f709655 100644 --- a/ZSharp/strops.h +++ b/ZSharp/strops.h @@ -60,4 +60,6 @@ float floatval(const string& s); string replace(const string& str, const string& strToReplace, const string& replaceWith); +bool isEscaped(const string& str, int curChar); + #endif