فهرست منبع

Fixed/cleanedup some casting issues.

Mark Sibly 9 سال پیش
والد
کامیت
18b7f85ee2
4فایلهای تغییر یافته به همراه94 افزوده شده و 36 حذف شده
  1. 24 7
      src/mx2new/class.monkey2
  2. 1 0
      src/mx2new/expr.monkey2
  3. 36 6
      src/mx2new/test.monkey2
  4. 33 23
      src/mx2new/type.monkey2

+ 24 - 7
src/mx2new/class.monkey2

@@ -452,13 +452,6 @@ Class ClassType Extends Type
 		Return inst
 	End
 	
-	Method ExtendsType:Bool( type:Type ) Override
-	
-		Local t:=DistanceToType( type )
-		
-		Return t>=0 And t<MAX_DISTANCE
-	End
-	
 	Method DistanceToType:Int( type:Type ) Override
 	
 		If type=Self Return 0
@@ -497,6 +490,30 @@ Class ClassType Extends Type
 		
 		Return -1
 	End
+
+	Method ExtendsType:Bool( type:Type ) Override
+	
+		Local t:=DistanceToType( type )
+		
+		Return t>=0 And t<MAX_DISTANCE
+	End
+	
+	Method CanCastToType:Bool( type:Type ) Override
+	
+		Local ctype:=TCast<ClassType>( type )
+		If Not ctype Return False
+		
+		Select cdecl.kind
+		Case "class"
+			If ctype.cdecl.kind="class" Return ctype.ExtendsType( Self )
+			If ctype.cdecl.kind="interface" Return True
+		Case "interface"
+			If ctype.cdecl.kind="class" Return True
+			If ctype.cdecl.kind="interface" Return True
+		End
+		
+		Return False
+	End
 	
 	Method InferType:Type( type:Type,infered:Type[] ) Override
 	

+ 1 - 0
src/mx2new/expr.monkey2

@@ -486,6 +486,7 @@ Class CastExpr Extends Expr
 		
 		Local value:=Self.expr.Semant( scope )
 		
+		'Cast operator?		
 		Local castOp:=value.FindValue( "cast" )
 		If castOp value=castOp.Invoke( Null )
 

+ 36 - 6
src/mx2new/test.monkey2

@@ -1,13 +1,43 @@
 
+Namespace test
+
+#Import "<std>"
+
+Using std..
+
+Interface I1
+End
+
+Interface I2
+	Method Test()
+End
+
+Class C1 Implements I1
+End
+
+Class C2 Implements I2
+End
+
+Class C3 Extends C1 Implements I2
+	Method Test()
+		Print "C3.Test"
+	End
+End
+
+Enum E
+	X,Y,Z
+End
+
 Function Main()
 
-	Local p:Void()
+	Local i1:I1=New C3
 	
-	p=Lambda()
-	End
+	Local i2:I2=Cast<I2>( i1 )
 	
-	Local i:Int Ptr
-	i+=1
+	i2.Test()
 	
-End
+	Local e:E=E.X
+	
+	Local t:Byte=e
 
+End

+ 33 - 23
src/mx2new/type.monkey2

@@ -106,23 +106,24 @@ Class Type Extends SNode
 		Return type=Self
 	End
 	
-	Method ExtendsType:Bool( type:Type ) Virtual
-		Return Equals( type )
-	End
-	
 	Method DistanceToType:Int( type:Type ) Virtual
 		If Equals( type ) Return 0
 		Return -1
 	End
 	
+	Method ExtendsType:Bool( type:Type ) Virtual
+		Return Equals( type )
+	End
+	
+	Method CanCastToType:Bool( type:Type ) Virtual
+		Return Equals( type )
+	End
+
 	Method InferType:Type( type:Type,args:Type[] ) Virtual
 		If Equals( type ) Return type	
 		Return Null
 	End
 	
-	Method CanCastToType:Bool( type:Type ) Virtual
-		Return DistanceToType( type )>=0 Or type.CanCastToType( Self )
-	End
 End
 
 Class ProxyType Extends Type
@@ -167,14 +168,14 @@ Class ProxyType Extends Type
 		Return _alias.Equals( type )
 	End
 	
-	Method ExtendsType:Bool( type:Type ) Override
-		Return _alias.ExtendsType( type )
-	End
-	
 	Method DistanceToType:Int( type:Type ) Override
 		Return _alias.DistanceToType( type )
 	End
 	
+	Method ExtendsType:Bool( type:Type ) Override
+		Return _alias.ExtendsType( type )
+	End
+	
 	Method InferType:Type( type:Type,args:Type[] ) Override
 		Return _alias.InferType( type,args )
 	End
@@ -244,21 +245,11 @@ Class PrimType Extends Type
 		Return type=Self 
 	End
 	
-	Method ExtendsType:Bool( type:Type ) Override
-	
-		Return type=Self Or ctype.DistanceToType( type )>=0
-	End
-	
 	Method DistanceToType:Int( type:Type ) Override
 	
 		If type=Self Return 0
-
-		Local ptype:=TCast<PrimType>( type )
-		If ptype
-			If ptype=BoolType Return MAX_DISTANCE
-			If IsNumeric And (ptype=StringType Or ptype.IsNumeric) Return MAX_DISTANCE
-			If Self=BoolType And ptype.IsNumeric Return MAX_DISTANCE
-		Endif
+		
+		If TCast<PrimType>( type ) Return MAX_DISTANCE
 		
 		Select type
 		Case CStringClass,WStringClass,Utf8StringClass
@@ -268,6 +259,25 @@ Class PrimType Extends Type
 		Return -1
 	End
 	
+	Method ExtendsType:Bool( type:Type ) Override
+	
+		Return type=Self Or ctype.DistanceToType( type )>=0
+	End
+	
+	Method CanCastToType:Bool( type:Type ) Override
+		
+		'prim->prim
+		If TCast<PrimType>( type ) Return True
+		
+		'integral->enum
+		If IsIntegral And TCast<EnumType>( type ) Return True
+		
+		'integral->pointer
+		If IsIntegral And TCast<PointerType>( type ) Return True
+		
+		Return False
+	End
+	
 	Property IsNumeric:Bool()
 		Select Self
 		Case FloatType,DoubleType Return True