Преглед изворни кода

Added generics parameter Extends support.
Fixed ordering of parameters.

woollybah пре 8 година
родитељ
комит
6689cdad46
4 измењених фајлова са 39 додато и 11 уклоњено
  1. 12 4
      decl.bmx
  2. 1 1
      iparser.bmx
  3. 12 5
      parser.bmx
  4. 14 1
      type.bmx

+ 12 - 4
decl.bmx

@@ -2062,7 +2062,7 @@ Type TClassDecl Extends TScopeDecl
 
 	Field lastOffset:Int
 
-	Field args:String[]
+	Field args:TTemplateArg[]
 	Field superTy:TIdentType
 	Field impltys:TIdentType[]
 
@@ -2080,7 +2080,7 @@ Type TClassDecl Extends TScopeDecl
 
 	'Global nullObjectClass:TClassDecl=New TNullDecl.Create( "{NULL}",Null,Null,Null,DECL_ABSTRACT|DECL_EXTERN )
 	
-	Method Create:TClassDecl( ident$,args:String[],superTy:TIdentType,impls:TIdentType[],attrs:Int )
+	Method Create:TClassDecl( ident$,args:TTemplateArg[],superTy:TIdentType,impls:TIdentType[],attrs:Int )
 		Self.ident=ident
 		Self.args=args
 		Self.superTy=superTy
@@ -2089,7 +2089,6 @@ Type TClassDecl Extends TScopeDecl
 		Self.objectType=New TObjectType.Create( Self )
 		If args
 			instances=New TList
-			'instances.AddLast Self
 		EndIf
 		Return Self
 	End Method
@@ -2213,7 +2212,16 @@ End Rem
 		instances.AddLast inst
 
 		For Local i:Int=0 Until args.Length
-			inst.InsertDecl New TAliasDecl.Create( args[i].ToString(),instArgs[i],0 )
+		
+			' ensure parameter types are compatible
+			If args[i].superTy Then
+				args[i].superTy = args[i].superTy.Semant()
+				If Not instArgs[i].EqualsType(args[i].superTy) And Not instArgs[i].ExtendsType(args[i].superTy) Then
+					Err "Type parameter '" + instArgs[i].ToString() + "' is not within its bound; should extend '" + args[i].superTy.ToString() + "'"
+				End If
+			End If
+		
+			inst.InsertDecl New TAliasDecl.Create( args[i].ident,instArgs[i],0 )
 		Next
 
 		For Local decl:TDecl=EachIn _decls

+ 1 - 1
iparser.bmx

@@ -508,7 +508,7 @@ Type TIParser
 		'If toke Parse toke
 		
 		Local id$=ParseIdent()
-		Local args:String[]
+		Local args:TTemplateArg[]
 		Local superTy:TIdentType
 		Local imps:TIdentType[]
 

+ 12 - 5
parser.bmx

@@ -2939,7 +2939,7 @@ End Rem
 		If toke Parse toke
 
 		Local id$=ParseIdent()
-		Local args:TStack = New TStack
+		Local args:TList = New TList
 		Local superTy:TIdentType
 		Local imps:TIdentType[]
 		Local meta:TMetadata
@@ -2968,7 +2968,15 @@ End Rem
 				'If args.Length=nargs args=args + New TClassDecl[10]
 				'args[nargs]=decl
 				'nargs:+1
-				args.Push ParseIdent()
+				Local arg:TTemplateArg = New TTemplateArg
+				arg.ident = ParseIdent()
+				
+				If CParse("extends") Then
+					arg.superTy = ParseIdentType()
+				End If
+				
+				args.AddLast arg
+
 			Until Not CParse(",")
 			'args=args[..nargs]
 
@@ -3088,9 +3096,9 @@ End Rem
 		meta = ParseMetaData()
 
 		
-		Local sargs:String[] = New String[args.Count()]
+		Local sargs:TTemplateArg[] = New TTemplateArg[args.Count()]
 		Local i:Int = 0
-		For Local arg:String = EachIn args
+		For Local arg:TTemplateArg = EachIn args
 			sargs[i] = arg
 			i :+ 1
 		Next
@@ -4224,4 +4232,3 @@ Type TCastDets
 	Field api:String
 	
 End Type
-

+ 14 - 1
type.bmx

@@ -1539,7 +1539,7 @@ Type TIdentType Extends TType
 	
 	
 	Method Semant:TType(ignoreNotFoundError:Int = 0)
-'If ident="TStack" DebugStop
+'If ident="IPair" DebugStop
 		If Not ident Return TType.nullObjectType
 
 		Local targs:TType[args.Length]
@@ -2009,3 +2009,16 @@ Type TLParamType Extends TParamType
 	End Method
 
 End Type
+
+Type TTemplateArg
+	Field ident:String
+	Field superTy:TType
+	
+	Method ToString:String()
+		Local s:String = ident
+		If superTy Then
+			s :+ " Extends " + superTy.ToString()
+		End If
+	End Method
+End Type
+