Jelajahi Sumber

Added simple multidimensional array initializers.

Mark Sibly 7 tahun lalu
induk
melakukan
4dd0c4f8b2

+ 5 - 3
modules/monkey/native/bbarray.h

@@ -81,9 +81,11 @@ template<class T,int D> struct bbArray{
 		void *p=bbGC::malloc( sizeof( Rep )+sizes[D-1]*sizeof(T) );
 			
 		_rep=new( p ) Rep( sizes );
-			
-		int i=0;
-		for( auto it=init.begin();it!=init.end();++it ) _rep->_data[i++]=*it;
+		
+		int i=0,n=sizes[D-1];
+		bbDebugAssert( n==init.size(),"Incorrect number of array initializers" );
+
+		for( auto it=init.begin();i<n; )_rep->_data[i++]=*it++;
 			
 		bbGC::endCtor( _rep );
 	}

+ 6 - 1
modules/monkey/newdocs/language/arrays.md

@@ -34,13 +34,18 @@ Declaring an array does not actually create an array. To do that you must use `N
 
 The syntax for creating an initialized array is:
 
-`New` _ElementType_[]( _Element0_`,`_Element1_`,`...etc )
+`New` _ElementType_`[` _DimensionSizes_ `]`( _Element0_`,`_Element1_`,`...etc )
+
+One dimesnional arrays can omit DimensionSizes when creating an initialized array:
+
+`New` _ElementType_`[` `]`( _Element0_`,`_Element1_`,`...etc )
 
 Here are some examples:
 
 ```
 Local ints:Int[]=New Int[10]				'Creates a ten element integer array.
 Local flts:=New Float[]( 1.0,3,5.1,7,9.2 )	'Creates a 5 element float array initialized to 1.0,3,5.1,7,9.2 
+Local flts2:=New Float[2,2]( 1,2,3,4 )		'Creates a 2x2 element float array initialized to 1,2,3,4
 ```
 
 @#### Iterating through arrays

+ 10 - 0
src/mx2cc/expr.monkey2

@@ -475,7 +475,16 @@ Class NewArrayExpr Extends Expr
 		If Not atype SemantError( "NewArrayExpr.OnSemant()" )
 		
 		If atype.IsGeneric Return New LiteralValue( atype,"" )
+
+		Local sizes:=SemantArgs( Self.sizes,scope )
+		sizes=UpCast( sizes,Type.IntType )
+
+		Local inits:=SemantArgs( Self.inits,scope )
+		inits=UpCast( inits,atype.elemType )
+		
+		Return New NewArrayValue( atype,sizes,inits )
 		
+		#rem
 '		If atype.elemType.IsGeneric Throw New SemantEx( "Array element type '"+atype.elemType.Name+"' is generic" )
 		
 		Local sizes:Value[],inits:Value[]
@@ -492,6 +501,7 @@ Class NewArrayExpr Extends Expr
 		Endif
 		
 		Return New NewArrayValue( atype,sizes,inits )
+		#end
 	End
 		
 	Method ToString:String() Override

+ 3 - 1
src/mx2cc/mx2cc.monkey2

@@ -22,8 +22,10 @@ Global opts_time:Bool
 
 Global StartDir:String
 
-Const TestArgs:="mx2cc makedocs mojo"
+'Const TestArgs:="mx2cc makemods"
  
+Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
+
 'Const TestArgs:="mx2cc makeapp -parse -geninfo src/mx2cc/test.monkey2"
 
 'Const TestArgs:="mx2cc makeapp -verbose src/mx2cc/catan/main.monkey2"

+ 7 - 1
src/mx2cc/parser.monkey2

@@ -1505,7 +1505,13 @@ Class Parser
 				
 				tail.type=type
 				
-				Return New NewArrayExpr( head,sizes,Null,srcpos,EndPos )
+				Local inits:Expr[]
+				If CParse( "(" ) 
+					inits=ParseExprs()
+					Parse( ")" )
+				Endif
+				
+				Return New NewArrayExpr( head,sizes,inits,srcpos,EndPos )
 			Endif
 			
 			If Toke="("

+ 10 - 7
src/mx2cc/test.monkey2

@@ -1,12 +1,15 @@
-#Import "<std>"
-
-Using std..
-
-Function Test( v:Variant )
-End
 
 Function Main()
 	
-	Test( New Vec2i[10] )
+	Local t:=New Int[3,3]( 
+	1,2,3,
+	4,5,6,
+	7,8,9 )
+	
+	For Local y:=0 Until 3
+		For Local x:=0 Until 3
+			Print t[x,y]
+		Next
+	Next
 	
 End

+ 6 - 1
src/mx2cc/translator_cpp.monkey2

@@ -2200,7 +2200,12 @@ Class Translator_CPP Extends Translator
 		'Uses( atype.elemType )
 		UsesType( atype.elemType )
 		
-		If value.inits Return ArrayName( atype )+"({"+TransArgs( value.inits )+"},"+value.inits.Length+")"
+		If value.inits 
+			If value.sizes
+				Return ArrayName( atype )+"({"+TransArgs( value.inits )+"},"+TransArgs( value.sizes )+")"
+			Endif
+			Return ArrayName( atype )+"({"+TransArgs( value.inits )+"},"+value.inits.Length+")"
+		Endif
 		
 		Return ArrayName( atype )+"("+TransArgs( value.sizes )+")"
 	End