Browse Source

WIP generic properties.

Mark Sibly 8 years ago
parent
commit
45cd1a120a

+ 1 - 1
src/mx2cc/class.monkey2

@@ -451,7 +451,7 @@ Class ClassType Extends Type
 		
 		Return node
 	End
-		
+	
 	Method FindType:Type( ident:String ) Override
 	
 		Local type:=FindType2( ident )

+ 6 - 3
src/mx2cc/expr.monkey2

@@ -150,11 +150,14 @@ Class IdentExpr Extends Expr
 	End
 	
 	Method OnSemant:Value( scope:Scope ) Override
-	
+		
 		Local value:=scope.FindValue( ident )
 		If value Return value
 		
-		Throw New SemantEx( "Identifier '"+ident+"' not found" )
+		Local type:=scope.FindType( ident )
+		If type Return New TypeValue( type )
+		
+		Throw New SemantEx( "Identifier '"+ident+"' Not found" )
 	End
 	
 	Method OnSemantType:Type( scope:Scope ) Override
@@ -184,7 +187,7 @@ Class MemberExpr Extends Expr
 	End
 	
 	Method OnSemant:Value( scope:Scope ) Override
-	
+		
 		Local value:=expr.SemantRValue( scope )
 		Local tv:=Cast<TypeValue>( value )
 	

+ 13 - 7
src/mx2cc/func.monkey2

@@ -449,16 +449,22 @@ Class FuncValue Extends Value
 			transFile.functions.Push( Self )
 			
 		Else
-		
-			If IsCtor Or IsMethod
 			
-				If fdecl.ident="new"
-					cscope.ctype.ctors.Push( Self )
-				Else
-					cscope.ctype.methods.Push( Self )
-				Endif
+			If IsCtor
+				cscope.ctype.ctors.Push( Self )
+			Elseif IsMethod
+				cscope.ctype.methods.Push( Self )
 			Endif
 		
+'			If IsCtor Or IsMethod
+			
+'				If fdecl.ident="new"
+'					cscope.ctype.ctors.Push( Self )
+'				Else
+'					cscope.ctype.methods.Push( Self )
+'				Endif
+'			Endif
+		
 			scope.transMembers.Push( Self )
 
 		Endif

+ 8 - 11
src/mx2cc/mx2cc.monkey2

@@ -38,9 +38,9 @@ Const MX2CC_VERSION_EXT:=""
 
 Global StartDir:String
 
-Const TestArgs:="mx2cc makedocs monkey std"
+'Const TestArgs:="mx2cc makedocs monkey std mojo"
  
-'Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
+Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
 
 'To build with old mx2cc...
 '
@@ -394,23 +394,20 @@ Function MakeDocs:Bool( args:String[] )
 	Next
 	
 	Local buf:=New StringStack
+	Local modsbuf:=New StringStack
 	
 	For Local modid:=Eachin EnumModules()
 
 		Local index:=LoadString( "docs/modules/"+modid+"/manual/index.js" )
-		If index
-			If buf.Length buf.Add( "," )
-			buf.Push( index )
-		Endif
+		If index buf.Push( index )
 		
 		index=LoadString( "docs/modules/"+modid+"/module/index.js" )
-		If index
-			If buf.Length buf.Add( "," )
-			buf.Push( index )
-		Endif
+		If index modsbuf.Push( index )
 	Next
 	
-	Local tree:=buf.Join( "" )
+	buf.Add( "{text:'Modules reference',children:[~n"+modsbuf.Join( "," )+"]}~n" )
+	
+	Local tree:=buf.Join( "," )
 	
 	Local page:=LoadString( "docs/new_docs_template.html" )
 	page=page.Replace( "${DOCS_TREE}",tree )

+ 5 - 0
src/mx2cc/parser.monkey2

@@ -499,6 +499,8 @@ Class Parser
 			
 			genArgs=ParseGenArgs()
 			
+			If genArgs And (flags & (DECL_SETTER|DECL_GETTER)) "Generic arguments can not appear here"
+			
 			If CParse( ":" )
 				type=Cast<FuncTypeExpr>( ParseType() )
 				If Not type Error( "Expecting function type" )
@@ -722,6 +724,7 @@ Class Parser
 		
 		Local func:=ParseFunc( flags )
 		decl.ident=func.ident
+		decl.genArgs=func.genArgs
 		
 		If func.IsGetter
 		
@@ -730,6 +733,7 @@ Class Parser
 			If Toke="setter"
 				decl.setFunc=ParseFunc( flags )
 				decl.setFunc.ident=decl.ident
+				decl.setFunc.genArgs=decl.genArgs
 			Endif
 			
 		Else If func.IsSetter
@@ -739,6 +743,7 @@ Class Parser
 			If Toke="getter"
 				decl.getFunc=ParseFunc( flags )
 				decl.getFunc.ident=decl.ident
+				decl.getFunc.genArgs=decl.genArgs
 			Endif
 			
 		Endif

+ 45 - 5
src/mx2cc/property.monkey2

@@ -3,12 +3,18 @@ Namespace mx2
 
 Class PropertyDecl Extends Decl
 
+	Field genArgs:String[]
 	Field getFunc:FuncDecl
 	Field setFunc:FuncDecl
 	
 	Method ToNode:SNode( scope:Scope ) Override
+		
+		Local types:=New Type[genArgs.Length]
+		For Local i:=0 Until types.Length
+			types[i]=New GenArgType( i,genArgs[i] )
+		Next
 	
-		Return New PropertyList( Self,scope )
+		Return New PropertyList( Self,scope,types,Null )
 	End
 
 End
@@ -24,12 +30,18 @@ Class PropertyList Extends FuncList
 	
 	Field type:Type
 	
-	Method New( pdecl:PropertyDecl,scope:Scope )
+	Field types:Type[]
+	Field instanceof:PropertyList
+	Field instances:Stack<PropertyList>
+	
+	Method New( pdecl:PropertyDecl,scope:Scope,types:Type[],instanceof:PropertyList )
 		Super.New( pdecl.ident,scope )
 		Self.pnode=pdecl
 		Self.pdecl=pdecl
 		Self.scope=scope
 		Self.cscope=Cast<ClassScope>( scope )
+		Self.types=types
+		Self.instanceof=instanceof
 	End
 	
 	Method ToString:String() Override
@@ -43,7 +55,7 @@ Class PropertyList Extends FuncList
 		
 		If pdecl.getFunc
 			Try
-				getFunc=New FuncValue( pdecl.getFunc,scope,Null,Null )
+				getFunc=New FuncValue( pdecl.getFunc,scope,types,instanceof?.getFunc )
 				getFunc.Semant()
 				type=getFunc.ftype.retType
 				If type.Equals( Type.VoidType ) Throw New SemantEx( "Property '"+pdecl.ident+"' getter has void type" )
@@ -54,9 +66,9 @@ Class PropertyList Extends FuncList
 
 		If pdecl.setFunc
 			Try
-				setFunc=New FuncValue( pdecl.setFunc,scope,Null,Null )
+				setFunc=New FuncValue( pdecl.setFunc,scope,types,instanceof?.setFunc )
 				setFunc.Semant()
-				If type And Not type.Equals( setFunc.ftype.argTypes[0] ) Throw New SemantEx( "Property '"+pdecl.ident+"' getter and setter have different types" )
+				If type And Not type.Equals( setFunc.ftype.argTypes[0] ) Throw New SemantEx( "Property '"+pdecl.ident+"' Getter And Setter have different types" )
 				If Not type type=setFunc.ftype.argTypes[0]
 				PushFunc( setFunc )
 			Catch ex:SemantEx
@@ -82,6 +94,27 @@ Class PropertyList Extends FuncList
 		Return New PropertyValue( Self,instance )
 	End
 	
+	Method GenInstance:PropertyList( types:Type[] )
+		
+		If instanceof Return instanceof.GenInstance( types )
+		
+		If types.Length<>Self.types.Length Throw New SemantEx( "Wrong number of generic type parameters" )
+		
+		If Not instances instances=New Stack<PropertyList>
+		
+		For Local inst:=Eachin instances
+			If TypesEqual( inst.types,types ) Return inst
+		Next
+		
+		Local plist:=New PropertyList( pdecl,scope,types,Self )
+		
+		instances.Add( plist )
+		
+		plist.Semant()
+		
+		Return plist
+	End
+	
 End
 
 Class PropertyValue Extends Value
@@ -158,4 +191,11 @@ Class PropertyValue Extends Value
 		Return New PropertyValue( plist,value )
 	End
 	
+'	Method GenInstance:Value( types:Type[] ) Override
+		
+'		Local plist:=Self.plist.GenInstance( types )
+		
+'		Return New PropertyValue( plist,instance )
+'	End
+	
 End

+ 30 - 14
src/mx2cc/test.monkey2

@@ -1,22 +1,38 @@
-
-#Import "test2"
-
-Class B
-
-	Method New( t:Int=0 )
-		Print "B.New"
+Class Type
+	Method M()
+		Print "HEY!"
 	End
-	
 End
 
-Class C Extends B
+Class Comp
+	
+	Const type:=New Type
+End
 
-	Method New()
-		Print "C.New"
+Class Entity
+	
+	Property P<T>:Int()
+		
+		Local t:=T.type
+		
+		t.M()
+		
+		Return 0
+		
+	Setter( t:Int )
+		
+		T.type.M()
+		
 	End
+	
 End
-
+	
 Function Main()
-
-	Local c:=New C
+	
+	Local e:=New Entity
+	
+	e.P<Comp> =10
+	
+	Print e.P<Comp>
+	
 End