diff --git a/Slang/Main.cpp b/Slang/Main.cpp index c30600d..9753657 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -1,4 +1,4 @@ -#define OLC_PGE_APPLICATION + #include "olcPixelGameEngine.h" #include #include @@ -9,6 +9,7 @@ #include "eval.h" #include "strops.h" #include "builtin.h" +#include "main.h" using namespace std; @@ -17,33 +18,6 @@ vector globalVariableValues; vector functions; vector> functionValues; -class Parser : public olc::PixelGameEngine -{ -public: - Parser() - { - sAppName = "Parser"; - } - -public: - bool OnUserCreate() override - { - // Called once at the start - return true; - } - - bool OnUserUpdate(float fElapsedTime) override - { - // Called once per frame - for (int x = 0; x < ScreenWidth(); x++) - for (int y = 0; y < ScreenHeight(); y++) - Draw(x, y, olc::Pixel(rand() % 128, rand() % 128, rand() % 128)); - return true; - } -}; - -string ExecuteFunction(string functionName, vector inputVarVals); - bool isNumber(const string& str) { for (char const& c : str) { @@ -52,18 +26,10 @@ bool isNumber(const string& str) return true; } -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 StringRaw(string str) { + str = trim(str); + if (str.size() < 3) return str; @@ -83,6 +49,8 @@ string StringRaw(string str) string Quoted(string str) { + str = trim(str); + string withQuotes; if (str[0] != '\"') @@ -118,20 +86,23 @@ string GetVariableValue(string varName, vector& variables, vector VarValues(vector varNames, vector& variables, vec for (int varIndex = 0; varIndex < varNames.size(); varIndex++) { + varNames[varIndex] = trim(varNames[varIndex]); + string typ = "string"; bool isVar = false; - // Checks against global vars - for (int v = 0; v < (int)globalVariables.size(); v++) - if (varNames[varIndex] == split(globalVariables[v], ' ')[1]) - { - typ = split(globalVariables[v], ' ')[0]; - isVar = true; - } - // Checks against local vars - for (int v = 0; v < (int)variables.size(); v++) - if (varNames[varIndex] == split(variables[v], ' ')[1]) - { - typ = split(variables[v], ' ')[0]; - isVar = true; - } + if (count(varNames[varIndex], '\"') == 0) + { + // Checks against global vars + for (int v = 0; v < (int)globalVariables.size(); v++) + if (varNames[varIndex] == split(globalVariables[v], ' ')[1]) + { + typ = split(globalVariables[v], ' ')[0]; + isVar = true; + } + // Checks against local vars + for (int v = 0; v < (int)variables.size(); v++) + if (varNames[varIndex] == split(variables[v], ' ')[1]) + { + typ = split(variables[v], ' ')[0]; + isVar = true; + } + } // If it is a var if (isVar) @@ -213,12 +189,12 @@ bool IsFunction(string funcName) string EvalExpression(string expression, vector& variables, vector& variableVals) { expression = trim(expression); - //cout << "EXPRESSION: " << expression << endl; + bool inQuotes = false; // If no operations are applied, then return self if ((count(expression, '+') == 0 && count(expression, '-') == 0 && count(expression, '*') == 0 && count(expression, '/') == 0 && count(expression, '(') == 0 && count(expression, '^') == 0) || split(expression, '.')[0] == "CPP") { - if (IsFunction(split(expression, '(')[0])) + if (IsFunction(split(expression, '(')[0]) && !inQuotes) { //cout << split(expression, '(')[0] << endl; string argContents = ""; @@ -233,7 +209,7 @@ string EvalExpression(string expression, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variableVals) { - string valARealValue = GetVariableValue(valA, variables, variableVals); - string valBRealValue = GetVariableValue(valB, variables, variableVals); + string valARealValue = EvalExpression(valA, variables, variableVals); + string valBRealValue = EvalExpression(valB, variables, variableVals); if (determinant == "==") return valARealValue == valBRealValue; @@ -416,10 +399,16 @@ string ProcessLine(vector> words, int lineNum, vector& va } if (words[lineNum][0] == "return") { - //cout << StringRaw(EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues)) << endl; + //cout << StringRaw(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end()))) << endl; return EvalExpression(unWrapVec(vector(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues); } + if (split(words[lineNum][0], '.')[0] == "CPP") + { + string output = EvalExpression(unWrapVec(words[lineNum]), variables, variableValues); + return output; + } + // Iterate through all functions for (int t = 0; t < (int)functions.size(); t++) { @@ -487,16 +476,18 @@ string ProcessLine(vector> words, int lineNum, vector& va } whileContents = removeTabs(whileContents, 1); - vector> words; + vector> innerWords; for (int i = 0; i < (int)whileContents.size(); i++) - words.push_back(split(whileContents[i], ' ')); + innerWords.push_back(split(whileContents[i], ' ')); while (BooleanLogic(whileParameters[0], whileParameters[1], whileParameters[2], variables, variableValues)) { //Iterate through all lines in while loop for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) { - ProcessLine(words, lineNum, variables, variableValues); + string returnVal = ProcessLine(innerWords, lineNum, variables, variableValues); + if (returnVal != "") + return returnVal; } } return ""; @@ -511,33 +502,75 @@ string ProcessLine(vector> words, int lineNum, vector& va ifParameters.push_back(words[lineNum][w]); int numOfBrackets = 1; - for (int p = lineNum + 2; p < (int)words.size(); p++) + lineNum += 2; + while (lineNum < (int)words.size()) { - numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); + numOfBrackets += countInVector(words[lineNum], "{") - countInVector(words[lineNum], "}"); if (numOfBrackets == 0) break; ifContents.push_back(""); - for (int w = 0; w < (int)words[p].size(); w++) + for (int w = 0; w < (int)words[lineNum].size(); w++) { - ifContents[(int)ifContents.size() - 1] += words[p][w] + " "; + ifContents[(int)ifContents.size() - 1] += words[lineNum][w] + " "; } + lineNum++; } ifContents = removeTabs(ifContents, 1); - vector> words; + vector> innerWords; for (int i = 0; i < (int)ifContents.size(); i++) - words.push_back(split(ifContents[i], ' ')); + innerWords.push_back(split(ifContents[i], ' ')); if (BooleanLogic(ifParameters[0], ifParameters[1], ifParameters[2], variables, variableValues)) { - //Iterate through all lines in while loop - for (int lineNum = 0; lineNum < (int)ifContents.size(); lineNum++) + //Iterate through all lines in if statement + for (int l = 0; l < (int)ifContents.size(); l++) { - ProcessLine(words, lineNum, variables, variableValues); + string returnVal = ProcessLine(innerWords, l, variables, variableValues); + if (returnVal != "") + return returnVal; } } + else if (words.size() > lineNum + 1) + if (words[lineNum + 1][0] == "else") + { + lineNum += 1; + + vector elseContents; + + int numOfBrackets = 1; + while (lineNum < (int)words.size()) + { + numOfBrackets += countInVector(words[lineNum], "{") - countInVector(words[lineNum], "}"); + if (numOfBrackets == 0) + break; + elseContents.push_back(""); + for (int w = 0; w < (int)words[lineNum].size(); w++) + { + elseContents[(int)elseContents.size() - 1] += words[lineNum][w] + " "; + } + lineNum++; + } + elseContents = removeTabs(elseContents, 2); + + vector> innerWords; + for (int i = 0; i < (int)elseContents.size(); i++) + words.push_back(split(elseContents[i], ' ')); + + //Iterate through all lines in else statement + for (int lineNum = 0; lineNum < (int)elseContents.size(); lineNum++) + { + ProcessLine(innerWords, lineNum, variables, variableValues); + } + return ""; + } return ""; } + //// Gathers else statement contents + //if (words[lineNum][0] == "else") + //{ + // + //} return ""; } @@ -560,7 +593,7 @@ string ExecuteFunction(string functionName, vector inputVarVals) vector variables; vector variableValues; vector functionNameParts = split(replace(functions[functionIndex], functionName + " ", ""), ','); - for (int i = 0; i < (int)functionNameParts.size(); i++) + for (int i = 0; i < (int)inputVarVals.size(); i++) { variables.push_back(trim(functionNameParts[i])); variableValues.push_back(EvalExpression(inputVarVals[i], variables, variableValues)); @@ -576,7 +609,7 @@ string ExecuteFunction(string functionName, vector inputVarVals) string returnVal = ""; try { - string returnVal = ProcessLine(words, lineNum, variables, variableValues); + returnVal = ProcessLine(words, lineNum, variables, variableValues); } catch (const std::exception&) { @@ -677,18 +710,5 @@ int main(int argc, char* argv[]) } parseSlang(scriptString.str()); - /*if (argc >= 2) - { - cout << argv[1]; - - Parser window1; - if (window1.Construct(128, 128, 2, 2)) - window1.Start(); - return 0; - } - else - { - return 1; - }*/ return 0; -} +} \ No newline at end of file diff --git a/Slang/Slang.vcxproj b/Slang/Slang.vcxproj index 2eac382..23400e3 100644 --- a/Slang/Slang.vcxproj +++ b/Slang/Slang.vcxproj @@ -147,6 +147,8 @@ + + diff --git a/Slang/Slang.vcxproj.filters b/Slang/Slang.vcxproj.filters index 04c9c74..6b7d44e 100644 --- a/Slang/Slang.vcxproj.filters +++ b/Slang/Slang.vcxproj.filters @@ -38,6 +38,12 @@ Source Files + + Source Files + + + Source Files + diff --git a/Slang/builtin.h b/Slang/builtin.h index b56a2bf..06bcab3 100644 --- a/Slang/builtin.h +++ b/Slang/builtin.h @@ -8,6 +8,7 @@ #include #include #include "strops.h" +#include "graphics.h" using namespace std; @@ -18,6 +19,8 @@ vector> builtinFunctionValues; vector builtinVars; vector builtinVarVals; +Parser mainWindow; + int GetBuiltins(string script) { script = replace(script, " ", "\t"); @@ -82,9 +85,22 @@ int GetBuiltins(string script) string CPPFunction(string name, vector args) { - cout << name << " -- " << args[0] << endl; if (name == "CPP.Math.Sin") return to_string(sin(stof(args[0]))); + else if (name == "CPP.Math.Cos") + return to_string(cos(stof(args[0]))); + else if (name == "CPP.Math.Tan") + return to_string(tan(stof(args[0]))); + else if (name == "CPP.Math.Round") + return to_string(round(stof(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]))) + 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]))); return ""; } diff --git a/Slang/builtin.slg b/Slang/builtin.slg index 0c9078e..1552e2c 100644 --- a/Slang/builtin.slg +++ b/Slang/builtin.slg @@ -1,7 +1,58 @@ -float pi = 3.14159265358979 +float PI = 3.14159265358979 +float EulersNumber = 2.71828183 float Sin(float input) { float out = CPP.Math.Sin(input) return out +} + +float Cos(float input) +{ + float out = CPP.Math.Cos(input) + return out +} + +float Tan(float input) +{ + float out = CPP.Math.Tan(input) + return out +} + +float Sigmoid(float input) +{ + float out = 1 / (1+EulersNumber^-input) + return out +} + +float Tanh(float input) +{ + float out = ((EulersNumber^input)-(EulersNumber^-input))/((EulersNumber^input)+(EulersNumber^-input)) + return out +} + +float Round(float input) +{ + float out = CPP.Math.Round(input) + return out +} + +float Clamp(float input, float min, float max) +{ + if input < min + { + return min + } + if input > max + { + return max + } + return input +} + +// Sets color of pixel to RGB value +float SetPixel(int x, int y, int r, int g, int b) +{ + string out = CPP.Graphics.SetPixel(x, y, r, g, b) + return out } \ No newline at end of file diff --git a/Slang/graphics.h b/Slang/graphics.h new file mode 100644 index 0000000..74b796d --- /dev/null +++ b/Slang/graphics.h @@ -0,0 +1,47 @@ + +#ifndef GRAPHICS_H +#define GRAPHICS_H + +#define OLC_PGE_APPLICATION + +#include "olcPixelGameEngine.h" +#include +#include +#include +#include +#include +#include +#include "strops.h" +#include "builtin.h" +#include "main.h" + +using namespace std; + +class Parser : public olc::PixelGameEngine +{ +public: + Parser() + { + sAppName = "Parser"; + } + +public: + bool OnUserCreate() override + { + // Called once at the start + return true; + } + + bool OnUserUpdate(float fElapsedTime) override + { + ExecuteFunction("Update", vector {""}); + + // Called once per frame + //for (int x = 0; x < ScreenWidth(); x++) + // for (int y = 0; y < ScreenHeight(); y++) + // Draw(x, y, olc::Pixel(rand() % 128, rand() % 128, rand() % 128)); + return true; + } +}; + +#endif \ No newline at end of file diff --git a/Slang/main.h b/Slang/main.h new file mode 100644 index 0000000..78a8caf --- /dev/null +++ b/Slang/main.h @@ -0,0 +1,9 @@ + +#ifndef MAIN_H +#define MAIN_H + +using namespace std; + +string ExecuteFunction(string functionName, vector inputVarVals); + +#endif \ No newline at end of file diff --git a/Slang/script.slg b/Slang/script.slg index 9373f28..8c922ed 100644 --- a/Slang/script.slg +++ b/Slang/script.slg @@ -1,37 +1,27 @@ -void Next(string in, string sin) -{ - string green = "bean" - green += " " - green += in - - print in - print sin - print green - return "awe" -} void Main(string input, int in) { - print pi + print "PI is: " + PI int x = 1 - - int a = 0 - int b = 1 - - print input - print in - int k = 0 + float k = 0 - while x < 100 + while x < 5 { - int k = 1 / (1+2.71828^-x) - print k float s = Sin(x) - print "Sin is " + s + int k = Sigmoid(s) + print k - x += 10 + x += 1 } - print Next("seen", "bop") + + CPP.Graphics.Init(64, 64, 4) +} + +void Update(string x, string y) +{ + SetPixel(x, y, 255, 0, 0) + y += 1 + // print "updating" } \ No newline at end of file