From 392879fbad6d620349fc46e12494465dccc6452e Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 11:51:49 -0500 Subject: [PATCH 01/33] Started optimization with unordered_map hash table --- Slang/Main.cpp | 231 ++++++++++++++++++++----------------------------- 1 file changed, 96 insertions(+), 135 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 968dbb7..f392206 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -10,13 +10,13 @@ #include "strops.h" #include "builtin.h" #include "main.h" +#include using namespace std; +using namespace boost; -vector globalVariables; -vector globalVariableValues; -vector functions; -vector> functionValues; +unordered_map globalVariableValues; +unordered_map> functionValues; bool isNumber(const string& str) { @@ -81,112 +81,81 @@ string RMParenthesis(string str) return withoutParenthesis; } -string GetVariableValue(string varName, vector& variables, vector& variableVals) +string GetVariableValue(string varName, unordered_map& variableVals) { - string typ = "string"; - bool isVar = false; - - if (count(varName, '\"') == 0) + auto iA = variableVals.find(varName); + if (iA != variableVals.end()) { - // Checks against global vars - for (int v = 0; v < (int)globalVariables.size(); v++) - if (varName == split(globalVariables[v], ' ')[1]) - { - typ = split(globalVariables[v], ' ')[0]; - isVar = true; - } - // Checks against local vars - for (int v = 0; v < (int)variables.size(); v++) - if (varName == split(variables[v], ' ')[1]) - { - typ = split(variables[v], ' ')[0]; - isVar = true; - } + return iA->second; } - - // If it is a var - if (isVar) + else { - // Checks against global vars - for (int v = 0; v < (int)globalVariables.size(); v++) - if (varName == split(globalVariables[v], ' ')[1]) - { - return globalVariableValues[v]; - } - // Checks against local vars - for (int v = 0; v < (int)variables.size(); v++) - if (varName == split(variables[v], ' ')[1]) - { - return variableVals[v]; - } + auto iB = globalVariableValues.find(varName); + if (iB != globalVariableValues.end()) + { + return iB->second; + } + else + { + return varName; + } } - - return varName; } -vector VarValues(vector varNames, vector& variables, vector& variableVals) +bool IsVar(string varName, unordered_map& variableVals) { - vector realValues; + auto iA = variableVals.find(varName); + if (iA != variableVals.end()) + { + return true; + } + return false; +} + +vector VarValues(vector varNames, unordered_map& variableVals) +{ + vector realValues; for (int varIndex = 0; varIndex < varNames.size(); varIndex++) { varNames[varIndex] = trim(varNames[varIndex]); - string typ = "string"; - bool isVar = false; - - if (count(varNames[varIndex], '\"') == 0) + auto iA = variableVals.find(varNames[varIndex]); + if (iA != variableVals.end()) { - // Checks against global vars - for (int v = 0; v < (int)globalVariables.size(); v++) - if (varNames[varIndex] == split(globalVariables[v], ' ')[1]) - { - typ = split(globalVariables[v], ' ')[0]; - isVar = true; - } - // Checks against local vars - for (int v = 0; v < (int)variables.size(); v++) - if (varNames[varIndex] == split(variables[v], ' ')[1]) - { - typ = split(variables[v], ' ')[0]; - isVar = true; - } - } - - // If it is a var - if (isVar) - { - // Checks against global vars - for (int v = 0; v < (int)globalVariables.size(); v++) - if (varNames[varIndex] == split(globalVariables[v], ' ')[1]) - { - realValues.push_back(globalVariableValues[v]); - } - // Checks against local vars - for (int v = 0; v < (int)variables.size(); v++) - if (varNames[varIndex] == split(variables[v], ' ')[1]) - { - realValues.push_back(variableVals[v]); - } + realValues.push_back(iA->second); } else - realValues.push_back(varNames[varIndex]); + { + auto iB = globalVariableValues.find(varNames[varIndex]); + if (iB != globalVariableValues.end()) + { + realValues.push_back(iB->second); + } + else + { + realValues.push_back(varNames[varIndex]); + } + } } return realValues; } -int IsFunction(string funcName) +string IsFunction(string funcName) { - // Iterate through all functions - for (int t = 0; t < (int)functions.size(); t++) - if (trim(funcName) == split(functions[t], ' ')[0]) - return t; - - return -1; + auto iA = functionValues.find(funcName); + if (iA != functionValues.end()) + { + return iA->first; + } + else + { + return ""; + } } -string EvalExpression(string expression, vector& variables, vector& variableVals) +string EvalExpression(string expression, unordered_map& variableVals) { expression = trim(expression); bool inQuotes = false; @@ -207,7 +176,7 @@ string EvalExpression(string expression, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variableVals) +bool BooleanLogic(string valA, string determinant, string valB, unordered_map& variableVals) { - string valARealValue = EvalExpression(valA, variables, variableVals); - string valBRealValue = EvalExpression(valB, variables, variableVals); + string valARealValue = EvalExpression(valA, variableVals); + string valBRealValue = EvalExpression(valB, variableVals); if (determinant == "==") return valARealValue == valBRealValue; @@ -345,51 +314,44 @@ bool BooleanLogic(string valA, string determinant, string valB, vector& return false; } -int evalEqu(vector str, vector& variables, vector& variableValues) +int evalEqu(vector str, unordered_map& variableValues) { - // Iterate all existing local variable names - for (int v = 0; v < (int)variables.size(); v++) + if (IsVar(str[0], variableValues)) { - if (str[0] == split(variables[v], ' ')[1]) - { - if (str[1] == "=") - variableValues[v] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variables, variableValues); - else if (str[1] == "+=") - variableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "-=") - variableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "*=") - variableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "/=") - variableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); + if (str[1] == "=") + variableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + else if (str[1] == "+=") + variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "-=") + variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "*=") + variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "/=") + variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); - //cout << variables[v] << " is " << variableValues[v] << endl; - return 0; - } + //cout << variables[v] << " is " << variableValues[v] << endl; + return 0; } - // Iterate all existing global variable names - for (int v = 0; v < (int)globalVariables.size(); v++) + else if (IsVar(str[0], globalVariableValues)) { - if (str[0] == split(globalVariables[v], ' ')[1]) - { - if (str[1] == "=") - globalVariableValues[v] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variables, variableValues); - else if (str[1] == "+=") - globalVariableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "-=") - globalVariableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "*=") - globalVariableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "/=") - globalVariableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); + if (str[1] == "=") + globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + else if (str[1] == "+=") + globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "-=") + globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "*=") + globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "/=") + globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); - //cout << variables[v] << " is " << variableValues[v] << endl; - return 0; - } + //cout << variables[v] << " is " << variableValues[v] << endl; + return 0; } + return 1; } -string ProcessLine(vector> words, int lineNum, vector& variables, vector& variableValues) +string ProcessLine(vector> words, int lineNum, unordered_map& variableValues) { if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') return ""; @@ -595,8 +557,7 @@ string ExecuteFunction(string functionName, vector inputVarVals, int fun else functionLines = functionValues[functionIndex]; - vector variables; - vector variableValues; + unordered_map variableValues; vector functionNameParts = split(replace(functions[functionIndex], functionName + " ", ""), ','); for (int i = 0; i < (int)inputVarVals.size(); i++) { @@ -716,4 +677,4 @@ int main(int argc, char* argv[]) parseSlang(scriptString.str()); return 0; -} \ No newline at end of file +} From f796358af67a4a9f31318458cfa2bca10a9ddd1d Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 12:05:54 -0500 Subject: [PATCH 02/33] Removed for loops in place of .substr() --- Slang/Main.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index f392206..537341e 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -38,8 +38,7 @@ string StringRaw(string str) if (str[0] != '\"') withoutQuotes += str[0]; - for (int ch = 1; ch < (int)str.size() - 1; ch++) - withoutQuotes += str[ch]; + withoutQuotes += str.substr(1, str.size()-2); if (str[str.size() - 1] != '\"') withoutQuotes += str[str.size() - 1]; @@ -72,8 +71,7 @@ string RMParenthesis(string str) if (str[0] != '(') withoutParenthesis += str[0]; - for (int ch = 1; ch < (int)str.size() - 1; ch++) - withoutParenthesis += str[ch]; + withoutParenthesis += str.substr(1, str.size()-2); if (str[str.size() - 1] != ')') withoutParenthesis += str[str.size() - 1]; From f4a5f3d0e2b9296b8c15c15c7f3d24e383550943 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 13:19:35 -0500 Subject: [PATCH 03/33] Continue converting to "any" type --- Slang/Main.cpp | 136 +++++++++++++++++++++---------------------------- 1 file changed, 58 insertions(+), 78 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 537341e..8b319e7 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -79,7 +79,7 @@ string RMParenthesis(string str) return withoutParenthesis; } -string GetVariableValue(string varName, unordered_map& variableVals) +any GetVariableValue(string varName, unordered_map& variableVals) { auto iA = variableVals.find(varName); if (iA != variableVals.end()) @@ -110,7 +110,7 @@ bool IsVar(string varName, unordered_map& variableVals) return false; } -vector VarValues(vector varNames, unordered_map& variableVals) +vector VarValues(vector varNames, unordered_map& variableVals) { vector realValues; @@ -140,20 +140,20 @@ vector VarValues(vector varNames, unordered_map& va return realValues; } -string IsFunction(string funcName) +bool IsFunction(string funcName) { auto iA = functionValues.find(funcName); if (iA != functionValues.end()) { - return iA->first; + return true; } else { - return ""; + return false; } } -string EvalExpression(string expression, unordered_map& variableVals) +any EvalExpression(string expression, unordered_map& variableVals) { expression = trim(expression); bool inQuotes = false; @@ -174,8 +174,7 @@ string EvalExpression(string expression, unordered_map& variableVal y++; } //cout << split(expression, '(')[0] << " " << argContents << endl; - string returnVal = ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableVals), funcIndex); - return returnVal; + return ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableVals), funcIndex); } else if (split(expression, '.')[0] == "CPP" && !inQuotes) { @@ -188,8 +187,7 @@ string EvalExpression(string expression, unordered_map& variableVal y++; } //cout << split(expression, '(')[0] << " " << unWrapVec(VarValues(split(argContents, ','), variables, variableVals)) << endl; - string returnVal = CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableVals)); - return returnVal; + return CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableVals)); } else return GetVariableValue(expression, variableVals); @@ -288,7 +286,7 @@ string EvalExpression(string expression, unordered_map& variableVal return Quoted(withoutParenthesis); } else - return to_string(evaluate(newExpression)); + return stof(evaluate(newExpression)); } bool BooleanLogic(string valA, string determinant, string valB, unordered_map& variableVals) @@ -319,13 +317,13 @@ int evalEqu(vector str, unordered_map& variableValues) if (str[1] == "=") variableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "+=") - variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + variableValues[str[0]] += EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "-=") - variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + variableValues[str[0]] -= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "*=") - variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + variableValues[str[0]] *= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "/=") - variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + variableValues[str[0]] /= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); //cout << variables[v] << " is " << variableValues[v] << endl; return 0; @@ -335,13 +333,13 @@ int evalEqu(vector str, unordered_map& variableValues) if (str[1] == "=") globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "+=") - globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + globalVariableValues[str[0]] += EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "-=") - globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + globalVariableValues[str[0]] -= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "*=") - globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + globalVariableValues[str[0]] *= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "/=") - globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + globalVariableValues[str[0]] /= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); //cout << variables[v] << " is " << variableValues[v] << endl; return 0; @@ -349,72 +347,54 @@ int evalEqu(vector str, unordered_map& variableValues) return 1; } -string ProcessLine(vector> words, int lineNum, unordered_map& variableValues) +any ProcessLine(vector> words, int lineNum, unordered_map& variableValues) { if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') - return ""; + return 0; + // If print statement (deprecated, now use CPP.System.Print() function) if (words[lineNum][0] == "print") { - cout << StringRaw(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues)) << endl; - return ""; + cout << StringRaw(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; + return 0; } - if (words[lineNum][0] == "return") { - //cout << StringRaw(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end()))) << endl; - return EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues); - } + // Check if function return + if (words[lineNum][0] == "return") + return EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues); - if (split(words[lineNum][0], '.')[0] == "CPP") + // Check if it is CPP Builtin function + if (words[lineNum][0][0] == 'C' && words[lineNum][0][1] == 'P' && words[lineNum][0][2] == 'P' && words[lineNum][0][3] == '.') + return EvalExpression(unWrapVec(words[lineNum]), variableValues); + + // Check if it is function + auto iA = functionValues.find(split(functions[t], ' ')[0]); + if (iA != functionValues.end()) { - string output = EvalExpression(unWrapVec(words[lineNum]), variables, variableValues); - return output; + ExecuteFunction(split(functions[t], ' ')[0], VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), split(functions[t], ' ')[0], "")), ','), variableValues), t); + return 0; } - - // Iterate through all functions - for (int t = 0; t < (int)functions.size(); t++) - { - if (split(words[lineNum][0], '(')[0] == split(functions[t], ' ')[0]) - { - ExecuteFunction(split(functions[t], ' ')[0], VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), split(functions[t], ' ')[0], "")), ','), variables, variableValues), t); - return ""; - } - } - - // First iterate through all types to see if line - // inits a variable then store it with it's value + + // Iterate through all types to see if line inits or + // re-inits a variable then store it with it's value for (int t = 0; t < (int)types.size(); t++) { if (words[lineNum][0] == types[t]) { - //Checks if it is a re-init of an existing variable - for (int v = 0; v < (int)variables.size(); v++) - { - if (words[lineNum][1] == split(variables[v], ' ')[1]) - { - evalEqu(vector(words[lineNum].begin() + 1, words[lineNum].end()), variables, variableValues); - - return ""; - } - } - - //Checks if it is variable - variables.push_back(words[lineNum][0] + " " + words[lineNum][1]); - variableValues.push_back(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 3, words[lineNum].end())), variables, variableValues)); - //cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl; - return ""; + variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); + return 0; } } - // Second, iterate all existing variable names - for (int v = 0; v < (int)variables.size(); v++) + + // Check existing variables + auto iB = variableValues.find(words[lineNum][0]); + if (iB != functionValues.end()) { - if (words[lineNum][0] == split(variables[v], ' ')[1]) - { - evalEqu(vector(words[lineNum].begin(), words[lineNum].end()), variables, variableValues); - - return ""; - } + // Evaluates what the sign (ex. '=', '+=') does to the value on the left by the value on the right + evalEqu(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); + return 0; } + // Gathers while loop contents if (words[lineNum][0] == "while") { @@ -442,17 +422,17 @@ string ProcessLine(vector> words, int lineNum, unordered_map> words, int lineNum, unordered_map> words, int lineNum, unordered_map> words, int lineNum, unordered_map inputVarVals, int functionIndex) From e17b89b97256328e51259d4b9a12accb4a3523c3 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 13:24:41 -0500 Subject: [PATCH 04/33] Update builtin.h --- Slang/builtin.h | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Slang/builtin.h b/Slang/builtin.h index 06bcab3..e69ef7c 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "strops.h" #include "graphics.h" @@ -21,6 +22,7 @@ vector builtinVarVals; Parser mainWindow; +// Initial script processing, which loads variables and functions from builtin int GetBuiltins(string script) { script = replace(script, " ", "\t"); @@ -83,16 +85,17 @@ int GetBuiltins(string script) return 0; } -string CPPFunction(string name, vector args) +// Executes +any CPPFunction(string name, vector args) { if (name == "CPP.Math.Sin") - return to_string(sin(stof(args[0]))); + return sin(stof(args[0])); else if (name == "CPP.Math.Cos") - return to_string(cos(stof(args[0]))); + return cos(stof(args[0])); else if (name == "CPP.Math.Tan") - return to_string(tan(stof(args[0]))); + return tan(stof(args[0])); else if (name == "CPP.Math.Round") - return to_string(round(stof(args[0]))); + return round(stof(args[0])); else if (name == "CPP.Graphics.Init") { cout << "\x1B[32mInit graphics\033[0m\t\t" << endl; @@ -101,8 +104,12 @@ string CPPFunction(string name, vector args) } else if (name == "CPP.Graphics.SetPixel") mainWindow.Draw(stoi(args[0]), stoi(args[1]), olc::Pixel(stoi(args[2]), stoi(args[3]), stoi(args[4]))); + else if (name == "CPP.System.Print") + cout << stoi(args[0]); + else if (name == "CPP.System.PrintLine") + cout << stoi(args[0]) << endl; - return ""; + return 0; } -#endif \ No newline at end of file +#endif From e72f58810e621c00688404b9084d0a69fad83ea2 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 13:32:34 -0500 Subject: [PATCH 05/33] Update builtin.slg --- Slang/builtin.slg | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Slang/builtin.slg b/Slang/builtin.slg index 8cb78e8..ef57819 100644 --- a/Slang/builtin.slg +++ b/Slang/builtin.slg @@ -1,6 +1,9 @@ +// Default variables, can be overwritten +// if re-initialized or changed float PI = 3.14159265358979 float EulersNumber = 2.71828183 +// Trigonometric function Sin float Sin(float input) { print input @@ -8,36 +11,42 @@ float Sin(float input) return out } +// Trigonometric function Cos float Cos(float input) { float out = CPP.Math.Cos(input) return out } +// Trigonometric function Tan float Tan(float input) { float out = CPP.Math.Tan(input) return out } +// Sigmoid activation function float Sigmoid(float input) { float out = 1 / (1+EulersNumber^-input) return out } +// Hyperbolic tangent activation function float Tanh(float input) { float out = ((EulersNumber^input)-(EulersNumber^-input))/((EulersNumber^input)+(EulersNumber^-input)) return out } +// Rounds input to nearest integer value float Round(float input) { float out = CPP.Math.Round(input) return out } +// Clamps input between min and max float Clamp(float input, float min, float max) { if input < min @@ -56,4 +65,18 @@ float SetPixel(int x, int y, int r, int g, int b) { string out = CPP.Graphics.SetPixel(x, y, r, g, b) return out -} \ No newline at end of file +} + +// Prints input value to console +float Print(string in) +{ + string out = CPP.System.Print(in) + return out +} + +// Prints input value to console with appended newline '\n' +float Printl(string in) +{ + string out = CPP.System.PrintLine(in) + return out +} From aba94781bad830be7e0f382c73b052d959e9a7d3 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 13:34:31 -0500 Subject: [PATCH 06/33] Update main.h --- Slang/main.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Slang/main.h b/Slang/main.h index cf8006c..59e3071 100644 --- a/Slang/main.h +++ b/Slang/main.h @@ -4,6 +4,6 @@ using namespace std; -string ExecuteFunction(string functionName, vector inputVarVals, int functionIndex); +any ExecuteFunction(string functionName, vector inputVarVals, int functionIndex); -#endif \ No newline at end of file +#endif From 93eff0b337c16671bb5a2b729c95ee18bda61973 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 13:52:54 -0500 Subject: [PATCH 07/33] Updated functions and var gathering --- Slang/Main.cpp | 55 ++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 8b319e7..4f4939b 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "eval.h" #include "strops.h" #include "builtin.h" @@ -26,6 +27,14 @@ bool isNumber(const string& str) return true; } +bool stob(string str) { + transform(str.begin(), str.end(), str.begin(), ::tolower); + istringstream is(str); + bool b; + is >> boolalpha >> b; + return b; +} + string StringRaw(string str) { str = trim(str); @@ -174,7 +183,7 @@ any EvalExpression(string expression, unordered_map& variableVals) y++; } //cout << split(expression, '(')[0] << " " << argContents << endl; - return ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableVals), funcIndex); + return ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableVals)); } else if (split(expression, '.')[0] == "CPP" && !inQuotes) { @@ -223,7 +232,7 @@ any EvalExpression(string expression, unordered_map& variableVals) i++; } - string returnVal = ExecuteFunction(name, VarValues(split(argContents, ','), variableVals), funcIndex); + string returnVal = ExecuteFunction(name, VarValues(split(argContents, ','), variableVals)); newExpression += returnVal; //cout << newExpression << endl; } @@ -371,7 +380,7 @@ any ProcessLine(vector> words, int lineNum, unordered_map> words, int lineNum, unordered_map inputVarVals, int functionIndex) +any ExecuteFunction(string functionName, vector inputVarVals) { //vector inputVarVals = split(replace(inVals, " ", ""), ','); vector functionLines; - //Get index of function - if (functionIndex == -1) - { - for (int f = 0; f < (int)functions.size(); f++) - if (split(functions[f], ' ')[0] == functionName) - { - functionLines = functionValues[f]; - functionIndex = f; - break; - } - } - else - functionLines = functionValues[functionIndex]; + + // Get contents of function + functionLines = functionValues[functionName]; unordered_map variableValues; vector functionNameParts = split(replace(functions[functionIndex], functionName + " ", ""), ','); @@ -573,9 +572,7 @@ int parseSlang(string script) vector lines = split(script, '\n'); vector> words; for (int i = 0; i < (int)lines.size(); i++) - { words.push_back(split(lines[i], ' ')); - } // 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 @@ -613,37 +610,43 @@ int parseSlang(string script) } } functionContents = removeTabs(functionContents, 1); - functions.push_back(functName); - functionValues.push_back(functionContents); + functionValues[functName] = functionContents; //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; } //Checks if it is variable else { - globalVariables.push_back(words[lineNum][0] + " " + words[lineNum][1]); - globalVariableValues.push_back((string)words[lineNum][3]); + if(words[lineNum][0] == "string") + globalVariableValues[words[lineNum][1]] = words[lineNum][3]; + else if(words[lineNum][0] == "int") + globalVariableValues[words[lineNum][1]] = stoi(words[lineNum][3]); + else if(words[lineNum][0] == "float") + globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]); + else if(words[lineNum][0] == "bool") + globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]); //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl; } } // Executes main, which is the starting function - ExecuteFunction("Main", vector {"\"hi\"", "0"}, -1); + ExecuteFunction("Main", vector {"hi", 0}); return 0; } int main(int argc, char* argv[]) { + // Get builtin script contents ifstream builtin("../Slang/builtin.slg"); stringstream builtinString; builtinString << builtin.rdbuf(); + // Gathers builtin functions and variables GetBuiltins(builtinString.str()); - functions = builtinFunctions; functionValues = builtinFunctionValues; - globalVariables = builtinVars; globalVariableValues = builtinVarVals; + // Get default script contents ifstream script("../Slang/script.slg"); stringstream scriptString; scriptString << script.rdbuf(); From a25df38803b5b59d29a240677091c7a1ad2a0282 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 13:58:14 -0500 Subject: [PATCH 08/33] Update builtin.h --- Slang/builtin.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Slang/builtin.h b/Slang/builtin.h index e69ef7c..6f396a4 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -15,10 +15,8 @@ using namespace std; vector types = { "int", "float", "string", "bool", "void", "null" }; -vector builtinFunctions; -vector> builtinFunctionValues; -vector builtinVars; -vector builtinVarVals; +unordered_map> builtinFunctionValues; +unordered_map& builtinVarVals; Parser mainWindow; @@ -70,14 +68,19 @@ int GetBuiltins(string script) } } functionContents = removeTabs(functionContents, 1); - builtinFunctions.push_back(functName); - builtinFunctionValues.push_back(functionContents); + builtinFunctionValues[functName] = functionContents; } //Checks if it is variable else { - builtinVars.push_back(words[lineNum][0] + " " + words[lineNum][1]); - builtinVarVals.push_back((string)words[lineNum][3]); + if(words[lineNum][0] == "string") + builtinVarVals[words[lineNum][1]] = words[lineNum][3]; + else if(words[lineNum][0] == "int") + builtinVarVals[words[lineNum][1]] = stoi(words[lineNum][3]); + else if(words[lineNum][0] == "float") + builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]); + else if(words[lineNum][0] == "bool") + builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]); //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl; } } From c360238d784736dc7b150c2d1e65ec3e00c43051 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 18:33:07 -0500 Subject: [PATCH 09/33] Continued conversion into 'any' variable and function storage --- Slang/Main.cpp | 85 +++++++++---------- Slang/Slang.vcxproj | 2 + Slang/anyops.h | 193 ++++++++++++++++++++++++++++++++++++++++++++ Slang/builtin.h | 19 ++--- 4 files changed, 249 insertions(+), 50 deletions(-) create mode 100644 Slang/anyops.h diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 4f4939b..abfc82f 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -7,11 +7,14 @@ #include #include #include +#include +#include + #include "eval.h" #include "strops.h" #include "builtin.h" #include "main.h" -#include +#include "anyops.h" using namespace std; using namespace boost; @@ -19,6 +22,7 @@ using namespace boost; unordered_map globalVariableValues; unordered_map> functionValues; + bool isNumber(const string& str) { for (char const& c : str) { @@ -221,8 +225,8 @@ any EvalExpression(string expression, unordered_map& variableVals) } //string varVal = GetVariableValue(name, variables, variableVals); - any funcPointer = IsFunction(name); - if (funcIndex != -1 && !inQuotes) + bool isFunc = IsFunction(name); + if (isFunc && !inQuotes) { string argContents = ""; i++; @@ -232,7 +236,7 @@ any EvalExpression(string expression, unordered_map& variableVals) i++; } - string returnVal = ExecuteFunction(name, VarValues(split(argContents, ','), variableVals)); + string returnVal = AnyAsString(ExecuteFunction(name, VarValues(split(argContents, ','), variableVals))); newExpression += returnVal; //cout << newExpression << endl; } @@ -247,7 +251,7 @@ any EvalExpression(string expression, unordered_map& variableVals) y++; } //cout << split(expression, '(')[0] << " " << argContents << endl; - string returnVal = CPPFunction(split(name, '(')[0], VarValues(split(argContents, ',') variableVals)); + string returnVal = AnyAsString(CPPFunction(split(name, '(')[0], VarValues(split(argContents, ','), variableVals))); newExpression += returnVal; } else @@ -255,7 +259,7 @@ any EvalExpression(string expression, unordered_map& variableVals) if (inQuotes) newExpression += name; else - newExpression += GetVariableValue(name, variableVals); + newExpression += AnyAsString(GetVariableValue(name, variableVals)); } i--; @@ -295,26 +299,26 @@ any EvalExpression(string expression, unordered_map& variableVals) return Quoted(withoutParenthesis); } else - return stof(evaluate(newExpression)); + return evaluate(newExpression); } bool BooleanLogic(string valA, string determinant, string valB, unordered_map& variableVals) { - string valARealValue = EvalExpression(valA, variableVals); - string valBRealValue = EvalExpression(valB, variableVals); + any valARealValue = EvalExpression(valA, variableVals); + any valBRealValue = EvalExpression(valB, variableVals); if (determinant == "==") - return valARealValue == valBRealValue; + return AnyAsString(valARealValue) == AnyAsString(valBRealValue); if (determinant == "!=") - return valARealValue != valBRealValue; + return AnyAsString(valARealValue) != AnyAsString(valBRealValue); if (determinant == ">=") - return floatval(valARealValue) >= floatval(valBRealValue); + return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue); if (determinant == "<=") - return floatval(valARealValue) <= floatval(valBRealValue); + return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue); if (determinant == ">") - return floatval(valARealValue) > floatval(valBRealValue); + return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); if (determinant == "<") - return floatval(valARealValue) < floatval(valBRealValue); + return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); return false; } @@ -326,13 +330,13 @@ int evalEqu(vector str, unordered_map& variableValues) if (str[1] == "=") variableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "+=") - variableValues[str[0]] += EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + variableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); else if (str[1] == "-=") - variableValues[str[0]] -= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); else if (str[1] == "*=") - variableValues[str[0]] *= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); else if (str[1] == "/=") - variableValues[str[0]] /= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); //cout << variables[v] << " is " << variableValues[v] << endl; return 0; @@ -342,13 +346,13 @@ int evalEqu(vector str, unordered_map& variableValues) if (str[1] == "=") globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); else if (str[1] == "+=") - globalVariableValues[str[0]] += EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + globalVariableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); else if (str[1] == "-=") - globalVariableValues[str[0]] -= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); else if (str[1] == "*=") - globalVariableValues[str[0]] *= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); else if (str[1] == "/=") - globalVariableValues[str[0]] /= EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); //cout << variables[v] << " is " << variableValues[v] << endl; return 0; @@ -359,13 +363,13 @@ int evalEqu(vector str, unordered_map& variableValues) any ProcessLine(vector> words, int lineNum, unordered_map& variableValues) { if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') - return 0; + return ""; // If print statement (deprecated, now use CPP.System.Print() function) if (words[lineNum][0] == "print") { - cout << StringRaw(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; - return 0; + cout << AnyAsString(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; + return ""; } // Check if function return @@ -377,11 +381,11 @@ any ProcessLine(vector> words, int lineNum, unordered_map> words, int lineNum, unordered_map(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); - return 0; + return ""; } } // Check existing variables auto iB = variableValues.find(words[lineNum][0]); - if (iB != functionValues.end()) + if (iB != variableValues.end()) { // Evaluates what the sign (ex. '=', '+=') does to the value on the left by the value on the right evalEqu(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); - return 0; + return ""; } // Gathers while loop contents @@ -436,12 +440,12 @@ any ProcessLine(vector> words, int lineNum, unordered_map> words, int lineNum, unordered_map> words, int lineNum, unordered_map inputVarVals) @@ -535,11 +539,10 @@ any ExecuteFunction(string functionName, vector inputVarVals) functionLines = functionValues[functionName]; unordered_map variableValues; - vector functionNameParts = split(replace(functions[functionIndex], functionName + " ", ""), ','); + vector functionNameParts = split(replace(functionValues[functionName], functionName + " ", ""), ','); for (int i = 0; i < (int)inputVarVals.size(); i++) { - variables.push_back(trim(functionNameParts[i])); - variableValues.push_back(EvalExpression(inputVarVals[i], variables, variableValues)); + variableValues[trim(functionNameParts[i])] = EvalExpression(inputVarVals[i], variables, variableValues); //cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl; } vector> words; diff --git a/Slang/Slang.vcxproj b/Slang/Slang.vcxproj index 23400e3..4f43d62 100644 --- a/Slang/Slang.vcxproj +++ b/Slang/Slang.vcxproj @@ -89,10 +89,12 @@ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true OldStyle + D:\Code\boost Console true + D:\Code\boost diff --git a/Slang/anyops.h b/Slang/anyops.h new file mode 100644 index 0000000..f86cc6f --- /dev/null +++ b/Slang/anyops.h @@ -0,0 +1,193 @@ + +#ifndef ANYOPS_H +#define ANYOPS_H + +using namespace boost; + +// Will convert type 'any' val to a bool +bool AnyAsBool(any val) +{ + try // Try converting to bool + { + return any_cast(val); + } + catch (const std::exception&) + { + try // Try converting to string + { + return any_cast(val) == "true"; + } + catch (const std::exception&) + { + try // Try converting to float + { + return any_cast(val) == 1.0f; + } + catch (const std::exception&) // Try converting to int + { + try + { + return any_cast(val) == 1; + } + catch (const std::exception&) // Does not convert, return + { + return false; + } + } + } + } + return false; +} + +// Will convert type 'any' val to a string +string AnyAsString(any val) +{ + try // Try converting to string + { + return any_cast(val); + } + catch (const std::exception&) + { + try // Try converting to int + { + return to_string(any_cast(val)); + } + catch (const std::exception&) + { + try // Try converting to float + { + return to_string(any_cast(val)); + } + catch (const std::exception&) // Try converting to bool + { + try + { + string i = "false"; + if (any_cast(val) == true) i = "true"; + return i; + } + catch (const std::exception&) // Does not convert, return + { + return ""; + } + } + } + } + return ""; +} + +// Will convert type 'any' val to a float +float AnyAsFloat(any val) +{ + try // Try converting to float + { + return any_cast(val); + } + catch (const std::exception&) + { + try // Try converting to int + { + return (float)any_cast(val); + } + catch (const std::exception&) + { + try // Try converting to string, then converting it to float + { + return stof(any_cast(val)); + } + catch (const std::exception&) // Try converting to bool + { + try + { + float i = 0; + if (any_cast(val) == true) i = 1; + return i; + } + catch (const std::exception&) // Does not convert, return + { + return 0; + } + } + } + } + return 0; +} + +// Will convert type 'any' val to an integer +int AnyAsInt(any val) +{ + try // Try converting to int + { + return any_cast(val); + } + catch (const std::exception&) + { + try // Try converting to float + { + return (int)any_cast(val); + } + catch (const std::exception&) + { + try // Try converting to string, then converting it to int + { + return stoi(any_cast(val)); + } + catch (const std::exception&) // Try converting to bool + { + try + { + int i = 0; + if (any_cast(val) == true) i = 1; + return i; + } + catch (const std::exception&) // Does not convert, return + { + return 0; + } + } + } + } + return 0; +} + +// Gets type of 'any' val +// 0 -> int; 1 -> float; 2 -> bool; 3 -> string; +int any_type(any val) +{ + try // Try converting to int + { + int i = any_cast(val); + return 0; + } + catch (const std::exception&) + { + try // Try converting to float + { + float f = any_cast(val); + return 1; + } + catch (const std::exception&) + { + try // Try converting to bool + { + bool b = any_cast(val); + return 2; + } + catch (const std::exception&) // Try converting to string + { + try + { + string s = any_cast(val); + return 3; + } + catch (const std::exception&) // Does not convert, return + { + return -1; + } + } + } + } + return -1; +} + +#endif \ No newline at end of file diff --git a/Slang/builtin.h b/Slang/builtin.h index 6f396a4..417655e 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -10,6 +10,7 @@ #include #include "strops.h" #include "graphics.h" +#include "anyops.h" using namespace std; @@ -89,28 +90,28 @@ int GetBuiltins(string script) } // Executes -any CPPFunction(string name, vector args) +any CPPFunction(string name, vector args) { if (name == "CPP.Math.Sin") - return sin(stof(args[0])); + return sin(AnyAsFloat(args[0])); else if (name == "CPP.Math.Cos") - return cos(stof(args[0])); + return cos(AnyAsFloat(args[0])); else if (name == "CPP.Math.Tan") - return tan(stof(args[0])); + return tan(AnyAsFloat(args[0])); else if (name == "CPP.Math.Round") - return round(stof(args[0])); + return AnyAsInt(args[0]); else if (name == "CPP.Graphics.Init") { cout << "\x1B[32mInit graphics\033[0m\t\t" << endl; - if (mainWindow.Construct(stoi(args[0]), stoi(args[1]), stoi(args[2]), stoi(args[2]))) + if (mainWindow.Construct(AnyAsInt(args[0]), AnyAsInt(args[1]), AnyAsInt(args[2]), AnyAsInt(args[2]))) mainWindow.Start(); } else if (name == "CPP.Graphics.SetPixel") - mainWindow.Draw(stoi(args[0]), stoi(args[1]), olc::Pixel(stoi(args[2]), stoi(args[3]), stoi(args[4]))); + mainWindow.Draw(AnyAsInt(args[0]), AnyAsInt(args[1]), olc::Pixel(AnyAsInt(args[2]), AnyAsInt(args[3]), AnyAsInt(args[4]))); else if (name == "CPP.System.Print") - cout << stoi(args[0]); + cout << AnyAsString(args[0]); else if (name == "CPP.System.PrintLine") - cout << stoi(args[0]) << endl; + cout << AnyAsString(args[0]) << endl; return 0; } From 83404f266f3c737e45687738af13c107725233f8 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 08:37:28 -0500 Subject: [PATCH 10/33] Changed many function inputs to const Removed unnecessary loops in ExecuteFunction which used a lot of time. Function arguments also now no longer require a type, just a name. Also, functions don't need a pre-defined return value (ex. void or int), and just have a functions declarer 'func' before the name. --- Slang/Main.cpp | 97 +++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 52 deletions(-) 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; } From 1cc2e9abe76fb093ceda9b1f5b60ae3a95b4642f Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 08:41:25 -0500 Subject: [PATCH 11/33] Updated to new function syntax --- Slang/builtin.slg | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Slang/builtin.slg b/Slang/builtin.slg index ef57819..76b3943 100644 --- a/Slang/builtin.slg +++ b/Slang/builtin.slg @@ -4,7 +4,7 @@ float PI = 3.14159265358979 float EulersNumber = 2.71828183 // Trigonometric function Sin -float Sin(float input) +func Sin(input) { print input float out = CPP.Math.Sin(input) @@ -12,42 +12,42 @@ float Sin(float input) } // Trigonometric function Cos -float Cos(float input) +func Cos(input) { float out = CPP.Math.Cos(input) return out } // Trigonometric function Tan -float Tan(float input) +func Tan(input) { float out = CPP.Math.Tan(input) return out } // Sigmoid activation function -float Sigmoid(float input) +func Sigmoid(input) { float out = 1 / (1+EulersNumber^-input) return out } // Hyperbolic tangent activation function -float Tanh(float input) +func Tanh(input) { float out = ((EulersNumber^input)-(EulersNumber^-input))/((EulersNumber^input)+(EulersNumber^-input)) return out } // Rounds input to nearest integer value -float Round(float input) +func Round(input) { float out = CPP.Math.Round(input) return out } // Clamps input between min and max -float Clamp(float input, float min, float max) +func Clamp(input, min, max) { if input < min { @@ -61,21 +61,21 @@ float Clamp(float input, float min, float max) } // Sets color of pixel to RGB value -float SetPixel(int x, int y, int r, int g, int b) +func SetPixel(x, y, r, g, b) { string out = CPP.Graphics.SetPixel(x, y, r, g, b) return out } // Prints input value to console -float Print(string in) +func Print(in) { string out = CPP.System.Print(in) return out } // Prints input value to console with appended newline '\n' -float Printl(string in) +func Printl(in) { string out = CPP.System.PrintLine(in) return out From ddf09173e4ae86513d24a950fb237cde6c90353a Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 08:43:28 -0500 Subject: [PATCH 12/33] Updated to newest function syntax --- Slang/script.slg | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Slang/script.slg b/Slang/script.slg index 3543223..1332f21 100644 --- a/Slang/script.slg +++ b/Slang/script.slg @@ -1,6 +1,6 @@ -void Main(string input, int in) +func Main(input, in) { print "PI is: " + PI int x = 1 @@ -21,7 +21,7 @@ void Main(string input, int in) CPP.Graphics.Init(64, 64, 4) } -void Update(string input) +func Update(input) { int x = 0 int y = 0 @@ -36,4 +36,4 @@ void Update(string input) x += 1 } print "updating" -} \ No newline at end of file +} From 4fb5500cc34730acdbb7a0ce46fc9d6be858cdb1 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 08:50:33 -0500 Subject: [PATCH 13/33] Actually, these are the new function changes --- Slang/Main.cpp | 92 ++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 80a3b1c..ae9f50d 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -573,56 +573,60 @@ int parseSlang(string script) // 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++) - for (int t = 0; t < (int)types.size(); t++) - if (words[lineNum][0] == types[t]) - { - //Checks if it is function - if (words[lineNum][(int)words[lineNum].size() - 1][(int)words[lineNum][(int)words[lineNum].size() - 1].size() - 1] == ')') + { + //Checks if it is function + if (words[lineNum][0] == "func") + { + vector> functionContents; + + 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) { - vector> functionContents; - - 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) - { - args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ","; - } - else - { - args += replace(replace(words[lineNum][w], "(", " "), ")", ""); - } - } - - args = replace(args, functName + ",", ""); - functionContents.push_back(vector{args}); - - int numOfBrackets = 1; - for (int p = lineNum + 3; p < (int)words.size(); p++) - { - numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); - if (numOfBrackets == 0) - break; - functionContents.push_back(removeTabs(words[p], 1)); - } - functionValues[functName] = functionContents; - //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; + args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ","; } - //Checks if it is variable else { - if(words[lineNum][0] == "string") - globalVariableValues[words[lineNum][1]] = words[lineNum][3]; - else if(words[lineNum][0] == "int") - globalVariableValues[words[lineNum][1]] = stoi(words[lineNum][3]); - else if(words[lineNum][0] == "float") - globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]); - else if(words[lineNum][0] == "bool") - globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]); - //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl; + args += replace(replace(words[lineNum][w], "(", " "), ")", ""); } } + + args = replace(args, functName + ",", ""); + functionContents.push_back(vector{args}); + + int numOfBrackets = 1; + for (int p = lineNum + 3; p < (int)words.size(); p++) + { + numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); + if (numOfBrackets == 0) + break; + functionContents.push_back(removeTabs(words[p], 1)); + } + functionValues[functName] = functionContents; + //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; + } + else + for (int t = 0; t < (int)types.size(); t++) + if (words[lineNum][0] == types[t]) + { + + //Checks if it is variable + else + { + if(words[lineNum][0] == "string") + globalVariableValues[words[lineNum][1]] = words[lineNum][3]; + else if(words[lineNum][0] == "int") + globalVariableValues[words[lineNum][1]] = stoi(words[lineNum][3]); + else if(words[lineNum][0] == "float") + globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]); + else if(words[lineNum][0] == "bool") + globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]); + //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl; + } + } + } // Executes main, which is the starting function ExecuteFunction("Main", vector {"hi", 0}); From c705771eaa1ea77c1524af1650b620adba34c6f8 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 09:17:03 -0500 Subject: [PATCH 14/33] ProcessLine if else, inline exist checks ProcessLine now does checks with if else statements, which should hopefully improve performance, and exist checks are inline if statements, so a new variable doesn't need to be initialized for a single use. --- Slang/Main.cpp | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index ae9f50d..98e47a2 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -115,12 +115,10 @@ any GetVariableValue(const string varName, const unordered_map& var bool IsVar(const string varName, const unordered_map& variableVals) { - auto iA = variableVals.find(varName); - if (iA != variableVals.end()) - { + if (variableVals.find(varName) != variableVals.end()) return true; - } - return false; + else + return false; } vector VarValues(const vector varNames, const unordered_map& variableVals) @@ -155,15 +153,10 @@ vector VarValues(const vector varNames, const unordered_map& variableVals) @@ -366,23 +359,22 @@ any ProcessLine(const vector>& words, const int lineNum, unordere return; // If print statement (deprecated, now use CPP.System.Print() function) - if (words[lineNum][0] == "print") + else if (words[lineNum][0] == "print") { cout << AnyAsString(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; return; } // Check if function return - if (words[lineNum][0] == "return") + else if (words[lineNum][0] == "return") return EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues); // Check if it is CPP Builtin function - if (words[lineNum][0][0] == 'C' && words[lineNum][0][1] == 'P' && words[lineNum][0][2] == 'P' && words[lineNum][0][3] == '.') + else if (words[lineNum][0][0] == 'C' && words[lineNum][0][1] == 'P' && words[lineNum][0][2] == 'P' && words[lineNum][0][3] == '.') return EvalExpression(unWrapVec(words[lineNum]), variableValues); // Check if it is function - auto iA = functionValues.find(trim(split(words[lineNum][0], '(')[0])); - if (iA != functionValues.end()) + else if (IsFunction(trim(split(words[lineNum][0], '(')[0]))) { ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues)); return; @@ -390,26 +382,23 @@ any ProcessLine(const vector>& words, const int lineNum, unordere // Iterate through all types to see if line inits or // re-inits a variable then store it with it's value - for (int t = 0; t < (int)types.size(); t++) + else if (countInVector(types, words[lineNum][0]) > 0) { - if (words[lineNum][0] == types[t]) - { - variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); - return; - } + variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); + return; } - // Check existing variables - auto iB = variableValues.find(words[lineNum][0]); - if (iB != variableValues.end()) + // Check existing variables: If matches, then it means + // the variables value is getting changed with an operator + else if (IsVar(words[lineNum][0], variableValues)) { - // Evaluates what the sign (ex. '=', '+=') does to the value on the left by the value on the right + // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right evalEqu(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); return; } // Gathers while loop contents - if (words[lineNum][0] == "while") + else if (words[lineNum][0] == "while") { vector whileContents; vector whileParameters; @@ -447,8 +436,9 @@ any ProcessLine(const vector>& words, const int lineNum, unordere } return; } + // Gathers if statement contents - if (words[lineNum][0] == "if") + else if (words[lineNum][0] == "if") { vector ifContents; vector ifParameters; From 22fa7fd57f9560b48c2f39d1747c3930934e5979 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 09:24:21 -0500 Subject: [PATCH 15/33] Changed catch error type to boost::bad_any_cast --- Slang/anyops.h | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Slang/anyops.h b/Slang/anyops.h index f86cc6f..38851ed 100644 --- a/Slang/anyops.h +++ b/Slang/anyops.h @@ -11,25 +11,25 @@ bool AnyAsBool(any val) { return any_cast(val); } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to string { return any_cast(val) == "true"; } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to float { return any_cast(val) == 1.0f; } - catch (const std::exception&) // Try converting to int + catch (boost::bad_any_cast &e) // Try converting to int { try { return any_cast(val) == 1; } - catch (const std::exception&) // Does not convert, return + catch (boost::bad_any_cast &e) // Does not convert, return { return false; } @@ -46,19 +46,19 @@ string AnyAsString(any val) { return any_cast(val); } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to int { return to_string(any_cast(val)); } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to float { return to_string(any_cast(val)); } - catch (const std::exception&) // Try converting to bool + catch (boost::bad_any_cast &e) // Try converting to bool { try { @@ -66,7 +66,7 @@ string AnyAsString(any val) if (any_cast(val) == true) i = "true"; return i; } - catch (const std::exception&) // Does not convert, return + catch (boost::bad_any_cast &e) // Does not convert, return { return ""; } @@ -83,19 +83,19 @@ float AnyAsFloat(any val) { return any_cast(val); } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to int { return (float)any_cast(val); } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to string, then converting it to float { return stof(any_cast(val)); } - catch (const std::exception&) // Try converting to bool + catch (boost::bad_any_cast &e) // Try converting to bool { try { @@ -103,7 +103,7 @@ float AnyAsFloat(any val) if (any_cast(val) == true) i = 1; return i; } - catch (const std::exception&) // Does not convert, return + catch (boost::bad_any_cast &e) // Does not convert, return { return 0; } @@ -120,19 +120,19 @@ int AnyAsInt(any val) { return any_cast(val); } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to float { return (int)any_cast(val); } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to string, then converting it to int { return stoi(any_cast(val)); } - catch (const std::exception&) // Try converting to bool + catch (boost::bad_any_cast &e) // Try converting to bool { try { @@ -140,7 +140,7 @@ int AnyAsInt(any val) if (any_cast(val) == true) i = 1; return i; } - catch (const std::exception&) // Does not convert, return + catch (boost::bad_any_cast &e) // Does not convert, return { return 0; } @@ -159,28 +159,28 @@ int any_type(any val) int i = any_cast(val); return 0; } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to float { float f = any_cast(val); return 1; } - catch (const std::exception&) + catch (boost::bad_any_cast &e) { try // Try converting to bool { bool b = any_cast(val); return 2; } - catch (const std::exception&) // Try converting to string + catch (boost::bad_any_cast &e) // Try converting to string { try { string s = any_cast(val); return 3; } - catch (const std::exception&) // Does not convert, return + catch (boost::bad_any_cast &e) // Does not convert, return { return -1; } @@ -190,4 +190,4 @@ int any_type(any val) return -1; } -#endif \ No newline at end of file +#endif From 5df1c50c1650529f303b179b1c3c9133208c57c3 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 09:54:02 -0500 Subject: [PATCH 16/33] Added warnings and errors and created functions for cleaner code --- Slang/Main.cpp | 80 ++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 98e47a2..8703c86 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -31,7 +31,7 @@ bool isNumber(const string& str) return true; } -bool stob(const string str) { +bool stob(string str) { transform(str.begin(), str.end(), str.begin(), ::tolower); istringstream is(str); bool b; @@ -39,7 +39,7 @@ bool stob(const string str) { return b; } -string StringRaw(const string s) +string StringRaw(const string& s) { string str = trim(s); @@ -59,7 +59,7 @@ string StringRaw(const string s) return withoutQuotes; } -string Quoted(const string s) +string Quoted(const string& s) { string str = trim(s); @@ -76,7 +76,7 @@ string Quoted(const string s) return withQuotes; } -string RMParenthesis(const string s) +string RMParenthesis(const string& s) { string str = trim(s); string withoutParenthesis; @@ -92,7 +92,7 @@ string RMParenthesis(const string s) return withoutParenthesis; } -any GetVariableValue(const string varName, const 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(const string varName, const unordered_map& var } } -bool IsVar(const string varName, const unordered_map& variableVals) +bool IsVar(const string& varName, const unordered_map& variableVals) { if (variableVals.find(varName) != variableVals.end()) return true; @@ -121,37 +121,33 @@ bool IsVar(const string varName, const unordered_map& variableVals) return false; } -vector VarValues(const vector varNames, const unordered_map& variableVals) +vector VarValues(const vector& varNames, const unordered_map& variableVals) { vector realValues; for (int varIndex = 0; varIndex < varNames.size(); varIndex++) { - varNames[varIndex] = trim(varNames[varIndex]); + string varName = trim(varNames[varIndex]); - auto iA = variableVals.find(varNames[varIndex]); + auto iA = variableVals.find(varName); if (iA != variableVals.end()) { realValues.push_back(iA->second); } else { - auto iB = globalVariableValues.find(varNames[varIndex]); + auto iB = globalVariableValues.find(varName); if (iB != globalVariableValues.end()) - { realValues.push_back(iB->second); - } else - { - realValues.push_back(varNames[varIndex]); - } + realValues.push_back(varName); } } return realValues; } -bool IsFunction(const string funcName) +bool IsFunction(const string& funcName) { if (functionValues.find(funcName) != functionValues.end()) return true; @@ -159,7 +155,20 @@ bool IsFunction(const string funcName) return false; } -any EvalExpression(const string ex, const unordered_map& variableVals) +int LogWarning(const string& warningText) +{ + cerr << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl; + return 1; +} + +int CriticalError(const string& errorText) +{ + cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl; + exit(EXIT_FAILURE); + return 2; +} + +any EvalExpression(const string& ex, const unordered_map& variableVals) { string expression = trim(ex); bool inQuotes = false; @@ -295,7 +304,7 @@ any EvalExpression(const string ex, const unordered_map& variableVa return evaluate(newExpression); } -bool BooleanLogic(const string valA, const string determinant, const string valB, const 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); @@ -316,7 +325,7 @@ bool BooleanLogic(const string valA, const string determinant, const string valB return false; } -int evalEqu(const vector str, unordered_map& variableValues) +int evalEqu(const vector& str, unordered_map& variableValues) { if (IsVar(str[0], variableValues)) { @@ -350,6 +359,7 @@ int evalEqu(const vector str, unordered_map& variableValues //cout << variables[v] << " is " << variableValues[v] << endl; return 0; } + LogWarning("uninitialized variable or typo in \'" << str[0] << "\'"); return 1; } @@ -542,8 +552,7 @@ any ExecuteFunction(const string functionName, const vector inputVarVals) } catch (const std::exception&) { - cout << "\x1B[31mERROR: \'" << unWrapVec(words[lineNum]) << "\'\nIn function: " << functionName << "\nLine: " << lineNum << "\033[0m\t\t" << endl; - exit(1); + CriticalError("\'" << unWrapVec(words[lineNum]) << "\'\nIn function: " << functionName << "\nLine: " << lineNum); } if (!returnVal.empty()) return returnVal; @@ -598,24 +607,17 @@ int parseSlang(string script) //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; } else - for (int t = 0; t < (int)types.size(); t++) - if (words[lineNum][0] == types[t]) - { - - //Checks if it is variable - else - { - if(words[lineNum][0] == "string") - globalVariableValues[words[lineNum][1]] = words[lineNum][3]; - else if(words[lineNum][0] == "int") - globalVariableValues[words[lineNum][1]] = stoi(words[lineNum][3]); - else if(words[lineNum][0] == "float") - globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]); - else if(words[lineNum][0] == "bool") - globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]); - //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl; - } - } + { + if(words[lineNum][0] == "string") + globalVariableValues[words[lineNum][1]] = StringRaw(words[lineNum][3]); + else if(words[lineNum][0] == "int") + globalVariableValues[words[lineNum][1]] = stoi(words[lineNum][3]); + else if(words[lineNum][0] == "float") + globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]); + else if(words[lineNum][0] == "bool") + globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]); + LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + lineNum); + } } // Executes main, which is the starting function From ec0e412217f5444dc4650bc6c3718cd5c387fd62 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 09:59:08 -0500 Subject: [PATCH 17/33] Added exception errors --- Slang/anyops.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Slang/anyops.h b/Slang/anyops.h index 38851ed..43cfdf6 100644 --- a/Slang/anyops.h +++ b/Slang/anyops.h @@ -31,12 +31,12 @@ bool AnyAsBool(any val) } catch (boost::bad_any_cast &e) // Does not convert, return { + LogWarning("invalid conversion to type \'bool\'") return false; } } } } - return false; } // Will convert type 'any' val to a string @@ -68,12 +68,12 @@ string AnyAsString(any val) } catch (boost::bad_any_cast &e) // Does not convert, return { + LogWarning("invalid conversion to type \'string\'") return ""; } } } } - return ""; } // Will convert type 'any' val to a float @@ -105,12 +105,12 @@ float AnyAsFloat(any val) } catch (boost::bad_any_cast &e) // Does not convert, return { + LogWarning("invalid conversion to type \'float\'") return 0; } } } } - return 0; } // Will convert type 'any' val to an integer @@ -142,12 +142,12 @@ int AnyAsInt(any val) } catch (boost::bad_any_cast &e) // Does not convert, return { + LogWarning("invalid conversion to type \'int\'") return 0; } } } } - return 0; } // Gets type of 'any' val @@ -182,12 +182,12 @@ int any_type(any val) } catch (boost::bad_any_cast &e) // Does not convert, return { + LogWarning("variable has no type") return -1; } } } } - return -1; } #endif From 26cc5eeeb4bfbeaa06b97e9f01ef95171372f6cb Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:02:15 -0500 Subject: [PATCH 18/33] Warn on function doesn't exit --- Slang/builtin.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Slang/builtin.h b/Slang/builtin.h index 417655e..10e5e88 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -112,6 +112,8 @@ any CPPFunction(string name, vector args) cout << AnyAsString(args[0]); else if (name == "CPP.System.PrintLine") cout << AnyAsString(args[0]) << endl; + else + LogWarning("CPP function \'" + name + "\' does not exist.") return 0; } From d761591b14ee3cee5a24199765325d72002fd57e Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:20:26 -0500 Subject: [PATCH 19/33] Changed to switch case for Equ and BooleanLogic and added warnings --- Slang/Main.cpp | 87 +++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 8703c86..3fdedf8 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -309,19 +309,25 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v any valARealValue = EvalExpression(valA, variableVals); any valBRealValue = EvalExpression(valB, variableVals); - if (determinant == "==") - return AnyAsString(valARealValue) == AnyAsString(valBRealValue); - if (determinant == "!=") - return AnyAsString(valARealValue) != AnyAsString(valBRealValue); - if (determinant == ">=") - return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue); - if (determinant == "<=") - return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue); - if (determinant == ">") - return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); - if (determinant == "<") - return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); - + switch(determinant) + { + case "==": + return AnyAsString(valARealValue) == AnyAsString(valBRealValue); + case "!=": + return AnyAsString(valARealValue) != AnyAsString(valBRealValue); + case ">=": + return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue); + case "<=": + return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue); + case ">": + return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); + case "<": + return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); + default: + LogWarning("invalid determinant \'" + determinant + "\'"); + return false; + } + return false; } @@ -329,34 +335,43 @@ int evalEqu(const vector& str, unordered_map& variableValue { if (IsVar(str[0], variableValues)) { - if (str[1] == "=") - variableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); - else if (str[1] == "+=") - variableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); - else if (str[1] == "-=") - variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - else if (str[1] == "*=") - variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - else if (str[1] == "/=") - variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + switch(str[1]) + { + case "=": + variableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + case "+=": + variableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + case "-=": + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + case "*=": + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + case "/=": + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + default: + LogWarning("invalid operator \'" + str[1] + "\'"); + return 1; + } - //cout << variables[v] << " is " << variableValues[v] << endl; return 0; } else if (IsVar(str[0], globalVariableValues)) { - if (str[1] == "=") - globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); - else if (str[1] == "+=") - globalVariableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); - else if (str[1] == "-=") - globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - else if (str[1] == "*=") - globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - else if (str[1] == "/=") - globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - - //cout << variables[v] << " is " << variableValues[v] << endl; + switch(str[1]) + { + case "=": + globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + case "+=": + globalVariableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + case "-=": + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + case "*=": + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + case "/=": + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + default: + LogWarning("invalid operator \'" + str[1] + "\'"); + return 1; + } return 0; } LogWarning("uninitialized variable or typo in \'" << str[0] << "\'"); From 4d820e24aa37595e7a41947e6f18721d1ebec6d1 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:41:39 -0500 Subject: [PATCH 20/33] Changed name of evalEqu to varOperation --- Slang/Main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 3fdedf8..5872b68 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -331,7 +331,7 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v return false; } -int evalEqu(const vector& str, unordered_map& variableValues) +int varOperation(const vector& str, unordered_map& variableValues) { if (IsVar(str[0], variableValues)) { @@ -418,7 +418,7 @@ any ProcessLine(const vector>& words, const int lineNum, unordere else if (IsVar(words[lineNum][0], variableValues)) { // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right - evalEqu(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); + varOperation(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); return; } From 20415f4ca2ec82ebcee4924ff5fba0869ca43fc5 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:45:29 -0500 Subject: [PATCH 21/33] Changed to const references and added warning --- Slang/eval.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Slang/eval.cpp b/Slang/eval.cpp index 873f073..9fd9af6 100644 --- a/Slang/eval.cpp +++ b/Slang/eval.cpp @@ -9,7 +9,7 @@ #include "strops.h" using namespace std; -float precedence(char op) { +float precedence(const char& op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') @@ -19,20 +19,22 @@ float precedence(char op) { return 0; } -float applyOp(float a, float b, char op) { +float applyOp(const float& a, const float& b, const char& op) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; case '^': return pow(a, b); + default: LogWarning("operator \'" << op << "\' does not exist"); } + return 0; } // Function that returns value of // expression after evaluation. -float evaluate(string tokens) { - tokens = replace(tokens, " ", ""); +float evaluate(const string& t) { + tokens = replace(t, " ", ""); float i; From f61fa779529b23d49df9e357271a8ffe069711d7 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:46:39 -0500 Subject: [PATCH 22/33] Changed to const references --- Slang/anyops.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Slang/anyops.h b/Slang/anyops.h index 43cfdf6..a95ca54 100644 --- a/Slang/anyops.h +++ b/Slang/anyops.h @@ -5,7 +5,7 @@ using namespace boost; // Will convert type 'any' val to a bool -bool AnyAsBool(any val) +bool AnyAsBool(const any& val) { try // Try converting to bool { @@ -40,7 +40,7 @@ bool AnyAsBool(any val) } // Will convert type 'any' val to a string -string AnyAsString(any val) +string AnyAsString(const any& val) { try // Try converting to string { @@ -77,7 +77,7 @@ string AnyAsString(any val) } // Will convert type 'any' val to a float -float AnyAsFloat(any val) +float AnyAsFloat(const any& val) { try // Try converting to float { @@ -114,7 +114,7 @@ float AnyAsFloat(any val) } // Will convert type 'any' val to an integer -int AnyAsInt(any val) +int AnyAsInt(const any& val) { try // Try converting to int { @@ -152,7 +152,7 @@ int AnyAsInt(any val) // Gets type of 'any' val // 0 -> int; 1 -> float; 2 -> bool; 3 -> string; -int any_type(any val) +int any_type(const any& val) { try // Try converting to int { From 40bb09d7c81e44f849e0c664fc102ae091db04d5 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:47:54 -0500 Subject: [PATCH 23/33] Changed to const references --- Slang/builtin.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Slang/builtin.h b/Slang/builtin.h index 10e5e88..0c5421b 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -22,9 +22,9 @@ unordered_map& builtinVarVals; Parser mainWindow; // Initial script processing, which loads variables and functions from builtin -int GetBuiltins(string script) +int GetBuiltins(const string& s) { - script = replace(script, " ", "\t"); + script = replace(s, " ", "\t"); vector lines = split(script, '\n'); vector> words; @@ -90,7 +90,7 @@ int GetBuiltins(string script) } // Executes -any CPPFunction(string name, vector args) +any CPPFunction(const string& name, const vector& args) { if (name == "CPP.Math.Sin") return sin(AnyAsFloat(args[0])); From 3448f39203332c81ef539a090a004842784a23cd Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:49:07 -0500 Subject: [PATCH 24/33] Update main.h --- Slang/main.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Slang/main.h b/Slang/main.h index 59e3071..1e85a82 100644 --- a/Slang/main.h +++ b/Slang/main.h @@ -4,6 +4,6 @@ using namespace std; -any ExecuteFunction(string functionName, vector inputVarVals, int functionIndex); +any ExecuteFunction(const string functionName, const vector inputVarVals) #endif From 741e05b500bf73cd92e17ed3d93127799c186eb3 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 10:53:18 -0500 Subject: [PATCH 25/33] Changed to const references --- Slang/strops.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Slang/strops.h b/Slang/strops.h index 79910c4..47d4d41 100644 --- a/Slang/strops.h +++ b/Slang/strops.h @@ -9,30 +9,30 @@ string rtrim(const string& s); string trim(const string& s); -vector split(string str, char del); +vector split(const string& str, const char& del); -int count(string str, char ch); +int count(const string& str, const char& ch); -int countNoOverlap(string str, char ch1, char ch2); +int countNoOverlap(const string& str, const char& ch1, const char& ch2); -int indexInStr(string str, char ch); +int indexInStr(const string& str, const char& ch); -int charIndexInVec(vector str, char ch); +int charIndexInVec(const vector& str, const char& ch); -int countInVector(vector str, string ch); +int countInVector(const vector& str, const string& ch); -string Vec2Str(vector str); +string Vec2Str(const vector& str); -vector removeTabs(vector str, int amnt); +vector removeTabs(const vector& str, const int& amnt); -vector rangeInVec(vector str, int min, int max); +vector rangeInVec(const vector& str, const int& min, const int& max); -string rangeInStr(string str, int min, int max); +string rangeInStr(const string& str, const int& min, const int& max); -string unWrapVec(vector vec); +string unWrapVec(const vector& vec); -float floatval(string s); +float floatval(const string& s); -string replace(string str, string strToReplace, string replaceWith); +string replace(const string& str, const string& strToReplace, const string& replaceWith); -#endif \ No newline at end of file +#endif From b1a1133d856795c7cb30dbbc35058a0254adef1d Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 15:10:55 -0500 Subject: [PATCH 26/33] Revert "Changed to switch case for Equ and BooleanLogic and added warnings" This reverts commit d761591b14ee3cee5a24199765325d72002fd57e. --- Slang/Main.cpp | 87 +++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 51 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 5872b68..b364309 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -309,25 +309,19 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v any valARealValue = EvalExpression(valA, variableVals); any valBRealValue = EvalExpression(valB, variableVals); - switch(determinant) - { - case "==": - return AnyAsString(valARealValue) == AnyAsString(valBRealValue); - case "!=": - return AnyAsString(valARealValue) != AnyAsString(valBRealValue); - case ">=": - return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue); - case "<=": - return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue); - case ">": - return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); - case "<": - return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); - default: - LogWarning("invalid determinant \'" + determinant + "\'"); - return false; - } - + if (determinant == "==") + return AnyAsString(valARealValue) == AnyAsString(valBRealValue); + if (determinant == "!=") + return AnyAsString(valARealValue) != AnyAsString(valBRealValue); + if (determinant == ">=") + return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue); + if (determinant == "<=") + return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue); + if (determinant == ">") + return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); + if (determinant == "<") + return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); + return false; } @@ -335,43 +329,34 @@ int varOperation(const vector& str, unordered_map& variable { if (IsVar(str[0], variableValues)) { - switch(str[1]) - { - case "=": - variableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); - case "+=": - variableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); - case "-=": - variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - case "*=": - variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - case "/=": - variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - default: - LogWarning("invalid operator \'" + str[1] + "\'"); - return 1; - } + if (str[1] == "=") + variableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + else if (str[1] == "+=") + variableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "-=") + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + else if (str[1] == "*=") + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + else if (str[1] == "/=") + variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + //cout << variables[v] << " is " << variableValues[v] << endl; return 0; } else if (IsVar(str[0], globalVariableValues)) { - switch(str[1]) - { - case "=": - globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); - case "+=": - globalVariableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); - case "-=": - globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - case "*=": - globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - case "/=": - globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - default: - LogWarning("invalid operator \'" + str[1] + "\'"); - return 1; - } + if (str[1] == "=") + globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + else if (str[1] == "+=") + globalVariableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "-=") + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + else if (str[1] == "*=") + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + else if (str[1] == "/=") + globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); + + //cout << variables[v] << " is " << variableValues[v] << endl; return 0; } LogWarning("uninitialized variable or typo in \'" << str[0] << "\'"); From 79b1b7102d2400dcb2f0322e2067fe34aaf2b8d7 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 5 Jan 2022 15:51:44 -0500 Subject: [PATCH 27/33] Fixed most errors, moved things around --- Slang/Main.cpp | 166 +++++++++++++---------------------------------- Slang/anyops.h | 14 ++-- Slang/builtin.h | 108 ++++++++++++++++-------------- Slang/eval.cpp | 6 +- Slang/graphics.h | 2 +- Slang/main.h | 3 +- Slang/strops.cpp | 98 ++++++++++++++++++++++++---- Slang/strops.h | 10 +++ 8 files changed, 216 insertions(+), 191 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index b364309..bc08690 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -22,76 +22,6 @@ using namespace boost; unordered_map globalVariableValues; unordered_map>> functionValues; - -bool isNumber(const string& str) -{ - for (char const& c : str) { - if (isdigit(c) == 0 && c != '.') return false; - } - return true; -} - -bool stob(string str) { - transform(str.begin(), str.end(), str.begin(), ::tolower); - istringstream is(str); - bool b; - is >> boolalpha >> b; - return b; -} - -string StringRaw(const string& s) -{ - string str = trim(s); - - if (str.size() < 3) - return str; - - string withoutQuotes; - - if (str[0] != '\"') - withoutQuotes += str[0]; - - withoutQuotes += str.substr(1, str.size()-2); - - if (str[str.size() - 1] != '\"') - withoutQuotes += str[str.size() - 1]; - - return withoutQuotes; -} - -string Quoted(const string& s) -{ - string str = trim(s); - - string withQuotes; - - if (str[0] != '\"') - withQuotes += '\"'; - - withQuotes += str; - - if (str[str.size() - 1] != '\"') - withQuotes += '\"'; - - return withQuotes; -} - -string RMParenthesis(const string& s) -{ - string str = trim(s); - string withoutParenthesis; - - if (str[0] != '(') - withoutParenthesis += str[0]; - - withoutParenthesis += str.substr(1, str.size()-2); - - if (str[str.size() - 1] != ')') - withoutParenthesis += str[str.size() - 1]; - - return withoutParenthesis; -} - any GetVariableValue(const string& varName, const unordered_map& variableVals) { auto iA = variableVals.find(varName); @@ -155,19 +85,6 @@ bool IsFunction(const string& funcName) return false; } -int LogWarning(const string& warningText) -{ - cerr << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl; - return 1; -} - -int CriticalError(const string& errorText) -{ - cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl; - exit(EXIT_FAILURE); - return 2; -} - any EvalExpression(const string& ex, const unordered_map& variableVals) { string expression = trim(ex); @@ -311,17 +228,19 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v if (determinant == "==") return AnyAsString(valARealValue) == AnyAsString(valBRealValue); - if (determinant == "!=") + else if (determinant == "!=") return AnyAsString(valARealValue) != AnyAsString(valBRealValue); - if (determinant == ">=") + else if (determinant == ">=") return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue); - if (determinant == "<=") + else if (determinant == "<=") return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue); - if (determinant == ">") + else if (determinant == ">") return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); - if (determinant == "<") + else if (determinant == "<") return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); - + else + LogWarning("unrecognized determinant \'" + determinant + "\'"); + return false; } @@ -339,7 +258,8 @@ int varOperation(const vector& str, unordered_map& variable variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); else if (str[1] == "/=") variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - + else + LogWarning("unrecognized operator \'" + str[1] + "\'"); //cout << variables[v] << " is " << variableValues[v] << endl; return 0; } @@ -355,24 +275,25 @@ int varOperation(const vector& str, unordered_map& variable globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); else if (str[1] == "/=") globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues)); - + else + LogWarning("unrecognized operator \'" + str[1] + "\'"); //cout << variables[v] << " is " << variableValues[v] << endl; return 0; } - LogWarning("uninitialized variable or typo in \'" << str[0] << "\'"); + LogWarning("uninitialized variable or typo in \'" + str[0] + "\'"); return 1; } -any ProcessLine(const vector>& words, const int lineNum, unordered_map& variableValues) +any ProcessLine(const vector>& words, int lineNum, unordered_map& variableValues) { if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') - return; + return any{}; // If print statement (deprecated, now use CPP.System.Print() function) else if (words[lineNum][0] == "print") { cout << AnyAsString(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; - return; + return any{}; } // Check if function return @@ -387,26 +308,26 @@ any ProcessLine(const vector>& words, const int lineNum, unordere else if (IsFunction(trim(split(words[lineNum][0], '(')[0]))) { ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues)); - return; + return any{}; } - + // Iterate through all types to see if line inits or // re-inits a variable then store it with it's value else if (countInVector(types, words[lineNum][0]) > 0) { variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); - return; + return any{}; } - + // Check existing variables: If matches, then it means // the variables value is getting changed with an operator else if (IsVar(words[lineNum][0], variableValues)) { // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right varOperation(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); - return; + return any{}; } - + // Gathers while loop contents else if (words[lineNum][0] == "while") { @@ -444,9 +365,9 @@ any ProcessLine(const vector>& words, const int lineNum, unordere return returnVal; } } - return; + return any{}; } - + // Gathers if statement contents else if (words[lineNum][0] == "if") { @@ -481,8 +402,8 @@ any ProcessLine(const vector>& words, const int lineNum, unordere //Iterate through all lines in if statement for (int l = 0; l < (int)ifContents.size(); l++) { - string returnVal = ProcessLine(innerWords, l, variableValues); - if (returnVal != 0) + any returnVal = ProcessLine(innerWords, l, variableValues); + if (!returnVal.empty()) return returnVal; } } @@ -510,16 +431,16 @@ any ProcessLine(const vector>& words, const int lineNum, unordere vector> innerWords; for (int i = 0; i < (int)elseContents.size(); i++) - words.push_back(split(elseContents[i], ' ')); + innerWords.push_back(split(elseContents[i], ' ')); //Iterate through all lines in else statement for (int lineNum = 0; lineNum < (int)elseContents.size(); lineNum++) { ProcessLine(innerWords, lineNum, variableValues); } - return; + return any{}; } - return; + return any{}; } //// Gathers else statement contents //if (words[lineNum][0] == "else") @@ -527,7 +448,7 @@ any ProcessLine(const vector>& words, const int lineNum, unordere // //} - return; + return any{}; } any ExecuteFunction(const string functionName, const vector inputVarVals) @@ -536,10 +457,10 @@ any ExecuteFunction(const string functionName, const vector inputVarVals) vector> words = functionValues[functionName]; unordered_map variableValues; - vector args = split(replace(functionValues[functionName][0][0], ','); + vector args = split(functionValues[functionName][0][0], ','); for (int i = 0; i < (int)inputVarVals.size(); i++) { - variableValues[trim(args[i])] = EvalExpression(inputVarVals[i], variables, variableValues); + variableValues[trim(args[i])] = EvalExpression(AnyAsString(inputVarVals[i]), variableValues); } //Iterate through all lines in function @@ -548,16 +469,16 @@ any ExecuteFunction(const string functionName, const vector inputVarVals) any returnVal = 0; try { - returnVal = ProcessLine(words, lineNum, variables, variableValues); + returnVal = ProcessLine(words, lineNum, variableValues); } catch (const std::exception&) { - CriticalError("\'" << unWrapVec(words[lineNum]) << "\'\nIn function: " << functionName << "\nLine: " << lineNum); + LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\nIn function: " + functionName + "\nLine: " + to_string(lineNum)); } if (!returnVal.empty()) return returnVal; } - return; + return any{}; } int parseSlang(string script) @@ -578,8 +499,8 @@ int parseSlang(string script) { vector> functionContents; - string functName = split(words[lineNum][1], "(")[0]; - + 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) @@ -591,7 +512,7 @@ int parseSlang(string script) args += replace(replace(words[lineNum][w], "(", " "), ")", ""); } } - + args = replace(args, functName + ",", ""); functionContents.push_back(vector{args}); @@ -608,15 +529,16 @@ int parseSlang(string script) } else { - if(words[lineNum][0] == "string") + if (words[lineNum][0] == "string") globalVariableValues[words[lineNum][1]] = StringRaw(words[lineNum][3]); - else if(words[lineNum][0] == "int") + else if (words[lineNum][0] == "int") globalVariableValues[words[lineNum][1]] = stoi(words[lineNum][3]); - else if(words[lineNum][0] == "float") + else if (words[lineNum][0] == "float") globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]); - else if(words[lineNum][0] == "bool") + else if (words[lineNum][0] == "bool") globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]); - LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + lineNum); + else + LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); } } diff --git a/Slang/anyops.h b/Slang/anyops.h index a95ca54..4430214 100644 --- a/Slang/anyops.h +++ b/Slang/anyops.h @@ -2,7 +2,11 @@ #ifndef ANYOPS_H #define ANYOPS_H +#include "builtin.h" +#include + using namespace boost; +using namespace std; // Will convert type 'any' val to a bool bool AnyAsBool(const any& val) @@ -31,7 +35,7 @@ bool AnyAsBool(const any& val) } catch (boost::bad_any_cast &e) // Does not convert, return { - LogWarning("invalid conversion to type \'bool\'") + LogWarning("invalid conversion to type \'bool\'"); return false; } } @@ -68,7 +72,7 @@ string AnyAsString(const any& val) } catch (boost::bad_any_cast &e) // Does not convert, return { - LogWarning("invalid conversion to type \'string\'") + LogWarning("invalid conversion to type \'string\'"); return ""; } } @@ -105,7 +109,7 @@ float AnyAsFloat(const any& val) } catch (boost::bad_any_cast &e) // Does not convert, return { - LogWarning("invalid conversion to type \'float\'") + LogWarning("invalid conversion to type \'float\'"); return 0; } } @@ -142,7 +146,7 @@ int AnyAsInt(const any& val) } catch (boost::bad_any_cast &e) // Does not convert, return { - LogWarning("invalid conversion to type \'int\'") + LogWarning("invalid conversion to type \'int\'"); return 0; } } @@ -182,7 +186,7 @@ int any_type(const any& val) } catch (boost::bad_any_cast &e) // Does not convert, return { - LogWarning("variable has no type") + LogWarning("variable has no type"); return -1; } } diff --git a/Slang/builtin.h b/Slang/builtin.h index 0c5421b..da283d2 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "strops.h" #include "graphics.h" #include "anyops.h" @@ -16,15 +17,29 @@ using namespace std; vector types = { "int", "float", "string", "bool", "void", "null" }; -unordered_map> builtinFunctionValues; -unordered_map& builtinVarVals; +unordered_map>> builtinFunctionValues; +unordered_map builtinVarVals; Parser mainWindow; + +int LogWarning(const string& warningText) +{ + cerr << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl; + return 1; +} + +int LogCriticalError(const string& errorText) +{ + cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl; + exit(EXIT_FAILURE); + return 2; +} + // Initial script processing, which loads variables and functions from builtin int GetBuiltins(const string& s) { - script = replace(s, " ", "\t"); + string script = replace(s, " ", "\t"); vector lines = split(script, '\n'); vector> words; @@ -36,56 +51,55 @@ int GetBuiltins(const string& s) // Go through entire script and iterate through all types to see if line is a // function declaration, then store it with it's value for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) - for (int t = 0; t < (int)types.size(); t++) - if (words[lineNum][0] == types[t]) - { - //Checks if it is function - if (words[lineNum][(int)words[lineNum].size() - 1][(int)words[lineNum][(int)words[lineNum].size() - 1].size() - 1] == ')') + { + //Checks if it is function + if (words[lineNum][0] == "func") + { + vector> functionContents; + + 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) { - vector functionContents; - - string functName; - for (int w = 1; w < (int)words[lineNum].size(); w++) { - if (w < (int)words[lineNum].size() - 1) - { - functName += replace(replace(words[lineNum][w], "(", " "), ")", "") + " "; - } - else - { - functName += replace(replace(words[lineNum][w], "(", " "), ")", ""); - } - } - - int numOfBrackets = 1; - for (int p = lineNum + 2; 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 = removeTabs(functionContents, 1); - builtinFunctionValues[functName] = functionContents; + args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ","; } - //Checks if it is variable else { - if(words[lineNum][0] == "string") - builtinVarVals[words[lineNum][1]] = words[lineNum][3]; - else if(words[lineNum][0] == "int") - builtinVarVals[words[lineNum][1]] = stoi(words[lineNum][3]); - else if(words[lineNum][0] == "float") - builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]); - else if(words[lineNum][0] == "bool") - builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]); - //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl; + args += replace(replace(words[lineNum][w], "(", " "), ")", ""); } } + args = replace(args, functName + ",", ""); + functionContents.push_back(vector{args}); + + int numOfBrackets = 1; + for (int p = lineNum + 3; p < (int)words.size(); p++) + { + numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); + if (numOfBrackets == 0) + break; + functionContents.push_back(removeTabs(words[p], 1)); + } + builtinFunctionValues[functName] = functionContents; + //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; + } + else + { + if (words[lineNum][0] == "string") + builtinVarVals[words[lineNum][1]] = StringRaw(words[lineNum][3]); + else if (words[lineNum][0] == "int") + builtinVarVals[words[lineNum][1]] = stoi(words[lineNum][3]); + else if (words[lineNum][0] == "float") + builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]); + else if (words[lineNum][0] == "bool") + builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]); + else + LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); + } + } + return 0; } @@ -113,7 +127,7 @@ any CPPFunction(const string& name, const vector& args) else if (name == "CPP.System.PrintLine") cout << AnyAsString(args[0]) << endl; else - LogWarning("CPP function \'" + name + "\' does not exist.") + LogWarning("CPP function \'" + name + "\' does not exist."); return 0; } diff --git a/Slang/eval.cpp b/Slang/eval.cpp index 9fd9af6..4373a00 100644 --- a/Slang/eval.cpp +++ b/Slang/eval.cpp @@ -7,6 +7,7 @@ #include #include "eval.h" #include "strops.h" +#include "builtin.h" using namespace std; float precedence(const char& op) { @@ -26,15 +27,16 @@ float applyOp(const float& a, const float& b, const char& op) { case '*': return a * b; case '/': return a / b; case '^': return pow(a, b); - default: LogWarning("operator \'" << op << "\' does not exist"); } + string s(1, op); + LogWarning("operator \'" + s + "\' does not exist"); return 0; } // Function that returns value of // expression after evaluation. float evaluate(const string& t) { - tokens = replace(t, " ", ""); + string tokens = replace(t, " ", ""); float i; diff --git a/Slang/graphics.h b/Slang/graphics.h index 0030841..918aba2 100644 --- a/Slang/graphics.h +++ b/Slang/graphics.h @@ -34,7 +34,7 @@ public: bool OnUserUpdate(float fElapsedTime) override { - ExecuteFunction("Update", vector {""}, -1); + ExecuteFunction("Update", vector {}); // Called once per frame //for (int x = 0; x < ScreenWidth(); x++) diff --git a/Slang/main.h b/Slang/main.h index 1e85a82..25cbe2d 100644 --- a/Slang/main.h +++ b/Slang/main.h @@ -3,7 +3,8 @@ #define MAIN_H using namespace std; +using namespace boost; -any ExecuteFunction(const string functionName, const vector inputVarVals) +any ExecuteFunction(const string functionName, const vector inputVarVals); #endif diff --git a/Slang/strops.cpp b/Slang/strops.cpp index 1cd112c..83b42de 100644 --- a/Slang/strops.cpp +++ b/Slang/strops.cpp @@ -4,10 +4,82 @@ #include #include #include "strops.h" +#include "builtin.h" using namespace std; const string WHITESPACE = " \n\r\t\f\v"; + +bool isNumber(const string& str) +{ + for (char const& c : str) { + if (isdigit(c) == 0 && c != '.') return false; + } + return true; +} + +bool stob(string str) +{ + transform(str.begin(), str.end(), str.begin(), ::tolower); + istringstream is(str); + bool b; + is >> boolalpha >> b; + return b; +} + +string StringRaw(const string& s) +{ + string str = trim(s); + + if (str.size() < 3) + return str; + + string withoutQuotes; + + if (str[0] != '\"') + withoutQuotes += str[0]; + + withoutQuotes += str.substr(1, str.size() - 2); + + if (str[str.size() - 1] != '\"') + withoutQuotes += str[str.size() - 1]; + + return withoutQuotes; +} + +string Quoted(const string& s) +{ + string str = trim(s); + + string withQuotes; + + if (str[0] != '\"') + withQuotes += '\"'; + + withQuotes += str; + + if (str[str.size() - 1] != '\"') + withQuotes += '\"'; + + return withQuotes; +} + +string RMParenthesis(const string& s) +{ + string str = trim(s); + string withoutParenthesis; + + if (str[0] != '(') + withoutParenthesis += str[0]; + + withoutParenthesis += str.substr(1, str.size() - 2); + + if (str[str.size() - 1] != ')') + withoutParenthesis += str[str.size() - 1]; + + return withoutParenthesis; +} + string ltrim(const string& s) { size_t start = s.find_first_not_of(WHITESPACE); @@ -24,7 +96,7 @@ string trim(const string& s) { return rtrim(ltrim(s)); } -vector split(string str, char del) { +vector split(const string& str, const char& del) { if (count(str, del) == 0) return vector{str}; @@ -51,7 +123,7 @@ vector split(string str, char del) { return splitWords; } -int count(string str, char ch) { +int count(const string& str, const char& ch) { int cnt = 0; for (int i = 0; i < (int)str.size(); i++) @@ -61,7 +133,7 @@ int count(string str, char ch) { return cnt; } -int countNoOverlap(string str, char ch1, char ch2) { +int countNoOverlap(const string& str, const char& ch1, const char& ch2) { int cnt = 0; bool waitingForClose = false; @@ -80,7 +152,7 @@ int countNoOverlap(string str, char ch1, char ch2) { return cnt; } -int indexInStr(string str, char ch) { +int indexInStr(const string& str, const char& ch) { for (int i = 0; i < (int)str.size(); i++) if (str[i] == ch) @@ -89,7 +161,7 @@ int indexInStr(string str, char ch) { return -1; } -int charIndexInVec(vector str, char ch) { +int charIndexInVec(const vector& str, const char& ch) { for (int i = 0; i < (int)str.size(); i++) for (int w = 0; w < (int)str[i].size(); w++) @@ -99,7 +171,7 @@ int charIndexInVec(vector str, char ch) { return -1; } -int countInVector(vector str, string ch) { +int countInVector(const vector& str, const string& ch) { int cnt = 0; for (int i = 0; i < (int)str.size(); i++) @@ -109,7 +181,7 @@ int countInVector(vector str, string ch) { return cnt; } -string Vec2Str(vector str) { +string Vec2Str(const vector& str) { string outStr; for (int i = 0; i < (int)str.size(); i++) @@ -118,7 +190,7 @@ string Vec2Str(vector str) { return outStr; } -vector removeTabs(vector str, int amnt) { +vector removeTabs(const vector& str, const int& amnt) { vector newStr; for (int i = 0; i < (int)str.size(); i++) @@ -135,7 +207,7 @@ vector removeTabs(vector str, int amnt) { return newStr; } -vector rangeInVec(vector str, int min, int max) { +vector rangeInVec(const vector& str, const int& min, int max) { if (max == -1) max = (int)str.size(); @@ -147,7 +219,7 @@ vector rangeInVec(vector str, int min, int max) { return newStr; } -string rangeInStr(string str, int min, int max) { +string rangeInStr(const string& str, const int& min, int max) { if (max == -1) max = (int)str.size(); @@ -159,7 +231,7 @@ string rangeInStr(string str, int min, int max) { return newStr; } -string unWrapVec(vector vec) { +string unWrapVec(const vector& vec) { string newStr; for (int i = 0; i < (int)vec.size(); i++) @@ -172,7 +244,7 @@ string unWrapVec(vector vec) { return newStr; } -float floatval(string s) +float floatval(const string& s) { float outfloat; @@ -188,7 +260,7 @@ float floatval(string s) return outfloat; } -string replace(string str, string strToReplace, string replaceWith) { +string replace(const string& str, const string& strToReplace, const string& replaceWith) { string newStr; string savedLetters; diff --git a/Slang/strops.h b/Slang/strops.h index 47d4d41..6ead244 100644 --- a/Slang/strops.h +++ b/Slang/strops.h @@ -3,6 +3,16 @@ using namespace std; +bool isNumber(const string& str); + +bool stob(string str); + +string StringRaw(const string& s); + +string Quoted(const string& s); + +string RMParenthesis(const string& s); + string ltrim(const string& s); string rtrim(const string& s); From 1702ae73b0d0e38b353e39a002aa3c89268d5059 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 6 Jan 2022 07:27:08 -0500 Subject: [PATCH 28/33] Update builtin.h --- Slang/builtin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Slang/builtin.h b/Slang/builtin.h index da283d2..0aa95ef 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include "strops.h" #include "graphics.h" From b99cb9ba82c79026ee174e6d6a897fde08883921 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 6 Jan 2022 07:29:30 -0500 Subject: [PATCH 29/33] Update builtin.h --- Slang/builtin.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Slang/builtin.h b/Slang/builtin.h index 0aa95ef..9a4574f 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -14,6 +14,7 @@ #include "anyops.h" using namespace std; +using namespace boost; vector types = { "int", "float", "string", "bool", "void", "null" }; From 41b90c3b4b2530999afaa10e4c49e4514382e54b Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 6 Jan 2022 16:51:04 -0500 Subject: [PATCH 30/33] (slightly) working version. Includes to other files needs fixing, then it should work --- Slang/CMakeLists.txt | 11 +++++++ Slang/Main.cpp | 56 +++++++++++++++++------------------ Slang/Slang.vcxproj | 9 ++++-- Slang/Slang.vcxproj.filters | 3 ++ Slang/anyops.h | 54 +++++++++++++++++---------------- Slang/boost_x64_release.props | 17 +++++++++++ Slang/builtin.h | 6 ++-- Slang/eval.cpp | 4 +-- Slang/eval.h | 2 +- Slang/graphics.h | 3 +- Slang/main.h | 3 +- Slang/strops.cpp | 14 +++++---- Slang/strops.h | 2 ++ 13 files changed, 113 insertions(+), 71 deletions(-) create mode 100644 Slang/CMakeLists.txt create mode 100644 Slang/boost_x64_release.props diff --git a/Slang/CMakeLists.txt b/Slang/CMakeLists.txt new file mode 100644 index 0000000..8116449 --- /dev/null +++ b/Slang/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) + +# set the project name +project(Slang) + +# add the executable +#add_executable(Main.cpp) + +target_include_directories(Slang PUBLIC + "${PROJECT_BINARY_DIR}" + ) \ No newline at end of file diff --git a/Slang/Main.cpp b/Slang/Main.cpp index bc08690..2976c50 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include "eval.h" @@ -19,10 +19,10 @@ using namespace std; using namespace boost; -unordered_map globalVariableValues; +unordered_map globalVariableValues; unordered_map>> functionValues; -any GetVariableValue(const string& varName, const unordered_map& variableVals) +boost::any GetVariableValue(const string& varName, const unordered_map& variableVals) { auto iA = variableVals.find(varName); if (iA != variableVals.end()) @@ -43,7 +43,7 @@ any GetVariableValue(const string& varName, const unordered_map& va } } -bool IsVar(const string& varName, const unordered_map& variableVals) +bool IsVar(const string& varName, const unordered_map& variableVals) { if (variableVals.find(varName) != variableVals.end()) return true; @@ -51,9 +51,9 @@ bool IsVar(const string& varName, const unordered_map& variableVals return false; } -vector VarValues(const vector& varNames, const unordered_map& variableVals) +vector VarValues(const vector& varNames, const unordered_map& variableVals) { - vector realValues; + vector realValues; for (int varIndex = 0; varIndex < varNames.size(); varIndex++) { @@ -85,7 +85,7 @@ bool IsFunction(const string& funcName) return false; } -any EvalExpression(const string& ex, const unordered_map& variableVals) +boost::any EvalExpression(const string& ex, const unordered_map& variableVals) { string expression = trim(ex); bool inQuotes = false; @@ -221,10 +221,10 @@ any EvalExpression(const string& ex, const unordered_map& variableV return evaluate(newExpression); } -bool BooleanLogic(const string& valA, const string& determinant, const string& valB, const 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); + boost::any valARealValue = EvalExpression(valA, variableVals); + boost::any valBRealValue = EvalExpression(valB, variableVals); if (determinant == "==") return AnyAsString(valARealValue) == AnyAsString(valBRealValue); @@ -244,7 +244,7 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v return false; } -int varOperation(const vector& str, unordered_map& variableValues) +int varOperation(const vector& str, unordered_map& variableValues) { if (IsVar(str[0], variableValues)) { @@ -284,16 +284,16 @@ int varOperation(const vector& str, unordered_map& variable return 1; } -any ProcessLine(const vector>& words, int lineNum, unordered_map& variableValues) +boost::any ProcessLine(const vector>& words, int lineNum, unordered_map& variableValues) { if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') - return any{}; + return boost::any{}; // If print statement (deprecated, now use CPP.System.Print() function) else if (words[lineNum][0] == "print") { cout << AnyAsString(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; - return any{}; + return boost::any{}; } // Check if function return @@ -308,7 +308,7 @@ any ProcessLine(const vector>& words, int lineNum, unordered_map< else if (IsFunction(trim(split(words[lineNum][0], '(')[0]))) { ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues)); - return any{}; + return boost::any{}; } // Iterate through all types to see if line inits or @@ -316,7 +316,7 @@ any ProcessLine(const vector>& words, int lineNum, unordered_map< else if (countInVector(types, words[lineNum][0]) > 0) { variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); - return any{}; + return boost::any{}; } // Check existing variables: If matches, then it means @@ -325,7 +325,7 @@ any ProcessLine(const vector>& words, int lineNum, unordered_map< { // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right varOperation(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); - return any{}; + return boost::any{}; } // Gathers while loop contents @@ -360,12 +360,12 @@ any ProcessLine(const vector>& words, int lineNum, unordered_map< //Iterate through all lines in while loop for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) { - any returnVal = ProcessLine(innerWords, lineNum, variableValues); + boost::any returnVal = ProcessLine(innerWords, lineNum, variableValues); if (AnyAsString(returnVal) != "") return returnVal; } } - return any{}; + return boost::any{}; } // Gathers if statement contents @@ -402,7 +402,7 @@ any ProcessLine(const vector>& words, int lineNum, unordered_map< //Iterate through all lines in if statement for (int l = 0; l < (int)ifContents.size(); l++) { - any returnVal = ProcessLine(innerWords, l, variableValues); + boost::any returnVal = ProcessLine(innerWords, l, variableValues); if (!returnVal.empty()) return returnVal; } @@ -438,9 +438,9 @@ any ProcessLine(const vector>& words, int lineNum, unordered_map< { ProcessLine(innerWords, lineNum, variableValues); } - return any{}; + return boost::any{}; } - return any{}; + return boost::any{}; } //// Gathers else statement contents //if (words[lineNum][0] == "else") @@ -448,15 +448,15 @@ any ProcessLine(const vector>& words, int lineNum, unordered_map< // //} - return any{}; + return boost::any{}; } -any ExecuteFunction(const string functionName, const vector inputVarVals) +boost::any ExecuteFunction(const string& functionName, const vector& inputVarVals) { // Get contents of function vector> words = functionValues[functionName]; - unordered_map variableValues; + unordered_map variableValues; vector args = split(functionValues[functionName][0][0], ','); for (int i = 0; i < (int)inputVarVals.size(); i++) { @@ -466,7 +466,7 @@ any ExecuteFunction(const string functionName, const vector inputVarVals) //Iterate through all lines in function for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) { - any returnVal = 0; + boost::any returnVal = 0; try { returnVal = ProcessLine(words, lineNum, variableValues); @@ -478,7 +478,7 @@ any ExecuteFunction(const string functionName, const vector inputVarVals) if (!returnVal.empty()) return returnVal; } - return any{}; + return boost::any{}; } int parseSlang(string script) @@ -543,7 +543,7 @@ int parseSlang(string script) } // Executes main, which is the starting function - ExecuteFunction("Main", vector {"hi", 0}); + ExecuteFunction("Main", vector {"hi", 0}); return 0; } diff --git a/Slang/Slang.vcxproj b/Slang/Slang.vcxproj index 4f43d62..43f7cc2 100644 --- a/Slang/Slang.vcxproj +++ b/Slang/Slang.vcxproj @@ -65,9 +65,11 @@ + + @@ -89,12 +91,12 @@ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true OldStyle - D:\Code\boost + D:\Code\boost\libs;%(AdditionalIncludeDirectories) Console true - D:\Code\boost + D:\Code\boost\stage\lib @@ -158,6 +160,9 @@ + + + diff --git a/Slang/Slang.vcxproj.filters b/Slang/Slang.vcxproj.filters index 6b7d44e..0800299 100644 --- a/Slang/Slang.vcxproj.filters +++ b/Slang/Slang.vcxproj.filters @@ -49,4 +49,7 @@ + + + \ No newline at end of file diff --git a/Slang/anyops.h b/Slang/anyops.h index 4430214..b89dadb 100644 --- a/Slang/anyops.h +++ b/Slang/anyops.h @@ -3,37 +3,39 @@ #define ANYOPS_H #include "builtin.h" -#include +#include using namespace boost; using namespace std; +int LogWarning(const string& warningText); + // Will convert type 'any' val to a bool -bool AnyAsBool(const any& val) +bool AnyAsBool(const boost::any& val) { try // Try converting to bool { return any_cast(val); } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to string { return any_cast(val) == "true"; } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to float { return any_cast(val) == 1.0f; } - catch (boost::bad_any_cast &e) // Try converting to int + catch (boost::bad_any_cast) // Try converting to int { try { return any_cast(val) == 1; } - catch (boost::bad_any_cast &e) // Does not convert, return + catch (boost::bad_any_cast) // Does not convert, return { LogWarning("invalid conversion to type \'bool\'"); return false; @@ -44,25 +46,25 @@ bool AnyAsBool(const any& val) } // Will convert type 'any' val to a string -string AnyAsString(const any& val) +string AnyAsString(const boost::any& val) { try // Try converting to string { return any_cast(val); } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to int { return to_string(any_cast(val)); } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to float { return to_string(any_cast(val)); } - catch (boost::bad_any_cast &e) // Try converting to bool + catch (boost::bad_any_cast) // Try converting to bool { try { @@ -70,7 +72,7 @@ string AnyAsString(const any& val) if (any_cast(val) == true) i = "true"; return i; } - catch (boost::bad_any_cast &e) // Does not convert, return + catch (boost::bad_any_cast) // Does not convert, return { LogWarning("invalid conversion to type \'string\'"); return ""; @@ -81,25 +83,25 @@ string AnyAsString(const any& val) } // Will convert type 'any' val to a float -float AnyAsFloat(const any& val) +float AnyAsFloat(const boost::any& val) { try // Try converting to float { return any_cast(val); } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to int { return (float)any_cast(val); } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to string, then converting it to float { return stof(any_cast(val)); } - catch (boost::bad_any_cast &e) // Try converting to bool + catch (boost::bad_any_cast) // Try converting to bool { try { @@ -107,7 +109,7 @@ float AnyAsFloat(const any& val) if (any_cast(val) == true) i = 1; return i; } - catch (boost::bad_any_cast &e) // Does not convert, return + catch (boost::bad_any_cast) // Does not convert, return { LogWarning("invalid conversion to type \'float\'"); return 0; @@ -118,25 +120,25 @@ float AnyAsFloat(const any& val) } // Will convert type 'any' val to an integer -int AnyAsInt(const any& val) +int AnyAsInt(const boost::any& val) { try // Try converting to int { return any_cast(val); } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to float { return (int)any_cast(val); } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to string, then converting it to int { return stoi(any_cast(val)); } - catch (boost::bad_any_cast &e) // Try converting to bool + catch (boost::bad_any_cast) // Try converting to bool { try { @@ -144,7 +146,7 @@ int AnyAsInt(const any& val) if (any_cast(val) == true) i = 1; return i; } - catch (boost::bad_any_cast &e) // Does not convert, return + catch (boost::bad_any_cast) // Does not convert, return { LogWarning("invalid conversion to type \'int\'"); return 0; @@ -156,35 +158,35 @@ int AnyAsInt(const any& val) // Gets type of 'any' val // 0 -> int; 1 -> float; 2 -> bool; 3 -> string; -int any_type(const any& val) +int any_type(const boost::any& val) { try // Try converting to int { int i = any_cast(val); return 0; } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to float { float f = any_cast(val); return 1; } - catch (boost::bad_any_cast &e) + catch (boost::bad_any_cast) { try // Try converting to bool { bool b = any_cast(val); return 2; } - catch (boost::bad_any_cast &e) // Try converting to string + catch (boost::bad_any_cast) // Try converting to string { try { string s = any_cast(val); return 3; } - catch (boost::bad_any_cast &e) // Does not convert, return + catch (boost::bad_any_cast) // Does not convert, return { LogWarning("variable has no type"); return -1; diff --git a/Slang/boost_x64_release.props b/Slang/boost_x64_release.props new file mode 100644 index 0000000..e7b305b --- /dev/null +++ b/Slang/boost_x64_release.props @@ -0,0 +1,17 @@ + + + + + + <_PropertySheetDisplayName>boost_x64_release + + + + D:\Code\boost;%(AdditionalIncludeDirectories) + + + D:\Code\boost\stage\lib;%(AdditionalLibraryDirectories) + + + + \ No newline at end of file diff --git a/Slang/builtin.h b/Slang/builtin.h index 9a4574f..d6a9794 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -7,11 +7,11 @@ #include #include #include -#include #include #include "strops.h" #include "graphics.h" #include "anyops.h" +#include using namespace std; using namespace boost; @@ -19,7 +19,7 @@ using namespace boost; vector types = { "int", "float", "string", "bool", "void", "null" }; unordered_map>> builtinFunctionValues; -unordered_map builtinVarVals; +unordered_map builtinVarVals; Parser mainWindow; @@ -105,7 +105,7 @@ int GetBuiltins(const string& s) } // Executes -any CPPFunction(const string& name, const vector& args) +boost::any CPPFunction(const string& name, const vector& args) { if (name == "CPP.Math.Sin") return sin(AnyAsFloat(args[0])); diff --git a/Slang/eval.cpp b/Slang/eval.cpp index 4373a00..1d2d422 100644 --- a/Slang/eval.cpp +++ b/Slang/eval.cpp @@ -7,7 +7,7 @@ #include #include "eval.h" #include "strops.h" -#include "builtin.h" +// #include "builtin.h" using namespace std; float precedence(const char& op) { @@ -29,7 +29,7 @@ float applyOp(const float& a, const float& b, const char& op) { case '^': return pow(a, b); } string s(1, op); - LogWarning("operator \'" + s + "\' does not exist"); + //LogWarning("operator \'" + s + "\' does not exist"); return 0; } diff --git a/Slang/eval.h b/Slang/eval.h index 8650d41..51ac197 100644 --- a/Slang/eval.h +++ b/Slang/eval.h @@ -1,6 +1,6 @@ #ifndef EVAL_H #define EVAL_H -float evaluate(std::string tokens); +float evaluate(const std::string& t); #endif \ No newline at end of file diff --git a/Slang/graphics.h b/Slang/graphics.h index 918aba2..389efe0 100644 --- a/Slang/graphics.h +++ b/Slang/graphics.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "strops.h" #include "builtin.h" #include "main.h" @@ -34,7 +35,7 @@ public: bool OnUserUpdate(float fElapsedTime) override { - ExecuteFunction("Update", vector {}); + ExecuteFunction("Update", vector {}); // Called once per frame //for (int x = 0; x < ScreenWidth(); x++) diff --git a/Slang/main.h b/Slang/main.h index 25cbe2d..afcb5e1 100644 --- a/Slang/main.h +++ b/Slang/main.h @@ -3,8 +3,7 @@ #define MAIN_H using namespace std; -using namespace boost; -any ExecuteFunction(const string functionName, const vector inputVarVals); +boost::any ExecuteFunction(const string& functionName, const vector& inputVarVals); #endif diff --git a/Slang/strops.cpp b/Slang/strops.cpp index 83b42de..60c0d39 100644 --- a/Slang/strops.cpp +++ b/Slang/strops.cpp @@ -3,8 +3,9 @@ #include #include #include +#include #include "strops.h" -#include "builtin.h" +//#include "builtin.h" using namespace std; const string WHITESPACE = " \n\r\t\f\v"; @@ -20,11 +21,12 @@ bool isNumber(const string& str) bool stob(string str) { - transform(str.begin(), str.end(), str.begin(), ::tolower); - istringstream is(str); - bool b; - is >> boolalpha >> b; - return b; + //transform(str.begin(), str.end(), str.begin(), ::tolower); + //istringstream is(str); + //bool b; + //is >> boolalpha >> b; + //return b; + return false; } string StringRaw(const string& s) diff --git a/Slang/strops.h b/Slang/strops.h index 6ead244..537e853 100644 --- a/Slang/strops.h +++ b/Slang/strops.h @@ -1,6 +1,8 @@ #ifndef STROPS_H #define STROPS_H +#include + using namespace std; bool isNumber(const string& str); From 7b737d779903e076e15f658bcb1bf7d428797d8b Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 6 Jan 2022 18:41:41 -0500 Subject: [PATCH 31/33] hgf --- Slang/Main.cpp | 39 ++++++++++++++++++++------------------- Slang/anyops.h | 19 ++++++++++++++++++- Slang/builtin.h | 10 ++++++++-- Slang/strops.cpp | 10 +++------- Slang/strops.h | 2 +- 5 files changed, 50 insertions(+), 30 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 2976c50..ff86d20 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -286,14 +286,17 @@ int varOperation(const vector& str, unordered_map& v boost::any ProcessLine(const vector>& words, int lineNum, unordered_map& variableValues) { + if (words[lineNum].size() == 0) + return nullType; + if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') - return boost::any{}; + return nullType; // If print statement (deprecated, now use CPP.System.Print() function) else if (words[lineNum][0] == "print") { cout << AnyAsString(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; - return boost::any{}; + return nullType; } // Check if function return @@ -308,7 +311,7 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder else if (IsFunction(trim(split(words[lineNum][0], '(')[0]))) { ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues)); - return boost::any{}; + return nullType; } // Iterate through all types to see if line inits or @@ -316,7 +319,7 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder else if (countInVector(types, words[lineNum][0]) > 0) { variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); - return boost::any{}; + return nullType; } // Check existing variables: If matches, then it means @@ -325,7 +328,7 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder { // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right varOperation(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); - return boost::any{}; + return nullType; } // Gathers while loop contents @@ -361,11 +364,11 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) { boost::any returnVal = ProcessLine(innerWords, lineNum, variableValues); - if (AnyAsString(returnVal) != "") + if (!returnVal.empty() && returnVal.empty() == false) return returnVal; } } - return boost::any{}; + return nullType; } // Gathers if statement contents @@ -403,7 +406,7 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder for (int l = 0; l < (int)ifContents.size(); l++) { boost::any returnVal = ProcessLine(innerWords, l, variableValues); - if (!returnVal.empty()) + if (!returnVal.empty() && returnVal.empty() == false) return returnVal; } } @@ -438,9 +441,9 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder { ProcessLine(innerWords, lineNum, variableValues); } - return boost::any{}; + return nullType; } - return boost::any{}; + return nullType; } //// Gathers else statement contents //if (words[lineNum][0] == "else") @@ -448,7 +451,7 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder // //} - return boost::any{}; + return nullType; } boost::any ExecuteFunction(const string& functionName, const vector& inputVarVals) @@ -457,11 +460,9 @@ boost::any ExecuteFunction(const string& functionName, const vector& vector> words = functionValues[functionName]; unordered_map variableValues; - vector args = split(functionValues[functionName][0][0], ','); + vector args = split(words[0][0], ','); for (int i = 0; i < (int)inputVarVals.size(); i++) - { - variableValues[trim(args[i])] = EvalExpression(AnyAsString(inputVarVals[i]), variableValues); - } + variableValues[args[i]] = inputVarVals[i]; //Iterate through all lines in function for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) @@ -475,10 +476,10 @@ boost::any ExecuteFunction(const string& functionName, const vector& { LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\nIn function: " + functionName + "\nLine: " + to_string(lineNum)); } - if (!returnVal.empty()) + if (!returnVal.empty() && returnVal.empty() == false) return returnVal; } - return boost::any{}; + return nullType; } int parseSlang(string script) @@ -537,8 +538,8 @@ int parseSlang(string script) globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]); else if (words[lineNum][0] == "bool") globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]); - else - LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); + //else + // LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); } } diff --git a/Slang/anyops.h b/Slang/anyops.h index b89dadb..b46cf31 100644 --- a/Slang/anyops.h +++ b/Slang/anyops.h @@ -75,7 +75,7 @@ string AnyAsString(const boost::any& val) catch (boost::bad_any_cast) // Does not convert, return { LogWarning("invalid conversion to type \'string\'"); - return ""; + return "ERR"; } } } @@ -196,4 +196,21 @@ int any_type(const boost::any& val) } } +// Gets if any is NullType +bool any_null(const boost::any& val) +{ + /*if (val.type() == typeid(NullType)) + return true;*/ + return false; + //try // Try converting to Null + //{ + // NullType n = any_cast(val); + // return true; + //} + //catch (boost::bad_any_cast) + //{ + // return false; + //} +} + #endif diff --git a/Slang/builtin.h b/Slang/builtin.h index d6a9794..b551da3 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -23,6 +23,12 @@ unordered_map builtinVarVals; Parser mainWindow; +class NullType { +public: + string type = "NULL"; +}; + +boost::any nullType; int LogWarning(const string& warningText) { @@ -96,8 +102,8 @@ int GetBuiltins(const string& s) builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]); else if (words[lineNum][0] == "bool") builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]); - else - LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); + //else + // LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); } } diff --git a/Slang/strops.cpp b/Slang/strops.cpp index 60c0d39..f85ed83 100644 --- a/Slang/strops.cpp +++ b/Slang/strops.cpp @@ -19,14 +19,10 @@ 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; - //is >> boolalpha >> b; - //return b; - return false; + bool b = trim(str) == "true"; + return b; } string StringRaw(const string& s) diff --git a/Slang/strops.h b/Slang/strops.h index 537e853..de67f3c 100644 --- a/Slang/strops.h +++ b/Slang/strops.h @@ -7,7 +7,7 @@ using namespace std; bool isNumber(const string& str); -bool stob(string str); +bool stob(const string& str); string StringRaw(const string& s); From bde9ed7222d4adaa7aed6c1e89c7bd374e32b9ad Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 10 Jan 2022 14:58:31 -0500 Subject: [PATCH 32/33] xdfg --- Slang/Main.cpp | 75 ++++++++++++++++++++++++++--------------------- Slang/anyops.h | 2 +- Slang/builtin.h | 4 +-- Slang/builtin.slg | 8 ++--- Slang/strops.cpp | 12 ++++++++ Slang/strops.h | 2 ++ 6 files changed, 62 insertions(+), 41 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index ff86d20..f1cd874 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -22,10 +22,10 @@ using namespace boost; unordered_map globalVariableValues; unordered_map>> functionValues; -boost::any GetVariableValue(const string& varName, const unordered_map& variableVals) +boost::any GetVariableValue(const string& varName, const unordered_map& variableValues) { - auto iA = variableVals.find(varName); - if (iA != variableVals.end()) + auto iA = variableValues.find(varName); + if (iA != variableValues.end()) { return iA->second; } @@ -43,24 +43,25 @@ boost::any GetVariableValue(const string& varName, const unordered_map& variableVals) +bool IsVar(const string& varName, const unordered_map& variableValues) { - if (variableVals.find(varName) != variableVals.end()) + if (variableValues.find(varName) != variableValues.end()) return true; else return false; } -vector VarValues(const vector& varNames, const unordered_map& variableVals) +vector VarValues(const vector& varNames, const unordered_map& variableValues) { vector realValues; for (int varIndex = 0; varIndex < varNames.size(); varIndex++) { string varName = trim(varNames[varIndex]); + cout << varName << endl; - auto iA = variableVals.find(varName); - if (iA != variableVals.end()) + auto iA = variableValues.find(varName); + if (iA != variableValues.end()) { realValues.push_back(iA->second); } @@ -84,8 +85,15 @@ bool IsFunction(const string& funcName) else return false; } +bool IsCPPFunction(const string& funcName) +{ + if (funcName[0] == 'C' && funcName[1] == 'P' && funcName[2] == 'P' && funcName[2] == '.') + return true; + else + return false; +} -boost::any EvalExpression(const string& ex, const unordered_map& variableVals) +boost::any EvalExpression(const string& ex, unordered_map& variableValues) { string expression = trim(ex); bool inQuotes = false; @@ -93,8 +101,8 @@ boost::any EvalExpression(const string& ex, const unordered_map& variableVals) +bool BooleanLogic(const string& valA, const string& determinant, const string& valB, unordered_map& variableValues) { - boost::any valARealValue = EvalExpression(valA, variableVals); - boost::any valBRealValue = EvalExpression(valB, variableVals); + boost::any valARealValue = EvalExpression(valA, variableValues); + boost::any valBRealValue = EvalExpression(valB, variableValues); if (determinant == "==") return AnyAsString(valARealValue) == AnyAsString(valBRealValue); @@ -286,9 +294,6 @@ int varOperation(const vector& str, unordered_map& v boost::any ProcessLine(const vector>& words, int lineNum, unordered_map& variableValues) { - if (words[lineNum].size() == 0) - return nullType; - if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') return nullType; @@ -318,13 +323,14 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder // re-inits a variable then store it with it's value else if (countInVector(types, words[lineNum][0]) > 0) { - variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); + //cout << unWrapVec(words[lineNum]) << endl; + variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(slice(words[lineNum], 3, -1)), variableValues); return nullType; } // Check existing variables: If matches, then it means // the variables value is getting changed with an operator - else if (IsVar(words[lineNum][0], variableValues)) + else if (IsVar(words[lineNum][0], variableValues) || IsVar(words[lineNum][0], globalVariableValues)) { // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right varOperation(vector(words[lineNum].begin(), words[lineNum].end()), variableValues); @@ -459,13 +465,16 @@ boost::any ExecuteFunction(const string& functionName, const vector& // Get contents of function vector> words = functionValues[functionName]; - unordered_map variableValues; - vector args = split(words[0][0], ','); - for (int i = 0; i < (int)inputVarVals.size(); i++) + unordered_map variableValues = {}; + vector args = words[0]; + for (int i = 0; i < (int)inputVarVals.size(); i++) { + variableValues[args[i]] = inputVarVals[i]; + cout << "\x1B[33m" << args[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl; + } //Iterate through all lines in function - for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) + for (int lineNum = 1; lineNum < (int)words.size(); lineNum++) { boost::any returnVal = 0; try @@ -514,8 +523,8 @@ int parseSlang(string script) } } - args = replace(args, functName + ",", ""); - functionContents.push_back(vector{args}); + args = replace(args, functName + " ", ""); + functionContents.push_back(split(args, ',')); int numOfBrackets = 1; for (int p = lineNum + 3; p < (int)words.size(); p++) diff --git a/Slang/anyops.h b/Slang/anyops.h index b46cf31..77d9fac 100644 --- a/Slang/anyops.h +++ b/Slang/anyops.h @@ -75,7 +75,7 @@ string AnyAsString(const boost::any& val) catch (boost::bad_any_cast) // Does not convert, return { LogWarning("invalid conversion to type \'string\'"); - return "ERR"; + return ""; } } } diff --git a/Slang/builtin.h b/Slang/builtin.h index b551da3..2fafdb9 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -78,8 +78,8 @@ int GetBuiltins(const string& s) } } - args = replace(args, functName + ",", ""); - functionContents.push_back(vector{args}); + args = replace(args, functName + " ", ""); + functionContents.push_back(split(args, ',')); int numOfBrackets = 1; for (int p = lineNum + 3; p < (int)words.size(); p++) diff --git a/Slang/builtin.slg b/Slang/builtin.slg index 76b3943..ac192f0 100644 --- a/Slang/builtin.slg +++ b/Slang/builtin.slg @@ -6,7 +6,7 @@ float EulersNumber = 2.71828183 // Trigonometric function Sin func Sin(input) { - print input + //Print(input) float out = CPP.Math.Sin(input) return out } @@ -70,13 +70,11 @@ func SetPixel(x, y, r, g, b) // Prints input value to console func Print(in) { - string out = CPP.System.Print(in) - return out + CPP.System.Print(in) } // Prints input value to console with appended newline '\n' func Printl(in) { - string out = CPP.System.PrintLine(in) - return out + CPP.System.PrintLine(in) } diff --git a/Slang/strops.cpp b/Slang/strops.cpp index f85ed83..3cc700d 100644 --- a/Slang/strops.cpp +++ b/Slang/strops.cpp @@ -217,6 +217,18 @@ vector rangeInVec(const vector& str, const int& min, int max) { return newStr; } +vector slice(vector const& v, int min, int max) +{ + if (max == -1) + max = (int)v.size() - 1; + + auto first = v.cbegin() + min; + auto last = v.cbegin() + max + 1; + + vector vec(first, last); + return vec; +} + string rangeInStr(const string& str, const int& min, int max) { if (max == -1) max = (int)str.size(); diff --git a/Slang/strops.h b/Slang/strops.h index de67f3c..b34fbf0 100644 --- a/Slang/strops.h +++ b/Slang/strops.h @@ -39,6 +39,8 @@ vector removeTabs(const vector& str, const int& amnt); vector rangeInVec(const vector& str, const int& min, const int& max); +vector slice(vector const& v, int min, int max); + string rangeInStr(const string& str, const int& min, const int& max); string unWrapVec(const vector& vec); From 42bf1ccccee1469108692867e3315049695a27f2 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 10 Jan 2022 16:22:55 -0500 Subject: [PATCH 33/33] FINALLLY!!!!!!!!! Working version !! --- Slang/Main.cpp | 45 +++++++++++++++++++++++---------------------- Slang/builtin.h | 21 +++++++++++---------- Slang/builtin.slg | 3 +-- Slang/script.slg | 6 ++++-- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index f1cd874..143160f 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -58,7 +58,7 @@ vector VarValues(const vector& varNames, const unordered_map for (int varIndex = 0; varIndex < varNames.size(); varIndex++) { string varName = trim(varNames[varIndex]); - cout << varName << endl; + //cout << varName << endl; auto iA = variableValues.find(varName); if (iA != variableValues.end()) @@ -98,6 +98,8 @@ boost::any EvalExpression(const string& ex, unordered_map& v string expression = trim(ex); bool inQuotes = false; + CompilerLog("OLDEXPRESSION: |" + expression + "|"); + // If no operations are applied, then return self if ((count(expression, '+') == 0 && count(expression, '-') == 0 && count(expression, '*') == 0 && count(expression, '/') == 0 && count(expression, '(') == 0 && count(expression, '^') == 0) || split(expression, '.')[0] == "CPP") { @@ -113,7 +115,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v y++; } - //cout << split(expression, '(')[0] << " " << argContents << endl; + CompilerLog(split(expression, '(')[0] + " " + AnyAsString(GetVariableValue(split(argContents, ',')[0], variableValues))); return ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues)); } else if (split(expression, '.')[0] == "CPP" && !inQuotes) @@ -126,7 +128,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v y++; } - //cout << split(expression, '(')[0] << " " << unWrapVec(VarValues(split(argContents, ','), variables, variableValues)) << endl; + CompilerLog(split(expression, '(')[0] + " " + argContents); return CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues)); } else @@ -163,6 +165,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v i++; } + CompilerLog(split(expression, '(')[0] + " " + AnyAsString(GetVariableValue(split(argContents, ',')[0], variableValues))); string returnVal = AnyAsString(ExecuteFunction(name, VarValues(split(argContents, ','), variableValues))); newExpression += returnVal; //cout << newExpression << endl; @@ -196,7 +199,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v newExpression += expression[i]; } } - //cout << "NEW EXPRESSION: " << newExpression << endl; + CompilerLog("NEW EXPRESSION: |" + newExpression + "|"); bool addStrings = false; for (int i = 0; i < (int)newExpression.size(); i++) @@ -223,7 +226,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v } //cout << "NewSTRING = " << Quoted(withoutParenthesis) << endl; - return Quoted(withoutParenthesis); + return withoutParenthesis; } else return evaluate(newExpression); @@ -248,7 +251,7 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); else LogWarning("unrecognized determinant \'" + determinant + "\'"); - + return false; } @@ -294,6 +297,9 @@ int varOperation(const vector& str, unordered_map& v boost::any ProcessLine(const vector>& words, int lineNum, unordered_map& variableValues) { + CompilerLog(unWrapVec(words[lineNum])); + CompilerLog(AnyAsString(GetVariableValue("out", variableValues))); + if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') return nullType; @@ -321,10 +327,10 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder // Iterate through all types to see if line inits or // re-inits a variable then store it with it's value - else if (countInVector(types, words[lineNum][0]) > 0) + else if (countInVector(types, trim(words[lineNum][0])) > 0) { - //cout << unWrapVec(words[lineNum]) << endl; variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(slice(words[lineNum], 3, -1)), variableValues); + CompilerLog("new var :: " + words[lineNum][1] + " = " + AnyAsString(variableValues[words[lineNum][1]])); return nullType; } @@ -370,7 +376,7 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) { boost::any returnVal = ProcessLine(innerWords, lineNum, variableValues); - if (!returnVal.empty() && returnVal.empty() == false) + if (!returnVal.empty()) return returnVal; } } @@ -412,7 +418,7 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder for (int l = 0; l < (int)ifContents.size(); l++) { boost::any returnVal = ProcessLine(innerWords, l, variableValues); - if (!returnVal.empty() && returnVal.empty() == false) + if (!returnVal.empty()) return returnVal; } } @@ -470,7 +476,7 @@ boost::any ExecuteFunction(const string& functionName, const vector& for (int i = 0; i < (int)inputVarVals.size(); i++) { variableValues[args[i]] = inputVarVals[i]; - cout << "\x1B[33m" << args[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl; + //cout << "\x1B[33m" << args[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl; } //Iterate through all lines in function @@ -485,7 +491,7 @@ boost::any ExecuteFunction(const string& functionName, const vector& { LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\nIn function: " + functionName + "\nLine: " + to_string(lineNum)); } - if (!returnVal.empty() && returnVal.empty() == false) + if (!returnVal.empty()) return returnVal; } return nullType; @@ -512,22 +518,17 @@ int parseSlang(string script) 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) - { - args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ","; - } - else - { - args += replace(replace(words[lineNum][w], "(", " "), ")", ""); - } + for (int w = 1; w < (int)words[lineNum].size(); w++) // Get all words from the instantiation line: these are the args + { + args += replace(replace(words[lineNum][w], "(", " "), ")", ""); } args = replace(args, functName + " ", ""); + CompilerLog(args); functionContents.push_back(split(args, ',')); int numOfBrackets = 1; - for (int p = lineNum + 3; p < (int)words.size(); p++) + for (int p = lineNum + 2; p < (int)words.size(); p++) { numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); if (numOfBrackets == 0) diff --git a/Slang/builtin.h b/Slang/builtin.h index 2fafdb9..1f58471 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -36,6 +36,12 @@ int LogWarning(const string& warningText) return 1; } +int CompilerLog(const string& logText) +{ + cerr << "\x1B[32mclog: " << logText << "\033[0m\t\t" << endl; + return 1; +} + int LogCriticalError(const string& errorText) { cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl; @@ -67,22 +73,17 @@ int GetBuiltins(const string& s) 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) - { - args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ","; - } - else - { - args += replace(replace(words[lineNum][w], "(", " "), ")", ""); - } + for (int w = 1; w < (int)words[lineNum].size(); w++) // Get all words from the instantiation line: these are the args + { + args += replace(replace(words[lineNum][w], "(", " "), ")", ""); } args = replace(args, functName + " ", ""); + CompilerLog(args); functionContents.push_back(split(args, ',')); int numOfBrackets = 1; - for (int p = lineNum + 3; p < (int)words.size(); p++) + for (int p = lineNum + 2; p < (int)words.size(); p++) { numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); if (numOfBrackets == 0) diff --git a/Slang/builtin.slg b/Slang/builtin.slg index ac192f0..cc542cf 100644 --- a/Slang/builtin.slg +++ b/Slang/builtin.slg @@ -1,12 +1,11 @@ // Default variables, can be overwritten // if re-initialized or changed -float PI = 3.14159265358979 +float PI = 3.14159265358979323846264338 float EulersNumber = 2.71828183 // Trigonometric function Sin func Sin(input) { - //Print(input) float out = CPP.Math.Sin(input) return out } diff --git a/Slang/script.slg b/Slang/script.slg index 1332f21..d34dec2 100644 --- a/Slang/script.slg +++ b/Slang/script.slg @@ -2,6 +2,7 @@ func Main(input, in) { + print "PI is: " + PI int x = 1 print x @@ -12,13 +13,14 @@ func Main(input, in) print x float s = Sin(x) print s - int k = Sigmoid(s) + k = Sigmoid(s) print k x += 1 } - CPP.Graphics.Init(64, 64, 4) + //CPP.Graphics.Init(64, 64, 4) + return 0 } func Update(input)