Browse Source

Fixed some method calls generation for built-in object methods.
Use standard function generation for super functions.
Fixed some String casts.
Modified semanting for classes.
Fixed cast to array.

woollybah 11 năm trước cách đây
mục cha
commit
c5d2ad99b0
4 tập tin đã thay đổi với 92 bổ sung48 xóa
  1. 45 22
      ctranslator.bmx
  2. 21 16
      decl.bmx
  3. 7 2
      expr.bmx
  4. 19 8
      parser.bmx

+ 45 - 22
ctranslator.bmx

@@ -321,12 +321,18 @@ Type TCTranslator Extends TTranslator
 			If Not TStringType(ty).cDecl Then
 				ty.Semant()
 			End If
-			If TObjectType(src).classDecl = TClassDecl.nullObjectClass Then
+			If TNullDecl(TObjectType(src).classDecl) Then
 				Return "&bbEmptyString"
 			End If
 			Return "bbObjectDowncast" + Bra(expr + ",&" + TStringType(ty).cDecl.munged)
 		End If
 
+		If TArrayType(ty) And TObjectType(src) Then
+			If TNullDecl(TObjectType(src).classDecl) Then
+				Return "&bbEmptyArray"
+			End If
+		End If
+
 		If TVarPtrType(src) And TNumericType(ty) Then
 			Return "*" + expr
 		End If
@@ -342,7 +348,10 @@ Type TCTranslator Extends TTranslator
 			Return expr
 		End If
 
-		If Not TObjectType(ty) Or Not TObjectType(src) InternalErr
+		If Not TObjectType(ty) Or Not TObjectType(src) Then
+			DebugStop
+			InternalErr
+		End If
 
 		Local t$=TransType(ty, "TODO: TransPtrCast")
 
@@ -444,7 +453,7 @@ Type TCTranslator Extends TTranslator
 		ty=ty.ActualType()
 		'src=src.ActualType()
 
-		If ty.EqualsType( src ) Return expr
+		If src.EqualsType( ty ) Return expr
 
 		Return TransPtrCast( ty,src,expr,"static" )
 
@@ -517,7 +526,7 @@ Type TCTranslator Extends TTranslator
 					Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 					Local class:String = Bra("(" + obj + TransSubExpr( lhs ) +")->clas")
 					'Local class:String = Bra("&" + decl.scope.munged)
-					Return class + "->md_" + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
+					Return class + "->" + TransFuncPrefix(decl.scope, decl.ident) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
 				Else If TInvokeMemberExpr(lhs)
 					' create a local variable of the inner invocation
 					Local lvar:String = CreateLocal(lhs)
@@ -535,7 +544,7 @@ Type TCTranslator Extends TTranslator
 					Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 					Local class:String = Bra("(" + obj + loc +")->clas")
 					'Local class:String = Bra("&" + decl.scope.munged)
-					Return class + "->md_" + decl.ident+TransArgs( args,decl, loc )
+					Return class + "->" + TransFuncPrefix(decl.scope, decl.ident) + decl.ident+TransArgs( args,decl, loc )
 				Else
 					InternalErr
 				End If
@@ -548,7 +557,7 @@ Type TCTranslator Extends TTranslator
 				Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 				Local class:String = Bra("(" + obj + "o)->clas")
 				'Local class:String = Bra("&" + decl.scope.munged)
-				Return class + "->md_" + decl.ident+TransArgs( args,decl, "o" )
+				Return class + "->" + TransFuncPrefix(decl.scope, decl.ident) + decl.ident+TransArgs( args,decl, "o" )
 			Else
 				Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 				'Local class:String = Bra("(" + obj + "o)->clas")
@@ -591,11 +600,12 @@ Type TCTranslator Extends TTranslator
 	End Method
 
 	Method TransSuperFunc$( decl:TFuncDecl,args:TExpr[] )
-		If decl.IsMethod()
-			Return decl.ClassScope().munged+".md_"+decl.ident+TransArgs( args,decl, "o" )
-		Else
-			Return decl.ClassScope().munged+".fn_"+decl.ident+TransArgs( args,decl)
-		End If
+		Return TransFunc(decl, args, Null)
+'		If decl.IsMethod()
+'			Return decl.ClassScope().munged+".md_"+decl.ident+TransArgs( args,decl, "o" )
+'		Else
+'			Return decl.ClassScope().munged+".fn_"+decl.ident+TransArgs( args,decl)
+'		End If
 	End Method
 
 	Method TransBuiltin$( decl:TFuncDecl,args:TExpr[] )
@@ -855,7 +865,7 @@ Type TCTranslator Extends TTranslator
 			If TFloatType( src ) Return Bra("(BBBYTE)"+t)
 			If TDoubleType( src ) Return Bra("(BBBYTE)"+t)
 			If TLongType( src ) Return Bra("(BBBYTE)"+t)
-			If TStringType( src ) Return t+".ToByte()"
+			If TStringType( src ) Return "bbStringToInt" + Bra(t)
 			If TByteVarPtrType( src ) Return Bra("*" + t)
 		Else If TShortType( dst )
 			If TShortType( src) Return t
@@ -864,7 +874,7 @@ Type TCTranslator Extends TTranslator
 			If TFloatType( src ) Return Bra("(BBSHORT)"+t)
 			If TDoubleType( src ) Return Bra("(BBSHORT)"+t)
 			If TLongType( src ) Return Bra("(BBSHORT)"+t)
-			If TStringType( src ) Return "bbStringToShort" + Bra(t)
+			If TStringType( src ) Return "bbStringToInt" + Bra(t)
 			If TShortVarPtrType( src ) Return Bra("*" + t)
 		Else If TVarPtrType( dst )
 			If Not TConstExpr(expr.expr) Then
@@ -938,6 +948,10 @@ Type TCTranslator Extends TTranslator
 				If TDoublePtrType( src ) Return Bra("(BBLONG*)"+t)
 				If TLongPtrType( src ) Return t
 			End If
+		Else If TArrayType( dst )
+			If TObjectType( src) And TObjectType( src ).classDecl.ident = "Array" Then
+				Return "bbArrayCastFromObject" + Bra(t + "," + TransArrayType(TArrayType( dst ).elemType))
+			End If
 		Else If TObjectType( dst )
 			If TArrayType( src ) Return Bra("(BBOBJECT)"+t)
 			If TStringType( src ) Return Bra("(BBOBJECT)"+t)
@@ -1637,6 +1651,9 @@ End Rem
 		For Local decl:TDecl=EachIn classDecl.Decls()
 			Local fdecl:TFuncDecl =TFuncDecl( decl )
 			If fdecl
+				If Not fdecl.IsSemanted()
+					fdecl.Semant()
+				End If
 				If reserved.Find("," + fdecl.ident.ToLower() + ",") = -1 Then
 					EmitBBClassFuncProto( fdecl )
 					Continue
@@ -1674,13 +1691,14 @@ End Rem
 
 			Local reserved:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
 
+			classDecl.SemantParts()
+
 			'Local fdecls:TFuncDecl[] = classDecl.GetAllFuncDecls(Null, False)
 			For Local decl:TDecl=EachIn classDecl.Decls()
 			'For Local fdecl:TFuncDecl = EachIn fdecls
 
 				Local fdecl:TFuncDecl =TFuncDecl( decl )
 				If fdecl
-					fdecl.Semant()
 
 					If reserved.Find("," + fdecl.ident.ToLower() + ",") = -1 Then
 						EmitClassFuncProto( fdecl )
@@ -1688,11 +1706,12 @@ End Rem
 					End If
 				EndIf
 
-				'Local gdecl:TGlobalDecl =TGlobalDecl( decl )
-				'If gdecl
+				Local gdecl:TGlobalDecl =TGlobalDecl( decl )
+				If gdecl
+					MungDecl gdecl
 				'	Emit "static "+TransRefType( gdecl.ty )+" "+gdecl.munged+";"
-				'	Continue
-				'EndIf
+					Continue
+				EndIf
 			Next
 
 			Emit ""
@@ -2415,10 +2434,10 @@ End Rem
 				MungDecl mdecl
 
 				'skip mdecls we are not interested in
-				If not TModuleDecl(mdecl) then continue
-				If app.mainModule = mdecl then continue
-				If mdecl.ident = "brl.classes" then continue
-				If mdecl.ident = "brl.blitzkeywords" Then continue
+				If Not TModuleDecl(mdecl) Then Continue
+				If app.mainModule = mdecl Then Continue
+				If mdecl.ident = "brl.classes" Then Continue
+				If mdecl.ident = "brl.blitzkeywords" Then Continue
 
 				EmitModuleInclude(TModuleDecl(mdecl))
 			Next
@@ -2443,6 +2462,8 @@ End Rem
 
 			For Local decl:TDecl=EachIn cdecl.Semanted()
 				MungDecl decl
+				
+				cdecl.SemantParts()
 			Next
 
 			EndLocalScope
@@ -2456,6 +2477,8 @@ End Rem
 
 			Local gdecl:TGlobalDecl=TGlobalDecl( decl )
 			If gdecl
+				MungDecl gdecl
+				
 				If Not TFunctionPtrType(gdecl.ty) Then
 					Emit "extern "+TransRefType( gdecl.ty, "" )+" "+gdecl.munged+";"	'forward reference...
 				Else

+ 21 - 16
decl.bmx

@@ -139,7 +139,6 @@ Type TDecl
 
 	Method Semant()
 		If IsSemanted() Return
-'DebugLog "Semant : " + ident
 		
 		If IsSemanting() Err "Cyclic declaration of '"+ident+"'."
 		
@@ -319,6 +318,10 @@ Type TConstDecl Extends TValDecl
 		End If
 	End Method
 	
+	Method ToString$()
+		Return "Const "+Super.ToString()
+	End Method
+
 End Type
 
 Type TVarDecl Extends TValDecl
@@ -940,7 +943,6 @@ Type TFuncDecl Extends TBlockDecl
 	End Method
 
 	Method OnSemant()
-
 		'semant ret type
 		If Not retTypeExpr Then
 			If Not retType Then ' may have previously been set (if this is a function pointer)
@@ -1012,16 +1014,21 @@ Type TFuncDecl Extends TBlockDecl
 'DebugLog "Checking Class : " + sclass.ident
 				Local found:Int
 				For Local decl:TFuncDecl=EachIn sclass.FuncDecls( )
-'DebugLog "Method = " + decl.ident
-					If Not decl.IsSemanted() Then
-						decl.Semant
-					End If
+					'If Not decl.IsSemanted() Then
+					'	decl.Semant
+					'End If
 					
 					If decl.ident.ToLower() = ident.ToLower() Then
+'DebugLog "Method = " + decl.ident
 					
 						If ident.ToLower() = "new" Continue
 'If ident = "Create" DebugStop
 						found=True
+
+						If Not decl.IsSemanted() Then
+							decl.Semant
+						End If
+
 						If EqualsFunc( decl ) 
 'DebugLog "Found"
 							overrides=TFuncDecl( decl.actual )
@@ -1367,8 +1374,6 @@ End Rem
 	
 	Method OnSemant()
 
-		'Print "Semanting "+ToString()
-		
 		PushEnv Self
 
 		'If Not IsTemplateInst()
@@ -1481,24 +1486,24 @@ End Rem
 		End If
 	End Method
 	
-	Method Semant()
-		If IsSemanted() Return
+	Method SemantParts()
+'		If IsSemanted() Return
 		
-		Super.Semant()
+'		Super.Semant()
 		
 		For Local decl:TConstDecl = EachIn Decls()
 			decl.Semant()
 		Next
-		
+
 		For Local decl:TGlobalDecl = EachIn Decls()
 			decl.Semant()
 		Next
 
 		' NOTE : we can't semant functions here as they cause cyclic errors.
-		'For Local decl:TFuncDecl = EachIn Decls()
-		'	decl.Semant()
-		'Next
-		
+		For Local decl:TFuncDecl = EachIn Decls()
+			decl.Semant()
+		Next
+
 	End Method
 	
 	'Ok, this dodgy looking beast 'resurrects' methods that may not currently be alive, but override methods that ARE.

+ 7 - 2
expr.bmx

@@ -121,7 +121,6 @@ Type TExpr
 						Err "Unable to convert from '" + args[i].exprType.ToString() + "()' to '" + funcDecl.argDecls[i].ty.ToString() + "'"
 					End If
 				End If
-
 				args[i]=args[i].Cast( funcDecl.argDecls[i].ty )
 			Else If funcDecl.argDecls[i].init
 				args[i]=funcDecl.argDecls[i].init
@@ -500,7 +499,7 @@ Type TInvokeMemberExpr Extends TExpr
 
 		If Not decl.IsSemanted() decl.Semant()
 		exprType=decl.retType
-		
+
 		args=SemantArgs( args )
 		args=CastArgs( args,decl )
 
@@ -865,8 +864,14 @@ Type TCastExpr Extends TExpr
 			exprType = ty
 			Return Self
 		End If
+		
+		If TArrayType(ty) And TObjectType(src) And TObjectType(src).classDecl.ident = "Array" Then
+			exprType = ty
+			Return Self
+		End If
 
 		If Not exprType
+			DebugStop
 			Err "Cannot convert from "+src.ToString()+" to "+ty.ToString()+"."
 		EndIf
 

+ 19 - 8
parser.bmx

@@ -411,6 +411,10 @@ Type TParser
 			End If
 		End Select
 
+		While CParse( "[]" )
+			ty=New TArrayType.Create( ty )
+		Wend
+
 		Return ty
 	End Method
 
@@ -591,7 +595,7 @@ Type TParser
 						eat=True
 						Exit
 					Case ".."
-						NextToke
+						'toker.NextToke
 					End Select
 				Forever
 			Else
@@ -813,8 +817,15 @@ Type TParser
 					_toke=_toker.Toke()
 					_tokeType=_toker.TokeType()
 					expr=New TIdentExpr.Create( ParseIdent() )
+					ty = ParseConstNumberType()
+					
+					If TArrayType(ty) Then
+						If Not TArrayType(ty).elemType Then
+							TArrayType(ty).elemType = New TIdentType.Create(TIdentExpr(expr).ident)
+							expr=New TIdentTypeExpr.Create( ty )
+						End If
+					End If
 
-					ParseConstNumberType()
 				EndIf
 
 				'expr=New TIdentExpr.Create( ParseIdent() )
@@ -2442,7 +2453,7 @@ End Rem
 	End Method
 
 
-	Method ParseCurrentFile:int(path:string, attrs:int)
+	Method ParseCurrentFile:Int(path:String, attrs:Int)
 
 		LoadExternCasts(path)
 
@@ -2541,21 +2552,21 @@ End Rem
 				Local includeToker:TToker = New TToker.Create(includeFile, includeSource)
 
 				'backup old vars
-				local oldToker:TToker = self._toker
+				Local oldToker:TToker = Self._toker
 
 				'assign temporary vars
-				self._toker = includeToker
+				Self._toker = includeToker
 
 				'parse the include file
 				parseCurrentFile(includeFile, attrs)
 
 				'restore backup vars
-				self._toker = oldToker
+				Self._toker = oldToker
 
 				'move on to next toke (after include "xyz.bmx")
 				NextToke
 
-rem
+Rem
 	old idea
 				'each parser holds multiple "_blocks" (TBlockDecl) in a
 				'list named "_blockStack" (TList)
@@ -2592,7 +2603,7 @@ endrem
 			End Select
 		Wend
 
-		return attrs
+		Return attrs
 	End Method