Browse Source

FIX: corrected handling of singleline-if + end

- single line ifs are now correctly advance-checking for "end if"
- added "ifthen"-test
- updated "end"-test to include end+concat-command
Ronny Otto 10 years ago
parent
commit
254db96902

+ 14 - 5
parser.bmx

@@ -1318,8 +1318,6 @@ Type TParser
 		Return ParseOrExpr()
 	End Method
 
-Rem
-	unused atm
 
 	Method ReadTillNextToken:string(amount:int=1)
 		'copy current toker and move one token forward
@@ -1331,7 +1329,7 @@ Rem
 		Next
 		return _toker._toke+" "+tok._toke
 	End Method
-endrem
+
 	
 	Method ParseIfStmt( term$, elseIfEndIfReadAheadCheck:Int = False )
 
@@ -1405,12 +1403,23 @@ endrem
                    Err("Expecting expression but encountered end-of-file")
 				'"endif" / "end if"
 				Case "endif", "end"
+					NextToke()
 				
 					If singleLineIf Then
-						Err "'EndIf' without matching 'If'"
+						'check for "end"-command ("if a=1 end")
+						'instead of just checking for the last token of
+						'a singleline-if ("~n") we will have to check for
+						'the existence of "if". Else we would not allow
+						'the code construct:
+						'if a=1 end; print "hi"
+
+						If ReadTillNextToken() = "end if"
+							Err "'EndIf' without matching 'If'"
+						Else
+							ParseEndStmt(False)
+						End If
 					End If
 				
-					NextToke()
 					'If currentToke = "endif" or (currentToke + _toke)="endif"
 					'	'do something if "endif/end if" happens ?
 					'Endif

+ 14 - 11
tests/framework/language/end_01.bmx

@@ -14,29 +14,32 @@ Framework BRL.StandardIO
 'If 1=0 then end end if
 
 'possible
-If 1=0 end
+If 1=0 End
+
+'check for correct singleline-ending check
+If 1=0 End;Print"ok"
 
 'possible
-If 1=0 then end
+If 1=0 Then End
 
 'not possible in blitzmax -albeit ";" should connect things?
 'If 1=0;end;endif
 
 'possible - whatever this does ?
-If 1=0;end
+If 1=0;End
 
 'possible
 If 1=0
-	end
-endif
+	End
+EndIf
 
 'possible
 If 1=0
-	end
-end if
+	End
+End If
 
 'possible - I prefer "Wend"
-while 1=0
+While 1=0
 	End
 End While
 
@@ -50,7 +53,7 @@ End While
 'if (end) then if (end) then end
 
 'not possible in blitzmax ;D
-rem
+Rem
 if end
 	if end
 		end
@@ -65,8 +68,8 @@ endrem
 
 
 'possible
-Function Call:int();end;EndFunction
+Function Call:Int();End;EndFunction
 
 'possible
-Function Call2:int();end;End Function
+Function Call2:Int();End;End Function
 

+ 53 - 0
tests/framework/language/ifthen_01.bmx

@@ -0,0 +1,53 @@
+SuperStrict
+
+Local i:Int = 0
+If i = 0 Then
+	If i <> 0 Then If i <> 3 Then Print i+" is not 1 or 2 or 3"
+	Print "hmm"
+EndIf
+
+
+Local a:Int=1, b:Int =0
+
+If a Then Print "single line"
+If a Print "single line" Else Print "single line b"
+
+If a Then Print "a" Else If b Then Print "b" Else Print "not b"
+If b Then Print "not b" Else If Not a Then Print "not a" Else Print "a"
+
+If a If b Then Print "chained single line"
+
+If a If b
+	Print "chained multi line"
+EndIf
+
+If a Then If b
+	Print "chained multi line then"
+EndIf
+
+If a Then Print "single line then" Else Print "single line else"
+
+If b Then Else Print "single line skipped then"
+
+
+'use variables, else compiler possibly "optimizes out" the if-thens
+Local c:Int = 1
+If c + 2 = 3
+	Print "calculation ok"
+Else
+	Print "cannot calculate"
+EndIf
+
+
+'end check
+If 1 = 1 Then
+	Print "hi"
+	End
+	Print "wont show"
+EndIf
+
+
+Function testme:int()
+EndFunction
+
+If Not testme() End

+ 9 - 0
tests/framework/language/ifthen_01.res

@@ -0,0 +1,9 @@
+hmm
+single line
+single line
+a
+a
+single line then
+single line skipped then
+calculation ok
+hi