Przeglądaj źródła

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 lat temu
rodzic
commit
c5d2ad99b0
4 zmienionych plików z 92 dodań i 48 usunięć
  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
 			If Not TStringType(ty).cDecl Then
 				ty.Semant()
 				ty.Semant()
 			End If
 			End If
-			If TObjectType(src).classDecl = TClassDecl.nullObjectClass Then
+			If TNullDecl(TObjectType(src).classDecl) Then
 				Return "&bbEmptyString"
 				Return "&bbEmptyString"
 			End If
 			End If
 			Return "bbObjectDowncast" + Bra(expr + ",&" + TStringType(ty).cDecl.munged)
 			Return "bbObjectDowncast" + Bra(expr + ",&" + TStringType(ty).cDecl.munged)
 		End If
 		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
 		If TVarPtrType(src) And TNumericType(ty) Then
 			Return "*" + expr
 			Return "*" + expr
 		End If
 		End If
@@ -342,7 +348,10 @@ Type TCTranslator Extends TTranslator
 			Return expr
 			Return expr
 		End If
 		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")
 		Local t$=TransType(ty, "TODO: TransPtrCast")
 
 
@@ -444,7 +453,7 @@ Type TCTranslator Extends TTranslator
 		ty=ty.ActualType()
 		ty=ty.ActualType()
 		'src=src.ActualType()
 		'src=src.ActualType()
 
 
-		If ty.EqualsType( src ) Return expr
+		If src.EqualsType( ty ) Return expr
 
 
 		Return TransPtrCast( ty,src,expr,"static" )
 		Return TransPtrCast( ty,src,expr,"static" )
 
 
@@ -517,7 +526,7 @@ Type TCTranslator Extends TTranslator
 					Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 					Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 					Local class:String = Bra("(" + obj + TransSubExpr( lhs ) +")->clas")
 					Local class:String = Bra("(" + obj + TransSubExpr( lhs ) +")->clas")
 					'Local class:String = Bra("&" + decl.scope.munged)
 					'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)
 				Else If TInvokeMemberExpr(lhs)
 					' create a local variable of the inner invocation
 					' create a local variable of the inner invocation
 					Local lvar:String = CreateLocal(lhs)
 					Local lvar:String = CreateLocal(lhs)
@@ -535,7 +544,7 @@ Type TCTranslator Extends TTranslator
 					Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 					Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 					Local class:String = Bra("(" + obj + loc +")->clas")
 					Local class:String = Bra("(" + obj + loc +")->clas")
 					'Local class:String = Bra("&" + decl.scope.munged)
 					'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
 				Else
 					InternalErr
 					InternalErr
 				End If
 				End If
@@ -548,7 +557,7 @@ Type TCTranslator Extends TTranslator
 				Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 				Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 				Local class:String = Bra("(" + obj + "o)->clas")
 				Local class:String = Bra("(" + obj + "o)->clas")
 				'Local class:String = Bra("&" + decl.scope.munged)
 				'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
 			Else
 				Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 				Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
 				'Local class:String = Bra("(" + obj + "o)->clas")
 				'Local class:String = Bra("(" + obj + "o)->clas")
@@ -591,11 +600,12 @@ Type TCTranslator Extends TTranslator
 	End Method
 	End Method
 
 
 	Method TransSuperFunc$( decl:TFuncDecl,args:TExpr[] )
 	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
 	End Method
 
 
 	Method TransBuiltin$( decl:TFuncDecl,args:TExpr[] )
 	Method TransBuiltin$( decl:TFuncDecl,args:TExpr[] )
@@ -855,7 +865,7 @@ Type TCTranslator Extends TTranslator
 			If TFloatType( src ) Return Bra("(BBBYTE)"+t)
 			If TFloatType( src ) Return Bra("(BBBYTE)"+t)
 			If TDoubleType( src ) Return Bra("(BBBYTE)"+t)
 			If TDoubleType( src ) Return Bra("(BBBYTE)"+t)
 			If TLongType( 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)
 			If TByteVarPtrType( src ) Return Bra("*" + t)
 		Else If TShortType( dst )
 		Else If TShortType( dst )
 			If TShortType( src) Return t
 			If TShortType( src) Return t
@@ -864,7 +874,7 @@ Type TCTranslator Extends TTranslator
 			If TFloatType( src ) Return Bra("(BBSHORT)"+t)
 			If TFloatType( src ) Return Bra("(BBSHORT)"+t)
 			If TDoubleType( src ) Return Bra("(BBSHORT)"+t)
 			If TDoubleType( src ) Return Bra("(BBSHORT)"+t)
 			If TLongType( 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)
 			If TShortVarPtrType( src ) Return Bra("*" + t)
 		Else If TVarPtrType( dst )
 		Else If TVarPtrType( dst )
 			If Not TConstExpr(expr.expr) Then
 			If Not TConstExpr(expr.expr) Then
@@ -938,6 +948,10 @@ Type TCTranslator Extends TTranslator
 				If TDoublePtrType( src ) Return Bra("(BBLONG*)"+t)
 				If TDoublePtrType( src ) Return Bra("(BBLONG*)"+t)
 				If TLongPtrType( src ) Return t
 				If TLongPtrType( src ) Return t
 			End If
 			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 )
 		Else If TObjectType( dst )
 			If TArrayType( src ) Return Bra("(BBOBJECT)"+t)
 			If TArrayType( src ) Return Bra("(BBOBJECT)"+t)
 			If TStringType( 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()
 		For Local decl:TDecl=EachIn classDecl.Decls()
 			Local fdecl:TFuncDecl =TFuncDecl( decl )
 			Local fdecl:TFuncDecl =TFuncDecl( decl )
 			If fdecl
 			If fdecl
+				If Not fdecl.IsSemanted()
+					fdecl.Semant()
+				End If
 				If reserved.Find("," + fdecl.ident.ToLower() + ",") = -1 Then
 				If reserved.Find("," + fdecl.ident.ToLower() + ",") = -1 Then
 					EmitBBClassFuncProto( fdecl )
 					EmitBBClassFuncProto( fdecl )
 					Continue
 					Continue
@@ -1674,13 +1691,14 @@ End Rem
 
 
 			Local reserved:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
 			Local reserved:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
 
 
+			classDecl.SemantParts()
+
 			'Local fdecls:TFuncDecl[] = classDecl.GetAllFuncDecls(Null, False)
 			'Local fdecls:TFuncDecl[] = classDecl.GetAllFuncDecls(Null, False)
 			For Local decl:TDecl=EachIn classDecl.Decls()
 			For Local decl:TDecl=EachIn classDecl.Decls()
 			'For Local fdecl:TFuncDecl = EachIn fdecls
 			'For Local fdecl:TFuncDecl = EachIn fdecls
 
 
 				Local fdecl:TFuncDecl =TFuncDecl( decl )
 				Local fdecl:TFuncDecl =TFuncDecl( decl )
 				If fdecl
 				If fdecl
-					fdecl.Semant()
 
 
 					If reserved.Find("," + fdecl.ident.ToLower() + ",") = -1 Then
 					If reserved.Find("," + fdecl.ident.ToLower() + ",") = -1 Then
 						EmitClassFuncProto( fdecl )
 						EmitClassFuncProto( fdecl )
@@ -1688,11 +1706,12 @@ End Rem
 					End If
 					End If
 				EndIf
 				EndIf
 
 
-				'Local gdecl:TGlobalDecl =TGlobalDecl( decl )
-				'If gdecl
+				Local gdecl:TGlobalDecl =TGlobalDecl( decl )
+				If gdecl
+					MungDecl gdecl
 				'	Emit "static "+TransRefType( gdecl.ty )+" "+gdecl.munged+";"
 				'	Emit "static "+TransRefType( gdecl.ty )+" "+gdecl.munged+";"
-				'	Continue
-				'EndIf
+					Continue
+				EndIf
 			Next
 			Next
 
 
 			Emit ""
 			Emit ""
@@ -2415,10 +2434,10 @@ End Rem
 				MungDecl mdecl
 				MungDecl mdecl
 
 
 				'skip mdecls we are not interested in
 				'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))
 				EmitModuleInclude(TModuleDecl(mdecl))
 			Next
 			Next
@@ -2443,6 +2462,8 @@ End Rem
 
 
 			For Local decl:TDecl=EachIn cdecl.Semanted()
 			For Local decl:TDecl=EachIn cdecl.Semanted()
 				MungDecl decl
 				MungDecl decl
+				
+				cdecl.SemantParts()
 			Next
 			Next
 
 
 			EndLocalScope
 			EndLocalScope
@@ -2456,6 +2477,8 @@ End Rem
 
 
 			Local gdecl:TGlobalDecl=TGlobalDecl( decl )
 			Local gdecl:TGlobalDecl=TGlobalDecl( decl )
 			If gdecl
 			If gdecl
+				MungDecl gdecl
+				
 				If Not TFunctionPtrType(gdecl.ty) Then
 				If Not TFunctionPtrType(gdecl.ty) Then
 					Emit "extern "+TransRefType( gdecl.ty, "" )+" "+gdecl.munged+";"	'forward reference...
 					Emit "extern "+TransRefType( gdecl.ty, "" )+" "+gdecl.munged+";"	'forward reference...
 				Else
 				Else

+ 21 - 16
decl.bmx

@@ -139,7 +139,6 @@ Type TDecl
 
 
 	Method Semant()
 	Method Semant()
 		If IsSemanted() Return
 		If IsSemanted() Return
-'DebugLog "Semant : " + ident
 		
 		
 		If IsSemanting() Err "Cyclic declaration of '"+ident+"'."
 		If IsSemanting() Err "Cyclic declaration of '"+ident+"'."
 		
 		
@@ -319,6 +318,10 @@ Type TConstDecl Extends TValDecl
 		End If
 		End If
 	End Method
 	End Method
 	
 	
+	Method ToString$()
+		Return "Const "+Super.ToString()
+	End Method
+
 End Type
 End Type
 
 
 Type TVarDecl Extends TValDecl
 Type TVarDecl Extends TValDecl
@@ -940,7 +943,6 @@ Type TFuncDecl Extends TBlockDecl
 	End Method
 	End Method
 
 
 	Method OnSemant()
 	Method OnSemant()
-
 		'semant ret type
 		'semant ret type
 		If Not retTypeExpr Then
 		If Not retTypeExpr Then
 			If Not retType Then ' may have previously been set (if this is a function pointer)
 			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
 'DebugLog "Checking Class : " + sclass.ident
 				Local found:Int
 				Local found:Int
 				For Local decl:TFuncDecl=EachIn sclass.FuncDecls( )
 				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
 					If decl.ident.ToLower() = ident.ToLower() Then
+'DebugLog "Method = " + decl.ident
 					
 					
 						If ident.ToLower() = "new" Continue
 						If ident.ToLower() = "new" Continue
 'If ident = "Create" DebugStop
 'If ident = "Create" DebugStop
 						found=True
 						found=True
+
+						If Not decl.IsSemanted() Then
+							decl.Semant
+						End If
+
 						If EqualsFunc( decl ) 
 						If EqualsFunc( decl ) 
 'DebugLog "Found"
 'DebugLog "Found"
 							overrides=TFuncDecl( decl.actual )
 							overrides=TFuncDecl( decl.actual )
@@ -1367,8 +1374,6 @@ End Rem
 	
 	
 	Method OnSemant()
 	Method OnSemant()
 
 
-		'Print "Semanting "+ToString()
-		
 		PushEnv Self
 		PushEnv Self
 
 
 		'If Not IsTemplateInst()
 		'If Not IsTemplateInst()
@@ -1481,24 +1486,24 @@ End Rem
 		End If
 		End If
 	End Method
 	End Method
 	
 	
-	Method Semant()
-		If IsSemanted() Return
+	Method SemantParts()
+'		If IsSemanted() Return
 		
 		
-		Super.Semant()
+'		Super.Semant()
 		
 		
 		For Local decl:TConstDecl = EachIn Decls()
 		For Local decl:TConstDecl = EachIn Decls()
 			decl.Semant()
 			decl.Semant()
 		Next
 		Next
-		
+
 		For Local decl:TGlobalDecl = EachIn Decls()
 		For Local decl:TGlobalDecl = EachIn Decls()
 			decl.Semant()
 			decl.Semant()
 		Next
 		Next
 
 
 		' NOTE : we can't semant functions here as they cause cyclic errors.
 		' 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
 	End Method
 	
 	
 	'Ok, this dodgy looking beast 'resurrects' methods that may not currently be alive, but override methods that ARE.
 	'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() + "'"
 						Err "Unable to convert from '" + args[i].exprType.ToString() + "()' to '" + funcDecl.argDecls[i].ty.ToString() + "'"
 					End If
 					End If
 				End If
 				End If
-
 				args[i]=args[i].Cast( funcDecl.argDecls[i].ty )
 				args[i]=args[i].Cast( funcDecl.argDecls[i].ty )
 			Else If funcDecl.argDecls[i].init
 			Else If funcDecl.argDecls[i].init
 				args[i]=funcDecl.argDecls[i].init
 				args[i]=funcDecl.argDecls[i].init
@@ -500,7 +499,7 @@ Type TInvokeMemberExpr Extends TExpr
 
 
 		If Not decl.IsSemanted() decl.Semant()
 		If Not decl.IsSemanted() decl.Semant()
 		exprType=decl.retType
 		exprType=decl.retType
-		
+
 		args=SemantArgs( args )
 		args=SemantArgs( args )
 		args=CastArgs( args,decl )
 		args=CastArgs( args,decl )
 
 
@@ -865,8 +864,14 @@ Type TCastExpr Extends TExpr
 			exprType = ty
 			exprType = ty
 			Return Self
 			Return Self
 		End If
 		End If
+		
+		If TArrayType(ty) And TObjectType(src) And TObjectType(src).classDecl.ident = "Array" Then
+			exprType = ty
+			Return Self
+		End If
 
 
 		If Not exprType
 		If Not exprType
+			DebugStop
 			Err "Cannot convert from "+src.ToString()+" to "+ty.ToString()+"."
 			Err "Cannot convert from "+src.ToString()+" to "+ty.ToString()+"."
 		EndIf
 		EndIf
 
 

+ 19 - 8
parser.bmx

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