Просмотр исходного кода

Added support for End statement.
Re-applied commenting out of Catch count check which seemed to have disappeared in the recent repo issues.

woollybah 11 лет назад
Родитель
Сommit
45c9a73fff
4 измененных файлов с 114 добавлено и 34 удалено
  1. 5 0
      ctranslator.bmx
  2. 87 32
      parser.bmx
  3. 18 0
      stmt.bmx
  4. 4 2
      translator.bmx

+ 5 - 0
ctranslator.bmx

@@ -1121,6 +1121,11 @@ Type TCTranslator Extends TTranslator
 		Return s
 	End Method
 	
+	Method TransEndStmt$( stmt:TEndStmt )
+		Emit "bbEnd();"
+	End Method
+
+	
 	'***** Declarations *****
 Rem	
 	Method EmitFuncProto( decl:TFuncDecl )

+ 87 - 32
parser.bmx

@@ -1068,9 +1068,8 @@ Type TParser
 	End Method
 
 	Method ParseIfStmt( term$ )
-
 		CParse "if"
-'DebugStop
+
 		Local expr:TExpr=ParseExpr()
 
 		CParse "then"
@@ -1103,13 +1102,30 @@ Type TParser
 				EndIf
 			Default
 				ParseStmt
+
+				' to handle "end" statement
+				If _toke = "end" Then
+					NextToke
+					If _toke = "if" Then
+						'_block.RemoveStmt ' remove the "end" statement we just added
+						If term="end" Then
+							Parse "if"
+							If eatTerm Then
+								eatTerm = False
+							End If
+							Exit
+						End If
+					Else
+						ParseEndStmt(False)
+					End If
+				End If
 			End Select
 		Wend
 		PopBlock
 
 		If eatTerm
 			NextToke
-			If term="end" CParse "if"
+			If term="end" Parse "if"
 		EndIf
 
 		Local stmt:TIfStmt=New TIfStmt.Create( expr,thenBlock,elseBlock )
@@ -1118,7 +1134,6 @@ Type TParser
 	End Method
 
 	Method ParseWhileStmt()
-
 		Parse "while"
 
 		Local expr:TExpr=ParseExpr()
@@ -1126,11 +1141,22 @@ Type TParser
 
 		PushBlock block
 		While Not CParse( "wend" )
-			If CParse( "end" )
-				CParse "while"
-				Exit
-			EndIf
+'			If CParse( "end" )
+'				CParse "while"
+'				Exit
+'			EndIf
 			ParseStmt
+
+			' to handle "end" statement
+			If _toke = "end" Then
+				NextToke
+				If _toke = "while" Then
+					NextToke
+					Exit
+				Else
+					ParseEndStmt(False)
+				End If
+			End If
 		Wend
 		PopBlock
 
@@ -1191,10 +1217,10 @@ Type TParser
 
 			PushBlock block
 			While Not CParse( "next" )
-				If CParse( "end" )
-					CParse "for"
-					Exit
-				EndIf
+				'If CParse( "end" )
+				'	CParse "for"
+				'	Exit
+				'EndIf
 				ParseStmt
 			Wend
 			PopBlock
@@ -1244,10 +1270,10 @@ Type TParser
 
 		PushBlock block
 		While Not CParse( "next" )
-			If CParse( "end" )
-				CParse "for"
-				Exit
-			EndIf
+			'If CParse( "end" )
+			'	CParse "for"
+			'	Exit
+			'EndIf
 			ParseStmt
 		Wend
 		PopBlock
@@ -1297,7 +1323,8 @@ Type TParser
 				ParseStmt
 			End If
 		Wend
-		If Not catches.Length() Err "Try block must have at least one catch block"
+		' TODO : handle case of no catch - perhaps throw the exception again.
+		'If Not catches.Length() Err "Try block must have at least one catch block"
 		PopBlock
 		NextToke
 		CParse "try"
@@ -1323,6 +1350,14 @@ Type TParser
 
 		_block.AddStmt New TAssertStmt.Create( expr, elseExpr )
 	End Method
+	
+	Method ParseEndStmt(eatEnd:Int = True)
+		If eatEnd Then
+			Parse "end"
+		End If
+		
+		_block.AddStmt New TEndStmt.Create( )
+	End Method
 
 	Method ParseSelectStmt()
 		Parse "select"
@@ -1361,6 +1396,15 @@ Type TParser
 				PushBlock block
 				While _toke<>"case" And _toke<>"default" And _toke<>"end" And _toke<>"endselect"
 					ParseStmt
+					
+					If _toke = "end" Then
+						NextToke
+						If _toke = "select" Then
+							Exit
+						Else
+							ParseEndStmt(False)
+						End If
+					End If
 				Wend
 				PopBlock
 
@@ -1382,6 +1426,15 @@ Type TParser
 					Err "Select statement can have only one default block."
 				End Select
 				ParseStmt
+
+				If _toke = "end" Then
+					NextToke
+					If _toke = "select" Then
+						Exit
+					Else
+						ParseEndStmt(False)
+					End If
+				End If
 			Wend
 			PopBlock
 		EndIf
@@ -1389,8 +1442,10 @@ Type TParser
 		SetErr
 
 		If Not CParse("endselect") Then
-			Parse "end"
-			Parse "select"
+			If Not CParse("select")
+				Parse "end"
+				Parse "select"
+			End If
 		End If
 	End Method
 
@@ -1457,10 +1512,10 @@ Type TParser
 			ParseTryStmt()
 		Case "throw"
 			ParseThrowStmt()
+		Case "end"
+			ParseEndStmt()
 		Default
-'If _toker._line = 246 Then
-'DebugStop
-'End If
+
 			Local expr:TExpr=ParsePrimaryExpr( True )
 
 			Select _toke.ToLower()
@@ -1472,7 +1527,6 @@ Type TParser
 					Local sym$= TToker._symbols[i]
 					If _toke.ToLower() = sym
 						_toke = TToker._symbols_map[i]
-'DebugLog _toke
 						Exit
 					EndIf
 				Next
@@ -1610,10 +1664,10 @@ Type TParser
 		EndIf
 
 		'meta data for variables
-		If CParse( "{" ) then
+		If CParse( "{" ) Then
 			'print "meta for variable: "+id+ " -> "+ParseMetaData()
 			ParseMetaData()
-		Endif
+		EndIf
 
 		Return decl
 	End Method
@@ -1665,7 +1719,7 @@ Type TParser
 
 		Repeat
 			'concat lines connected with ".."
-			If _toke =".." then HandleDotsLineConnector()
+			If _toke =".." Then HandleDotsLineConnector()
 
 			'append current token to metaDataString
 			metaDataString :+ _toke
@@ -1673,14 +1727,14 @@ Type TParser
 			NextToke()
 
 			'reached end of meta data declaration
-			If _toke="}" then Exit
+			If _toke="}" Then Exit
 		Forever
 
 		'continue to next token
 		NextToke()
 
 		'parse this into something
-		return metaDataString
+		Return metaDataString
 	End Method
 
 
@@ -1722,7 +1776,7 @@ Type TParser
 			Local nargs:Int
 			Repeat
 				' handle end-of-line "dot dot return"
-				If _toke =".." then HandleDotsLineConnector()
+				If _toke =".." Then HandleDotsLineConnector()
 
 				Local id$=ParseIdent()
 
@@ -1753,7 +1807,7 @@ Type TParser
 				If _toke=")" Exit
 
 				' handle end-of-line "dot dot return"
-				If _toke =".." then HandleDotsLineConnector()
+				If _toke =".." Then HandleDotsLineConnector()
 
 				Parse ","
 			Forever
@@ -1852,14 +1906,15 @@ End If
 				If (Not meth And CParse("function")) Or (meth And CParse("method"))
 					Exit
 				End If
+				
+				' handle "end" statement
+				ParseEndStmt(False)
 			EndIf
 
 			ParseStmt
 		Wend
 		PopBlock
 
-		'DebugStop ' BaH
-
 		NextToke
 		If toke CParse toke
 

+ 18 - 0
stmt.bmx

@@ -443,3 +443,21 @@ Type TAssertStmt Extends TStmt
 		Return _trans.TransAssertStmt( Self )
 	End Method
 End Type
+
+Type TEndStmt Extends TStmt
+	
+	Method Create:TEndStmt( )
+		Return Self
+	End Method
+
+	Method OnCopy:TStmt( scope:TScopeDecl )
+		Return New TEndStmt.Create( )
+	End Method
+	
+	Method OnSemant()
+	End Method
+	
+	Method Trans$()
+		Return _trans.TransEndStmt( Self )
+	End Method
+End Type

+ 4 - 2
translator.bmx

@@ -613,7 +613,7 @@ End Rem
 	
 	'returns unreachable status!
 	Method EmitBlock:Int( block:TBlockDecl )
-
+'DebugStop
 		'If ENV_CONFIG="debug"
 		'	If TFuncDecl( block ) EmitPushErr
 		'EndIf
@@ -625,7 +625,7 @@ End Rem
 			_errInfo=stmt.errInfo
 			
 			If unreachable And ENV_LANG<>"as"
-				'If stmt.errInfo Print "Unreachable:"+stmt.errInfo
+				If stmt.errInfo Print "Unreachable:"+stmt.errInfo
 				Exit
 			EndIf
 
@@ -795,6 +795,8 @@ End Rem
 		Emit "// TODO : assert statement"
 
 	End Method
+
+	Method TransEndStmt$( stmt:TEndStmt ) Abstract
 	
 	'module
 	Method TransApp( app:TAppDecl ) Abstract