diff --git a/Slang/Main.cpp b/Slang/Main.cpp index abfc82f..80a3b1c 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -20,7 +20,7 @@ using namespace std; using namespace boost; unordered_map globalVariableValues; -unordered_map> functionValues; +unordered_map>> functionValues; bool isNumber(const string& str) @@ -31,7 +31,7 @@ bool isNumber(const string& str) return true; } -bool stob(string str) { +bool stob(const string str) { transform(str.begin(), str.end(), str.begin(), ::tolower); istringstream is(str); bool b; @@ -39,9 +39,9 @@ bool stob(string str) { return b; } -string StringRaw(string str) +string StringRaw(const string s) { - str = trim(str); + string str = trim(s); if (str.size() < 3) return str; @@ -59,9 +59,9 @@ string StringRaw(string str) return withoutQuotes; } -string Quoted(string str) +string Quoted(const string s) { - str = trim(str); + string str = trim(s); string withQuotes; @@ -76,9 +76,9 @@ string Quoted(string str) return withQuotes; } -string RMParenthesis(string str) +string RMParenthesis(const string s) { - str = trim(str); + string str = trim(s); string withoutParenthesis; if (str[0] != '(') @@ -92,7 +92,7 @@ string RMParenthesis(string str) return withoutParenthesis; } -any GetVariableValue(string varName, unordered_map& variableVals) +any GetVariableValue(const string varName, const unordered_map& variableVals) { auto iA = variableVals.find(varName); if (iA != variableVals.end()) @@ -113,7 +113,7 @@ any GetVariableValue(string varName, unordered_map& variableVals) } } -bool IsVar(string varName, unordered_map& variableVals) +bool IsVar(const string varName, const unordered_map& variableVals) { auto iA = variableVals.find(varName); if (iA != variableVals.end()) @@ -123,7 +123,7 @@ bool IsVar(string varName, unordered_map& variableVals) return false; } -vector VarValues(vector varNames, unordered_map& variableVals) +vector VarValues(const vector varNames, const unordered_map& variableVals) { vector realValues; @@ -153,7 +153,7 @@ vector VarValues(vector varNames, unordered_map& varia return realValues; } -bool IsFunction(string funcName) +bool IsFunction(const string funcName) { auto iA = functionValues.find(funcName); if (iA != functionValues.end()) @@ -166,9 +166,9 @@ bool IsFunction(string funcName) } } -any EvalExpression(string expression, unordered_map& variableVals) +any EvalExpression(const string ex, const unordered_map& variableVals) { - expression = trim(expression); + string expression = trim(ex); bool inQuotes = false; // If no operations are applied, then return self @@ -302,7 +302,7 @@ any EvalExpression(string expression, unordered_map& variableVals) return evaluate(newExpression); } -bool BooleanLogic(string valA, string determinant, string valB, unordered_map& variableVals) +bool BooleanLogic(const string valA, const string determinant, const string valB, const unordered_map& variableVals) { any valARealValue = EvalExpression(valA, variableVals); any valBRealValue = EvalExpression(valB, variableVals); @@ -323,7 +323,7 @@ bool BooleanLogic(string valA, string determinant, string valB, unordered_map str, unordered_map& variableValues) +int evalEqu(const vector str, unordered_map& variableValues) { if (IsVar(str[0], variableValues)) { @@ -360,16 +360,16 @@ int evalEqu(vector str, unordered_map& variableValues) return 1; } -any ProcessLine(vector> words, int lineNum, unordered_map& variableValues) +any ProcessLine(const vector>& words, const int lineNum, unordered_map& variableValues) { if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') - return ""; + return; // If print statement (deprecated, now use CPP.System.Print() function) if (words[lineNum][0] == "print") { cout << AnyAsString(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; - return ""; + return; } // Check if function return @@ -385,7 +385,7 @@ any ProcessLine(vector> words, int lineNum, unordered_map> words, int lineNum, unordered_map(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); - return ""; + return; } } @@ -405,7 +405,7 @@ any ProcessLine(vector> words, int lineNum, unordered_map functionLines; - // Get contents of function - functionLines = functionValues[functionName]; + vector> words = functionValues[functionName]; unordered_map variableValues; - vector functionNameParts = split(replace(functionValues[functionName], functionName + " ", ""), ','); + vector args = split(replace(functionValues[functionName][0][0], ','); for (int i = 0; i < (int)inputVarVals.size(); i++) { - variableValues[trim(functionNameParts[i])] = EvalExpression(inputVarVals[i], variables, variableValues); - //cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl; + variableValues[trim(args[i])] = EvalExpression(inputVarVals[i], variables, variableValues); } - vector> words; - for (int i = 0; i < (int)functionLines.size(); i++) - words.push_back(split(functionLines[i], ' ')); //Iterate through all lines in function - for (int lineNum = 0; lineNum < (int)functionLines.size(); lineNum++) + for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) { - string returnVal = ""; + any returnVal = 0; try { returnVal = ProcessLine(words, lineNum, variables, variableValues); @@ -562,10 +555,10 @@ any ExecuteFunction(string functionName, vector inputVarVals) cout << "\x1B[31mERROR: \'" << unWrapVec(words[lineNum]) << "\'\nIn function: " << functionName << "\nLine: " << lineNum << "\033[0m\t\t" << endl; exit(1); } - if (returnVal != "") + if (!returnVal.empty()) return returnVal; } - return ""; + return; } int parseSlang(string script) @@ -586,33 +579,33 @@ int parseSlang(string script) //Checks if it is function if (words[lineNum][(int)words[lineNum].size() - 1][(int)words[lineNum][(int)words[lineNum].size() - 1].size() - 1] == ')') { - vector functionContents; + vector> functionContents; - string functName; + string functName = split(words[lineNum][1], "(")[0]; + + string args = ""; for (int w = 1; w < (int)words[lineNum].size(); w++) { if (w < (int)words[lineNum].size() - 1) { - functName += replace(replace(words[lineNum][w], "(", " "), ")", "") + " "; + args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ","; } else { - functName += replace(replace(words[lineNum][w], "(", " "), ")", ""); + args += replace(replace(words[lineNum][w], "(", " "), ")", ""); } } + + args = replace(args, functName + ",", ""); + functionContents.push_back(vector{args}); int numOfBrackets = 1; - for (int p = lineNum + 2; p < (int)words.size(); p++) + for (int p = lineNum + 3; p < (int)words.size(); p++) { numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); if (numOfBrackets == 0) break; - functionContents.push_back(""); - for (int w = 0; w < (int)words[p].size(); w++) - { - functionContents[(int)functionContents.size() - 1] += words[p][w] + " "; - } + functionContents.push_back(removeTabs(words[p], 1)); } - functionContents = removeTabs(functionContents, 1); functionValues[functName] = functionContents; //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; }