From 350072a6acac9c60d024d24493375fedeaf6e0c2 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Tue, 24 May 2022 18:24:32 -0400 Subject: [PATCH 01/29] Change from c_str to std::string --- ZSharp/Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index 24b673d..eab0dd3 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -803,7 +803,7 @@ int main(int argc, char* argv[]) // Change the current working directory to that of the script int chErr = chdir(projectDirectory.c_str()); if (chErr < 0) - LogCriticalError("Failed to change directory to: \"" + projectDirectory.c_str() + "\", error num: " + chErr); + LogCriticalError("Failed to change directory to: \"" + projectDirectory + "\", error num: " + chErr); #if DEVELOPER_MESSAGES InterpreterLog("Changed directory to " + projectDirectory + "..."); #endif From 2cce226575dfada2345cb1eede8cfff2731f0aaa Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Tue, 24 May 2022 21:40:58 -0400 Subject: [PATCH 02/29] Working on platformer --- examples/Platformer/script.zs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/Platformer/script.zs b/examples/Platformer/script.zs index 34cbbb6..4a1b5e2 100644 --- a/examples/Platformer/script.zs +++ b/examples/Platformer/script.zs @@ -41,7 +41,7 @@ func Start() global Sprite g_groundSprite = ZS.Graphics.Sprite("./square.png", groundPos, groundScale, 0) Vec2 instructionsPos = NVec2(centerOfScreen.x, centerOfScreen.y) - global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move, and Spacebar to Jump", "./arial.ttf", instructionsPos, 20, 0, 255, 255, 255) + global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", instructionsPos, 20, 0, 255, 255, 255) global Vec2 g_playerTargetPosition = playerPos @@ -92,7 +92,7 @@ func Update(deltaTime) { float newX = g_playerTargetPosition.x float stopSpeed = deltaTime * 7 float lerpedX = Lerp(oldX, newX, stopSpeed) - g_playerSprite.position = NVec2(lerpedX, newY) + g_playerSprite.position = NVec2(lerpedX, g_playerTargetPosition.y) // Finally draws all of the sprites From 3d8fca05ba1407964eeafdf9fa897af03a50236d Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 25 May 2022 07:25:14 -0400 Subject: [PATCH 03/29] Add description --- examples/Platformer/extra-include.zs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/Platformer/extra-include.zs b/examples/Platformer/extra-include.zs index 29ef010..2cda6e0 100644 --- a/examples/Platformer/extra-include.zs +++ b/examples/Platformer/extra-include.zs @@ -1,5 +1,9 @@ +// This is an "included" ZS file, which can keep your main script +// tidier and more organized or easily share the same functions +// across multiple programs. + func TestInclude() { print "Hello World!" -} \ No newline at end of file +} From 86af2af61a3f21d008c66d428eec728b9ee29d3d Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 25 May 2022 07:46:22 -0400 Subject: [PATCH 04/29] Optimize script --- examples/Platformer/script.zs | 54 ++++++++++++++--------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/examples/Platformer/script.zs b/examples/Platformer/script.zs index 4a1b5e2..2f132ed 100644 --- a/examples/Platformer/script.zs +++ b/examples/Platformer/script.zs @@ -8,6 +8,7 @@ float g_jumpHeight = 20 float g_currPlayerSpeed = 400 bool g_running = false +bool g_colliding = false float g_gravitySpeed = -86 @@ -17,7 +18,7 @@ func Main() { //SplitThread(ThreadedFunction()) - // Immediately creates the window, then Start(), then the game loop. The game loop calls Update() every frame + // Immediately creates the window, then runs Start(), then the game loop. The game loop calls Update() every frame ZS.Graphics.Init("Platformer game", g_screenw, g_screenh, g_resolutionScale) } @@ -28,33 +29,20 @@ func ThreadedFunction() func Start() { - float centerX = g_screenw / 2 - float centerY = g_screenh / 2 - global Vec2 g_screencenter = NVec2(centerX, centerY) + global Vec2 g_screencenter = NVec2(g_screenw / 2, g_screenh / 2) - Vec2 playerPos = g_screencenter - Vec2 playerScale = NVec2(16, 16) - global Sprite g_playerSprite = ZS.Graphics.Sprite("./mariostill.png", playerPos, playerScale, 0) + global Sprite g_playerSprite = ZS.Graphics.Sprite("./mariostill.png", g_screencenter, NVec2(16, 16), 0) - Vec2 groundPos = NVec2(g_screencenter.x, 192) - Vec2 groundScale = NVec2(256, 16) - global Sprite g_groundSprite = ZS.Graphics.Sprite("./square.png", groundPos, groundScale, 0) + global Sprite g_groundSprite = ZS.Graphics.Sprite("./square.png", NVec2(g_screencenter.x, 192), NVec2(256, 16), 0) - Vec2 instructionsPos = NVec2(centerOfScreen.x, centerOfScreen.y) - global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", instructionsPos, 20, 0, 255, 255, 255) + global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", NVec2(centerOfScreen.x, centerOfScreen.y), 11, 0, 255, 255, 255) global Vec2 g_playerTargetPosition = playerPos - - int i = 0 - while i < 10 { - i += 1 - print "while iter : " + i - } } func Update(deltaTime) { float fps = 1 / deltaTime - print "FPS: " + fps + print "FPS: " + fps + "\r" //TestInclude() //// Test automatic conversion from bool to int @@ -64,41 +52,41 @@ func Update(deltaTime) { // Shift key lets you sprint g_running = GetKey("SHIFT_L") - if g_running == true{ + if g_running == true { g_currPlayerSpeed = g_playerRunSpeed } - if g_running == false - { + if g_running == false { g_currPlayerSpeed = g_playerWalkSpeed } // Move Left And Right if GetKey("A") == true { - float newY = g_playerSprite.position.y - float newX = g_playerTargetPosition.x - g_currPlayerSpeed * deltaTime - g_playerTargetPosition = NVec2(newX, newY) + g_playerTargetPosition = NVec2(newX, g_playerSprite.position.y) } if GetKey("D") == true - { - float newY = g_playerSprite.position.y - + { float newX = g_playerTargetPosition.x + g_currPlayerSpeed * deltaTime - g_playerTargetPosition = NVec2(newX, newY) + g_playerTargetPosition = NVec2(newX, g_playerSprite.position.y) } + + // Apply gravity + g_colliding = Colliding(g_playerSprite, g_groundSprite) + if g_colliding == false { + g_playerTargetPosition.y -= deltaTime * g_gravitySpeed + } + // Lerps from old position to destination smoothly - float oldX = g_playerSprite.position.x - float newX = g_playerTargetPosition.x float stopSpeed = deltaTime * 7 - float lerpedX = Lerp(oldX, newX, stopSpeed) + float lerpedX = Lerp(g_playerSprite.position.x, g_playerTargetPosition.x, stopSpeed) g_playerSprite.position = NVec2(lerpedX, g_playerTargetPosition.y) - // Finally draws all of the sprites ZS.Graphics.Draw(g_playerSprite) ZS.Graphics.Draw(g_groundSprite) + // Draw the text ZS.Graphics.DrawText(g_instructionsText) } From 63ba06713cb622d903f494a40ca5713f00b1353f Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 25 May 2022 09:06:47 -0400 Subject: [PATCH 05/29] Update betweenChars function To make more efficient and allow for strings with special characters as function arguments --- ZSharp/strops.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/ZSharp/strops.cpp b/ZSharp/strops.cpp index f3ae7ad..326c352 100644 --- a/ZSharp/strops.cpp +++ b/ZSharp/strops.cpp @@ -190,20 +190,25 @@ string betweenChars(const string& str, const char& openChar, const char& closeCh { string content = ""; - int waitingForClose = 0; + int startPos = 0; + int endPos = (int)str.size(); for (int i = 0; i < (int)str.size(); i++) { - if (waitingForClose > 0 && !(str[i] == closeChar && waitingForClose == 1)) - content += str[i]; - - if (str[i] == openChar) - waitingForClose++; - else if (str[i] == closeChar) - waitingForClose--; + if (str[i] == openChar){ + startPos = i+1; + break; + } + } + for (int i = (int)str.size()-1; i >=0; i--) + { + if (str[i] == closeChar){ + endPos = i-(startPos-1); + break; + } } - return content; + return str.substr(startPos, endPos); } bool startsWith(const string& str, const string& lookFor) From e0966a79206a34f3f92ab8bfb02cfd463b2dfb72 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 25 May 2022 10:33:59 -0400 Subject: [PATCH 06/29] Update if and while Make `if` and `while` more efficient, and allow using the `break` and `continue` keywords --- ZSharp/Main.cpp | 55 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index a9e8d24..e08afb6 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -385,7 +385,7 @@ int varOperation(const vector& str, unordered_map& v boost::any ProcessLine(const vector>& words, int& lineNum, unordered_map& variableValues) { // Check if the first two chars are '//', which would make it a comment - if (words.at(lineNum).at(0)[0] == '/' && words.at(lineNum).at(0)[1] == '/') + if (startsWith(words.at(lineNum).at(0), "//")) return nullType; // If print statement (deprecated, now use ZS.System.Print() function) @@ -478,6 +478,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde vector> whileContents; vector whileParameters; + // Gather the parameters that must be == true for the loop to run int numOfBrackets = 0; for (int w = 1; w < (int)words.at(lineNum).size(); w++) { if (count(words.at(lineNum).at(w), '{') == 0) @@ -489,8 +490,22 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde break; } } + + // If the statement is already false, don't bother gathering the contents + if(BooleanLogic(whileParameters.at(0), whileParameters.at(1), whileParameters.at(2), variableValues) == false){ + lineNum++; + while (lineNum < (int)words.size()) + { + numOfBrackets += countInVector(words.at(lineNum), "{") - countInVector(words.at(lineNum), "}"); + if (numOfBrackets == 0) + break; + lineNum++; + } + return nullType; + } - lineNum += 1; + // Gather the contents + lineNum++; while (lineNum < (int)words.size()) { numOfBrackets += countInVector(words.at(lineNum), "{") - countInVector(words.at(lineNum), "}"); @@ -502,11 +517,16 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde whileContents = removeTabsWdArry(whileContents, 1); + // Loop while true while (BooleanLogic(whileParameters.at(0), whileParameters.at(1), whileParameters.at(2), variableValues)) { //Iterate through all lines in while loop for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) { + if(startsWith(whileContents.at(0), "continue")) + break; // Stops iterating through lines and return to beginning + if(startsWith(whileContents.at(0), "break")) + return nullType; // Stops iterating through lines and leave while loop boost::any returnVal = ProcessLine(whileContents, lineNum, variableValues); if (!returnVal.empty()) return returnVal; @@ -521,6 +541,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde vector> ifContents; vector ifParameters; + // Gather the parameters that must be == true for the loop to run int numOfBrackets = 0; for (int w = 1; w < (int)words.at(lineNum).size(); w++) { if (count(words.at(lineNum).at(w), '{') == 0) @@ -533,6 +554,20 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde } } + // If the statement is already false, don't bother gathering the contents + if(BooleanLogic(whileParameters.at(0), whileParameters.at(1), whileParameters.at(2), variableValues) == false){ + lineNum++; + while (lineNum < (int)words.size()) + { + numOfBrackets += countInVector(words.at(lineNum), "{") - countInVector(words.at(lineNum), "}"); + if (numOfBrackets == 0) + break; + lineNum++; + } + return nullType; + } + + // Gather the contents lineNum++; while (lineNum < (int)words.size()) { @@ -542,22 +577,24 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde ifContents.push_back(words.at(lineNum)); lineNum++; } - ifContents = removeTabsWdArry(ifContents, 1); + // Execute if true if (BooleanLogic(ifParameters.at(0), ifParameters.at(1), ifParameters.at(2), variableValues)) { //Iterate through all lines in if statement for (int l = 0; l < (int)ifContents.size(); l++) { + if(startsWith(whileContents.at(0), "break")) + return nullType; // Stops iterating through lines and leave while loop boost::any returnVal = ProcessLine(ifContents, l, variableValues); if (!returnVal.empty()) return returnVal; } } - else if (words.size() > lineNum + 1) + else if (words.size() > lineNum) { - if (words[lineNum + 1].at(0) == "else") + if (words[lineNum].at(0) == "else") { vector> elseContents; vector elseParameters; @@ -583,7 +620,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde elseContents = removeTabsWdArry(elseContents, 1); - //Iterate through all lines in if statement + //Iterate through all lines in else statement for (int l = 0; l < (int)elseContents.size(); l++) { boost::any returnVal = ProcessLine(elseContents, l, variableValues); @@ -595,12 +632,6 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde } return nullType; } - //// Gathers else statement contents - //if (words[lineNum][0] == "else") - //{ - // - //} - return nullType; } From 1ad70199406134230f0abf8758ee6157b07bf48c Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 25 May 2022 10:45:15 -0400 Subject: [PATCH 07/29] Allow for single args as bool ex. `if 3` instead of `if 3 == 3` --- ZSharp/Main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index e08afb6..686ef41 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -279,8 +279,10 @@ boost::any EvalExpression(const string& ex, unordered_map& v bool BooleanLogic(const string& valA, const string& determinant, const string& valB, unordered_map& variableValues) { - boost::any valARealValue = EvalExpression(valA, variableValues); - boost::any valBRealValue = EvalExpression(valB, variableValues); + if(valA) + boost::any valARealValue = EvalExpression(valA, variableValues); + if(valB) + boost::any valBRealValue = EvalExpression(valB, variableValues); #if DEVELOPER_MESSAGES == true InterpreterLog(AnyAsString(valARealValue) + " " + determinant + " " + AnyAsString(valBRealValue) + " : " + AnyAsString(valA) + " " + determinant + " " + AnyAsString(valB) + " : " + to_string(AnyAsString(valARealValue) == AnyAsString(valBRealValue))); #endif @@ -296,6 +298,8 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); else if (determinant == "<") return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); + else if (determinant == "") + return AnyAsBool(valARealValue) == true; else LogWarning("unrecognized determinant \'" + determinant + "\'"); From 7d7033cd615b0c1320e00ce8efb619532d40afe2 Mon Sep 17 00:00:00 2001 From: Kaputchino <61330453+Kaputchino@users.noreply.github.com> Date: Wed, 25 May 2022 18:56:07 +0200 Subject: [PATCH 08/29] add functions --- ZSharp/ZS.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/ZSharp/ZS.h b/ZSharp/ZS.h index 175beef..c5956e8 100644 --- a/ZSharp/ZS.h +++ b/ZSharp/ZS.h @@ -142,5 +142,64 @@ func GetKey(keyName) //{ // ZS.System.SplitThread(function) //} + +//Function made by Kaputchino + +//return the number of combinaison + +func Comb(n, r) +{ + return Perm(n ,r) / Fac(r) +} +//return the number of permutation + +func Perm(n, r) +{ + if n < 0 + { + ZS.System.PrintLine("n muss be superior or equal to 0") + return -1 + } + if r < 0 + { + ZS.System.PrintLine("r muss be superior or equal to 0") + return -1 + } + if r > n + { + ZS.System.PrintLine("r muss be inferor or equal to n") + return -1 + } + return Fac(n) / Fac(n - r) +} +//return the factorial of a number + +func Fac(x) +{ + int r = 1 + while x > 1 + { + r = r * x + x = x - 1 + } + return r +} +//return exp(x) by using the taylor method, not extremly accurate + +func TaylorExp(x) +{ + float sum = 0 + float term = 1 + int i = 1 + float sumterm = 1 + while sum != sumterm + { + sum = sumterm + term = term * x / i + i = i + 1 + sumterm = sumterm + term + } + return sum +} )" -; \ No newline at end of file +; From 7ce2d39b93a8babba49e926bde1ba8cf52d738f0 Mon Sep 17 00:00:00 2001 From: Kaputchino <61330453+Kaputchino@users.noreply.github.com> Date: Wed, 25 May 2022 19:02:29 +0200 Subject: [PATCH 09/29] sort correctly the added functions --- ZSharp/ZS.h | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/ZSharp/ZS.h b/ZSharp/ZS.h index c5956e8..46a5ccd 100644 --- a/ZSharp/ZS.h +++ b/ZSharp/ZS.h @@ -151,27 +151,7 @@ func Comb(n, r) { return Perm(n ,r) / Fac(r) } -//return the number of permutation -func Perm(n, r) -{ - if n < 0 - { - ZS.System.PrintLine("n muss be superior or equal to 0") - return -1 - } - if r < 0 - { - ZS.System.PrintLine("r muss be superior or equal to 0") - return -1 - } - if r > n - { - ZS.System.PrintLine("r muss be inferor or equal to n") - return -1 - } - return Fac(n) / Fac(n - r) -} //return the factorial of a number func Fac(x) @@ -201,5 +181,28 @@ func TaylorExp(x) } return sum } + +//return the number of permutation + +func Perm(n, r) +{ + if n < 0 + { + ZS.System.PrintLine("n muss be superior or equal to 0") + return -1 + } + if r < 0 + { + ZS.System.PrintLine("r muss be superior or equal to 0") + return -1 + } + if r > n + { + ZS.System.PrintLine("r muss be inferor or equal to n") + return -1 + } + return Fac(n) / Fac(n - r) +} + )" ; From b321a36b7d8158e8c9f2a8817eae510bd533251b Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 25 May 2022 15:20:17 -0400 Subject: [PATCH 10/29] Fix formatting, spelling --- ZSharp/ZS.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ZSharp/ZS.h b/ZSharp/ZS.h index 46a5ccd..7c80341 100644 --- a/ZSharp/ZS.h +++ b/ZSharp/ZS.h @@ -3,6 +3,9 @@ using namespace std; std::string ZSContents = R"( +//////////////////////////////////////////////////////////////////////////////// +// ↓ DEFAULT BUILTIN ↓ + // Default variables, can be overwritten // if re-initialized or changed float PI = 3.14159265358979323846264338 @@ -138,22 +141,23 @@ func GetKey(keyName) return b } +// WIP //func SplitThread(function) //{ // ZS.System.SplitThread(function) //} -//Function made by Kaputchino -//return the number of combinaison +////////////////////////////////////////////////////// +// ↓ MADE BY KAPUTCHINO ↓ +// Return the number of combinations func Comb(n, r) { - return Perm(n ,r) / Fac(r) + return Perm(n, r) / Fac(r) } -//return the factorial of a number - +// Return the factorial of a number func Fac(x) { int r = 1 @@ -164,8 +168,8 @@ func Fac(x) } return r } -//return exp(x) by using the taylor method, not extremly accurate +// Return exp(x) by using the taylor method, not extremly accurate func TaylorExp(x) { float sum = 0 @@ -182,8 +186,7 @@ func TaylorExp(x) return sum } -//return the number of permutation - +// Return the number of permutations func Perm(n, r) { if n < 0 From a1dcb55468f8b041e58bfa7a0fe1ad39b48d5d59 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Wed, 25 May 2022 17:34:06 -0400 Subject: [PATCH 11/29] Antialiased text as an extra `Text` option, update platformer --- ZSharp/Main.cpp | 18 ++++---- ZSharp/builtin.h | 12 +++++- ZSharp/graphics.h | 27 +++++++++--- ZSharp/strops.cpp | 2 +- examples/Platformer/script.zs | 78 +++++++++++++++++++++++++---------- 5 files changed, 100 insertions(+), 37 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index 686ef41..b158923 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -279,10 +279,12 @@ boost::any EvalExpression(const string& ex, unordered_map& v bool BooleanLogic(const string& valA, const string& determinant, const string& valB, unordered_map& variableValues) { - if(valA) - boost::any valARealValue = EvalExpression(valA, variableValues); - if(valB) - boost::any valBRealValue = EvalExpression(valB, variableValues); + boost::any valARealValue; + boost::any valBRealValue; + if(valA != "") + valARealValue = EvalExpression(valA, variableValues); + if(valB != "") + valBRealValue = EvalExpression(valB, variableValues); #if DEVELOPER_MESSAGES == true InterpreterLog(AnyAsString(valARealValue) + " " + determinant + " " + AnyAsString(valBRealValue) + " : " + AnyAsString(valA) + " " + determinant + " " + AnyAsString(valB) + " : " + to_string(AnyAsString(valARealValue) == AnyAsString(valBRealValue))); #endif @@ -527,9 +529,9 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde //Iterate through all lines in while loop for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) { - if(startsWith(whileContents.at(0), "continue")) + if(startsWith(whileContents.at(0).at(0), "continue")) break; // Stops iterating through lines and return to beginning - if(startsWith(whileContents.at(0), "break")) + if(startsWith(whileContents.at(0).at(0), "break")) return nullType; // Stops iterating through lines and leave while loop boost::any returnVal = ProcessLine(whileContents, lineNum, variableValues); if (!returnVal.empty()) @@ -559,7 +561,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde } // If the statement is already false, don't bother gathering the contents - if(BooleanLogic(whileParameters.at(0), whileParameters.at(1), whileParameters.at(2), variableValues) == false){ + if(BooleanLogic(ifParameters.at(0), ifParameters.at(1), ifParameters.at(2), variableValues) == false){ lineNum++; while (lineNum < (int)words.size()) { @@ -589,7 +591,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde //Iterate through all lines in if statement for (int l = 0; l < (int)ifContents.size(); l++) { - if(startsWith(whileContents.at(0), "break")) + if(startsWith(ifContents.at(0).at(0), "break")) return nullType; // Stops iterating through lines and leave while loop boost::any returnVal = ProcessLine(ifContents, l, variableValues); if (!returnVal.empty()) diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index c7e65ca..70fdcb7 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -403,8 +403,16 @@ boost::any ZSFunction(const string& name, const vector& args) if (!fileExists(StringRaw(AnyAsString(args.at(1))))) LogCriticalError("Failed to create 'Text' object: \"" + StringRaw(AnyAsString(args.at(1))) + "\""); - Text t(StringRaw(AnyAsString(args.at(0))), StringRaw(AnyAsString(args.at(1))), any_cast(args.at(2)), AnyAsFloat(args.at(3)), AnyAsFloat(args.at(4)), (Uint8)AnyAsFloat(args.at(5)), (Uint8)AnyAsFloat(args.at(6)), (Uint8)AnyAsFloat(args.at(7))); - return t; + if (args.size() <= 8) + { + Text t(StringRaw(AnyAsString(args.at(0))), StringRaw(AnyAsString(args.at(1))), any_cast(args.at(2)), AnyAsFloat(args.at(3)), AnyAsFloat(args.at(4)), (Uint8)AnyAsFloat(args.at(5)), (Uint8)AnyAsFloat(args.at(6)), (Uint8)AnyAsFloat(args.at(7)), true); + return t; + } + else + { + Text t(StringRaw(AnyAsString(args.at(0))), StringRaw(AnyAsString(args.at(1))), any_cast(args.at(2)), AnyAsFloat(args.at(3)), AnyAsFloat(args.at(4)), (Uint8)AnyAsFloat(args.at(5)), (Uint8)AnyAsFloat(args.at(6)), (Uint8)AnyAsFloat(args.at(7)), AnyAsBool(args.at(8))); + return t; + } } else if (name == "ZS.Graphics.DrawText") any_cast(args.at(0)).Draw(); diff --git a/ZSharp/graphics.h b/ZSharp/graphics.h index a1a650c..5986d63 100644 --- a/ZSharp/graphics.h +++ b/ZSharp/graphics.h @@ -523,8 +523,8 @@ class Sprite class Text { public: - Text(std::string content, std::string pathToFont, Vec2 position, float fontSize, double angle, Uint8 r, Uint8 g, Uint8 b) - : position(position), angle(angle), content(content), pathToFont(pathToFont), fontSize(fontSize), r(r), g(g), b(b) + Text(std::string content, std::string pathToFont, Vec2 position, float fontSize, double angle, Uint8 r, Uint8 g, Uint8 b, bool antialias) + : position(position), angle(angle), content(content), pathToFont(pathToFont), fontSize(fontSize), r(r), g(g), b(b), antialias(antialias) { rect.x = position.x; rect.y = position.y; @@ -538,8 +538,12 @@ class Text { SDL_Color color = { r, g, b }; - SDL_Surface* surface = TTF_RenderText_Blended(font, content.c_str(), color); - + SDL_Surface* surface; + if (antialias) + surface = TTF_RenderText_Blended(font, content.c_str(), color); + else + surface = TTF_RenderText_Solid(font, content.c_str(), color); + texture = SDL_CreateTextureFromSurface(gRenderer, surface); TTF_SizeText(font, content.c_str(), &rect.w, &rect.h); @@ -559,7 +563,11 @@ class Text { SDL_Color color = { r, g, b }; - SDL_Surface* surface = TTF_RenderText_Blended(font, content.c_str(), color); + SDL_Surface* surface; + if (antialias) + surface = TTF_RenderText_Blended(font, content.c_str(), color); + else + surface = TTF_RenderText_Solid(font, content.c_str(), color); SDL_DestroyTexture(texture); texture = SDL_CreateTextureFromSurface(gRenderer, surface); @@ -604,6 +612,8 @@ class Text return content; if (componentName == "pathToFont") return pathToFont; + if (componentName == "antialias") + return antialias; return 0; } @@ -711,12 +721,19 @@ class Text else if (oper == "+=") content += AnyAsString(otherVal); } + else if (componentName == "antialias") + { + if (oper == "=") + antialias = AnyAsBool(otherVal); + } // Updates changes to text Update(); return *this; } + bool antialias = true; + Vec2 position; Vec2 scale; float fontSize; diff --git a/ZSharp/strops.cpp b/ZSharp/strops.cpp index 326c352..4f3cdcc 100644 --- a/ZSharp/strops.cpp +++ b/ZSharp/strops.cpp @@ -203,7 +203,7 @@ string betweenChars(const string& str, const char& openChar, const char& closeCh for (int i = (int)str.size()-1; i >=0; i--) { if (str[i] == closeChar){ - endPos = i-(startPos-1); + endPos = i-(startPos); // or startPos-1 idk I cant do math right now break; } } diff --git a/examples/Platformer/script.zs b/examples/Platformer/script.zs index 2f132ed..57139b9 100644 --- a/examples/Platformer/script.zs +++ b/examples/Platformer/script.zs @@ -2,17 +2,20 @@ int g_screenw = 256 int g_screenh = 224 int g_resolutionScale = 3 -float g_playerWalkSpeed = 400 -float g_playerRunSpeed = 700 -float g_jumpHeight = 20 +float g_playerWalkSpeed = 150 +float g_playerRunSpeed = 210 +float g_jumpHeight = 200 float g_currPlayerSpeed = 400 bool g_running = false bool g_colliding = false -float g_gravitySpeed = -86 +float g_gravitySpeed = 86 -include "./extra-include.zs" +bool g_jumping = false +float g_jumpingTime = 0 + +//include "./extra-include.zs" func Main() { @@ -29,20 +32,25 @@ func ThreadedFunction() func Start() { + float centerX = g_screenw / 2 + float centerY = g_screenh / 2 global Vec2 g_screencenter = NVec2(g_screenw / 2, g_screenh / 2) global Sprite g_playerSprite = ZS.Graphics.Sprite("./mariostill.png", g_screencenter, NVec2(16, 16), 0) global Sprite g_groundSprite = ZS.Graphics.Sprite("./square.png", NVec2(g_screencenter.x, 192), NVec2(256, 16), 0) - global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", NVec2(centerOfScreen.x, centerOfScreen.y), 11, 0, 255, 255, 255) + global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", NVec2(g_screencenter.x, g_screencenter.y), 8, 0, 255, 255, 255) + g_instructionsText.antialias = false - global Vec2 g_playerTargetPosition = playerPos + global Vec2 g_playerTargetPosition = g_screencenter } -func Update(deltaTime) { +func Update(deltaTime) +{ float fps = 1 / deltaTime - print "FPS: " + fps + "\r" + g_jumpingTime += deltaTime + //print "FPS: " + fps //TestInclude() //// Test automatic conversion from bool to int @@ -52,33 +60,61 @@ func Update(deltaTime) { // Shift key lets you sprint g_running = GetKey("SHIFT_L") - if g_running == true { + if g_running == true + { g_currPlayerSpeed = g_playerRunSpeed } - if g_running == false { + + if g_running == false + { g_currPlayerSpeed = g_playerWalkSpeed } // Move Left And Right - if GetKey("A") == true + if GetKey("D") == true { - float newX = g_playerTargetPosition.x - g_currPlayerSpeed * deltaTime - g_playerTargetPosition = NVec2(newX, g_playerSprite.position.y) + g_playerTargetPosition.x += g_currPlayerSpeed * deltaTime } - if GetKey("D") == true - { - float newX = g_playerTargetPosition.x + g_currPlayerSpeed * deltaTime - g_playerTargetPosition = NVec2(newX, g_playerSprite.position.y) + if GetKey("A") == true + { + g_playerTargetPosition.x -= g_currPlayerSpeed * deltaTime } + // Apply gravity g_colliding = Colliding(g_playerSprite, g_groundSprite) - if g_colliding == false { - g_playerTargetPosition.y -= deltaTime * g_gravitySpeed + if g_colliding == false + { + if g_jumping == false + { + g_playerTargetPosition.y += deltaTime * g_gravitySpeed + } + if g_jumping == true + { + g_playerTargetPosition.y -= (g_jumpHeight*deltaTime) + (deltaTime*g_gravitySpeed*-g_jumpingTime*5) + print g_jumpingTime + } + } + if g_colliding == true + { + if GetKey("SPACE") == false + { + if g_jumpingTime > 1 + { + g_jumping = false + } + } + if GetKey("SPACE") == true + { + g_jumping = true + print "jump" + g_jumpingTime + g_jumpingTime = 0 + g_playerTargetPosition.y -= 2 + } } // Lerps from old position to destination smoothly - float stopSpeed = deltaTime * 7 + float stopSpeed = deltaTime * 15 float lerpedX = Lerp(g_playerSprite.position.x, g_playerTargetPosition.x, stopSpeed) g_playerSprite.position = NVec2(lerpedX, g_playerTargetPosition.y) From a4f7bdab2b3cb97ab75155d231135572f361d6b1 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 26 May 2022 08:16:38 -0400 Subject: [PATCH 12/29] Remove useless lines during preprocessing --- ZSharp/Main.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index b158923..51f1d14 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -679,13 +679,19 @@ boost::any ExecuteFunction(const string& functionName, const vector& 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.) #if DEVELOPER_MESSAGES InterpreterLog("Contents:\n" + script); #endif // Split the script by ;, 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> words; for (int i = 0; i < (int)lines.size(); i++) // Then split said lines into indiviual words words.push_back(split(lines.at(i), ' ')); @@ -693,7 +699,7 @@ int parseZSharp(string script) #if DEVELOPER_MESSAGES InterpreterLog("Gather variables & functions..."); #endif - // First go through entire script and iterate through all types to see if line is a variable + // Go through entire script and iterate through all types to see if line is a variable // or function declaration, then store it with it's value for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) { @@ -867,7 +873,7 @@ int main(int argc, char* argv[]) #endif } else - { // If no script is provided as an argument then throw error + { // If no script is provided as an argument throw error LogWarning("No script provided! Please drag and drop .ZS file over interpreter executable file, or provide it's path as a command-line argument."); cout << "Press Enter to Continue"; cin.ignore(); From 865c2761bbea84c6b6372fbe327c5f2f14f0155c Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 26 May 2022 08:36:45 -0400 Subject: [PATCH 13/29] Update Main.cpp --- ZSharp/Main.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index 51f1d14..cad36cf 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -680,11 +680,8 @@ boost::any ExecuteFunction(const string& functionName, const vector& int parseZSharp(string script) { //script = replace(script, " ", "\t"); // Replace spaces with tabs (not really required, and will break purposefull whitespace in strings etc.) -#if DEVELOPER_MESSAGES - InterpreterLog("Contents:\n" + script); -#endif - // Split the script by ;, signifying a line ending + // 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)) == "") @@ -692,9 +689,17 @@ int parseZSharp(string script) lines.erase(lines.begin() + i); } } +#if DEVELOPER_MESSAGES + InterpreterLog("Contents:\n"); +#endif vector> words; for (int i = 0; i < (int)lines.size(); i++) // Then split said lines into indiviual words + { words.push_back(split(lines.at(i), ' ')); +#if DEVELOPER_MESSAGES + cout << lines.at(i); +#endif + } #if DEVELOPER_MESSAGES InterpreterLog("Gather variables & functions..."); From f0321d9927a5aa61215326dcd951c4b6886661c4 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 26 May 2022 10:49:10 -0400 Subject: [PATCH 14/29] Update extra-include.zs --- examples/Platformer/extra-include.zs | 94 ++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/examples/Platformer/extra-include.zs b/examples/Platformer/extra-include.zs index 2cda6e0..e19986a 100644 --- a/examples/Platformer/extra-include.zs +++ b/examples/Platformer/extra-include.zs @@ -7,3 +7,97 @@ func TestInclude() { print "Hello World!" } +////////////////////////////////////////////////////////////////////////////// +// Benchmark, to check if performance suffers from too many functions/variables +////////////////////////////////////////////////////////////////////////////// +func b1() +{ + print "Hello World!" +} +func b2() +{ + print "Hello World!" +} +func b3() +{ + print "Hello World!" +} +func b4() +{ + print "Hello World!" +} +func b5() +{ + print "Hello World!" +} +func b6() +{ + print "Hello World!" +} +func b7() +{ + print "Hello World!" +} +func b8() +{ + print "Hello World!" +} +func b9() +{ + print "Hello World!" +} +func b10() +{ + print "Hello World!" +} +func b11() +{ + print "Hello World!" +} +func b12() +{ + print "Hello World!" +} +func b13() +{ + print "Hello World!" +} +func b14() +{ + print "Hello World!" +} +func b15() +{ + print "Hello World!" +} +func b16() +{ + print "Hello World!" +} + +int a = 4 +int b = 4 +int c = 4 +int d = 4 +int e = 4 +int f = 4 +int g = 4 +int h = 4 +int i = 4 +int j = 4 +int k = 4 +int l = 4 +int m = 4 +int n = 4 +int o = 4 +int p = 4 +int q = 4 +int r = 4 +int s = 4 +int t = 4 +int u = 4 +int v = 4 +int w = 4 +int x = 4 +int y = 4 +int z = 4 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 15/29] (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 } From bfc3c4d7276bba54f0e745c628e63206ec20b2cd Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Thu, 26 May 2022 21:02:08 -0400 Subject: [PATCH 16/29] ^ --- ZSharp/Main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index 7a211ac..3313905 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -743,6 +743,7 @@ int parseZSharp(string script) if (numOfBrackets == 0) break; functionContents.push_back(words.at(lineNum)); + cout << functName << "<" << args << ">" << endl; lineNum++; } //functionContents = removeTabsWdArry(functionContents, 1); From 89392bc34dfeba1395047498e75cca0f825618b0 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 08:14:03 -0400 Subject: [PATCH 17/29] Update Main.cpp Disable for comment checking, make `ZS.` check safer, allow more variables to be initialized outside of functions (global), let people set the global builtin variable of `EXIT_WHEN_DONE` to true or false, depending if they are a developer and want to leave the console window open after the program finishes. --- ZSharp/Main.cpp | 62 ++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index 3313905..fc5c85c 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -391,9 +391,9 @@ int varOperation(const vector& str, unordered_map& v boost::any ProcessLine(const vector>& words, int& lineNum, unordered_map& variableValues) { - // Check if the first two chars are '//', which would make it a comment - if (startsWith(words.at(lineNum).at(0), "//")) - return nullType; + //// Check if the first two chars are '//', which would make it a comment + //if (startsWith(words.at(lineNum).at(0), "//")) + // return nullType; // If print statement (deprecated, now use ZS.System.Print() function) else if (words.at(lineNum).at(0) == "print") @@ -407,7 +407,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde return EvalExpression(unWrapVec(vector(words.at(lineNum).begin() + 1, words.at(lineNum).end())), variableValues); // Check if it is ZS Builtin function - else if (words.at(lineNum).at(0)[0] == 'Z' && words.at(lineNum).at(0)[1] == 'S' && words.at(lineNum).at(0)[2] == '.') + else if (startsWith(words.at(lineNum).at(0), "ZS.")) return EvalExpression(unWrapVec(words.at(lineNum)), variableValues); // Check if it is function @@ -657,7 +657,7 @@ boost::any ExecuteFunction(const string& functionName, const vector& { variableValues[funcArgs[i]] = inputVarVals[i]; #if DEVELOPER_MESSAGES == true - cout << functionName + " \x1B[33m" << funcArgs[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl; + cout << "in " << functionName + " " << funcArgs[i] << " == " << AnyAsString(inputVarVals[i]) << endl; #endif } } @@ -803,26 +803,34 @@ int parseZSharp(string script) InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); #endif } - else if (words.at(lineNum).at(0) == "int") { - globalVariableValues[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3)); -#if DEVELOPER_MESSAGES == true - InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); -#endif - } - else if (words.at(lineNum).at(0) == "float") { - globalVariableValues[words.at(lineNum).at(1)] = stof(words.at(lineNum).at(3)); -#if DEVELOPER_MESSAGES == true - InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); -#endif - } - else if (words.at(lineNum).at(0) == "bool") { - globalVariableValues[words.at(lineNum).at(1)] = stob(words.at(lineNum).at(3)); -#if DEVELOPER_MESSAGES == true - InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); -#endif + + // Iterate through all types to see if line inits or + // re-inits a variable then store it with it's value + else if (countInVector(types, trim(words.at(lineNum).at(0))) > 0) + { + //cout << words.at(lineNum).at(1) << "=" << unWrapVec(slice(words.at(lineNum), 3, -1)) << "=" << AnyAsString(EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues)) << endl; + globalVariableValues[words.at(lineNum).at(1)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues); } - /*else - LogWarning("unrecognized type \'" + words.at(lineNum).at(0) + "\' on line: " + to_string(lineNum));*/ +// else if (words.at(lineNum).at(0) == "int") { +// globalVariableValues[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3)); +//#if DEVELOPER_MESSAGES == true +// InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); +//#endif +// } +// else if (words.at(lineNum).at(0) == "float") { +// globalVariableValues[words.at(lineNum).at(1)] = stof(words.at(lineNum).at(3)); +//#if DEVELOPER_MESSAGES == true +// InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); +//#endif +// } +// else if (words.at(lineNum).at(0) == "bool") { +// globalVariableValues[words.at(lineNum).at(1)] = stob(words.at(lineNum).at(3)); +//#if DEVELOPER_MESSAGES == true +// InterpreterLog("Load script variable " + words.at(lineNum).at(1) + "..."); +//#endif +// } + else + LogWarning("unrecognized type \'" + words.at(lineNum).at(0) + "\' on line: " + to_string(lineNum)); } } @@ -941,6 +949,12 @@ int main(int argc, char* argv[]) exit(1); } } + else if (AnyAsBool(GetVariableValue("EXIT_WHEN_DONE", globalVariableValues)) == false) + { + cout << "Press Enter to Continue"; + cin.ignore(); + exit(1); + } #endif // Else exit automatically return 0; } From 5e89ac588465ac17e65ee3719a1f62f64c99c5a0 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 08:17:57 -0400 Subject: [PATCH 18/29] Fix typos, add `EXIT_WHEN_DONE` variable, change formatting --- ZSharp/ZS.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ZSharp/ZS.h b/ZSharp/ZS.h index 7c80341..6e62a7c 100644 --- a/ZSharp/ZS.h +++ b/ZSharp/ZS.h @@ -4,13 +4,17 @@ using namespace std; std::string ZSContents = R"( //////////////////////////////////////////////////////////////////////////////// -// ↓ DEFAULT BUILTIN ↓ +// BUILTIN +//////////////////////////////////////////////////////////////////////////////// // Default variables, can be overwritten // if re-initialized or changed float PI = 3.14159265358979323846264338 float EulersNumber = 2.71828183 +// This variable tells the interpreter if it should close the console window when the program has finished running. +bool EXIT_WHEN_DONE = true + // Trigonometric function Sin func Sin(input) { @@ -148,8 +152,10 @@ func GetKey(keyName) //} -////////////////////////////////////////////////////// -// ↓ MADE BY KAPUTCHINO ↓ + +//////////////////////////// +// ↓ MADE BY KAPUTCHINO ↓ // +//////////////////////////// // Return the number of combinations func Comb(n, r) @@ -191,17 +197,17 @@ func Perm(n, r) { if n < 0 { - ZS.System.PrintLine("n muss be superior or equal to 0") + ZS.System.PrintLine("n must be superior or equal to 0") return -1 } if r < 0 { - ZS.System.PrintLine("r muss be superior or equal to 0") + ZS.System.PrintLine("r must be superior or equal to 0") return -1 } if r > n { - ZS.System.PrintLine("r muss be inferor or equal to n") + ZS.System.PrintLine("r must be inferior or equal to n") return -1 } return Fac(n) / Fac(n - r) From 33805294d3803bdfda84fb060291886a54b6299c Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 08:24:30 -0400 Subject: [PATCH 19/29] add `EXIT_WHEN_DONE = false` --- examples/Platformer/script.zs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/Platformer/script.zs b/examples/Platformer/script.zs index 9aa9b66..e7b1aa2 100644 --- a/examples/Platformer/script.zs +++ b/examples/Platformer/script.zs @@ -20,6 +20,7 @@ float g_jumpingTime = 0 func Main() { //SplitThread(ThreadedFunction()) + EXIT_WHEN_DONE = false // Immediately creates the window, then runs Start(), then the game loop. The game loop calls Update() every frame ZS.Graphics.Init("Platformer game", g_screenw, g_screenh, g_resolutionScale) From 438f366ba419c93c6fbd808f79397af1ae41ecd4 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 08:24:56 -0400 Subject: [PATCH 20/29] add `EXIT_WHEN_DONE = false` --- examples/Pong-Example-Project/script.zs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/Pong-Example-Project/script.zs b/examples/Pong-Example-Project/script.zs index 7f75a3c..2be1b22 100644 --- a/examples/Pong-Example-Project/script.zs +++ b/examples/Pong-Example-Project/script.zs @@ -16,6 +16,8 @@ bool aiOn = false // so if you never call ZS.Graphics.Init, then Start won't run func Main() { + EXIT_WHEN_DONE = false + // Immediately creates the window, then Start(), then the game loop. The game loop calls Update() every frame ZS.Graphics.Init("This is a pong game", SCREENW, SCREENH) } From 49429461e82381f2e378d342b69822b944b00c84 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 08:40:19 -0400 Subject: [PATCH 21/29] Add more error checking --- ZSharp/Main.cpp | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index fc5c85c..f83eb16 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -78,7 +78,10 @@ boost::any GetVariableValue(const string& varName, const unordered_map& variableValues) { - if (variableValues.find(split(varName, '.')[0]) != variableValues.end() && split(varName, '.')[0] != "ZS") + if(split(varName, '.')[0] == "ZS") + return false; + + if (variableValues.find(split(varName, '.')[0]) != variableValues.end()) return true; else return false; @@ -121,10 +124,7 @@ bool IsFunction(const string& funcName) } bool IsZSFunction(const string& funcName) { - if (funcName[0] == 'Z' && funcName[1] == 'S' && funcName[2] == '.') - return true; - else - return false; + return startsWith(funcName, "ZS."); } boost::any EvalExpression(const string& ex, unordered_map& variableValues) @@ -406,11 +406,11 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde else if (words.at(lineNum).at(0) == "return") return EvalExpression(unWrapVec(vector(words.at(lineNum).begin() + 1, words.at(lineNum).end())), variableValues); - // Check if it is ZS Builtin function + // Check if it is ZS Builtin function call else if (startsWith(words.at(lineNum).at(0), "ZS.")) return EvalExpression(unWrapVec(words.at(lineNum)), variableValues); - // Check if it is function + // Check if it is function call else if (IsFunction(trim(split(words.at(lineNum).at(0), '(')[0]))) { // No args provided @@ -447,7 +447,14 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde // Check if global variable declaration else if (trim(words.at(lineNum).at(0)) == "global") { - globalVariableValues[words.at(lineNum).at(2)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 4, -1)), variableValues); + try + { + globalVariableValues[words.at(lineNum).at(2)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 4, -1)), variableValues); + } + catch (const std::exception&) + { + LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum)) + ", couldn't initialize variable."); + } return nullType; } @@ -455,8 +462,14 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde // re-inits a variable then store it with it's value else if (countInVector(types, trim(words.at(lineNum).at(0))) > 0) { - //cout << words.at(lineNum).at(1) << "=" << unWrapVec(slice(words.at(lineNum), 3, -1)) << "=" << AnyAsString(EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues)) << endl; - variableValues[words.at(lineNum).at(1)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues); + try + { + variableValues[words.at(lineNum).at(1)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues); + } + catch (const std::exception&) + { + LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum)) + ", couldn't initialize variable."); + } return nullType; } @@ -673,6 +686,7 @@ boost::any ExecuteFunction(const string& functionName, const vector& } catch (const std::exception&) { + LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum))); } } return nullType; From d24044a242373ce0969a46fc68c52a55824fb58c Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 19:16:41 -0400 Subject: [PATCH 22/29] Fix issues, add features * Allow for odd spacing, (ex. tabs, no tabs, spaces, etc.) * Rename from determinant to comparer * Fix syntax * Fix issues with single arg function calls, and `Print` and `Printl` functions now work * Escape sequences now work as they should --- ZSharp/Main.cpp | 123 ++++++++++++++---------- ZSharp/ZS.h | 8 +- ZSharp/builtin.h | 9 +- ZSharp/strops.cpp | 29 +++++- examples/Platformer/extra-include.zs | 34 +++---- examples/Platformer/script.zs | 13 +-- examples/Pong-Example-Project/script.zs | 2 +- 7 files changed, 129 insertions(+), 89 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index f83eb16..8d39ba8 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -3,7 +3,7 @@ #include #include //bool DEVELOPER_MESSAGES = true; -#define DEVELOPER_MESSAGES true +#define DEVELOPER_MESSAGES false #define EXAMPLE_PROJECT false #define NAMEVERSION "ZSharp v2.1.0-alpha" @@ -83,8 +83,8 @@ bool IsVar(const string& varName, const unordered_map& varia if (variableValues.find(split(varName, '.')[0]) != variableValues.end()) return true; - else - return false; + + return false; } // Return a vector of values that correspond to a vector of input variable names @@ -115,6 +115,20 @@ vector VarValues(const vector& varNames, unordered_map& vec, unordered_map& variableValues) +{ + cout << "<"; + for (int i = 0; i < vec.size(); i++) + { + cout << AnyAsString(GetVariableValue(vec.at(i), globalVariableValues)); + cout << AnyAsString(GetVariableValue(vec.at(i), variableValues)); + cout << " | "; + } + cout << ">"; + cout << endl; +} + + bool IsFunction(const string& funcName) { if (functionValues.find(funcName) != functionValues.end()) @@ -147,30 +161,26 @@ boost::any EvalExpression(const string& ex, unordered_map& v // start -> FuncCall(0, x, OtherFunc(a)) // changeto -> 0, x, OtherFunc(a) string insideFunArgs = betweenChars(expression, '(', ')'); -#if DEVELOPER_MESSAGES == true - cout << insideFunArgs << endl; -#endif vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); #if DEVELOPER_MESSAGES == true cout << "[" << unWrapVec(argList) << "]" << endl; + printVarValues(argList, variableValues); #endif vector funcArgs = VarValues(argList, variableValues); - return ExecuteFunction(trim(split(expression, '(')[0]), funcArgs); + return ExecuteFunction(split(expression, '(')[0], funcArgs); } else if (isZS && !inQuotes) { // start -> FuncCall(0, x, OtherFunc(a)) // changeto -> 0, x, OtherFunc(a) string insideFunArgs = betweenChars(expression, '(', ')'); -#if DEVELOPER_MESSAGES == true - cout << insideFunArgs << endl; -#endif vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); #if DEVELOPER_MESSAGES == true - cout << "[" << unWrapVec(argList) << "]" << endl; + cout << split(expression, '(')[0]<< " [" << unWrapVec(argList) << "]" << endl; + printVarValues(argList, variableValues); #endif vector funcArgs = VarValues(argList, variableValues); - return ZSFunction(trim(split(expression, '(')[0]), funcArgs); + return ZSFunction(split(expression, '(')[0], funcArgs); } else return GetVariableValue(expression, variableValues); @@ -201,15 +211,13 @@ boost::any EvalExpression(const string& ex, unordered_map& v // start -> FuncCall(0, x, OtherFunc(a)) // changeto -> 0, x, OtherFunc(a) string insideFunArgs = betweenChars(expression, '(', ')'); -#if DEVELOPER_MESSAGES == true - cout << insideFunArgs << endl; -#endif vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); #if DEVELOPER_MESSAGES == true - cout << unWrapVec(argList) << endl; + cout << "[" << unWrapVec(argList) << "]" << endl; + printVarValues(argList, variableValues); #endif vector funcArgs = VarValues(argList, variableValues); - string returnVal = AnyAsString(ExecuteFunction(trim(split(expression, '(')[0]), funcArgs)); + string returnVal = AnyAsString(ExecuteFunction(split(expression, '(')[0], funcArgs)); newExpression += returnVal; } else if (split(name, '.')[0] == "ZS" && !inQuotes) @@ -217,15 +225,13 @@ boost::any EvalExpression(const string& ex, unordered_map& v // start -> FuncCall(0, x, OtherFunc(a)) // changeto -> 0, x, OtherFunc(a) string insideFunArgs = betweenChars(expression, '(', ')'); -#if DEVELOPER_MESSAGES == true - cout << insideFunArgs << endl; -#endif vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); #if DEVELOPER_MESSAGES == true - cout << unWrapVec(argList) << endl; + cout << "[" << unWrapVec(argList) << "]" << endl; + printVarValues(argList, variableValues); #endif vector funcArgs = VarValues(argList, variableValues); - string returnVal = AnyAsString(ExecuteFunction(trim(split(expression, '(')[0]), funcArgs)); + string returnVal = AnyAsString(ZSFunction(split(expression, '(')[0], funcArgs)); newExpression += returnVal; } else @@ -278,7 +284,7 @@ boost::any EvalExpression(const string& ex, unordered_map& v return evaluate(newExpression); } -bool BooleanLogic(const string& valA, const string& determinant, const string& valB, unordered_map& variableValues) +bool BooleanLogic(const string& valA, const string& comparer, const string& valB, unordered_map& variableValues) { boost::any valARealValue; boost::any valBRealValue; @@ -287,24 +293,24 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v if(valB != "") valBRealValue = EvalExpression(valB, variableValues); #if DEVELOPER_MESSAGES == true - InterpreterLog(AnyAsString(valARealValue) + " " + determinant + " " + AnyAsString(valBRealValue) + " : " + AnyAsString(valA) + " " + determinant + " " + AnyAsString(valB) + " : " + to_string(AnyAsString(valARealValue) == AnyAsString(valBRealValue))); + InterpreterLog(AnyAsString(valARealValue) + " " + comparer + " " + AnyAsString(valBRealValue) + " : " + AnyAsString(valA) + " " + comparer + " " + AnyAsString(valB) + " : " + to_string(AnyAsString(valARealValue) == AnyAsString(valBRealValue))); #endif - if (determinant == "==") + if (comparer == "==") return any_compare(valARealValue, valBRealValue); - else if (determinant == "!=") + else if (comparer == "!=") return !any_compare(valARealValue, valBRealValue); - else if (determinant == ">=") + else if (comparer == ">=") return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue); - else if (determinant == "<=") + else if (comparer == "<=") return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue); - else if (determinant == ">") + else if (comparer == ">") return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); - else if (determinant == "<") + else if (comparer == "<") return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); - else if (determinant == "") + else if (comparer == "") return AnyAsBool(valARealValue) == true; else - LogWarning("unrecognized determinant \'" + determinant + "\'"); + LogWarning("unrecognized comparer \'" + comparer + "\'"); return false; } @@ -396,7 +402,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde // return nullType; // If print statement (deprecated, now use ZS.System.Print() function) - else if (words.at(lineNum).at(0) == "print") + if (words.at(lineNum).at(0) == "print") { cout << StringRaw(AnyAsString(EvalExpression(unWrapVec(vector(words.at(lineNum).begin() + 1, words.at(lineNum).end())), variableValues))) << endl; return nullType; @@ -411,19 +417,23 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde return EvalExpression(unWrapVec(words.at(lineNum)), variableValues); // Check if it is function call - else if (IsFunction(trim(split(words.at(lineNum).at(0), '(')[0]))) + else if (IsFunction(split(words.at(lineNum).at(0), '(')[0])) { // No args provided - if (count(words.at(lineNum).at(0), '(') > 0 && count(words.at(lineNum).at(0), ')') > 0) - ExecuteFunction(trim(split(words.at(lineNum).at(0), '(')[0]), vector()); + if (indexInStr(words.at(lineNum).at(0), ')') - indexInStr(words.at(lineNum).at(0), '(')<=1) + ExecuteFunction(split(words.at(lineNum).at(0), '(')[0], vector()); else { // Args provided, parse them first // start -> FuncCall(0, x, OtherFunc(a)) // changeto -> 0, x, OtherFunc(a) string insideFunArgs = betweenChars(unWrapVec(words.at(lineNum)), '(', ')'); vector argList = splitNoOverlap(insideFunArgs, ',', '(', ')'); +#if DEVELOPER_MESSAGES == true + cout << unWrapVec(argList) << endl; + printVarValues(argList, variableValues); +#endif vector funcArgs = VarValues(argList, variableValues); - ExecuteFunction(trim(split(words.at(lineNum).at(0), '(')[0]), funcArgs); + ExecuteFunction(split(words.at(lineNum).at(0), '(')[0], funcArgs); } return nullType; } @@ -445,7 +455,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde } // Check if global variable declaration - else if (trim(words.at(lineNum).at(0)) == "global") + else if (words.at(lineNum).at(0) == "global") { try { @@ -453,14 +463,14 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde } catch (const std::exception&) { - LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum)) + ", couldn't initialize variable."); + LogCriticalError("Error at line: " + to_string(lineNum) + ", " + unWrapVec(words.at(lineNum)) + ", couldn't initialize variable."); } return nullType; } // Iterate through all types to see if line inits or // re-inits a variable then store it with it's value - else if (countInVector(types, trim(words.at(lineNum).at(0))) > 0) + else if (countInVector(types, words.at(lineNum).at(0)) > 0) { try { @@ -468,7 +478,7 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde } catch (const std::exception&) { - LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum)) + ", couldn't initialize variable."); + LogCriticalError("Error at line: " + to_string(lineNum) + ", " + unWrapVec(words.at(lineNum)) + ", couldn't initialize variable."); } return nullType; } @@ -543,13 +553,22 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde //Iterate through all lines in while loop for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) { - if(startsWith(whileContents.at(0).at(0), "continue")) + if(whileContents.at(lineNum).at(0)== "continue") break; // Stops iterating through lines and return to beginning - if(startsWith(whileContents.at(0).at(0), "break")) + if(whileContents.at(lineNum).at(0)== "break") return nullType; // Stops iterating through lines and leave while loop boost::any returnVal = ProcessLine(whileContents, lineNum, variableValues); - if (!returnVal.empty()) - return returnVal; + if (!returnVal.empty()) { + try + { + BREAK t = any_cast(returnVal); + return nullType; + } + catch (boost::bad_any_cast) + { + return returnVal; + } + } } } return nullType; @@ -605,8 +624,8 @@ boost::any ProcessLine(const vector>& words, int& lineNum, unorde //Iterate through all lines in if statement for (int l = 0; l < (int)ifContents.size(); l++) { - if(startsWith(ifContents.at(0).at(0), "break")) - return nullType; // Stops iterating through lines and leave while loop + if(ifContents.at(l).at(0)== "break") + return breakReOp; // Stops iterating through lines and leave while loop boost::any returnVal = ProcessLine(ifContents, l, variableValues); if (!returnVal.empty()) return returnVal; @@ -686,7 +705,7 @@ boost::any ExecuteFunction(const string& functionName, const vector& } catch (const std::exception&) { - LogCriticalError("Error at line: " + lineNum + ", " + unWrapVec(words.at(lineNum))); + LogCriticalError("Error at line: " + to_string(lineNum) + ", " + unWrapVec(words.at(lineNum))); } } return nullType; @@ -742,10 +761,10 @@ int parseZSharp(string script) // args += replace(replace(words.at(lineNum).at(w), "(", " "), ")", ""); // } string args = betweenChars(unWrapVec(words.at(lineNum)), '(', ')'); - cout << functName << "<" << args << ">" << endl; + //cout << functName << "<" << args << ">" << endl; //args = trim(replace(args, functName, "")); - functionContents.push_back(split(args, ',')); + functionContents.push_back(split(replace(args, " ", ""), ',')); int numOfBrackets = countInVector(words.at(lineNum), "{") - countInVector(words.at(lineNum), "}"); @@ -757,7 +776,7 @@ int parseZSharp(string script) if (numOfBrackets == 0) break; functionContents.push_back(words.at(lineNum)); - cout << functName << "<" << args << ">" << endl; + //cout << functName << "<" << args << ">" << endl; lineNum++; } //functionContents = removeTabsWdArry(functionContents, 1); @@ -823,7 +842,7 @@ int parseZSharp(string script) else if (countInVector(types, trim(words.at(lineNum).at(0))) > 0) { //cout << words.at(lineNum).at(1) << "=" << unWrapVec(slice(words.at(lineNum), 3, -1)) << "=" << AnyAsString(EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues)) << endl; - globalVariableValues[words.at(lineNum).at(1)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), variableValues); + globalVariableValues[words.at(lineNum).at(1)] = EvalExpression(unWrapVec(slice(words.at(lineNum), 3, -1)), globalVariableValues); } // else if (words.at(lineNum).at(0) == "int") { // globalVariableValues[words.at(lineNum).at(1)] = stoi(words.at(lineNum).at(3)); diff --git a/ZSharp/ZS.h b/ZSharp/ZS.h index 6e62a7c..0e1b68b 100644 --- a/ZSharp/ZS.h +++ b/ZSharp/ZS.h @@ -107,15 +107,15 @@ func SetPixel(x, y, r, g, b) } // Prints input value to console -func Print(in) +func Print(strToPrint) { - ZS.System.Print(in) + ZS.System.Print(strToPrint) } // Prints input value to console with appended newline '\n' -func Printl(in) +func Printl(strToPrint) { - ZS.System.PrintLine(in) + ZS.System.PrintLine(strToPrint) } // Creates new sprite class diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index 3410413..57c9d72 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -72,8 +72,13 @@ class NullType { public: string type = "NULL"; }; +class BREAK { +public: + string type = "BREAK"; +}; boost::any nullType; +boost::any breakReOp; float clamp(float v, float min, float max) { @@ -425,9 +430,9 @@ boost::any ZSFunction(const string& name, const vector& args) else if (name == "ZS.Input.GetKey") return KEYS[StringRaw(any_cast(args.at(0)))] == 1; else if (name == "ZS.System.Print") - cout << StringRaw(AnyAsString(args.at(0))); + cout << StringRaw(AnyAsString(args.at(0))) << StringRaw(AnyAsString(args.at(0))).length(); else if (name == "ZS.System.PrintLine") - cout << StringRaw(AnyAsString(args.at(0))) << endl; + cout << StringRaw(AnyAsString(args.at(0))) << StringRaw(AnyAsString(args.at(0))).length() << endl; else if (name == "ZS.System.Vec2") { Vec2 v(AnyAsFloat(args.at(0)), AnyAsFloat(args.at(1))); diff --git a/ZSharp/strops.cpp b/ZSharp/strops.cpp index 01608a7..dc2a18e 100644 --- a/ZSharp/strops.cpp +++ b/ZSharp/strops.cpp @@ -26,6 +26,31 @@ bool stob(const string& str) return b; } +string unescape(const string& s) +{ + string res; + string::const_iterator it = s.begin(); + while (it != s.end()) + { + char c = *it++; + if (c == '\\' && it != s.end()) + { + switch (*it++) { + case '\\': c = '\\'; break; + case 'n': c = '\n'; break; + case 't': c = '\t'; break; + // all other escapes + default: + // invalid escape sequence - skip it. alternatively you can copy it as is, throw an exception... + continue; + } + } + res += c; + } + + return res; +} + string StringRaw(const string& s) { string str = trim(s); @@ -43,7 +68,7 @@ string StringRaw(const string& s) if (str[str.size() - 1] != '\"') withoutQuotes += str[str.size() - 1]; - return withoutQuotes; + return unescape(withoutQuotes); } string Quoted(const string& s) @@ -426,4 +451,4 @@ bool isEscaped(const string& str, int curChar) return true; return false; -} +} \ No newline at end of file diff --git a/examples/Platformer/extra-include.zs b/examples/Platformer/extra-include.zs index e19986a..4ae0e42 100644 --- a/examples/Platformer/extra-include.zs +++ b/examples/Platformer/extra-include.zs @@ -5,74 +5,74 @@ func TestInclude() { - print "Hello World!" + Printl("Hello World!") } ////////////////////////////////////////////////////////////////////////////// // Benchmark, to check if performance suffers from too many functions/variables ////////////////////////////////////////////////////////////////////////////// func b1() { - print "Hello World!" + Printl("Hello World!") } func b2() { - print "Hello World!" + Printl("Hello World!") } func b3() { - print "Hello World!" + Printl("Hello World!") } func b4() { - print "Hello World!" + Printl("Hello World!") } func b5() { - print "Hello World!" + Printl("Hello World!") } func b6() { - print "Hello World!" + Printl("Hello World!") } func b7() { - print "Hello World!" + Printl("Hello World!") } func b8() { - print "Hello World!" + Printl("Hello World!") } func b9() { - print "Hello World!" + Printl("Hello World!") } func b10() { - print "Hello World!" + Printl("Hello World!") } func b11() { - print "Hello World!" + Printl("Hello World!") } func b12() { - print "Hello World!" + Printl("Hello World!") } func b13() { - print "Hello World!" + Printl("Hello World!") } func b14() { - print "Hello World!" + Printl("Hello World!") } func b15() { - print "Hello World!" + Printl("Hello World!") } func b16() { - print "Hello World!" + Printl("Hello World!") } int a = 4 diff --git a/examples/Platformer/script.zs b/examples/Platformer/script.zs index e7b1aa2..8b08b97 100644 --- a/examples/Platformer/script.zs +++ b/examples/Platformer/script.zs @@ -28,7 +28,7 @@ func Main() func ThreadedFunction() { - print "threaded:" + Printl("threaded:") } func Start() @@ -51,12 +51,8 @@ func Update(deltaTime) { float fps = 1 / deltaTime g_jumpingTime += deltaTime - //print "FPS: " + fps + Printl("FPS: " + fps) //TestInclude() - - //// Test automatic conversion from bool to int - //int c = GetKey("A") - //print "Test: " + c // Shift key lets you sprint g_running = GetKey("SHIFT_L") @@ -84,19 +80,15 @@ 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) - print g_jumpingTime } } if g_colliding == true @@ -111,7 +103,6 @@ func Update(deltaTime) if GetKey("SPACE") == true { g_jumping = true - print "jump" + g_jumpingTime g_jumpingTime = 0 g_playerTargetPosition.y -= 2 } diff --git a/examples/Pong-Example-Project/script.zs b/examples/Pong-Example-Project/script.zs index 2be1b22..e83f93e 100644 --- a/examples/Pong-Example-Project/script.zs +++ b/examples/Pong-Example-Project/script.zs @@ -64,7 +64,7 @@ func Start() func Update(deltaTime) { //float FPS = 1 / deltaTime - //print "FPS: " + FPS + //Printl("FPS: " + FPS) // Handles Left Paddle Movement // From 1188412769fc3c98522d030db9e4dc7c6534114e Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 19:35:07 -0400 Subject: [PATCH 23/29] Change version, fix wrong ball rotation, use circle sprite --- ZSharp/Main.cpp | 2 +- ZSharp/ZSharpInstallerMakerInnoSetup.iss | 2 +- examples/Pong-Example-Project/ball.png | Bin 0 -> 403 bytes examples/Pong-Example-Project/script.zs | 4 ++-- 4 files changed, 4 insertions(+), 4 deletions(-) create mode 100644 examples/Pong-Example-Project/ball.png diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index 8d39ba8..37a3063 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -5,7 +5,7 @@ //bool DEVELOPER_MESSAGES = true; #define DEVELOPER_MESSAGES false #define EXAMPLE_PROJECT false -#define NAMEVERSION "ZSharp v2.1.0-alpha" +#define NAMEVERSION "ZSharp v2.1.1-alpha" #if defined(__unix__) #define UNIX true diff --git a/ZSharp/ZSharpInstallerMakerInnoSetup.iss b/ZSharp/ZSharpInstallerMakerInnoSetup.iss index 729149a..f3befbb 100644 --- a/ZSharp/ZSharpInstallerMakerInnoSetup.iss +++ b/ZSharp/ZSharpInstallerMakerInnoSetup.iss @@ -2,7 +2,7 @@ ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! #define MyAppName "Z-Sharp" -#define MyAppVersion "2.1.0-alpha" +#define MyAppVersion "2.1.1-alpha" #define MyAppPublisher "AstroSam" #define MyAppURL "https://github.com/sam-astro/Z-Sharp" #define MyAppExeName "ZSharp.exe" diff --git a/examples/Pong-Example-Project/ball.png b/examples/Pong-Example-Project/ball.png new file mode 100644 index 0000000000000000000000000000000000000000..54ee51d56792df90b004cbfc6bc49d632b47efee GIT binary patch literal 403 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=jKx9jP7LeL$-D%zeLYJ`>`xuP>?g8QKW&7q1WZ_U+st5 z4NMmp7c68bVaU*6kY$kKa$s+00|`C+%TOW!5xXbq#@^KC-!`GKqQSpCu(6_1-9@0W zqN#n!Jhiz>3t!KevuQWWJQ3Xkhc9jIeyf)FqVT88d!=QjK2Iv9Zdj7)_E9Y{^KuAZ v6w8|2b3)q+F9~fcy(YA+@)zIwx1Zzx=S_)7)-5XshBAYvtDnm{r-UW|^7W@x literal 0 HcmV?d00001 diff --git a/examples/Pong-Example-Project/script.zs b/examples/Pong-Example-Project/script.zs index e83f93e..932b113 100644 --- a/examples/Pong-Example-Project/script.zs +++ b/examples/Pong-Example-Project/script.zs @@ -41,7 +41,7 @@ func Start() Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle) global Vec2 rPaddleTargetPosition = NVec2(rOffset, yPosPaddle) - global Sprite ballSpr = ZS.Graphics.Sprite("./square.png", ballPos, ballScale, 0) + global Sprite ballSpr = ZS.Graphics.Sprite("./ball.png", ballPos, ballScale, 0) global Sprite lPaddle = ZS.Graphics.Sprite("./square.png", lPaddlePosition, paddleScale, 0) global Sprite rPaddle = ZS.Graphics.Sprite("./square.png", rPaddlePosition, paddleScale, 0) @@ -239,7 +239,7 @@ func HandleBallBounce() difference -= ballY float paddleHeight = rPaddle.scale.y float normalizedRelativeIntersectionY = difference / (paddleHeight / 2) - float bounceAngle = normalizedRelativeIntersectionY * 0.523599 + float bounceAngle = normalizedRelativeIntersectionY * -0.523599 float ballVx = ballSpeed ballVx *= Cos(bounceAngle) ballVx *= -1 From 582656be7bde4bb59f7ef1b6643e651773acdc01 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 19:43:39 -0400 Subject: [PATCH 24/29] Update exit message --- ZSharp/Main.cpp | 8 ++++---- ZSharp/builtin.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index 37a3063..a526e79 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -940,7 +940,7 @@ int main(int argc, char* argv[]) else { // If no script is provided as an argument throw error LogWarning("No script provided! Please drag and drop .ZS file over interpreter executable file, or provide it's path as a command-line argument."); - cout << "Press Enter to Continue"; + InterpreterLog("Press Enter to Exit..."); cin.ignore(); exit(1); } @@ -965,7 +965,7 @@ int main(int argc, char* argv[]) // Entire script has been run, exit. #if DEVELOPER_MESSAGES // If built with developer messages, then verify exit - cout << "Press Enter to Continue"; + InterpreterLog("Press Enter to Exit..."); cin.ignore(); exit(1); #else @@ -977,14 +977,14 @@ int main(int argc, char* argv[]) if (a == "-ve") // If the '-ve' (verify exit) option is used, ask for verification on exit { - cout << "Press Enter to Continue"; + InterpreterLog("Press Enter to Exit..."); cin.ignore(); exit(1); } } else if (AnyAsBool(GetVariableValue("EXIT_WHEN_DONE", globalVariableValues)) == false) { - cout << "Press Enter to Continue"; + InterpreterLog("Press Enter to Exit..."); cin.ignore(); exit(1); } diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index 57c9d72..b43a5b0 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -207,7 +207,7 @@ int LogCriticalError(const string& errorText) PrintColored("ZSharp: ", yellowFGColor, "", true); PrintColored(errorText, redFGColor, "", true); cerr << std::endl; - cout << "Press Enter to Continue"; + InterpreterLog("Press Enter to Exit..."); cin.ignore(); exit(1); //cerr << "\x1B[34m[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] \x1B[33mZSharp: \x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl; From c40d902cef1e93a0b334168f179bc32976817dc3 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 19:51:25 -0400 Subject: [PATCH 25/29] Attempt to fix unix time get --- ZSharp/builtin.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index b43a5b0..2e616c8 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -162,7 +162,13 @@ int InterpreterLog(const string& logText) tm bt{}; #if UNIX - //localtime_r(&timer, &bt); + time_t currentTime; + struct tm* localTime; + time(¤tTime); + localTime = localtime(&utc); + Hour = localTime->tm_hour; + Min = localTime->tm_min; + Sec = localTime->tm_sec; #elif WINDOWS localtime_s(&bt, &timer); Hour = bt.tm_hour; From dc580c22578ec18e6b3215bc70ce0ef9061ba7fc Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 19:57:55 -0400 Subject: [PATCH 26/29] Update builtin.h --- ZSharp/builtin.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index 2e616c8..f4014dc 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -164,8 +164,10 @@ int InterpreterLog(const string& logText) #if UNIX time_t currentTime; struct tm* localTime; - time(¤tTime); - localTime = localtime(&utc); + + time(¤tTime); // Get the current time + localTime = localtime(¤tTime); // Convert the current time to the local time + Hour = localTime->tm_hour; Min = localTime->tm_min; Sec = localTime->tm_sec; From b25fee150a092b247a6ab7d02f5110b296bf7745 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 20:00:15 -0400 Subject: [PATCH 27/29] Update builtin.h --- ZSharp/builtin.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index f4014dc..97793b1 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -198,9 +198,17 @@ int LogCriticalError(const string& errorText) time_t timer = time(0); tm bt{}; -#if defined(__unix__) - //localtime_r(&timer, &bt); -#elif defined(_MSC_VER) +#if UNIX + time_t currentTime; + struct tm* localTime; + + time(¤tTime); // Get the current time + localTime = localtime(¤tTime); // Convert the current time to the local time + + Hour = localTime->tm_hour; + Min = localTime->tm_min; + Sec = localTime->tm_sec; +#elif WINDOWS localtime_s(&bt, &timer); Hour = bt.tm_hour; Min = bt.tm_min; From a8ed99a7b44b1167e865690112113bc2fa7f9a15 Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 20:14:07 -0400 Subject: [PATCH 28/29] Don't print escape sequences in logs, print raw --- ZSharp/builtin.h | 6 +++--- ZSharp/strops.cpp | 20 ++++++++++++++++++++ ZSharp/strops.h | 2 ++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index 97793b1..d27fd36 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -146,7 +146,7 @@ void PrintColored(std::string text, std::string fgColor, std::string bgColor, bo int LogWarning(const string& warningText) { PrintColored("WARNING: ", yellowFGColor, "", true); - PrintColored(warningText, yellowFGColor, "", true); + PrintColored(escaped(warningText), yellowFGColor, "", true); cerr << std::endl; //cout << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl; return 1; @@ -184,7 +184,7 @@ int InterpreterLog(const string& logText) PrintColored("[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] ", blueFGColor, "", true); PrintColored("ZSharp: ", yellowFGColor, "", true); - PrintColored(logText, greenFGColor, "", true); + PrintColored(escaped(logText), greenFGColor, "", true); cout << std::endl; //cout << "\x1B[34m[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] \x1B[33mZSharp: \x1B[32m" << logText << "\033[0m\t\t" << endl; return 1; @@ -221,7 +221,7 @@ int LogCriticalError(const string& errorText) PrintColored("[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] ", blueFGColor, "", true); PrintColored("ZSharp: ", yellowFGColor, "", true); - PrintColored(errorText, redFGColor, "", true); + PrintColored(escaped(errorText), redFGColor, "", true); cerr << std::endl; InterpreterLog("Press Enter to Exit..."); cin.ignore(); diff --git a/ZSharp/strops.cpp b/ZSharp/strops.cpp index dc2a18e..5cfe884 100644 --- a/ZSharp/strops.cpp +++ b/ZSharp/strops.cpp @@ -51,6 +51,26 @@ string unescape(const string& s) return res; } +std::string escaped(const std::string& input) +{ + std::string output; + output.reserve(input.size()); + for (const char c : input) { + switch (c) { + case '\a': output += "\\a"; break; + case '\b': output += "\\b"; break; + case '\f': output += "\\f"; break; + case '\n': output += "\\n"; break; + case '\r': output += "\\r"; break; + case '\t': output += "\\t"; break; + case '\v': output += "\\v"; break; + default: output += c; break; + } + } + + return output; +} + string StringRaw(const string& s) { string str = trim(s); diff --git a/ZSharp/strops.h b/ZSharp/strops.h index 24a171d..31ae45e 100644 --- a/ZSharp/strops.h +++ b/ZSharp/strops.h @@ -69,4 +69,6 @@ bool isEscaped(const string& str, int curChar); bool startsWith(const string& str, const string& lookFor); +std::string escaped(const std::string& input); + #endif From 68c5ef214f22d143f5fa4270e353c0ac60d1390f Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Fri, 27 May 2022 20:21:27 -0400 Subject: [PATCH 29/29] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3a8d766..9b52781 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ func Main() if s == "r" { - print s + " is r" + Printl(s + " is r") } int functionNumber = ExampleFunction("A", s) @@ -99,8 +99,8 @@ func Main() /// be assigned at all on execute and can be left blank func ExampleFunction(inputA, inputB) { - print "In A is: " + inputA - print "In B is: " + inputB + Printl("In A is: " + inputA) + Printl("In B is: " + inputB) // Return a value to the valling location return 4