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