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] 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; }