mirror of
https://github.com/sam-astro/Z-Sharp.git
synced 2025-12-11 16:22:12 +00:00
Created expression evaluator, and allow for comments and commenting out code
This commit is contained in:
parent
0a7a8fb709
commit
ee8197a921
372
Slang/Main.cpp
372
Slang/Main.cpp
@ -6,6 +6,8 @@
|
||||
#include <regex>
|
||||
#include <limits>
|
||||
#include <algorithm>
|
||||
#include "eval.h"
|
||||
#include "strops.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -51,231 +53,14 @@ bool isNumber(const string& str)
|
||||
return true;
|
||||
}
|
||||
|
||||
const string WHITESPACE = " \n\r\t\f\v";
|
||||
|
||||
string ltrim(const string& s)
|
||||
{
|
||||
size_t start = s.find_first_not_of(WHITESPACE);
|
||||
return (start == string::npos) ? "" : s.substr(start);
|
||||
}
|
||||
|
||||
string rtrim(const string& s)
|
||||
{
|
||||
size_t end = s.find_last_not_of(WHITESPACE);
|
||||
return (end == string::npos) ? "" : s.substr(0, end + 1);
|
||||
}
|
||||
|
||||
string trim(const string& s) {
|
||||
return rtrim(ltrim(s));
|
||||
}
|
||||
|
||||
vector<string> split(string str, char del) {
|
||||
// declaring temp string to store the curr "word" upto del
|
||||
string temp = "";
|
||||
vector<string> splitWords;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
{
|
||||
// If cur char is not del, then append it to the cur "word", otherwise
|
||||
// you have completed the word, print it, and start a new word.
|
||||
if (str[i] != del)
|
||||
{
|
||||
temp += str[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
splitWords.push_back(temp);
|
||||
temp = "";
|
||||
}
|
||||
}
|
||||
splitWords.push_back(temp);
|
||||
|
||||
return splitWords;
|
||||
}
|
||||
|
||||
int count(string str, char ch) {
|
||||
int cnt = 0;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
if (str[i] == ch)
|
||||
cnt++;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int countNoOverlap(string str, char ch1, char ch2) {
|
||||
int cnt = 0;
|
||||
|
||||
bool waitingForClose = false;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
{
|
||||
if (str[i] == ch1)
|
||||
waitingForClose = true;
|
||||
else if (str[i] == ch2 && waitingForClose == true)
|
||||
{
|
||||
cnt++;
|
||||
waitingForClose = false;
|
||||
}
|
||||
}
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int indexInStr(string str, char ch) {
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
if (str[i] == ch)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int charIndexInVec(vector<string> str, char ch) {
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
for (int w = 0; w < (int)str[i].size(); w++)
|
||||
if (str[i][w] == ch)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int countInVector(vector<string> str, string ch) {
|
||||
int cnt = 0;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
if (str[i] == ch)
|
||||
cnt++;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
string Vec2Str(vector<string> str) {
|
||||
string outStr;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
outStr += str[i] + "\n";
|
||||
|
||||
return outStr;
|
||||
}
|
||||
|
||||
vector<string> removeTabs(vector<string> str, int amnt) {
|
||||
vector<string> newStr;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
{
|
||||
newStr.push_back("");
|
||||
|
||||
for (int c = 0; c < (int)str[i].size(); c++)
|
||||
{
|
||||
if (str[i][c] != '\t' || c >= amnt)
|
||||
newStr[i] += str[i][c];
|
||||
}
|
||||
}
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
vector<string> rangeInVec(vector<string> str, int min, int max) {
|
||||
if (max == -1)
|
||||
max = (int)str.size();
|
||||
|
||||
vector<string> newStr;
|
||||
|
||||
for (int i = min; i < (int)str.size() && i < max; i++)
|
||||
newStr.push_back(str[i]);
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
string rangeInStr(string str, int min, int max) {
|
||||
if (max == -1)
|
||||
max = (int)str.size();
|
||||
|
||||
string newStr;
|
||||
|
||||
for (int i = min; i < (int)str.size() && i < max; i++)
|
||||
newStr += str[i];
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
string unWrapVec(vector<string> vec) {
|
||||
string newStr;
|
||||
|
||||
for (int i = 0; i < (int)vec.size(); i++)
|
||||
{
|
||||
newStr += vec[i];
|
||||
if (i != (int)vec.size() - 1)
|
||||
newStr += " ";
|
||||
}
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
float floatval(string s)
|
||||
{
|
||||
float outfloat;
|
||||
|
||||
if (s == "inf")
|
||||
outfloat = numeric_limits<float>::max();
|
||||
else if (s == "-inf")
|
||||
outfloat = -numeric_limits<float>::max();
|
||||
else if (s == "")
|
||||
outfloat = 0;
|
||||
else
|
||||
outfloat = stof(s);
|
||||
|
||||
return outfloat;
|
||||
}
|
||||
|
||||
string replace(string str, string strToReplace, string replaceWith) {
|
||||
string newStr;
|
||||
string savedLetters;;
|
||||
|
||||
int sameLetters = 0;
|
||||
int startReplaceIndex = 0;
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
{
|
||||
if (str[i] == strToReplace[sameLetters])
|
||||
{
|
||||
savedLetters += str[i];
|
||||
if (sameLetters == 0)
|
||||
startReplaceIndex = i;
|
||||
sameLetters++;
|
||||
|
||||
if ((int)strToReplace.size() == sameLetters)
|
||||
{
|
||||
//cout << "replaced " << "\"" << strToReplace << "\"" << startReplaceIndex << endl;
|
||||
newStr += replaceWith;
|
||||
sameLetters = 0;
|
||||
savedLetters = "";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
newStr += savedLetters + str[i];
|
||||
sameLetters = 0;
|
||||
savedLetters = "";
|
||||
}
|
||||
}
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
string AddItem(string varName, string variableContent, string addItem)
|
||||
{
|
||||
string typ = split(varName, ' ')[0];
|
||||
|
||||
if (typ == "int" || typ == "float" || typ == "double" && isNumber(addItem))
|
||||
{
|
||||
return to_string(floatval(to_string(floatval(variableContent) + floatval(addItem))));
|
||||
}
|
||||
else
|
||||
{
|
||||
return variableContent + addItem;
|
||||
}
|
||||
}
|
||||
|
||||
string GetRealValue(string varName, vector<string>& variables, vector<string>& variableVals)
|
||||
@ -298,28 +83,6 @@ string GetRealValue(string varName, vector<string>& variables, vector<string>& v
|
||||
isVar = true;
|
||||
}
|
||||
|
||||
//if ((typ == "int" || typ == "float" || typ == "double") && isVar)
|
||||
//{
|
||||
// // Checks against global vars
|
||||
// for (int v = 0; v < (int)globalVariables.size(); v++)
|
||||
// if (varName == split(globalVariables[v], ' ')[1])
|
||||
// {
|
||||
// if (globalVariableValues[v] == "inf")
|
||||
// globalVariableValues[v] = 2147483646;
|
||||
// if (globalVariableValues[v] == "-inf")
|
||||
// globalVariableValues[v] = -2147483646;
|
||||
// }
|
||||
// // Checks against local vars
|
||||
// for (int v = 0; v < (int)variables.size(); v++)
|
||||
// if (varName == split(variables[v], ' ')[1])
|
||||
// {
|
||||
// if (variableVals[v] == "inf")
|
||||
// variableVals[v] = 2147483646;
|
||||
// if (variableVals[v] == "-inf")
|
||||
// variableVals[v] = -2147483646;
|
||||
// }
|
||||
//}
|
||||
|
||||
// If it is a var
|
||||
if (isVar)
|
||||
{
|
||||
@ -339,39 +102,83 @@ string GetRealValue(string varName, vector<string>& variables, vector<string>& v
|
||||
else if (!isVar && count(varName, '\"') > 0)
|
||||
{
|
||||
string withoutQuotes;
|
||||
|
||||
for (int ch = 1; ch < (int)varName.size() - 1; ch++)
|
||||
{
|
||||
withoutQuotes += varName[ch];
|
||||
}
|
||||
|
||||
return withoutQuotes;
|
||||
}
|
||||
|
||||
return varName;
|
||||
}
|
||||
|
||||
string EvalExpression(string expression, vector<string>& variables, vector<string>& variableVals)
|
||||
{
|
||||
string newExpression = "";
|
||||
|
||||
for (int i = 0; i < expression.size(); i++)
|
||||
{
|
||||
if (isalpha(expression[i]))
|
||||
{
|
||||
string name = "";
|
||||
|
||||
string val = "";
|
||||
|
||||
while (i < expression.size() && isalpha(expression[i]))
|
||||
{
|
||||
name += expression[i];
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
newExpression += GetRealValue(val, variables, variableVals);
|
||||
|
||||
i--;
|
||||
}
|
||||
else
|
||||
{
|
||||
newExpression += expression[i];
|
||||
}
|
||||
}
|
||||
|
||||
vector<string> trimmedVersion = split(replace(replace(newExpression, "(", ""), ")", ""), '+');
|
||||
bool addStrings = false;
|
||||
string newStr = "";
|
||||
for (int i = 0; i < newExpression.size(); i++)
|
||||
if (isalpha(expression[i]))
|
||||
{
|
||||
addStrings = true;
|
||||
break;
|
||||
}
|
||||
if (addStrings)
|
||||
{
|
||||
for (int i = 0; i < trimmedVersion.size(); i++)
|
||||
newStr += trimmedVersion[i];
|
||||
|
||||
return newStr;
|
||||
}
|
||||
else
|
||||
return to_string(evaluate(newExpression));
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (determinant == "==") {
|
||||
if (determinant == "==")
|
||||
return valARealValue == valBRealValue;
|
||||
}
|
||||
if (determinant == "!=") {
|
||||
if (determinant == "!=")
|
||||
return valARealValue != valBRealValue;
|
||||
}
|
||||
if (determinant == ">=") {
|
||||
if (determinant == ">=")
|
||||
return floatval(valARealValue) >= floatval(valBRealValue);
|
||||
}
|
||||
if (determinant == "<=") {
|
||||
if (determinant == "<=")
|
||||
return floatval(valARealValue) <= floatval(valBRealValue);
|
||||
}
|
||||
if (determinant == ">") {
|
||||
if (determinant == ">")
|
||||
return floatval(valARealValue) > floatval(valBRealValue);
|
||||
}
|
||||
if (determinant == "<") {
|
||||
if (determinant == "<")
|
||||
return floatval(valARealValue) < floatval(valBRealValue);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -383,15 +190,15 @@ int evalEqu(vector<string> str, vector<string>& variables, vector<string>& varia
|
||||
if (str[0] == split(variables[v], ' ')[1])
|
||||
{
|
||||
if (str[1] == "=")
|
||||
variableValues[v] = GetRealValue(str[2], variables, variableValues);
|
||||
variableValues[v] = EvalExpression("(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "+=")
|
||||
variableValues[v] = AddItem(variables[v], variableValues[v], GetRealValue(str[2], variables, variableValues));
|
||||
variableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector<string>(str.begin()+2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "-=")
|
||||
variableValues[v] = to_string(floatval(variableValues[v]) - floatval(GetRealValue(str[2], variables, variableValues)));
|
||||
variableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "*=")
|
||||
variableValues[v] = to_string(floatval(variableValues[v]) * floatval(GetRealValue(str[2], variables, variableValues)));
|
||||
variableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "/=")
|
||||
variableValues[v] = to_string(floatval(variableValues[v]) / floatval(GetRealValue(str[2], variables, variableValues)));
|
||||
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;
|
||||
return 0;
|
||||
@ -403,15 +210,15 @@ int evalEqu(vector<string> str, vector<string>& variables, vector<string>& varia
|
||||
if (str[0] == split(globalVariables[v], ' ')[1])
|
||||
{
|
||||
if (str[1] == "=")
|
||||
globalVariableValues[v] = GetRealValue(str[2], variables, variableValues);
|
||||
globalVariableValues[v] = EvalExpression("(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "+=")
|
||||
globalVariableValues[v] = AddItem(variables[v], globalVariableValues[v], GetRealValue(str[2], variables, variableValues));
|
||||
globalVariableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "-=")
|
||||
globalVariableValues[v] = to_string(floatval(globalVariableValues[v]) - floatval(GetRealValue(str[2], variables, variableValues)));
|
||||
globalVariableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "*=")
|
||||
globalVariableValues[v] = to_string(floatval(globalVariableValues[v]) * floatval(GetRealValue(str[2], variables, variableValues)));
|
||||
globalVariableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||
else if (str[1] == "/=")
|
||||
globalVariableValues[v] = to_string(floatval(globalVariableValues[v]) / floatval(GetRealValue(str[2], variables, variableValues)));
|
||||
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;
|
||||
return 0;
|
||||
@ -419,40 +226,11 @@ int evalEqu(vector<string> str, vector<string>& variables, vector<string>& varia
|
||||
}
|
||||
}
|
||||
|
||||
string PEMDAS(string equ, vector<string>& variables, vector<string>& variableValues)
|
||||
{
|
||||
if (split(equ, ',').size() == 1)
|
||||
return equ;
|
||||
|
||||
int parenthesisSetsCount = countNoOverlap(equ, '(', ')');
|
||||
|
||||
vector<string> equationWords = split(equ, ' ');
|
||||
for (int s = 0; s < parenthesisSetsCount; s++)
|
||||
{
|
||||
int startOfNextParenthesis = 0;
|
||||
int numofParenthesis = 0;
|
||||
vector<string> insideParenthesis;
|
||||
for (int p = startOfNextParenthesis; p < (int)equationWords.size(); p++)
|
||||
{
|
||||
numofParenthesis += count(equationWords[p], '(') - count(equationWords[p], ')');
|
||||
if (numofParenthesis == 0)
|
||||
{
|
||||
startOfNextParenthesis = indexInStr(equationWords[charIndexInVec(equationWords, '(')], '(');
|
||||
break;
|
||||
}
|
||||
insideParenthesis.push_back("");
|
||||
for (int w = 0; w < (int)equationWords[p].size(); w++)
|
||||
{
|
||||
insideParenthesis[(int)insideParenthesis.size() - 1] += equationWords[p][w] + " ";
|
||||
}
|
||||
}
|
||||
|
||||
equ = replace(equ, "(" + unWrapVec(insideParenthesis) + ")", PEMDAS(unWrapVec(insideParenthesis), variables, variableValues));
|
||||
}
|
||||
}
|
||||
|
||||
int ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& variables, vector<string>& variableValues)
|
||||
{
|
||||
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
|
||||
return 0;
|
||||
|
||||
if (words[lineNum][0] == "print") {
|
||||
cout << GetRealValue(words[lineNum][1], variables, variableValues) << endl;
|
||||
return 0;
|
||||
@ -480,7 +258,7 @@ 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(inputs, variables, variableValues);
|
||||
evalEqu(vector<string>(words[lineNum].begin() + 1, words[lineNum].end()), variables, variableValues);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -489,7 +267,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));
|
||||
cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() -1] << endl;
|
||||
cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,13 +140,17 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="eval.cpp" />
|
||||
<ClCompile Include="Main.cpp" />
|
||||
<ClCompile Include="strops.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="eval.h" />
|
||||
<ClInclude Include="olcPixelGameEngine.h" />
|
||||
<ClInclude Include="strops.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="script.sl" />
|
||||
<None Include="script.slg" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@ -18,13 +18,25 @@
|
||||
<ClCompile Include="Main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="eval.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="strops.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="olcPixelGameEngine.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="eval.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="strops.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="script.sl" />
|
||||
<None Include="script.slg" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
152
Slang/eval.cpp
Normal file
152
Slang/eval.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
// CPP program to evaluate a given expression
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <regex>
|
||||
#include <limits>
|
||||
#include <stack>
|
||||
#include "eval.h"
|
||||
using namespace std;
|
||||
|
||||
float precedence(char op) {
|
||||
if (op == '+' || op == '-')
|
||||
return 1;
|
||||
if (op == '*' || op == '/')
|
||||
return 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
float applyOp(float a, float b, char op) {
|
||||
switch (op) {
|
||||
case '+': return a + b;
|
||||
case '-': return a - b;
|
||||
case '*': return a * b;
|
||||
case '/': return a / b;
|
||||
}
|
||||
}
|
||||
|
||||
// Function that returns value of
|
||||
// expression after evaluation.
|
||||
float evaluate(string tokens) {
|
||||
float i;
|
||||
|
||||
stack <float> values;
|
||||
stack <char> ops;
|
||||
|
||||
for (i = 0; i < tokens.length(); i++) {
|
||||
|
||||
// Current token is a whitespace,
|
||||
// skip it.
|
||||
if (tokens[i] == ' ')
|
||||
continue;
|
||||
|
||||
// Current token is an opening
|
||||
// brace, push it to 'ops'
|
||||
else if (tokens[i] == '(')
|
||||
{
|
||||
ops.push(tokens[i]);
|
||||
}
|
||||
|
||||
// Current token is a number, push
|
||||
// it to stack for numbers.
|
||||
else if (isdigit(tokens[i])) {
|
||||
float val = 0;
|
||||
|
||||
bool encounteredDecimal = false;
|
||||
int decPlaces = 0;
|
||||
// There may be more than one
|
||||
// digits in number.
|
||||
while (i < tokens.length() && (isdigit(tokens[i]) || tokens[i] == '.'))
|
||||
{
|
||||
if (tokens[i] == '.')
|
||||
{
|
||||
encounteredDecimal = true;
|
||||
i++;
|
||||
decPlaces++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (encounteredDecimal == true)
|
||||
{
|
||||
val = (val)+(tokens[i] - '0') / pow(10.0f, decPlaces);
|
||||
decPlaces++;
|
||||
}
|
||||
else
|
||||
val = (val * 10) + (tokens[i] - '0');
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
values.push(val);
|
||||
|
||||
i--;
|
||||
}
|
||||
// Closing brace encountered, solve
|
||||
// entire brace.
|
||||
else if (tokens[i] == ')')
|
||||
{
|
||||
while (!ops.empty() && ops.top() != '(')
|
||||
{
|
||||
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 (!ops.empty())
|
||||
ops.pop();
|
||||
}
|
||||
// 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));
|
||||
}
|
||||
|
||||
ops.push(tokens[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Entire expression has been parsed at this
|
||||
// point, apply remaining ops to remaining
|
||||
// values.
|
||||
while (!ops.empty()) {
|
||||
float val2 = values.top();
|
||||
values.pop();
|
||||
|
||||
float val1 = values.top();
|
||||
values.pop();
|
||||
|
||||
char op = ops.top();
|
||||
ops.pop();
|
||||
|
||||
values.push(applyOp(val1, val2, op));
|
||||
}
|
||||
|
||||
return values.top();
|
||||
}
|
||||
|
||||
// Used to test evaluator
|
||||
//int main() {
|
||||
// cout << evaluate("10 + 2 * 6") << "\n";
|
||||
// cout << evaluate("100 * 2 + 12") << "\n";
|
||||
// cout << evaluate("100 * ( 2 + 12 )") << "\n";
|
||||
// cout << evaluate("0.05*0.05");
|
||||
// return 0;
|
||||
//}
|
||||
6
Slang/eval.h
Normal file
6
Slang/eval.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef EVAL_H
|
||||
#define EVAL_H
|
||||
|
||||
float evaluate(std::string tokens);
|
||||
|
||||
#endif
|
||||
@ -1,155 +0,0 @@
|
||||
//#include <iostream>
|
||||
//#include <fstream>
|
||||
//#include <string>
|
||||
//#include <regex>
|
||||
//#include "Main.cpp"
|
||||
//
|
||||
//using namespace std;
|
||||
//
|
||||
//int ProcessLine(vector<vector<string>> words, int l, vector<string> variables, vector<string> variableValues)
|
||||
//{
|
||||
// if (words[l][0] == "print") {
|
||||
// cout << GetRealValue(words[l][1], variables, variableValues) << endl;
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// // Iterate through all functions
|
||||
// for (int t = 0; t < (int)functions.size(); t++)
|
||||
// {
|
||||
// if (words[l][0] == split(functions[t], ' ')[0])
|
||||
// {
|
||||
// ExecuteFunction(words[l][0], rangeInVec(words[l], 1, -1));
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // First iterate through all types to see if line
|
||||
// // is a variable then store it with it's value
|
||||
// for (int t = 0; t < (int)types.size(); t++)
|
||||
// {
|
||||
// if (words[l][0] == types[t])
|
||||
// {
|
||||
// //Checks if it is variable
|
||||
// variables.push_back(words[l][1]);
|
||||
// variableValues.push_back((string)words[l][3]);
|
||||
// //cout << words[l][1] << " is " << words[l][3] << endl << endl;
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
// // Second, iterate all existing local variable names
|
||||
// for (int v = 0; v < (int)variables.size(); v++)
|
||||
// {
|
||||
// if (words[l][0] == variables[v])
|
||||
// {
|
||||
// if (words[l][1] == "=")
|
||||
// variableValues[v] = words[l][2];
|
||||
// else if (words[l][1] == "+=")
|
||||
// variableValues[v] = AddItem(variableValues[v], words[l][2]);
|
||||
// else if (words[l][1] == "-=")
|
||||
// variableValues[v] = to_string(stof(variableValues[v]) - stof(words[l][2]));
|
||||
// else if (words[l][1] == "*=")
|
||||
// variableValues[v] = to_string(stof(variableValues[v]) * stof(words[l][2]));
|
||||
// else if (words[l][1] == "/=")
|
||||
// variableValues[v] = to_string(stof(variableValues[v]) / stof(words[l][2]));
|
||||
//
|
||||
// //cout << words[l][1] << " is " << words[l][3] << endl << endl;
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
// // Third, iterate all existing global variable names
|
||||
// for (int v = 0; v < (int)globalVariables.size(); v++)
|
||||
// {
|
||||
// if (words[l][0] == globalVariables[v])
|
||||
// {
|
||||
// if (words[l][1] == "=")
|
||||
// globalVariableValues[v] = words[l][2];
|
||||
// else if (words[l][1] == "+=")
|
||||
// globalVariableValues[v] = AddItem(globalVariableValues[v], words[l][2]);
|
||||
// else if (words[l][1] == "-=")
|
||||
// globalVariableValues[v] = to_string(stof(globalVariableValues[v]) - stof(words[l][2]));
|
||||
// else if (words[l][1] == "*=")
|
||||
// globalVariableValues[v] = to_string(stof(globalVariableValues[v]) * stof(words[l][2]));
|
||||
// else if (words[l][1] == "/=")
|
||||
// globalVariableValues[v] = to_string(stof(globalVariableValues[v]) / stof(words[l][2]));
|
||||
//
|
||||
// //cout << words[l][1] << " is " << words[l][3] << endl << endl;
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
// // Gathers while loop contents
|
||||
// if (words[l][0] == "while")
|
||||
// {
|
||||
// vector<string> whileContents;
|
||||
// vector<string> whileParameters;
|
||||
//
|
||||
// for (int w = 1; w < (int)words[l].size(); w++)
|
||||
// whileParameters.push_back(words[l][w]);
|
||||
//
|
||||
// int numOfBrackets = 1;
|
||||
// for (int p = l + 2; p < (int)words.size(); p++)
|
||||
// {
|
||||
// numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}");
|
||||
// if (numOfBrackets == 0)
|
||||
// break;
|
||||
// whileContents.push_back("");
|
||||
// for (int w = 0; w < (int)words[p].size(); w++)
|
||||
// {
|
||||
// whileContents[(int)whileContents.size() - 1] += words[p][w] + " ";
|
||||
// }
|
||||
// }
|
||||
// whileContents = removeTabs(whileContents, 1);
|
||||
//
|
||||
// vector<vector<string>> words;
|
||||
// for (int i = 0; i < (int)whileContents.size(); i++)
|
||||
// words.push_back(split(whileContents[i], ' '));
|
||||
//
|
||||
// while (BooleanLogic(whileParameters[0], whileParameters[1], whileParameters[2], variables, variableValues))
|
||||
// {
|
||||
// //Iterate through all lines in while loop
|
||||
// for (int l = 0; l < (int)whileContents.size(); l++)
|
||||
// {
|
||||
// ProcessLine(words, l, variables, variableValues);
|
||||
// }
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
// // Gathers if statement contents
|
||||
// if (words[l][0] == "if")
|
||||
// {
|
||||
// vector<string> ifContents;
|
||||
// vector<string> ifParameters;
|
||||
//
|
||||
// for (int w = 1; w < (int)words[l].size(); w++)
|
||||
// ifParameters.push_back(words[l][w]);
|
||||
//
|
||||
// int numOfBrackets = 1;
|
||||
// for (int p = l + 2; p < (int)words.size(); p++)
|
||||
// {
|
||||
// numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}");
|
||||
// if (numOfBrackets == 0)
|
||||
// break;
|
||||
// ifContents.push_back("");
|
||||
// for (int w = 0; w < (int)words[p].size(); w++)
|
||||
// {
|
||||
// ifContents[(int)ifContents.size() - 1] += words[p][w] + " ";
|
||||
// }
|
||||
// }
|
||||
// ifContents = removeTabs(ifContents, 1);
|
||||
//
|
||||
// vector<vector<string>> words;
|
||||
// for (int i = 0; i < (int)ifContents.size(); i++)
|
||||
// words.push_back(split(ifContents[i], ' '));
|
||||
//
|
||||
// if (BooleanLogic(ifParameters[0], ifParameters[1], ifParameters[2], variables, variableValues))
|
||||
// {
|
||||
// //Iterate through all lines in while loop
|
||||
// for (int l = 0; l < (int)ifContents.size(); l++)
|
||||
// {
|
||||
// ProcessLine(words, l, variables, variableValues);
|
||||
// }
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// return 0;
|
||||
//}
|
||||
@ -25,14 +25,11 @@ void Main(string input, int in2)
|
||||
|
||||
while x < 100
|
||||
{
|
||||
k = a
|
||||
k += b
|
||||
a = b
|
||||
b = k
|
||||
|
||||
k = 10*4/3+(6.2-5.8)
|
||||
print k
|
||||
string str = x
|
||||
|
||||
str += "::"
|
||||
//str += "::"
|
||||
str += k
|
||||
|
||||
x += 10
|
||||
|
||||
220
Slang/strops.cpp
Normal file
220
Slang/strops.cpp
Normal file
@ -0,0 +1,220 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <regex>
|
||||
#include <limits>
|
||||
#include "strops.h"
|
||||
using namespace std;
|
||||
|
||||
const string WHITESPACE = " \n\r\t\f\v";
|
||||
|
||||
string ltrim(const string& s)
|
||||
{
|
||||
size_t start = s.find_first_not_of(WHITESPACE);
|
||||
return (start == string::npos) ? "" : s.substr(start);
|
||||
}
|
||||
|
||||
string rtrim(const string& s)
|
||||
{
|
||||
size_t end = s.find_last_not_of(WHITESPACE);
|
||||
return (end == string::npos) ? "" : s.substr(0, end + 1);
|
||||
}
|
||||
|
||||
string trim(const string& s) {
|
||||
return rtrim(ltrim(s));
|
||||
}
|
||||
|
||||
vector<string> split(string str, char del) {
|
||||
// declaring temp string to store the curr "word" upto del
|
||||
string temp = "";
|
||||
vector<string> splitWords;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
{
|
||||
// If cur char is not del, then append it to the cur "word", otherwise
|
||||
// you have completed the word, print it, and start a new word.
|
||||
if (str[i] != del)
|
||||
{
|
||||
temp += str[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
splitWords.push_back(temp);
|
||||
temp = "";
|
||||
}
|
||||
}
|
||||
splitWords.push_back(temp);
|
||||
|
||||
return splitWords;
|
||||
}
|
||||
|
||||
int count(string str, char ch) {
|
||||
int cnt = 0;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
if (str[i] == ch)
|
||||
cnt++;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int countNoOverlap(string str, char ch1, char ch2) {
|
||||
int cnt = 0;
|
||||
|
||||
bool waitingForClose = false;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
{
|
||||
if (str[i] == ch1)
|
||||
waitingForClose = true;
|
||||
else if (str[i] == ch2 && waitingForClose == true)
|
||||
{
|
||||
cnt++;
|
||||
waitingForClose = false;
|
||||
}
|
||||
}
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int indexInStr(string str, char ch) {
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
if (str[i] == ch)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int charIndexInVec(vector<string> str, char ch) {
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
for (int w = 0; w < (int)str[i].size(); w++)
|
||||
if (str[i][w] == ch)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int countInVector(vector<string> str, string ch) {
|
||||
int cnt = 0;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
if (str[i] == ch)
|
||||
cnt++;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
string Vec2Str(vector<string> str) {
|
||||
string outStr;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
outStr += str[i] + "\n";
|
||||
|
||||
return outStr;
|
||||
}
|
||||
|
||||
vector<string> removeTabs(vector<string> str, int amnt) {
|
||||
vector<string> newStr;
|
||||
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
{
|
||||
newStr.push_back("");
|
||||
|
||||
for (int c = 0; c < (int)str[i].size(); c++)
|
||||
{
|
||||
if (str[i][c] != '\t' || c >= amnt)
|
||||
newStr[i] += str[i][c];
|
||||
}
|
||||
}
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
vector<string> rangeInVec(vector<string> str, int min, int max) {
|
||||
if (max == -1)
|
||||
max = (int)str.size();
|
||||
|
||||
vector<string> newStr;
|
||||
|
||||
for (int i = min; i < (int)str.size() && i < max; i++)
|
||||
newStr.push_back(str[i]);
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
string rangeInStr(string str, int min, int max) {
|
||||
if (max == -1)
|
||||
max = (int)str.size();
|
||||
|
||||
string newStr;
|
||||
|
||||
for (int i = min; i < (int)str.size() && i < max; i++)
|
||||
newStr += str[i];
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
string unWrapVec(vector<string> vec) {
|
||||
string newStr;
|
||||
|
||||
for (int i = 0; i < (int)vec.size(); i++)
|
||||
{
|
||||
newStr += vec[i];
|
||||
if (i != (int)vec.size() - 1)
|
||||
newStr += " ";
|
||||
}
|
||||
|
||||
return newStr;
|
||||
}
|
||||
|
||||
float floatval(string s)
|
||||
{
|
||||
float outfloat;
|
||||
|
||||
if (s == "inf")
|
||||
outfloat = numeric_limits<float>::max();
|
||||
else if (s == "-inf")
|
||||
outfloat = -numeric_limits<float>::max();
|
||||
else if (s == "")
|
||||
outfloat = 0;
|
||||
else
|
||||
outfloat = stof(s);
|
||||
|
||||
return outfloat;
|
||||
}
|
||||
|
||||
string replace(string str, string strToReplace, string replaceWith) {
|
||||
string newStr;
|
||||
string savedLetters;;
|
||||
|
||||
int sameLetters = 0;
|
||||
int startReplaceIndex = 0;
|
||||
for (int i = 0; i < (int)str.size(); i++)
|
||||
{
|
||||
if (str[i] == strToReplace[sameLetters])
|
||||
{
|
||||
savedLetters += str[i];
|
||||
if (sameLetters == 0)
|
||||
startReplaceIndex = i;
|
||||
sameLetters++;
|
||||
|
||||
if ((int)strToReplace.size() == sameLetters)
|
||||
{
|
||||
//cout << "replaced " << "\"" << strToReplace << "\"" << startReplaceIndex << endl;
|
||||
newStr += replaceWith;
|
||||
sameLetters = 0;
|
||||
savedLetters = "";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
newStr += savedLetters + str[i];
|
||||
sameLetters = 0;
|
||||
savedLetters = "";
|
||||
}
|
||||
}
|
||||
|
||||
return newStr;
|
||||
}
|
||||
38
Slang/strops.h
Normal file
38
Slang/strops.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef STROPS_H
|
||||
#define STROPS_H
|
||||
|
||||
using namespace std;
|
||||
|
||||
string ltrim(const string& s);
|
||||
|
||||
string rtrim(const string& s);
|
||||
|
||||
string trim(const string& s);
|
||||
|
||||
vector<string> split(string str, char del);
|
||||
|
||||
int count(string str, char ch);
|
||||
|
||||
int countNoOverlap(string str, char ch1, char ch2);
|
||||
|
||||
int indexInStr(string str, char ch);
|
||||
|
||||
int charIndexInVec(vector<string> str, char ch);
|
||||
|
||||
int countInVector(vector<string> str, string ch);
|
||||
|
||||
string Vec2Str(vector<string> str);
|
||||
|
||||
vector<string> removeTabs(vector<string> str, int amnt);
|
||||
|
||||
vector<string> rangeInVec(vector<string> str, int min, int max);
|
||||
|
||||
string rangeInStr(string str, int min, int max);
|
||||
|
||||
string unWrapVec(vector<string> vec);
|
||||
|
||||
float floatval(string s);
|
||||
|
||||
string replace(string str, string strToReplace, string replaceWith);
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user