2
0
Эх сурвалжийг харах

Added Variant.EnumValue and TypeInfo.MakeEnum.

Mark Sibly 7 жил өмнө
parent
commit
34825f8b19

+ 15 - 12
modules/monkey/native/bbtypeinfo.cpp

@@ -8,7 +8,7 @@ namespace{
 }
 
 #define BB_PRIM_GETTYPE( TYPE,ID ) bbTypeInfo *bbGetType( TYPE const& ){ \
-	static bbPrimTypeInfo info( ID ); \
+	static bbPrimTypeInfo<TYPE> info( ID ); \
 	return &info; \
 }
 
@@ -70,12 +70,20 @@ bbArray<bbTypeInfo*> bbTypeInfo::interfaceTypes(){
 	
 bbBool bbTypeInfo::extendsType( bbTypeInfo *type ){
 	return this==type;
-//	bbRuntimeError( "Type '"+name+"' is not a class or interface type" );
-//	return false;
 }
 	
 bbArray<bbDeclInfo*> bbTypeInfo::getDecls(){
-	bbRuntimeError( "Type '"+name+"' is not a class or interface type!!" );
+	bbRuntimeError( "Type '"+name+"' is not a class or interface type" );
+	return {};
+}
+
+bbVariant bbTypeInfo::makeEnum( int ){
+	bbRuntimeError( "Type '"+name+"' is not an enum type" );
+	return {};
+}
+
+int bbTypeInfo::getEnum( bbVariant ){
+	bbRuntimeError( "Type '"+name+"' is not an enum type" );
 	return {};
 }
 
@@ -193,14 +201,6 @@ bbTypeInfo *bbObject::typeof()const{
 }
 
 
-// ***** bbPrimTypeInfo *****
-
-bbPrimTypeInfo::bbPrimTypeInfo( bbString name ){
-	this->name=name;
-	this->kind="Primitive";
-}
-
-
 // ***** bbClassDecls *****
 
 bbClassDecls::bbClassDecls( bbClassTypeInfo *classType ){
@@ -285,3 +285,6 @@ bbClassTypeInfo *bbClassTypeInfo::getNamespace( bbString name ){
 	bbClassTypeInfo *nmspace=new bbClassTypeInfo( name,"Namespace" );
 	return nmspace;
 }
+
+// ***** EnumTypeInfo *****
+

+ 13 - 44
modules/monkey/native/bbtypeinfo.h

@@ -42,6 +42,11 @@ struct bbTypeInfo{
 	
 	virtual bbArray<bbDeclInfo*> getDecls();
 	
+	virtual bbVariant makeEnum( int value );
+	
+	virtual int getEnum( bbVariant );
+	
+	
 	bbDeclInfo *getDecl( bbString name );
 	
 	bbDeclInfo *getDecl( bbString name,bbTypeInfo *type );
@@ -80,9 +85,13 @@ struct bbObjectTypeInfo : public bbTypeInfo{
 	bbArray<bbDeclInfo*> getDecls();
 };
 
-struct bbPrimTypeInfo : public bbTypeInfo{
+template<class T> struct bbPrimTypeInfo : public bbTypeInfo{
 
-	bbPrimTypeInfo( bbString name );
+	bbPrimTypeInfo( bbString name ){
+		this->name=name;
+		this->kind="Primitive";
+	}
+	
 };
 
 template<class T> struct bbPointerTypeInfo : public bbTypeInfo{
@@ -91,10 +100,11 @@ template<class T> struct bbPointerTypeInfo : public bbTypeInfo{
 		this->name=bbGetType<T>()->name+" Ptr";
 		this->kind="Pointer";
 	}
-	
+
 	bbTypeInfo *pointeeType(){
 		return bbGetType<T>();
 	}
+	
 };
 
 template<class T,int D> struct bbArrayTypeInfo : public bbTypeInfo{
@@ -147,47 +157,6 @@ template<class...A> struct bbFunctionTypeInfo<void,A...> : public bbTypeInfo{
 
 };
 
-/*
-struct bbClassDecls{
-
-	bbClassDecls *_succ;
-	bbDeclInfo **_decls=0;
-	int _numDecls=0;
-
-	bbClassDecls( bbClassTypeInfo *classType );
-	
-	bbDeclInfo **decls();
-	
-	int numDecls();
-	
-	virtual bbDeclInfo **initDecls(){
-		return 0;
-	}
-};
-
-struct bbClassTypeInfo : public bbTypeInfo{
-
-	bbClassTypeInfo *_succ=0;
-	bbClassDecls *_decls=0;
-	
-	bbClassTypeInfo( bbString name,bbString kind );
-	
-	bbTypeInfo *superType();
-	
-	bbArray<bbTypeInfo*> interfaceTypes();
-	
-	bbBool extendsType( bbTypeInfo *type );
-	
-	bbArray<bbDeclInfo*> getDecls();
-	
-	bbString toString(){
-		return kind+" "+name;
-	}
-	
-	static bbClassTypeInfo *getNamespace( bbString name );
-};
-*/
-
 #define BB_GETTYPE_DECL( TYPE ) bbTypeInfo *bbGetType( TYPE const& );
 
 BB_GETTYPE_DECL( bbBool )

+ 5 - 0
modules/monkey/native/bbvariant.h

@@ -164,6 +164,11 @@ struct bbVariant{
 		return _ref<T>( 0 );
 	}
 	
+	int enumValue()const{
+	
+		return getType()->getEnum( *this );
+	}
+	
 };
 
 extern template struct bbVariant::Rep<bbBool>;

+ 14 - 0
modules/monkey/types.monkey2

@@ -423,6 +423,13 @@ Struct @Variant="bbVariant"
 	#rem monkeydoc Dynamic type of the variant value.
 	#end
 	Property DynamicType:TypeInfo()="getDynamicType"
+		
+	#rem monkeydoc Gets the integer enum value of an enum variant.
+	
+	The type of the variant must be an enum type, or a runtime error will occur.
+	
+	#end
+	Property EnumValue:Int()="enumValue"
 End
 
 #rem monkeydoc Primtive array type.
@@ -643,6 +650,13 @@ Class @TypeInfo Extends Void="bbTypeInfo"
 	#rem monkeydoc Checks whether a class extends a class.
 	#end
 	Method ExtendsType:Bool( type:TypeInfo )="extendsType"
+		
+	#rem monkeydoc Creates an enum variant from an arbitrary int value.
+
+	This type must represent an enum type.
+	
+	#end
+	Method MakeEnum:Variant( enumValue:Int )="makeEnum"
 
 	#rem monkeydoc Gets a user defined type by name.
 	

+ 13 - 9
modules/reflection/tests/enum.monkey2

@@ -7,10 +7,7 @@ Enum E
 	A,B,C
 End
 
-Function Test( e:E )
-	
-	Print "e="+Int( e )
-End
+Global G:E
 
 Function Main()
 	
@@ -24,14 +21,21 @@ Function Main()
 	
 	For Local decl:=Eachin type.GetDecls()
 		
-		Local e:=Cast<E>( decl.Get( Null ) )
+		Local e:=decl.Get( Null )
 		
-		Print decl.Name+"="+Int( e )
+		Print decl.Name+"="+e.EnumValue
 	
 	Next
 	
-	Local rtest:=TypeInfo.GetType( "test" ).GetDecl( "Test" )
+	Local g:=TypeInfo.GetType( "test" ).GetDecl( "G" )
+	
+	'one way To set an enum...
+	g.Set( Null,type.MakeEnum( E.A ) )
+	Print g.Get( Null ).EnumValue
+	
+	'another way to set an enum...!
+	g.Set( Null,type.MakeEnum( 2 ) )
+	Print g.Get( Null ).EnumValue
+	
 	
-	rtest.Invoke( Null,New Variant[]( E.C ) )
-
 End

+ 31 - 8
src/mx2cc/test.monkey2

@@ -1,13 +1,36 @@
-#Import "<std>"
+Namespace test
 
-Function SearchMods:String[](path:String)
-	
-	Local result:=New std.collections.List<String>
-	
-	Return result.ToArray<Int>()
+#Reflect test
+
+Enum E
+	A,B,C
 End
 
+Global G:E
+
 Function Main()
 	
-	SearchMods("")
-End
+	Print Typeof<E>
+
+	Local e:=E.A
+	
+	Print Typeof( e )
+	
+	Local type:=Typeof( e )
+	
+	For Local decl:=Eachin type.GetDecls()
+		
+		Local e:=decl.Get( Null )
+		
+		Print decl.Name+"="+e.EnumValue
+	
+	Next
+	
+	Local g:=TypeInfo.GetType( "test" ).GetDecl( "G" )
+	
+	g.Set( Null,type.MakeEnum( E.A ) )
+	Print g.Get( Null ).EnumValue
+
+	g.Set( Null,type.MakeEnum( 2 ) )
+	Print g.Get( Null ).EnumValue
+End

+ 10 - 1
src/mx2cc/translator_cpp.monkey2

@@ -988,8 +988,8 @@ Class Translator_CPP Extends Translator
 		Local decls:=New StringStack
 		
 		For Local it:=Eachin etype.scope.nodes
-			
 			decls.Add( "bbLiteralDecl<"+ename+">(~q"+it.Key+"~q,("+ename+")"+Cast<LiteralValue>( it.Value ).value+")" )
+'			decls.Add( "bbLiteralDecl<int>(~q"+it.Key+"~q,"+Cast<LiteralValue>( it.Value ).value+")" )
 		Next
 
 		Emit( "return bbMembers("+decls.Join( "," )+");" )
@@ -1000,6 +1000,15 @@ Class Translator_CPP Extends Translator
 		Emit( rname+"():bbEnumTypeInfo(~q"+etype.Name+"~q){" )
 		Emit( "}" )
 		
+		'MakeEnum
+		Emit( "bbVariant makeEnum( int i ){" )
+		Emit( "return bbVariant( ("+ename+")i );" )
+		Emit( "}" )
+		
+		Emit( "int getEnum( bbVariant v ){" )
+		Emit( "return (int)v.get<"+ename+">();" )
+		Emit( "}" )
+		
 		Emit( "};" )
 		
 		Emit( rname+" "+rname+"::instance;" )