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;
@ -585,7 +585,7 @@ int parseZSharp(string script)
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), ' '));
@ -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

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