mirror of
https://github.com/sam-astro/Z-Sharp.git
synced 2025-12-11 16:22:12 +00:00
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
This commit is contained in:
parent
ee8197a921
commit
49c26219fc
@ -63,7 +63,17 @@ string AddItem(string varName, string variableContent, string addItem)
|
||||
return variableContent + addItem;
|
||||
}
|
||||
|
||||
string GetRealValue(string varName, vector<string>& variables, vector<string>& 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<string>& variables, vector<string>& variableVals)
|
||||
{
|
||||
string typ = "string";
|
||||
bool isVar = false;
|
||||
@ -99,22 +109,29 @@ string GetRealValue(string varName, vector<string>& variables, vector<string>& 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<string>& variables, vector<string>& 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<string>& variables, vector<strin
|
||||
{
|
||||
string name = "";
|
||||
|
||||
string val = "";
|
||||
|
||||
while (i < expression.size() && isalpha(expression[i]))
|
||||
{
|
||||
name += expression[i];
|
||||
@ -131,7 +146,7 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
|
||||
i++;
|
||||
}
|
||||
|
||||
newExpression += GetRealValue(val, variables, variableVals);
|
||||
newExpression += GetVariableValue(name, variables, variableVals);
|
||||
|
||||
i--;
|
||||
}
|
||||
@ -140,22 +155,35 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
|
||||
newExpression += expression[i];
|
||||
}
|
||||
}
|
||||
cout << "NEW EXPRESSION: " << newExpression << endl;
|
||||
|
||||
vector<string> 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<string>& variables, vector<strin
|
||||
|
||||
bool BooleanLogic(string valA, string determinant, string valB, vector<string>& variables, vector<string>& 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<string> str, vector<string>& variables, vector<string>& varia
|
||||
if (str[0] == split(variables[v], ' ')[1])
|
||||
{
|
||||
if (str[1] == "=")
|
||||
variableValues[v] = EvalExpression("(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
variableValues[v] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variables, variableValues);
|
||||
else if (str[1] == "+=")
|
||||
variableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector<string>(str.begin()+2, str.end())) + ")", variables, variableValues);
|
||||
variableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "-=")
|
||||
variableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "*=")
|
||||
@ -200,7 +228,7 @@ int evalEqu(vector<string> str, vector<string>& variables, vector<string>& varia
|
||||
else if (str[1] == "/=")
|
||||
variableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector<string>(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<string> str, vector<string>& variables, vector<string>& varia
|
||||
if (str[0] == split(globalVariables[v], ' ')[1])
|
||||
{
|
||||
if (str[1] == "=")
|
||||
globalVariableValues[v] = EvalExpression("(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
globalVariableValues[v] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variables, variableValues);
|
||||
else if (str[1] == "+=")
|
||||
globalVariableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "-=")
|
||||
@ -220,7 +248,7 @@ int evalEqu(vector<string> str, vector<string>& variables, vector<string>& varia
|
||||
else if (str[1] == "/=")
|
||||
globalVariableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector<string>(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<vector<string>> words, int lineNum, vector<string>& varia
|
||||
return 0;
|
||||
|
||||
if (words[lineNum][0] == "print") {
|
||||
cout << GetRealValue(words[lineNum][1], variables, variableValues) << endl;
|
||||
cout << EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -247,7 +275,7 @@ int ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& 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<vector<string>> words, int lineNum, vector<string>& varia
|
||||
{
|
||||
if (words[lineNum][1] == split(variables[v], ' ')[1])
|
||||
{
|
||||
vector<string> inputs = { words[lineNum][1], words[lineNum][2], words[lineNum][3] };
|
||||
evalEqu(vector<string>(words[lineNum].begin() + 1, words[lineNum].end()), variables, variableValues);
|
||||
|
||||
return 0;
|
||||
@ -266,7 +293,7 @@ int ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& 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<string>(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<vector<string>> words, int lineNum, vector<string>& varia
|
||||
{
|
||||
if (words[lineNum][0] == split(variables[v], ' ')[1])
|
||||
{
|
||||
vector<string> inputs = { words[lineNum][0], words[lineNum][1], words[lineNum][2] };
|
||||
evalEqu(inputs, variables, variableValues);
|
||||
evalEqu(vector<string>(words[lineNum].begin(), words[lineNum].end()), variables, variableValues);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -381,7 +407,7 @@ int ExecuteFunction(string functionName, vector<string> 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<vector<string>> words;
|
||||
@ -457,7 +483,7 @@ int parseSlang(string script)
|
||||
}
|
||||
|
||||
// Executes main, which is the starting function
|
||||
ExecuteFunction("Main", vector<string> {"hi", "7"});
|
||||
ExecuteFunction("Main", vector<string> {"\"hi\"", "7"});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <limits>
|
||||
#include <stack>
|
||||
#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 <float> values;
|
||||
stack <char> 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;
|
||||
//}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user