Skip to content

Commit

Permalink
Important change to nested if statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
10xJSChad authored Jul 24, 2021
1 parent 06958a2 commit a15d007
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
funcName = ""
funcCode = []
recordCode = False
skip = False
skip = 0

#Strips leading spaces
def stripSpaces(cmd):
Expand All @@ -30,12 +30,13 @@ async def parseCommand(cmd):
cmd = cmd.lower() #Converts the line to lowercase to prepare it for further parsing
splitCmd = cmd.split(" ") #Splits the string by space to prepare for further parsing

if splitCmd[0] == "end" and splitCmd[1] == "if" and skip == True: #Checks if the first word in the splitCmd list is "end", then checks if
#the second word is "if", marking the end of an if statement.
skip = False #Sets the skip variable to false, making the interpreter stop ignoring future lines of BulbScript code.
if splitCmd[0] == "if" and skip > 0: #Increments the skip variable, this is needed for nested conditionals to work
skip += 1
if splitCmd[0] == "end" and splitCmd[1] == "if" and skip > 0: #Decrements it on an "end if" statement
skip -= 1

if skip: return
if skip > 0: return

if recordCode: funcCode.append(cmd) #Appends lines of BulbScript code to the funcCode list if a function has been declared
if splitCmd[0] == "func":
funcName = splitCmd[1] #Sets the function name to the first word after a space
Expand Down Expand Up @@ -94,7 +95,7 @@ async def parseCommand(cmd):
if splitCmd[0] == "stop":
return "stop"
if splitCmd[0] == "if":
skip = not (bulbVariables.compareVariable(splitCmd[1], splitCmd[3], splitCmd[2]))
if not (bulbVariables.compareVariable(splitCmd[1], splitCmd[3], splitCmd[2])): skip += 1

async def parseCode(codeToParse, runOnce=False):
loop = True
Expand All @@ -115,4 +116,4 @@ async def parseCode(codeToParse, runOnce=False):

sys.path.append('/bulbScript_functions')
sys.path.append('/user_functions')
asyncio.run(initialize(open("bulbScript_scripts/" + config.loadedScript, "r")))
asyncio.run(initialize(open("bulbScript_scripts/" + config.loadedScript, "r")))

1 comment on commit a15d007

@10xJSChad
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, nested if statements would not work because the skip variable would be set to False whenever the end of an if statement would be found.
The skip variable was used to determine when to skip parsing code that was in an if statement where the condition was not met. Example below

var i = 5 

if i == 6 //skip is now set to True, because i != 6
    print Hello World //This is not parsed because skip is True
end if

When nested conditionals are used, this could quickly create issues.

var i = 5
var j = 10

if i == 6 //skip is True
    if j == 10 //Doesn't matter what the condition is
        print Hello World
    end if //The end of an if statement is found, and skip is set to false
    print Hello World-er //This code will be executed, despite the condition for the first if statement not being met
end if

With the the new changes:

if i == 6 //skip is set to 1, from 0
    if j == 10 //Another if statement is found, skip is incremented to 2
        print Hello World
    end if //The end of an if statement is found, and skip is decremented to 1
    print Hello World-er //This code will not be executed, because skip > 0
end if //The end of an if statement is found, and skip is decremented to 0, code will now be parsed again

Please sign in to comment.