Browse Source

Fix for enum params in overloaded funcs and cleanups.

Mark Sibly 9 years ago
parent
commit
95321ae9f7

+ 1 - 1
src/mx2cc/class.monkey2

@@ -666,7 +666,7 @@ Class ClassScope Extends Scope
 		Next
 		If args args="<"+args.Slice( 1 )+">"
 		
-		If ctype.cdecl.ident.StartsWith( "@" ) Return ctype.cdecl.ident.Slice( 1  ).Capitalize()+args
+		If ctype.cdecl.ident.StartsWith( "@" ) Return ctype.cdecl.ident.Slice( 1 ).Capitalize()+args
 		
 		Return outer.Name+"."+ctype.cdecl.ident+args
 	End

+ 3 - 1
src/mx2cc/decl.monkey2

@@ -123,8 +123,10 @@ Class FileDecl Extends Decl
 	Field exhfile:String	
 	Field hfile:String
 	Field cfile:String
-	Field functions:=New Stack<FuncValue>
+	
+	Field enums:=New Stack<EnumType>
 	Field classes:=New Stack<ClassType>
 	Field globals:=New Stack<VarValue>
+	Field functions:=New Stack<FuncValue>
 
 End

+ 20 - 36
src/mx2cc/enum.monkey2

@@ -15,14 +15,16 @@ End
 Class EnumType Extends Type
 
 	Field edecl:EnumDecl
+	Field transFile:FileDecl
+
 	Field scope:EnumScope
-	
-	Field superType:EnumType
 	Field nextInit:Int
 	
 	Method New( edecl:EnumDecl,outer:Scope )
 		Self.pnode=edecl
 		Self.edecl=edecl
+		Self.transFile=outer.FindFile().fdecl
+		
 		Self.scope=New EnumScope( Self,outer )
 	End
 	
@@ -31,7 +33,7 @@ Class EnumType Extends Type
 	End
 	
 	Property Name:String() Override
-		Return scope.Name+"."+edecl.ident
+		Return scope.Name
 	End
 	
 	Property TypeId:String() Override
@@ -40,27 +42,6 @@ Class EnumType Extends Type
 	
 	Method OnSemant:SNode() Override
 	
-		If edecl.superType
-			Try
-				superType=TCast<EnumType>( edecl.superType.Semant( scope ) )
-				
-				If Not superType Or superType.edecl.kind<>"enum"
-					Throw New SemantEx( "Cant find super type "+edecl.superType.ToString(),edecl )
-				Endif
-				
-				nextInit=superType.nextInit
-				
-			Catch ex:ParseEx
-			End
-			
-		Endif
-	
-'		Local escope:=scope
-'		If edecl.IsExtern
-'			escope=escope.FindFile()
-'			If edecl.IsPublic escope=escope.outer
-'		Endif
-		
 		For Local decl:=Eachin edecl.members
 		
 			Local vdecl:=Cast<VarDecl>( decl )
@@ -70,6 +51,7 @@ Class EnumType Extends Type
 				Local symbol:=vdecl.symbol
 				If Not symbol symbol="@"+vdecl.ident			
 				Local value:=New LiteralValue( Self,symbol )
+				
 				scope.Insert( decl.ident,value )
 				
 			Else
@@ -85,12 +67,20 @@ Class EnumType Extends Type
 				Endif
 				
 				Local value:=New LiteralValue( Self,String( nextInit  ) )
+				
 				scope.Insert( decl.ident,value )
+				
 				nextInit+=1
 				
 			Endif
 		Next
 		
+		If Not edecl.IsExtern And Not scope.IsGeneric
+	
+			scope.FindFile().fdecl.enums.Push( Self )
+		
+		Endif
+		
 		Return Self
 	End
 	
@@ -99,8 +89,6 @@ Class EnumType Extends Type
 		Local node:=scope.GetNode( ident )
 		If node Return node
 		
-		If superType Return superType.FindNode( ident )
-		
 		Return Null
 	End
 	
@@ -114,15 +102,6 @@ Class EnumType Extends Type
 			If ptype.IsIntegral Return MAX_DISTANCE
 			Return -1
 		Endif
-	
-		Local etype:=TCast<EnumType>( type )
-		If Not etype Return -1
-		
-		Local dist:=0
-		While etype
-			If etype=Self Return dist
-			etype=etype.superType
-		Wend
 		
 		Return -1
 	End
@@ -132,11 +111,16 @@ End
 Class EnumScope Extends Scope
 
 	Field etype:EnumType
-
+	
 	Method New( etype:EnumType,outer:Scope )
 		Super.New( outer )
 		
 		Self.etype=etype
 	End
+	
+	Property Name:String() Override
+	
+		Return outer.Name+"."+etype.edecl.ident
+	End
 
 End

+ 11 - 2
src/mx2cc/mung.monkey2

@@ -122,6 +122,13 @@ Function ScopeName:String( scope:Scope )
 		Return sym
 	End
 	
+	Local escope:=Cast<EnumScope>( scope )
+	If escope
+		Local etype:=escope.etype
+		Local sym:=ScopeName( scope.outer )+"_"+MungIdent( etype.edecl.ident )
+		Return sym
+	Endif
+	
 	Return ScopeName( scope.outer )
 End
 
@@ -136,12 +143,14 @@ Function EnumName:String( etype:EnumType )
 	
 	If edecl.IsExtern Return edecl.ident
 	
-	Return "bbInt"
+	Return "t_"+ScopeName( etype.scope )
 End
 
 Function EnumValueName:String( etype:EnumType,value:String )
 
-	If Not value.StartsWith( "@" ) Return value
+	If Not value.StartsWith( "@" )
+		Return EnumName( etype )+"("+value+")"
+	Endif
 	
 	value=value.Slice( 1 )
 	

+ 1 - 1
src/mx2cc/mx2.monkey2

@@ -48,4 +48,4 @@ Using lib.c
 ' 3) edit .sh and .bat files to use new version (common.sh, common.bat)
 ' 4) ./rebuildall
 '
-Const MX2CC_VERSION:="1.0.0"
+Const MX2CC_VERSION:="1.0.1"

+ 4 - 4
src/mx2cc/mx2cc.monkey2

@@ -18,17 +18,17 @@ Using mx2..
 
 Global StartDir:String
 
-'Const TestArgs:="mx2cc makemods"
+Const TestArgs:="mx2cc makemods"
 
-Const TestArgs:="mx2cc makedocs monkey std mojo"
+'Const TestArgs:="mx2cc makedocs monkey std mojo"
 
-'Const TestArgs:="mx2cc makeapp src/mx2new/test.monkey2"
+'Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
 
 'Const TestArgs:="mx2cc makeapp src/ted2/ted2.monkey2"
 
 'Const TestArgs:="mx2cc makemods -clean -config=release monkey libc miniz stb-image hoedown std"
 
-'Const TestArgs:="mx2cc makeapp -verbose -target=desktop -config=release src/mx2new/mx2cc.monkey2"
+'Const TestArgs:="mx2cc makeapp -verbose -target=desktop -config=release src/mx2cc/mx2cc.monkey2"
 
 Function Main()
 

+ 1 - 1
src/mx2cc/scope.monkey2

@@ -33,7 +33,7 @@ Class Scope
 		Return ""
 	End
 	
-	'Is generic scope? ie: scope has access to generic types.
+	'Is generic scope? ie: does scope have access to any generic types?
 	'
 	Property IsGeneric:Bool() Virtual
 	

+ 13 - 2
src/mx2cc/stmtexpr.monkey2

@@ -76,9 +76,20 @@ Class AssignStmtExpr Extends StmtExpr
 	Method OnSemant:Stmt( block:Block ) Override
 	
 		Local op:=Self.op
-	
 		Local lhs:=Self.lhs.Semant( block )
-		Local rhs:=Self.rhs.Semant( block )
+		Local rhs:Value=Null
+		
+		If op<>"="
+			Local etype:=TCast<EnumType>( lhs.type )
+			If etype
+				lhs=lhs.RemoveSideEffects( block )
+				Local t:=New BinaryopExpr( op.Slice( 0,-1 ),New ValueExpr( lhs,srcpos,endpos ),Self.rhs,srcpos,endpos )
+				rhs=t.Semant( block )
+				op="="
+			Endif
+		Endif
+		
+		If not rhs rhs=Self.rhs.Semant( block )
 		
 		If op<>"=" And lhs.IsLValue
 		

+ 28 - 2
src/mx2cc/test.monkey2

@@ -1,8 +1,34 @@
 
 Namespace test
 
-#rem
-#end
+Enum MyEnum
+	X=1,Y,Z=10
+End
+
+Class Map<T>
+
+	Enum Color
+		Red,Black
+	End
+
+	Field color:Color
+		
+	Method New()
+		color=Color.Red
+	End
+End
+
+
 Function Main()
+
+	Local map:=New Map<Int>
+
+	Local x:=MyEnum.X
+	Local y:=MyEnum.Y
+	
+	Local t:=x|y
+	
+	t|=MyEnum.Z
+	
 End
 

+ 26 - 5
src/mx2cc/translator.monkey2

@@ -289,10 +289,9 @@ Class Translator
 	
 	Field _refs:=New Map<SNode,Bool>
 	
-'	Field _refs:=New StringMap<SNode>
 	Field _refsVars:=New Stack<VarValue>
 	Field _refsFuncs:=New Stack<FuncValue>
-	Field _refsTypes:=New Stack<ClassType>
+	Field _refsTypes:=New Stack<Type>
 
 	Field _incs:=New StringMap<FileDecl>
 	
@@ -316,10 +315,13 @@ Class Translator
 		Next
 		
 		EmitBr()
-		For Local ctype:=Eachin _refsTypes
+		For Local type:=Eachin _refsTypes
 		
-			If Not Included( ctype.transFile ) 
-			
+			Local ctype:=TCast<ClassType>( type )
+			If ctype
+		
+				If Included( ctype.transFile ) Continue
+				
 				Local cname:=ClassName( ctype )
 				Emit( "struct "+ClassName( ctype )+";" )
 				
@@ -329,8 +331,20 @@ Class Translator
 					Emit( "bbString bbDBType("+tname+"*);" )
 					Emit( "bbString bbDBValue("+tname+"*);" )
 				Endif
+					
+				Continue
+			Endif
+			
+			Local etype:=TCast<EnumType>( type )
+			If etype
+			
+				If Included( etype.transFile ) Continue
 				
+				Emit( "enum class "+EnumName( etype )+";" )
+				
+				Continue
 			Endif
+
 		Next
 		_refsTypes.Clear()
 		
@@ -420,6 +434,13 @@ Class Translator
 			Return
 		Endif
 		
+		Local etype:=TCast<EnumType>( type )
+		If etype
+			If AddRef( etype ) Return
+			_refsTypes.Push( etype )
+			Return
+		Endif
+		
 		Local ftype:=TCast<FuncType>( type )
 		If ftype
 			Refs( ftype.retType )

+ 25 - 17
src/mx2cc/translator_cpp.monkey2

@@ -46,6 +46,11 @@ Class Translator_CPP Extends Translator
 		
 		Emit( "// ***** Internal *****" )
 		
+		EmitBr()
+		For Local etype:=Eachin fdecl.enums
+			Emit( "enum class "+EnumName( etype )+";" )
+		Next
+		
 		EmitBr()
 		For Local ctype:=Eachin fdecl.classes
 			Emit( "struct "+ClassName( ctype )+";" )
@@ -92,7 +97,7 @@ Class Translator_CPP Extends Translator
 		BeginDeps()
 		
 		Emit( "// ***** Internal *****" )
-
+		
 		EmitBr()
 		For Local vvar:=Eachin fdecl.globals
 			Uses( vvar.type )
@@ -999,16 +1004,6 @@ Class Translator_CPP Extends Translator
 		Local lhs:=Trans( stmt.lhs )
 		Local rhs:=Trans( stmt.rhs )
 
-		Local etype:=TCast<EnumType>( stmt.lhs.type )
-		If etype And etype.edecl.IsExtern
-			If op<>"="
-				If stmt.lhs.HasSideEffects Print "Danger Will Robinson!!!!!!"
-				rhs=lhs+op.Slice( 0,-1 )+rhs
-				op="="
-			Endif
-			rhs=EnumName( etype )+"("+rhs+")"
-		Endif
-		
 		Emit( lhs+op+rhs+";" )
 	End
 
@@ -1418,10 +1413,15 @@ Class Translator_CPP Extends Translator
 		Case "not" op="!"
 		End
 		
-		Local t:=op+Trans( value.value )
-		
 		Local etype:=TCast<EnumType>( value.type )
-		If etype And etype.edecl.IsExtern t=EnumName( etype )+"("+t+")"
+
+		Local t:=Trans( value.value )
+
+		If etype t="int("+t+")"
+		
+		t=op+t
+		
+		If etype t=EnumName( etype )+"("+t+")"
 		
 		Return t
 	End
@@ -1442,8 +1442,10 @@ Class Translator_CPP Extends Translator
 			Endif
 			
 		Case "mod"
+		
 			Local ptype:=TCast<PrimType>( value.type )
 			If ptype=Type.FloatType Or ptype=Type.DoubleType Return "std::fmod("+Trans( value.lhs )+","+Trans( value.rhs )+")"
+			
 			op="%"
 		Case "and" op="&&"
 		Case "or" op="||"
@@ -1452,10 +1454,16 @@ Class Translator_CPP Extends Translator
 		Case "shr" op=">>"
 		End
 		
-		Local t:="("+Trans( value.lhs )+op+Trans( value.rhs )+")"
-		
 		Local etype:=TCast<EnumType>( value.type )
-		If etype And etype.edecl.IsExtern t=EnumName( etype )+t
+
+		Local lhs:=Trans( value.lhs )
+		Local rhs:=Trans( value.rhs )
+		
+		If etype lhs="int("+lhs+")" ; rhs="int("+rhs+")"
+		
+		Local t:="("+lhs+op+rhs+")"
+		
+		If etype t=EnumName( etype )+"("+t+")"
 		
 		Return t
 	End