From 49c26219fc9124bcd7434329bc0ec12526c84ef4 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Sat, 1 Jan 2022 22:53:35 -0500 Subject: [PATCH] Worked on evaluator and concatenating strings Next I need to better differentiate raw and quoted strings because the system converts at the wrong time and never converts back --- Slang/Main.cpp | 92 +++++++++++++++++++++++++++++++----------------- Slang/eval.cpp | 56 +++++++++++++++++++---------- Slang/script.slg | 9 ++--- Slang/strops.cpp | 2 +- 4 files changed, 103 insertions(+), 56 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index b408b85..810eff1 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -63,7 +63,17 @@ string AddItem(string varName, string variableContent, string addItem) return variableContent + addItem; } -string GetRealValue(string varName, vector& variables, vector& variableVals) +string StringRaw(string str) +{ + string withoutQuotes; + + for (int ch = 1; ch < (int)str.size() - 1; ch++) + withoutQuotes += str[ch]; + + return withoutQuotes; +} + +string GetVariableValue(string varName, vector& variables, vector& variableVals) { string typ = "string"; bool isVar = false; @@ -99,22 +109,29 @@ string GetRealValue(string varName, vector& variables, vector& v return variableVals[v]; } } - else if (!isVar && count(varName, '\"') > 0) - { - string withoutQuotes; + //else if (!isVar && count(varName, '\"') > 0) + //{ + // string withoutQuotes; - for (int ch = 1; ch < (int)varName.size() - 1; ch++) - withoutQuotes += varName[ch]; + // for (int ch = 1; ch < (int)varName.size() - 1; ch++) + // withoutQuotes += varName[ch]; - return withoutQuotes; - } + // return withoutQuotes; + //} return varName; } string EvalExpression(string expression, vector& variables, vector& variableVals) { + expression = trim(expression); + cout << "EXPRESSION: " << expression << endl; + + if (count(expression, '+') == 0 && count(expression, '-') == 0 && count(expression, '*') == 0 && count(expression, '/') == 0) + return GetVariableValue(expression, variables, variableVals); + string newExpression = ""; + bool inQuotes = false; for (int i = 0; i < expression.size(); i++) { @@ -122,8 +139,6 @@ string EvalExpression(string expression, vector& variables, vector& variables, vector& variables, vector trimmedVersion = split(replace(replace(newExpression, "(", ""), ")", ""), '+'); bool addStrings = false; - string newStr = ""; - for (int i = 0; i < newExpression.size(); i++) - if (isalpha(expression[i])) + for (int i = 0; i < (int)newExpression.size(); i++) + if (isalpha(newExpression[i]) || newExpression[i] == '\"') { addStrings = true; break; } if (addStrings) { - for (int i = 0; i < trimmedVersion.size(); i++) - newStr += trimmedVersion[i]; + string newStr = ""; + inQuotes = false; + string withoutParenthesis = ""; + for (int i = 0; i < (int)newExpression.size(); i++) + { + if (newExpression[i] == '\"') + { + inQuotes = !inQuotes; + continue; + } + if (inQuotes) + withoutParenthesis += newExpression[i]; + if (!inQuotes && newExpression[i] != '(' && newExpression[i] != ')' && newExpression[i] != '+' && newExpression[i] != ' ') + withoutParenthesis += newExpression[i]; + } - return newStr; + cout << "NewSTRING = " << withoutParenthesis << endl; + return withoutParenthesis; } else return to_string(evaluate(newExpression)); @@ -163,8 +191,8 @@ string EvalExpression(string expression, vector& variables, vector& variables, vector& variableVals) { - string valARealValue = GetRealValue(valA, variables, variableVals); - string valBRealValue = GetRealValue(valB, variables, variableVals); + string valARealValue = GetVariableValue(valA, variables, variableVals); + string valBRealValue = GetVariableValue(valB, variables, variableVals); if (determinant == "==") return valARealValue == valBRealValue; @@ -190,9 +218,9 @@ int evalEqu(vector str, vector& variables, vector& varia if (str[0] == split(variables[v], ' ')[1]) { if (str[1] == "=") - variableValues[v] = EvalExpression("(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); + 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); + 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] == "*=") @@ -200,7 +228,7 @@ int evalEqu(vector str, vector& variables, vector& varia else if (str[1] == "/=") variableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl << endl; + cout << variables[v] << " is " << variableValues[v] << endl; return 0; } } @@ -210,7 +238,7 @@ int evalEqu(vector str, vector& variables, vector& varia if (str[0] == split(globalVariables[v], ' ')[1]) { if (str[1] == "=") - globalVariableValues[v] = EvalExpression("(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); + 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] == "-=") @@ -220,7 +248,7 @@ int evalEqu(vector str, vector& variables, vector& varia else if (str[1] == "/=") globalVariableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl << endl; + cout << variables[v] << " is " << variableValues[v] << endl; return 0; } } @@ -232,7 +260,7 @@ int ProcessLine(vector> words, int lineNum, vector& varia return 0; if (words[lineNum][0] == "print") { - cout << GetRealValue(words[lineNum][1], variables, variableValues) << endl; + cout << EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues) << endl; return 0; } @@ -247,7 +275,7 @@ int ProcessLine(vector> words, int lineNum, vector& varia } // First iterate through all types to see if line - // is a variable then store it with it's value + // 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]) @@ -257,7 +285,6 @@ int ProcessLine(vector> words, int lineNum, vector& varia { if (words[lineNum][1] == split(variables[v], ' ')[1]) { - vector inputs = { words[lineNum][1], words[lineNum][2], words[lineNum][3] }; evalEqu(vector(words[lineNum].begin() + 1, words[lineNum].end()), variables, variableValues); return 0; @@ -266,7 +293,7 @@ int ProcessLine(vector> words, int lineNum, vector& varia //Checks if it is variable variables.push_back(words[lineNum][0] + " " + words[lineNum][1]); - variableValues.push_back(GetRealValue((string)words[lineNum][3], variables, variableValues)); + 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 0; } @@ -276,8 +303,7 @@ int ProcessLine(vector> words, int lineNum, vector& varia { if (words[lineNum][0] == split(variables[v], ' ')[1]) { - vector inputs = { words[lineNum][0], words[lineNum][1], words[lineNum][2] }; - evalEqu(inputs, variables, variableValues); + evalEqu(vector(words[lineNum].begin(), words[lineNum].end()), variables, variableValues); return 0; } @@ -381,7 +407,7 @@ int ExecuteFunction(string functionName, vector inputVarVals) for (int i = 0; i < (int)functionNameParts.size(); i++) { variables.push_back(trim(functionNameParts[i])); - variableValues.push_back(GetRealValue(trim(inputVarVals[i]), variables, variableValues)); + variableValues.push_back(EvalExpression(inputVarVals[i], variables, variableValues)); cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl; } vector> words; @@ -457,7 +483,7 @@ int parseSlang(string script) } // Executes main, which is the starting function - ExecuteFunction("Main", vector {"hi", "7"}); + ExecuteFunction("Main", vector {"\"hi\"", "7"}); return 0; } diff --git a/Slang/eval.cpp b/Slang/eval.cpp index 86b439b..873f073 100644 --- a/Slang/eval.cpp +++ b/Slang/eval.cpp @@ -6,6 +6,7 @@ #include #include #include "eval.h" +#include "strops.h" using namespace std; float precedence(char op) { @@ -13,6 +14,8 @@ float precedence(char op) { return 1; if (op == '*' || op == '/') return 2; + if (op == '^') + return 3; return 0; } @@ -22,16 +25,20 @@ float applyOp(float a, float b, char op) { case '-': return a - b; case '*': return a * b; case '/': return a / b; + case '^': return pow(a, b); } } // Function that returns value of // expression after evaluation. float evaluate(string tokens) { + tokens = replace(tokens, " ", ""); + float i; stack values; stack ops; + bool negative = false; for (i = 0; i < tokens.length(); i++) { @@ -49,9 +56,9 @@ float evaluate(string tokens) { // Current token is a number, push // it to stack for numbers. - else if (isdigit(tokens[i])) { + else if (isdigit(tokens[i])) + { float val = 0; - bool encounteredDecimal = false; int decPlaces = 0; // There may be more than one @@ -76,7 +83,7 @@ float evaluate(string tokens) { i++; } - + values.push(val); i--; @@ -105,21 +112,34 @@ float evaluate(string tokens) { // Current token is an operator. else { - while (!ops.empty() && precedence(ops.top()) - >= precedence(tokens[i])) { - float val2 = values.top(); - values.pop(); - - float val1 = values.top(); - values.pop(); - - char op = ops.top(); - ops.pop(); - - values.push(applyOp(val1, val2, op)); + if (tokens[i] == '-' && (i == 0 || tokens[i-1] == '*' || tokens[i-1] == '/' || tokens[i-1] == '+' || tokens[i-1] == '-' || tokens[i-1] == '^')) + { + negative = true; + continue; } + else + { + while (!ops.empty() && precedence(ops.top()) >= precedence(tokens[i])) { + float val2 = values.top(); + values.pop(); - ops.push(tokens[i]); + float val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + ops.push(tokens[i]); + } + } + + if (negative) + { + values.top() *= -1; + negative = false; } } @@ -144,9 +164,9 @@ float evaluate(string tokens) { // Used to test evaluator //int main() { -// cout << evaluate("10 + 2 * 6") << "\n"; +// cout << evaluate("-10 + 2 * 6 * -3") << "\n"; // cout << evaluate("100 * 2 + 12") << "\n"; -// cout << evaluate("100 * ( 2 + 12 )") << "\n"; +// cout << evaluate("100 * (0 + (12 - 3))") << "\n"; // cout << evaluate("0.05*0.05"); // return 0; //} diff --git a/Slang/script.slg b/Slang/script.slg index 428611d..267427e 100644 --- a/Slang/script.slg +++ b/Slang/script.slg @@ -1,4 +1,3 @@ -int x = 1 void Next(string in, string sin) { @@ -13,7 +12,7 @@ void Next(string in, string sin) void Main(string input, int in2) { - int x = 0 + int x = 1 int a = 0 int b = 1 @@ -25,11 +24,13 @@ void Main(string input, int in2) while x < 100 { - k = 10*4/3+(6.2-5.8) + int k = 1 / (1+2.71828^-x) print k + print "X is " + x string str = x - //str += "::" + // str= 0+(" :: ") + str += " :: " str += k x += 10 diff --git a/Slang/strops.cpp b/Slang/strops.cpp index 9f21f36..2a8b19f 100644 --- a/Slang/strops.cpp +++ b/Slang/strops.cpp @@ -187,7 +187,7 @@ float floatval(string s) string replace(string str, string strToReplace, string replaceWith) { string newStr; - string savedLetters;; + string savedLetters; int sameLetters = 0; int startReplaceIndex = 0;