From 3c684f2153ea15e7ef945d967b016db1999922ea Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Tue, 24 May 2022 16:10:39 -0400 Subject: [PATCH] Much better function ARG parsing, allowing other functions as ARGs --- ZSharp/Main.cpp | 103 ++++++++++++++++++++++------------------------ ZSharp/strops.cpp | 14 ++++++- 2 files changed, 61 insertions(+), 56 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index c21c08e..d9f56c6 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -143,34 +143,33 @@ boost::any EvalExpression(const string& ex, unordered_map& v //bool isFunc = IsFunction(split(expression, '(')[0]); if (isFunc && !inQuotes) { - //cout << split(expression, '(')[0] << endl; - //string argContents = ""; - //int y = indexInStr(expression, '(') + 1; - //while (y < expression.size() && expression[y] != ')') - //{ - // argContents += expression[y]; - // y++; - //} - //return ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues)); - // start -> FuncCall(0, x, OtherFunc(a)) // changeto -> 0, x, OtherFunc(a) string insideFunArgs = betweenChars(expression, '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << insideFunArgs << endl; +#endif vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << "["< funcArgs = VarValues(argList, variableValues); return ExecuteFunction(trim(split(expression, '(')[0]), funcArgs); } else if (isZS && !inQuotes) { - string argContents = ""; - int y = indexInStr(expression, '(') + 1; - while (y < expression.size() && expression[y] != ')') - { - argContents += expression[y]; - - y++; - } - return ZSFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues)); + // start -> FuncCall(0, x, OtherFunc(a)) + // changeto -> 0, x, OtherFunc(a) + string insideFunArgs = betweenChars(expression, '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << insideFunArgs << endl; +#endif + vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << "[" << unWrapVec(argList) << "]" << endl; +#endif + vector funcArgs = VarValues(argList, variableValues); + return ZSFunction(trim(split(expression, '(')[0]), funcArgs); } else return GetVariableValue(expression, variableValues); @@ -198,38 +197,34 @@ boost::any EvalExpression(const string& ex, unordered_map& v bool isFunc = IsFunction(name); if (isFunc && !inQuotes) { - //string argContents = ""; - //i++; - //while (i < expression.size() && expression[i] != ')') - //{ - // argContents += expression[i]; - - // i++; - //} - //string returnVal = AnyAsString(ExecuteFunction(name, VarValues(split(argContents, ','), variableValues))); - //newExpression += returnVal; - - // start -> FuncCall(0, x, OtherFunc(a)) // changeto -> 0, x, OtherFunc(a) string insideFunArgs = betweenChars(expression, '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << insideFunArgs << endl; +#endif vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << unWrapVec(argList) << endl; +#endif vector funcArgs = VarValues(argList, variableValues); string returnVal = AnyAsString(ExecuteFunction(trim(split(expression, '(')[0]), funcArgs)); newExpression += returnVal; } else if (split(name, '.')[0] == "ZS" && !inQuotes) { - string argContents = ""; - int y = indexInStr(expression, '(') + 1; - while (y < expression.size() && expression[y] != ')') - { - argContents += expression[y]; - - y++; - } - //cout << split(expression, '(')[0] << " " << argContents << endl; - string returnVal = AnyAsString(ZSFunction(split(name, '(')[0], VarValues(split(argContents, ','), variableValues))); + // start -> FuncCall(0, x, OtherFunc(a)) + // changeto -> 0, x, OtherFunc(a) + string insideFunArgs = betweenChars(expression, '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << insideFunArgs << endl; +#endif + vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << unWrapVec(argList) << endl; +#endif + vector funcArgs = VarValues(argList, variableValues); + string returnVal = AnyAsString(ExecuteFunction(trim(split(expression, '(')[0]), funcArgs)); newExpression += returnVal; } else @@ -245,8 +240,8 @@ boost::any EvalExpression(const string& ex, unordered_map& v else { newExpression += expression[i]; - } } +} #if DEVELOPER_MESSAGES == true //InterpreterLog(" new expression: |" + newExpression + "|"); #endif @@ -546,7 +541,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde ifContents.push_back(words.at(lineNum)); lineNum++; } - + ifContents = removeTabsWdArry(ifContents, 1); if (BooleanLogic(ifParameters.at(0), ifParameters.at(1), ifParameters.at(2), variableValues)) @@ -565,7 +560,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde { vector> elseContents; vector elseParameters; - + int numOfBrackets = 0; for (int w = 1; w < (int)words.at(lineNum).size(); w++) { if (count(words.at(lineNum).at(w), '{') != 0) @@ -574,7 +569,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde break; } } - + lineNum++; while (lineNum < (int)words.size()) { @@ -584,9 +579,9 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde elseContents.push_back(words.at(lineNum)); lineNum++; } - + elseContents = removeTabsWdArry(elseContents, 1); - + //Iterate through all lines in if statement for (int l = 0; l < (int)elseContents.size(); l++) { @@ -594,7 +589,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde if (!returnVal.empty()) return returnVal; } - + } } return nullType; @@ -626,7 +621,7 @@ boost::any ExecuteFunction(const string& functionName, const vector& cout << functionName + " \x1B[33m" << funcArgs[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl; #endif } - } +} //Iterate through all lines in function for (int lineNum = 1; lineNum < (int)words.size(); lineNum++) @@ -701,7 +696,7 @@ int parseZSharp(string script) functionContents.push_back(removeTabs(words.at(p), 1)); } functionValues[functName] = functionContents; - } + } else { if (words.at(lineNum).at(0) == "include") @@ -760,8 +755,8 @@ int parseZSharp(string script) } /*else LogWarning("unrecognized type \'" + words.at(lineNum).at(0) + "\' on line: " + to_string(lineNum));*/ - } - } + } +} return 0; } @@ -790,7 +785,7 @@ int main(int argc, char* argv[]) #if DEVELOPER_MESSAGES cout << scriptPath << endl; #endif - if (!fileExists(scriptPath) + if (!fileExists(scriptPath)) LogCriticalError("Failed to load script from path: \"" + scriptPath + "\""); std::string projectDirectory = scriptPath.substr(0, scriptPath.find_last_of("/\\")); @@ -806,7 +801,7 @@ int main(int argc, char* argv[]) // Change the current working directory to that of the script int chErr = chdir(projectDirectory.c_str()); - if(chErr < 0) + if (chErr < 0) LogCriticalError("Failed to change directory to: \"" + projectDirectory.c_str() + "\", error num: " + chErr); #if DEVELOPER_MESSAGES InterpreterLog("Changed directory to " + projectDirectory + "..."); diff --git a/ZSharp/strops.cpp b/ZSharp/strops.cpp index 9691ee3..cb53cfa 100644 --- a/ZSharp/strops.cpp +++ b/ZSharp/strops.cpp @@ -159,16 +159,26 @@ vector splitNoOverlap(const string& str, const char& splitBy, const char string tmpStr = ""; for (int i = 0; i < (int)str.size(); i++) { + if (i == (int)str.size() - 1) + { + newStr.push_back(tmpStr+ str[i]); + break; + } + if (str[i] == splitBy && openCount == 0) { newStr.push_back(tmpStr); tmpStr = ""; continue; } - else if (str[i] == openChar) + else if (str[i] == openChar) { + tmpStr += str[i]; openCount += 1; - else if (str[i] == closeChar) + } + else if (str[i] == closeChar) { + tmpStr += str[i]; openCount -= 1; + } else tmpStr += str[i]; }