(broken) Allow for any amount of whitespace

This commit is contained in:
sam-astro 2022-05-26 21:01:44 -04:00
parent f0321d9927
commit 44b2e40e94
4 changed files with 137 additions and 115 deletions

View File

@ -2,7 +2,8 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
#define DEVELOPER_MESSAGES false //bool DEVELOPER_MESSAGES = true;
#define DEVELOPER_MESSAGES true
#define EXAMPLE_PROJECT false #define EXAMPLE_PROJECT false
#define NAMEVERSION "ZSharp v2.1.0-alpha" #define NAMEVERSION "ZSharp v2.1.0-alpha"
@ -430,7 +431,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
// Check if it is a SplitThread call // Check if it is a SplitThread call
else if (startsWith(words.at(lineNum).at(0), "SplitThread")) else if (startsWith(words.at(lineNum).at(0), "SplitThread"))
{ {
vector<string> lineContents = removeTabs(words.at(lineNum), 10); vector<string> lineContents = words.at(lineNum);
cout << "New Thread: " << words.at(lineNum).at(0) << endl; cout << "New Thread: " << words.at(lineNum).at(0) << endl;
//lineContents.at(0) = betweenChars(lineContents.at(0), '(', ')'); //lineContents.at(0) = betweenChars(lineContents.at(0), '(', ')');
@ -521,7 +522,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
lineNum++; lineNum++;
} }
whileContents = removeTabsWdArry(whileContents, 1); //whileContents = removeTabsWdArry(whileContents, 1);
// Loop while true // Loop while true
while (BooleanLogic(whileParameters.at(0), whileParameters.at(1), whileParameters.at(2), variableValues)) while (BooleanLogic(whileParameters.at(0), whileParameters.at(1), whileParameters.at(2), variableValues))
@ -583,7 +584,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
ifContents.push_back(words.at(lineNum)); ifContents.push_back(words.at(lineNum));
lineNum++; lineNum++;
} }
ifContents = removeTabsWdArry(ifContents, 1); //ifContents = removeTabsWdArry(ifContents, 1);
// Execute if true // Execute if true
if (BooleanLogic(ifParameters.at(0), ifParameters.at(1), ifParameters.at(2), variableValues)) if (BooleanLogic(ifParameters.at(0), ifParameters.at(1), ifParameters.at(2), variableValues))
@ -624,7 +625,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
lineNum++; lineNum++;
} }
elseContents = removeTabsWdArry(elseContents, 1); //elseContents = removeTabsWdArry(elseContents, 1);
//Iterate through all lines in else statement //Iterate through all lines in else statement
for (int l = 0; l < (int)elseContents.size(); l++) 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.) //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 // Split the script by newline, signifying a line ending
vector<string> lines = split(script, '\n'); vector<string> beforeProcessLines = split(script, '\n');
for (int i = 0; i < (int)lines.size(); i++){ // Then split said lines into indiviual words vector<string> lines;
if((lines.at(i)[0] == "/" && lines.at(i)[1] == "/") || trim(lines.at(i)) == "") for (int i = 0; i < (int)beforeProcessLines.size(); i++){ // Then split said lines into indiviual words
{ // Remove line if it is a comment or if it is blank if(!startsWith(trim(beforeProcessLines.at(i)), "//") && trim(beforeProcessLines.at(i)) != "")
lines.erase(lines.begin() + i); { // dont include line if it is a comment or if it is blank
lines.push_back(trim(beforeProcessLines.at(i)));
} }
} }
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
@ -697,7 +699,7 @@ int parseZSharp(string script)
{ {
words.push_back(split(lines.at(i), ' ')); words.push_back(split(lines.at(i), ' '));
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES
cout << lines.at(i); cout << unWrapVec(words.at(i)) << endl;
#endif #endif
} }
@ -718,18 +720,34 @@ int parseZSharp(string script)
InterpreterLog("Load script function " + functName + "..."); InterpreterLog("Load script function " + functName + "...");
#endif #endif
string args = ""; //string args = "";
if (indexInStr(unWrapVec(words.at(lineNum)), ')') - indexInStr(unWrapVec(words.at(lineNum)), '(') > 1) //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 // 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) // if (count(words.at(lineNum).at(w), '{') == 0)
args += replace(replace(words.at(lineNum).at(w), "(", " "), ")", ""); // 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, ',')); 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++) { for (int w = 1; w < (int)words.at(lineNum).size(); w++) {
if (count(words.at(lineNum).at(w), '{') != 0) { if (count(words.at(lineNum).at(w), '{') != 0) {
numOfBrackets = 1; numOfBrackets = 1;
@ -743,7 +761,7 @@ int parseZSharp(string script)
if (numOfBrackets == 0) if (numOfBrackets == 0)
break; break;
functionContents.push_back(removeTabs(words.at(p), 1)); functionContents.push_back(removeTabs(words.at(p), 1));
} }*/
functionValues[functName] = functionContents; functionValues[functName] = functionContents;
} }
else else
@ -817,9 +835,9 @@ int main(int argc, char* argv[])
cout << endl << endl; cout << endl << endl;
// Gathers builtin functions and variables // Gathers builtin functions and variables
GetBuiltins(ZSContents); parseZSharp(ZSContents);
functionValues = builtinFunctionValues; //functionValues = builtinFunctionValues;
globalVariableValues = builtinVarVals; //globalVariableValues = builtinVarVals;
std::string scriptTextContents; std::string scriptTextContents;
@ -828,7 +846,7 @@ int main(int argc, char* argv[])
{ {
std::string scriptPath; std::string scriptPath;
if (EXAMPLE_PROJECT) 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 else
scriptPath = argv[1]; scriptPath = argv[1];
#if DEVELOPER_MESSAGES #if DEVELOPER_MESSAGES

View File

@ -28,8 +28,8 @@ using namespace boost;
vector<string> types = { "int", "float", "string", "bool", "void", "null", "Sprite", "Vec2", "Text" }; vector<string> types = { "int", "float", "string", "bool", "void", "null", "Sprite", "Vec2", "Text" };
unordered_map<string, vector<vector<string>>> builtinFunctionValues; //unordered_map<string, vector<vector<string>>> builtinFunctionValues;
unordered_map<string, boost::any> builtinVarVals; //unordered_map<string, boost::any> builtinVarVals;
// Foreground colors // Foreground colors
const std::string blackFGColor = "\x1B[30m"; const std::string blackFGColor = "\x1B[30m";
@ -276,90 +276,90 @@ bool AxisAlignedCollision(const Sprite& a, const Sprite& b) // AABB - AABB colli
return collisionX && collisionY; return collisionX && collisionY;
} }
// Initial script processing, which loads variables and functions from builtin //// Initial script processing, which loads variables and functions from builtin
int GetBuiltins(std::string s) //int GetBuiltins(std::string s)
{ //{
std::string script = replace(s, " ", "\t"); // std::string script = replace(s, " ", "\t");
//
vector<string> lines = split(script, '\n'); // vector<string> lines = split(script, '\n');
vector<vector<string>> words; // vector<vector<string>> words;
for (int i = 0; i < (int)lines.size(); i++) // for (int i = 0; i < (int)lines.size(); i++)
{ // {
words.push_back(split(lines.at(i), ' ')); // words.push_back(split(lines.at(i), ' '));
} // }
//
// Go through entire script and iterate through all types to see if line is a // // Go through entire script and iterate through all types to see if line is a
// function declaration, then store it with it's value // // function declaration, then store it with it's value
for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) // for (int lineNum = 0; lineNum < (int)words.size(); lineNum++)
{ // {
//Checks if it is function // //Checks if it is function
if (words.at(lineNum).at(0) == "func") // if (words.at(lineNum).at(0) == "func")
{ // {
vector<vector<string>> functionContents; // vector<vector<string>> functionContents;
//
string functName = split(words.at(lineNum).at(1), '(')[0]; // string functName = split(words.at(lineNum).at(1), '(')[0];
//
#if DEVELOPER_MESSAGES == true //#if DEVELOPER_MESSAGES == true
InterpreterLog("Load builtin function " + functName + "..."); // InterpreterLog("Load builtin function " + functName + "...");
#endif //#endif
//
string args = ""; // string args = "";
for (int w = 1; w < (int)words.at(lineNum).size(); w++) // Get all words from the instantiation line: these are the 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(replace(words.at(lineNum).at(w), "(", " "), ")", "");
} // }
//
args = replace(args, functName + " ", ""); // args = replace(args, functName + " ", "");
functionContents.push_back(split(args, ',')); // functionContents.push_back(split(args, ','));
//
int numOfBrackets = 1; // int numOfBrackets = 1;
for (int p = lineNum + 2; p < (int)words.size(); p++) // for (int p = lineNum + 2; p < (int)words.size(); p++)
{ // {
numOfBrackets += countInVector(words.at(p), "{") - countInVector(words.at(p), "}"); // numOfBrackets += countInVector(words.at(p), "{") - countInVector(words.at(p), "}");
if (numOfBrackets == 0) // if (numOfBrackets == 0)
break; // break;
functionContents.push_back(removeTabs(words.at(p), 1)); // functionContents.push_back(removeTabs(words.at(p), 1));
} // }
builtinFunctionValues[functName] = functionContents; // builtinFunctionValues[functName] = functionContents;
//cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; // //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl;
} // }
else // else
{ // {
if (words.at(lineNum).at(0) == "string") // if (words.at(lineNum).at(0) == "string")
{ // {
builtinVarVals[words.at(lineNum).at(1)] = StringRaw(words.at(lineNum).at(3)); // builtinVarVals[words.at(lineNum).at(1)] = StringRaw(words.at(lineNum).at(3));
#if DEVELOPER_MESSAGES == true //#if DEVELOPER_MESSAGES == true
InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); // InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "...");
#endif //#endif
} // }
else if (words.at(lineNum).at(0) == "int") // else if (words.at(lineNum).at(0) == "int")
{ // {
builtinVarVals[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3)); // builtinVarVals[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3));
#if DEVELOPER_MESSAGES == true //#if DEVELOPER_MESSAGES == true
InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); // InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "...");
#endif //#endif
} // }
else if (words.at(lineNum).at(0) == "float") // else if (words.at(lineNum).at(0) == "float")
{ // {
builtinVarVals[words.at(lineNum).at(1)] = stof(words.at(lineNum).at(3)); // builtinVarVals[words.at(lineNum).at(1)] = stof(words.at(lineNum).at(3));
#if DEVELOPER_MESSAGES == true //#if DEVELOPER_MESSAGES == true
InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); // InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "...");
#endif //#endif
} // }
else if (words.at(lineNum).at(0) == "bool") // else if (words.at(lineNum).at(0) == "bool")
{ // {
builtinVarVals[words.at(lineNum).at(1)] = stob(words.at(lineNum).at(3)); // builtinVarVals[words.at(lineNum).at(1)] = stob(words.at(lineNum).at(3));
#if DEVELOPER_MESSAGES == true //#if DEVELOPER_MESSAGES == true
InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "..."); // InterpreterLog("Load builtin variable " + words.at(lineNum).at(1) + "...");
#endif //#endif
} // }
//else // //else
// LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum)); // // LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum));
} // }
} // }
//
return 0; // return 0;
} //}
// Executes // Executes
boost::any ZSFunction(const string& name, const vector<boost::any>& args) boost::any ZSFunction(const string& name, const vector<boost::any>& args)

View File

@ -8,7 +8,8 @@
//#include "builtin.h" //#include "builtin.h"
using namespace std; 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) bool isNumber(const string& str)

View File

@ -83,15 +83,18 @@ func Update(deltaTime)
// Apply gravity // Apply gravity
g_colliding = Colliding(g_playerSprite, g_groundSprite) g_colliding = Colliding(g_playerSprite, g_groundSprite)
print g_colliding
if g_colliding == false if g_colliding == false
{ {
print g_jumping
if g_jumping == false if g_jumping == false
{ {
g_playerTargetPosition.y += deltaTime * g_gravitySpeed g_playerTargetPosition.y += deltaTime * g_gravitySpeed
print "grav"
} }
if g_jumping == true 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 print g_jumpingTime
} }
} }
@ -128,6 +131,6 @@ func Update(deltaTime)
func Colliding(a, b) func Colliding(a, b)
{ {
bool b = ZS.Physics.AxisAlignedCollision(a, b) bool bo = ZS.Physics.AxisAlignedCollision(a, b)
return b return bo
} }