Fixing Break << Back



In most programming languages (for example: C, C++, C#, Java, Javascript, Kotlin, Pascal, Swift, etc etc) there is a concept of breaking out of the current loop you are in. In all languages mentioned above, it uses the keyword "break" to accomplish such task. In VDF, however, the keyword "Break" will break out of the current scope. If the current scope happens to be a loop, "break" will break out of the loop.

Repeat
	Readln sLine
	If (sLine="") Break
	Send ProcessLine sLine
Until (SeqEof)

So far so good. If "break" is hit, the line "Send ProcessLine" statment will be skipped. What if someone comes in after you, and do something like this -

Repeat
	Readln sLine
	If (sLine="") Begin
		Send DoSomeCleanup
		Break
	End
	Send ProcessLine sLine // It will execute even after "break" is executed.
Until (SeqEof)

Now "break" breaks out of the Begin/End block (which is the scope that the "break" statement is in). That someone just unwittingly breaks the original logic.

Use UI
Integer i
For i From 1 to 5
    If (i=3) Break Begin 
    Showln i
Loop
InKey i

The above code is meant to skip printing out the number 3. "Break Begin" combo acts similar to "Continue" in other programming languages. However in other programming languages, "Continue" jumps to the beginning of the loop. "Break Begin" in VDF jumps to the beginning of the current scope. What could go wrong?

Use UI
Integer i
For i From 1 to 5
    If (i=3) Begin
    	Break Begin 
    End
    Showln i
Loop
InKey i

Someone (could be yourself) decides to put the "Break Begin" statement inside a "Begin/End" block. Now you have an infinite loop in your hand.


There have been many debates on the DAW forum/newsgroup regarding whether this should be the correct/desired behavior. Regardless whether it is correct/desired, it's too late to change it now since changing the functionality of "break" now can potentially break a lot of existing code. So, what should we do?

Some DAW forum members have proposed creating a new command specifically for breaking out loop. I would agree. Unfortunately I started programming in VDF version 7.3, and the only knowledge I have on creating commands is from reading FMAC. DAW still hasn't created such command in all these years of the existence of DF/VDF/DF, chances are that nothing will be done in this regard in the foreseeable future. Well, time to brush up my FMAC skill and attempt on creating command to break out of a loop instead of breaking out from the current scope.

#COMMAND BREAKLOOP
	#IF !0=0
		#POP S$
	   	#PUSH !s
	#ENDIF
    #IFDEF WHILE$!s 
    	Goto END$!s
    #ELSE
    	#IFDEF WHILE#!s
    		Goto END$!s
    	#ELSE
			#SET S$ !s-1
			BREAKLOOP !s
		#ENDIF
	#ENDIF
#ENDCOMMAND

#COMMAND CONTINUELOOP
	#IF !0=0
		#POP S$
	   	#PUSH !s
	#ENDIF
    #IFDEF WHILE$!s 
    	Goto BEGIN$!s
    #ELSE
    	#IFDEF WHILE#!s
    		Goto BEGIN$!s
    	#ELSE
			#SET S$ !s-1
			CONTINUELOOP !s
		#ENDIF
	#ENDIF
#ENDCOMMAND

Here is my test for it.

Integer i j
If (True) Begin
	For i From 0 to 10
		If (True) Begin
			Repeat
				If (True) Begin
					If (True) Begin
						For J From 0 to 100
							If (True) Begin
								Showln "Breaking While"
								BreakLoop
							End
							Showln "Error:While"
						Loop
						Showln "Breaking Repeat"
						BreakLoop
					End
					Showln "Error:Repeat"
				End
			Until (False)
			Showln "Breaking For"
			BreakLoop
		End
		Showln "Error For"
	Loop
	Showln "Yes"
End
InKey i	
Free Web Hosting