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

Added initial support for "New <instance>".

woollybah 11 лет назад
Родитель
Сommit
e6950a8c82
5 измененных файлов с 77 добавлено и 11 удалено
  1. 7 3
      ctranslator.bmx
  2. 12 1
      expr.bmx
  3. 1 1
      options.bmx
  4. 46 0
      tests/framework/types/new_var_01.bmx
  5. 11 6
      type.bmx

+ 7 - 3
ctranslator.bmx

@@ -1209,10 +1209,14 @@ EndRem
 	Method TransNewObjectExpr$( expr:TNewObjectExpr )
 		Local t$
 
-		If ClassHasObjectField(expr.classDecl) Then
-			t = "bbObjectNew(&" + expr.classDecl.actual.munged + ")"
+		If expr.instanceExpr Then
+			t = "bbObjectNew(" + expr.instanceExpr.Trans() + "->clas)"
 		Else
-			t = "bbObjectAtomicNew(&" + expr.classDecl.actual.munged + ")"
+			If ClassHasObjectField(expr.classDecl) Then
+				t = "bbObjectNew(&" + expr.classDecl.actual.munged + ")"
+			Else
+				t = "bbObjectAtomicNew(&" + expr.classDecl.actual.munged + ")"
+			End If
 		End If
 		'Local t$="(new "+expr.classDecl.actual.munged+")"
 		'If expr.ctor t:+"->"+expr.ctor.actual.munged+TransArgs( expr.args,expr.ctor )

+ 12 - 1
expr.bmx

@@ -580,6 +580,7 @@ Type TNewObjectExpr Extends TExpr
 	Field args:TExpr[]
 	Field ctor:TFuncDecl
 	Field classDecl:TClassDecl
+	Field instanceExpr:TExpr
 
 	Method Create:TNewObjectExpr( ty:TType,args:TExpr[] )
 		Self.ty=ty
@@ -597,7 +598,17 @@ Type TNewObjectExpr Extends TExpr
 		Local it:TIdentType = TIdentType(ty)
 		Local iArgs:TExpr[] = CopyArgs(args)
 
-		ty=ty.Semant()
+		ty=ty.Semant(True)
+		If Not ty Then
+			' maybe it's an instance of a type ?
+			Local decl:TVarDecl = TVarDecl(_env.FindDecl(it.ident))
+			If decl And TObjectType(decl.ty) Then
+				ty = decl.ty
+				instanceExpr = New TVarExpr.Create(decl).Semant()
+			Else
+				Err "Type '"+it.ident+"' not found"
+			End If
+		End If
 		args=SemantArgs( args )
 
 		Local objTy:TObjectType=TObjectType( ty )

+ 1 - 1
options.bmx

@@ -25,7 +25,7 @@ SuperStrict
 
 Import "base.configmap.bmx"
 
-Const version:String = "0.20"
+Const version:String = "0.21"
 
 Const BUILDTYPE_APP:Int = 0
 Const BUILDTYPE_MODULE:Int = 1

+ 46 - 0
tests/framework/types/new_var_01.bmx

@@ -0,0 +1,46 @@
+SuperStrict
+
+Framework brl.standardio
+
+Local t:TType = New TType
+Global c:TType = New TType
+
+Local n:Object = cloneObject(t)
+Local m:Object = New c
+
+Local x:TXType = New TXType
+' doesn't work... yet
+Local o:Object' = New x.s
+
+If TType(t) Then
+	Print "t is a TType"
+End If
+
+If TType(n) Then
+	Print "n is a TType"
+End If
+
+If TType(m) Then
+	Print "m is a TType"
+End If
+
+If TType(o) Then
+	Print "o is a TType"
+End If
+
+
+Function cloneObject:Object(obj:Object)
+	'create a new instance of the objects type
+	 Local clone:Object = New obj
+	Return clone
+End Function
+
+Type TType
+
+End Type
+
+Type TXType
+
+	Field s:TType = New TType
+
+End Type

+ 11 - 6
type.bmx

@@ -44,7 +44,7 @@ Type TType
 		Return EqualsType( ty )
 	End Method
 	
-	Method Semant:TType()
+	Method Semant:TType(option:Int = False)
 		Return Self
 	End Method
 
@@ -560,7 +560,7 @@ Type TStringType Extends TType
 		Return cdecl
 	End Method
 	
-	Method Semant:TType()
+	Method Semant:TType(option:Int = 0)
 		GetClass()
 		Return Self
 	End Method
@@ -608,7 +608,7 @@ Type TArrayType Extends TType
 		Return (arrayType And ( TVoidType( elemType ) Or elemType.EqualsType( arrayType.elemType ) )) Or IsPointerType(ty, 0, TType.T_POINTER) <> Null Or (TObjectType( ty ) And TObjectType( ty ).classDecl.ident="Object")
 	End Method
 	
-	Method Semant:TType()
+	Method Semant:TType(option:Int = False)
 		Local ty:TType=elemType.Semant()
 		If ty<>elemType Return New TArrayType.Create( ty, dims )
 		Return Self
@@ -722,7 +722,7 @@ Type TIdentType Extends TType
 	'End Method
 	
 	
-	Method Semant:TType()
+	Method Semant:TType(ignoreNotFoundError:Int = 0)
 'If ident="obj" DebugStop
 		If Not ident Return TType.nullObjectType
 
@@ -769,7 +769,12 @@ Type TIdentType Extends TType
 				End If
 			End If
 		EndIf
-		If Not ty Err "Type '"+tyid+"' not found"
+		If Not ty Then
+			If ignoreNotFoundError Then
+				Return Null
+			End If
+			Err "Type '"+tyid+"' not found"
+		End If
 		
 		If (_flags & T_VAR) And TObjectType(ty) Then
 			ty = New TObjectType.Create(TObjectType(ty).classDecl)
@@ -918,7 +923,7 @@ Type TFunctionPtrType Extends TType
 		Return ty
 	End Method
 
-	Method Semant:TType()
+	Method Semant:TType(option:Int = False)
 		func.Semant()
 		Return Self
 	End Method