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] 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; }