From 44b2e40e94d0e1bbf88bc774ba37999824e930ba Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 26 May 2022 21:01:44 -0400 Subject: [PATCH] (broken) Allow for any amount of whitespace --- ZSharp/Main.cpp | 68 +++++++++----- ZSharp/builtin.h | 172 +++++++++++++++++----------------- ZSharp/strops.cpp | 3 +- examples/Platformer/script.zs | 9 +- 4 files changed, 137 insertions(+), 115 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index cad36cf..7a211ac 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -2,7 +2,8 @@ #include #include #include -#define DEVELOPER_MESSAGES false +//bool DEVELOPER_MESSAGES = true; +#define DEVELOPER_MESSAGES true #define EXAMPLE_PROJECT false #define NAMEVERSION "ZSharp v2.1.0-alpha" @@ -430,7 +431,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde // Check if it is a SplitThread call else if (startsWith(words.at(lineNum).at(0), "SplitThread")) { - vector lineContents = removeTabs(words.at(lineNum), 10); + vector lineContents = words.at(lineNum); cout << "New Thread: " << words.at(lineNum).at(0) << endl; //lineContents.at(0) = betweenChars(lineContents.at(0), '(', ')'); @@ -521,7 +522,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde lineNum++; } - whileContents = removeTabsWdArry(whileContents, 1); + //whileContents = removeTabsWdArry(whileContents, 1); // Loop while true while (BooleanLogic(whileParameters.at(0), whileParameters.at(1), whileParameters.at(2), variableValues)) @@ -583,7 +584,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde ifContents.push_back(words.at(lineNum)); lineNum++; } - ifContents = removeTabsWdArry(ifContents, 1); + //ifContents = removeTabsWdArry(ifContents, 1); // Execute if true if (BooleanLogic(ifParameters.at(0), ifParameters.at(1), ifParameters.at(2), variableValues)) @@ -624,7 +625,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde lineNum++; } - elseContents = removeTabsWdArry(elseContents, 1); + //elseContents = removeTabsWdArry(elseContents, 1); //Iterate through all lines in else statement for (int l = 0; l < (int)elseContents.size(); l++) @@ -682,11 +683,12 @@ int parseZSharp(string script) //script = replace(script, " ", "\t"); // Replace spaces with tabs (not really required, and will break purposefull whitespace in strings etc.) // Split the script by newline, signifying a line ending - vector lines = split(script, '\n'); - for (int i = 0; i < (int)lines.size(); i++){ // Then split said lines into indiviual words - if((lines.at(i)[0] == "/" && lines.at(i)[1] == "/") || trim(lines.at(i)) == "") - { // Remove line if it is a comment or if it is blank - lines.erase(lines.begin() + i); + vector beforeProcessLines = split(script, '\n'); + vector lines; + for (int i = 0; i < (int)beforeProcessLines.size(); i++){ // Then split said lines into indiviual words + if(!startsWith(trim(beforeProcessLines.at(i)), "//") && trim(beforeProcessLines.at(i)) != "") + { // dont include line if it is a comment or if it is blank + lines.push_back(trim(beforeProcessLines.at(i))); } } #if DEVELOPER_MESSAGES @@ -697,7 +699,7 @@ int parseZSharp(string script) { words.push_back(split(lines.at(i), ' ')); #if DEVELOPER_MESSAGES - cout << lines.at(i); + cout << unWrapVec(words.at(i)) << endl; #endif } @@ -718,18 +720,34 @@ int parseZSharp(string script) InterpreterLog("Load script function " + functName + "..."); #endif - string args = ""; - if (indexInStr(unWrapVec(words.at(lineNum)), ')') - indexInStr(unWrapVec(words.at(lineNum)), '(') > 1) - for (int w = 1; w < (int)words.at(lineNum).size(); w++) // Get all words from the instantiation line: these are the args - { - if (count(words.at(lineNum).at(w), '{') == 0) - args += replace(replace(words.at(lineNum).at(w), "(", " "), ")", ""); - } + //string args = ""; + //if (indexInStr(unWrapVec(words.at(lineNum)), ')') - indexInStr(unWrapVec(words.at(lineNum)), '(') > 1) + // for (int w = 0; w < (int)words.at(lineNum).size(); w++) // Get all words from the instantiation line: these are the args + // { + // if (count(words.at(lineNum).at(w), '{') == 0) + // args += replace(replace(words.at(lineNum).at(w), "(", " "), ")", ""); + // } + string args = betweenChars(unWrapVec(words.at(lineNum)), '(', ')'); + cout << functName << "<" << args << ">" << endl; - args = trim(replace(args, functName + " ", "")); + //args = trim(replace(args, functName, "")); functionContents.push_back(split(args, ',')); - int numOfBrackets = 0; + + int numOfBrackets = countInVector(words.at(lineNum), "{") - countInVector(words.at(lineNum), "}"); + // Gather the contents + lineNum++; + while (lineNum < (int)words.size()) + { + numOfBrackets += countInVector(words.at(lineNum), "{") - countInVector(words.at(lineNum), "}"); + if (numOfBrackets == 0) + break; + functionContents.push_back(words.at(lineNum)); + lineNum++; + } + //functionContents = removeTabsWdArry(functionContents, 1); + + /*int numOfBrackets = 0; for (int w = 1; w < (int)words.at(lineNum).size(); w++) { if (count(words.at(lineNum).at(w), '{') != 0) { numOfBrackets = 1; @@ -743,7 +761,7 @@ int parseZSharp(string script) if (numOfBrackets == 0) break; functionContents.push_back(removeTabs(words.at(p), 1)); - } + }*/ functionValues[functName] = functionContents; } else @@ -817,9 +835,9 @@ int main(int argc, char* argv[]) cout << endl << endl; // Gathers builtin functions and variables - GetBuiltins(ZSContents); - functionValues = builtinFunctionValues; - globalVariableValues = builtinVarVals; + parseZSharp(ZSContents); + //functionValues = builtinFunctionValues; + //globalVariableValues = builtinVarVals; std::string scriptTextContents; @@ -828,7 +846,7 @@ int main(int argc, char* argv[]) { std::string scriptPath; if (EXAMPLE_PROJECT) - scriptPath = "D:\\Code\\Z-Sharp\\Releases\\ZS-Win-x64-Base\\Pong-Example-Project\\script.zs"; + scriptPath = "D:\\Code\\Z-Sharp\\examples\\Platformer\\script.zs"; else scriptPath = argv[1]; #if DEVELOPER_MESSAGES diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index 70fdcb7..3410413 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -28,8 +28,8 @@ using namespace boost; vector types = { "int", "float", "string", "bool", "void", "null", "Sprite", "Vec2", "Text" }; -unordered_map>> builtinFunctionValues; -unordered_map builtinVarVals; +//unordered_map>> builtinFunctionValues; +//unordered_map builtinVarVals; // Foreground colors const std::string blackFGColor = "\x1B[30m"; @@ -276,90 +276,90 @@ bool AxisAlignedCollision(const Sprite& a, const Sprite& b) // AABB - AABB colli return collisionX && collisionY; } -// Initial script processing, which loads variables and functions from builtin -int GetBuiltins(std::string s) -{ - std::string script = replace(s, " ", "\t"); - - vector lines = split(script, '\n'); - vector> words; - for (int i = 0; i < (int)lines.size(); i++) - { - words.push_back(split(lines.at(i), ' ')); - } - - // Go through entire script and iterate through all types to see if line is a - // function declaration, then store it with it's value - for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) - { - //Checks if it is function - if (words.at(lineNum).at(0) == "func") - { - vector> functionContents; - - string functName = split(words.at(lineNum).at(1), '(')[0]; - -#if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin function " + functName + "..."); -#endif - - string args = ""; - for (int w = 1; w < (int)words.at(lineNum).size(); w++) // Get all words from the instantiation line: these are the args - { - args += replace(replace(words.at(lineNum).at(w), "(", " "), ")", ""); - } - - args = replace(args, functName + " ", ""); - functionContents.push_back(split(args, ',')); - - int numOfBrackets = 1; - for (int p = lineNum + 2; p < (int)words.size(); p++) - { - numOfBrackets += countInVector(words.at(p), "{") - countInVector(words.at(p), "}"); - if (numOfBrackets == 0) - break; - functionContents.push_back(removeTabs(words.at(p), 1)); - } - builtinFunctionValues[functName] = functionContents; - //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; - } - else - { - if (words.at(lineNum).at(0) == "string") - { - builtinVarVals[words.at(lineNum).at(1)] = StringRaw(words.at(lineNum).at(3)); -#if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); -#endif - } - else if (words.at(lineNum).at(0) == "int") - { - builtinVarVals[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3)); -#if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); -#endif - } - else if (words.at(lineNum).at(0) == "float") - { - builtinVarVals[words.at(lineNum).at(1)] = stof(words.at(lineNum).at(3)); -#if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); -#endif - } - else if (words.at(lineNum).at(0) == "bool") - { - builtinVarVals[words.at(lineNum).at(1)] = stob(words.at(lineNum).at(3)); -#if DEVELOPER_MESSAGES == true - InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); -#endif - } - //else - // LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); - } - } - - return 0; -} +//// Initial script processing, which loads variables and functions from builtin +//int GetBuiltins(std::string s) +//{ +// std::string script = replace(s, " ", "\t"); +// +// vector lines = split(script, '\n'); +// vector> words; +// for (int i = 0; i < (int)lines.size(); i++) +// { +// words.push_back(split(lines.at(i), ' ')); +// } +// +// // Go through entire script and iterate through all types to see if line is a +// // function declaration, then store it with it's value +// for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) +// { +// //Checks if it is function +// if (words.at(lineNum).at(0) == "func") +// { +// vector> functionContents; +// +// string functName = split(words.at(lineNum).at(1), '(')[0]; +// +//#if DEVELOPER_MESSAGES == true +// InterpreterLog("Load builtin function " + functName + "..."); +//#endif +// +// string args = ""; +// for (int w = 1; w < (int)words.at(lineNum).size(); w++) // Get all words from the instantiation line: these are the args +// { +// args += replace(replace(words.at(lineNum).at(w), "(", " "), ")", ""); +// } +// +// args = replace(args, functName + " ", ""); +// functionContents.push_back(split(args, ',')); +// +// int numOfBrackets = 1; +// for (int p = lineNum + 2; p < (int)words.size(); p++) +// { +// numOfBrackets += countInVector(words.at(p), "{") - countInVector(words.at(p), "}"); +// if (numOfBrackets == 0) +// break; +// functionContents.push_back(removeTabs(words.at(p), 1)); +// } +// builtinFunctionValues[functName] = functionContents; +// //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; +// } +// else +// { +// if (words.at(lineNum).at(0) == "string") +// { +// builtinVarVals[words.at(lineNum).at(1)] = StringRaw(words.at(lineNum).at(3)); +//#if DEVELOPER_MESSAGES == true +// InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); +//#endif +// } +// else if (words.at(lineNum).at(0) == "int") +// { +// builtinVarVals[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3)); +//#if DEVELOPER_MESSAGES == true +// InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); +//#endif +// } +// else if (words.at(lineNum).at(0) == "float") +// { +// builtinVarVals[words.at(lineNum).at(1)] = stof(words.at(lineNum).at(3)); +//#if DEVELOPER_MESSAGES == true +// InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); +//#endif +// } +// else if (words.at(lineNum).at(0) == "bool") +// { +// builtinVarVals[words.at(lineNum).at(1)] = stob(words.at(lineNum).at(3)); +//#if DEVELOPER_MESSAGES == true +// InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); +//#endif +// } +// //else +// // LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); +// } +// } +// +// return 0; +//} // Executes boost::any ZSFunction(const string& name, const vector& args) diff --git a/ZSharp/strops.cpp b/ZSharp/strops.cpp index 4f3cdcc..01608a7 100644 --- a/ZSharp/strops.cpp +++ b/ZSharp/strops.cpp @@ -8,7 +8,8 @@ //#include "builtin.h" using namespace std; -const string WHITESPACE = " \n\r\t\f\v"; +const string WHITESPACE = " \t\f"; +//const string WHITESPACE = " \n\r\t\f\v"; bool isNumber(const string& str) diff --git a/examples/Platformer/script.zs b/examples/Platformer/script.zs index 57139b9..9aa9b66 100644 --- a/examples/Platformer/script.zs +++ b/examples/Platformer/script.zs @@ -83,15 +83,18 @@ func Update(deltaTime) // Apply gravity g_colliding = Colliding(g_playerSprite, g_groundSprite) + print g_colliding if g_colliding == false { + print g_jumping if g_jumping == false { g_playerTargetPosition.y += deltaTime * g_gravitySpeed + print "grav" } if g_jumping == true { - g_playerTargetPosition.y -= (g_jumpHeight*deltaTime) + (deltaTime*g_gravitySpeed*-g_jumpingTime*5) + g_playerTargetPosition.y -= (g_jumpHeight * deltaTime) + (deltaTime * g_gravitySpeed * -g_jumpingTime * 5) print g_jumpingTime } } @@ -128,6 +131,6 @@ func Update(deltaTime) func Colliding(a, b) { - bool b = ZS.Physics.AxisAlignedCollision(a, b) - return b + bool bo = ZS.Physics.AxisAlignedCollision(a, b) + return bo }