Преглед на файлове

Implemented user New and Delete method generation.
Fixed calls to new instances (eg. New Type.Method() )

woollybah преди 11 години
родител
ревизия
7bf0a2146c
променени са 6 файла, в които са добавени 141 реда и са изтрити 33 реда
  1. 27 11
      ctranslator.bmx
  2. 15 13
      decl.bmx
  3. 19 4
      expr.bmx
  4. 1 0
      parser.bmx
  5. 67 0
      tests/framework/types/hierarchy_01.bmx
  6. 12 5
      type.bmx

+ 27 - 11
ctranslator.bmx

@@ -268,10 +268,20 @@ Type TCTranslator Extends TTranslator
 'DebugStop				
 				End If
 'If decl.ident = "Eof" DebugStop
-Local cdecl:TClassDecl = TObjectType(TVarExpr(lhs).decl.ty).classDecl
-Local obj:String = Bra("struct " + cdecl.munged + "_obj*")
-Local class:String = Bra("(" + obj + TransSubExpr( lhs ) + ")->clas")
-Return class + "->md_" + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
+				
+				If TVarExpr(lhs) Then
+					Local cdecl:TClassDecl = TObjectType(TVarExpr(lhs).decl.ty).classDecl
+					Local obj:String = Bra("struct " + cdecl.munged + "_obj*")
+					Local class:String = Bra("(" + obj + TransSubExpr( lhs ) + ")->clas")
+					Return class + "->md_" + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
+				Else If TNewObjectExpr(lhs) Then
+DebugStop
+					Local cdecl:TClassDecl = TNewObjectExpr(lhs).classDecl
+					Local class:String = cdecl.munged
+					Return class + ".md_" + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
+				Else
+					InternalErr
+				End If
 				'Return TransSubExpr( lhs )+"->"+decl.munged+TransArgs( args,decl )
 				'Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
 			End If
@@ -375,7 +385,7 @@ Return class + "->md_" + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
 	End Method
 		
 	Method TransSelfExpr$( expr:TSelfExpr )
-		Return "this"
+		Return "o"
 	End Method
 	
 	Method TransCastExpr$( expr:TCastExpr )
@@ -1472,9 +1482,12 @@ End Rem
 			Emit fld
 		Next
 
-		Emit "// TODO: implement our own New() code"
-		' TODO
-		
+		Local decl:TFuncDecl = classDecl.FindFuncDecl("New")
+		If decl Then
+			decl.Semant
+			EmitBlock decl
+		End If
+
 		'
 		Emit "}"
 	End Method
@@ -1490,9 +1503,12 @@ End Rem
 			Emit "int _" + classid + "_Delete(BBObject *o) {"
 		End If
 		
-		Emit "// TODO: implement our own Delete() code"
-		' TODO
-	
+		Local decl:TFuncDecl = classDecl.FindFuncDecl("Delete")
+		If decl Then
+			decl.Semant
+			EmitBlock decl
+		End If
+		
 		
 		' field cleanup
 		For Local decl:TFieldDecl=EachIn classDecl.Decls()

+ 15 - 13
decl.bmx

@@ -875,23 +875,25 @@ Type TFuncDecl Extends TBlockDecl
 		If cdecl sclass=TClassDecl( cdecl.superClass )
 		
 		'prefix call to super ctor if necessary
-		If IsCtor() And superCtor=Null 
-			If sclass.FindFuncDecl( "new", Null )
-				superCtor=New TInvokeSuperExpr.Create( "new" )
-				stmts.AddFirst New TExprStmt.Create( superCtor )
-			EndIf
-		EndIf
+'		If IsCtor() And superCtor=Null And sclass
+'			If sclass.FindFuncDecl( "new", Null )
+'				superCtor=New TInvokeSuperExpr.Create( "new" )
+'				stmts.AddFirst New TExprStmt.Create( superCtor )
+'			EndIf
+'		EndIf
 		
 		'append a return statement if necessary
 		If Not IsExtern() And Not TVoidType( retType ) And Not TReturnStmt( stmts.Last() )
-			Local stmt:TReturnStmt
-			If IsCtor()
-				stmt=New TReturnStmt.Create( Null )
-			Else
+			If Not isCtor() And Not (isMethod() And ident.ToLower() = "delete") 
+				Local stmt:TReturnStmt
+			'If IsCtor()
+			'	stmt=New TReturnStmt.Create( Null )
+			'Else
 				stmt=New TReturnStmt.Create( New TConstExpr.Create( retType,"" ) )
-			EndIf
-			stmt.errInfo=errInfo
-			stmts.AddLast stmt
+			'EndIf
+				stmt.errInfo=errInfo
+				stmts.AddLast stmt
+			End If
 		EndIf
 
 		'check we exactly match an override

+ 19 - 4
expr.bmx

@@ -468,6 +468,9 @@ Type TNewObjectExpr Extends TExpr
 	Method Semant:TExpr()
 		If exprType Return Self
 		
+		Local it:TIdentType = TIdentType(ty)
+		Local iArgs:TExpr[] = CopyArgs(args)
+
 		ty=ty.Semant()
 		args=SemantArgs( args )
 		
@@ -485,16 +488,28 @@ Type TNewObjectExpr Extends TExpr
 
 		If classDecl.IsExtern()
 			If args Err "No suitable constructor found for class "+classDecl.ToString()+"."
-		Else
+'		Else
 'DebugStop
-			ctor=classDecl.FindFuncDecl( "new",args )
-			If Not ctor	Err "No suitable constructor found for class "+classDecl.ToString()+"."
-			args=CastArgs( args,ctor )
+'			ctor=classDecl.FindFuncDecl( "new",args )
+'			If Not ctor	Err "No suitable constructor found for class "+classDecl.ToString()+"."
+'			args=CastArgs( args,ctor )
 		EndIf
 		
 		classDecl.attrs:|CLASS_INSTANCED
 
 		exprType=ty
+		
+		If it Then
+			Local i:Int=it.ident.FindLast( "." )
+			If i > 0 Then
+				Local fdecl:TFuncDecl = classDecl.FindFuncDecl(it.ident[i+1..], iArgs)
+				If fdecl Then
+					Return New TInvokeMemberExpr.Create( Self,fdecl, iArgs ).Semant()
+				End If
+			End If
+		End If
+		
+		
 		Return Self
 	End Method
 	

+ 1 - 0
parser.bmx

@@ -817,6 +817,7 @@ Type TParser
 		Case "."
 			expr=New TScopeExpr.Create( _module )
 		Case "new"
+'DebugStop
 			NextToke
 			Local ty:TType=ParseType()
 			If CParse( "[" )

+ 67 - 0
tests/framework/types/hierarchy_01.bmx

@@ -0,0 +1,67 @@
+SuperStrict
+
+Framework BRL.StandardIO
+
+
+Local elise:TVehicle = New TSportsCar.Create("Lotus", "Elise")
+Local esprit:TVehicle = New TSportsCar.Create("Lotus", "Esprit")
+TSportsCar(esprit).SetTurbo()
+
+Print elise.Dump()
+Print esprit.Dump()
+
+
+Type TVehicle
+
+	Field make:String
+	Field model:String
+	
+	Method Create:TVehicle(make:String, model:String)
+		Self.make = make
+		Self.model = model
+		Return Self
+	End Method
+	
+	Method Wheels:Int() Abstract
+	
+	Method Dump:String()
+		Return make + " " + model
+	End Method
+
+End Type
+
+
+Type TMotorbike Extends TVehicle
+
+	Method Wheels:Int()
+		Return 2
+	End Method
+
+End Type
+
+Type TCar Extends TVehicle
+
+	Method Wheels:Int()
+		Return 4
+	End Method
+
+End Type
+
+
+Type TSportsCar Extends TCar
+
+	Field turbo:Int
+	
+	Method SetTurbo()
+		turbo = True
+	End Method
+
+	Method Dump:String()
+		Local t:String
+		If Not turbo Then
+			t = "No "
+		End If
+		Return Super.Dump() + " : " + t + "Turbo"
+	End Method
+
+End Type

+ 12 - 5
type.bmx

@@ -484,11 +484,18 @@ Type TIdentType Extends TType
 			tyid=ident
 			ty=_env.FindType( tyid,targs )
 		Else
-			Local modid$=ident[..i]
-			Local mdecl:TModuleDecl=_env.FindModuleDecl( modid )
-			If Not mdecl Err "Module '"+modid+"' not found"
-			tyid=ident[i+1..]
-			ty=mdecl.FindType( tyid,targs )
+			' try scope search first
+			tyid=ident[..i]
+			ty=_env.FindType( tyid,targs )
+			
+			If Not ty Then
+				' no? now try module search
+				Local modid$=ident[..i]
+				Local mdecl:TModuleDecl=_env.FindModuleDecl( modid )
+				If Not mdecl Err "Module '"+modid+"' not found"
+				tyid=ident[i+1..]
+				ty=mdecl.FindType( tyid,targs )
+			End If
 		EndIf
 		If Not ty Err "Type '"+tyid+"' not found"
 		Return ty